--- xemacs-21.1.9/lib-src/mmencode.c.chmou Thu Mar 23 18:26:34 2000 +++ xemacs-21.1.9/lib-src/mmencode.c Thu Mar 23 18:26:55 2000 @@ -20,6 +20,13 @@ #include #include +/* required for open */ +#include +#include +#include + +extern int errno; + static void output64chunk(int c1, int c2, int c3, int pads, FILE *outfile); @@ -429,6 +436,29 @@ } } +/* not safe on nfs filesystems, but this is close */ +FILE * +safeopen(char *filename, char *mode) +{ + FILE *ret; + int fd; + int flags; + + if(mode[0] == 'w') { + flags = O_EXCL | O_CREAT | O_WRONLY; + } + else { + flags = O_RDONLY; + } + + fd = open(filename, flags); + + if(fd == -1) { + return 0; + } + + return fdopen(fd, mode); +} /* Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore) @@ -454,6 +484,7 @@ int main(int argc, char *argv[]) { int encode = 1, which = BASE64, i, portablenewlines = 0; + struct stat theStat; FILE *fp = stdin; FILE *fpo = stdout; @@ -465,7 +496,12 @@ fprintf(stderr, "mimencode: -o requires a file name.\n"); exit(-1); } - fpo = fopen(argv[i], "w"); + fpo = safeopen(argv[i], "w"); + if(fpo == 0) { + printf("Can't open output file(%s): %s\n", + argv[i], strerror(errno)); + exit(1); + } if (!fpo) { perror(argv[i]); exit(-1); @@ -490,15 +526,35 @@ } } else { #ifdef MSDOS - if (encode) - fp = fopen(argv[i], "rb"); + if (encode) { + fp = safeopen(argv[i], "rb"); + if(fp == 0) { + exit(1); + } + + } else { - fp = fopen(argv[i], "rt"); + fp = safeopen(argv[i], "rt"); + if(fp == 0) exit(1); setmode(fileno(fpo), O_BINARY); } /* else */ #else - fp = fopen(argv[i], "r"); + fp = safeopen(argv[i], "r"); + if(fp == 0) { + printf("Unable to open read file(%s): %s\n", + argv[i], strerror(errno)); + exit(1); + } + /* ok change the permissions on the output file to match + the permissions on the input file */ + if(fstat(fileno(fp), &theStat) == -1) { + printf("Unable to stat read file: %s\n", strerror(errno)); + exit(3); + } + fchmod(fileno(fpo), + theStat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)); + #endif /* MSDOS */ if (!fp) { perror(argv[i]);