--- pgp/src/fileio.c 2018/04/24 16:38:30 1.1.1.2 +++ pgp/src/fileio.c 2018/04/24 16:39:12 1.1.1.3 @@ -33,37 +33,41 @@ #include #include #include -#if !defined(MACH) && !defined(BSD) -#include +#ifdef _BSD +#include #endif +extern int errno; #endif /* UNIX */ #ifdef VMS #include #endif -#if (defined(VMS) || defined(UNIX)) && !defined(F_OK) -#define F_OK 0 -#define X_OK 1 -#define W_OK 2 -#define R_OK 4 -#endif /* !F_OK */ - #include "random.h" #include "mpilib.h" #include "mpiio.h" #include "fileio.h" #include "language.h" #include "pgp.h" -#ifdef MSDOS +#include "exitpgp.h" +#include "charset.h" +#include "system.h" +#if defined(MSDOS) || defined(OS2) #include #include #endif +#if (defined(VMS) || defined(UNIX)) && !defined(F_OK) +#define F_OK 0 +#define X_OK 1 +#define W_OK 2 +#define R_OK 4 +#endif /* !F_OK */ + /* 1st character of temporary file extension */ #define TMP_EXT '$' /* extensions are '.$##' */ /* The PGPPATH environment variable */ -char PGPPATH[] = "PGPPATH"; +static char PGPPATH[] = "PGPPATH"; /* Disk buffers, used here and in crypto.c */ byte textbuf[DISKBUFSIZE]; @@ -84,22 +88,7 @@ boolean file_exists(char *filename) #endif } /* file_exists */ - -boolean file_ok_write(char *filename) -/* Returns TRUE iff file can be opened for writing. - Does not harm file! -*/ -{ FILE *f; - if (file_exists (filename)) - return(TRUE); - if ((f = fopen(filename,FOPWBIN)) == NULL) - return(FALSE); - fclose(f); - remove(filename); - return(TRUE); -} /* file_ok_write */ - -boolean is_regular_file(char *filename) +static boolean is_regular_file(char *filename) { #ifdef S_ISREG struct stat st; @@ -109,7 +98,7 @@ boolean is_regular_file(char *filename) #endif } -int wipeout(FILE *f) +static int wipeout(FILE *f) { /* Completely overwrite and erase file, so that no sensitive information is left on the disk. NOTE: File MUST be open for read/write. @@ -208,8 +197,7 @@ boolean no_extension(char *filename) /* Returns TRUE if user left off file extension, allowing default. */ { #ifdef MULTIPLE_DOTS /* filename can have more than one dot */ - if (has_extension(filename, CTX_EXTENSION) || - has_extension(filename, ASC_EXTENSION) || + if (has_extension(filename, ASC_EXTENSION) || has_extension(filename, PGP_EXTENSION) || has_extension(filename, SIG_EXTENSION) || is_tempfile(filename)) @@ -257,21 +245,24 @@ void default_extension(char *filename, c } /* default_extension */ #ifndef MAX_NAMELEN -#if defined(AMIGA) || defined(NeXT) +#if defined(AMIGA) || defined(NeXT) || (defined(BSD) && BSD > 41) #define MAX_NAMELEN 255 +#else +#include #endif #endif -void truncate_name(char *path, int ext_len) +static void truncate_name(char *path, int ext_len) { /* truncate the filename so that an extension can be tacked on. */ #ifdef UNIX /* for other systems this is a no-op */ - char dir[MAX_PATH], *p; - int namemax; - + char *p; #ifdef MAX_NAMELEN /* overrides the use of pathconf() */ - namemax = MAX_NAMELEN; + int namemax = MAX_NAMELEN; #else + int namemax; #ifdef _PC_NAME_MAX + char dir[MAX_PATH]; + strcpy(dir, path); if ((p = strrchr(dir, '/')) == NULL) strcpy(dir, "."); @@ -283,8 +274,12 @@ void truncate_name(char *path, int ext_l if ((namemax = pathconf(dir, _PC_NAME_MAX)) <= ext_len) return; #else +#ifdef NAME_MAX + namemax = NAME_MAX; +#else namemax = 14; -#endif +#endif /* NAME_MAX */ +#endif /* _PC_NAME_MAX */ #endif /* MAX_NAMELEN */ if ((p = strrchr(path, '/')) == NULL) @@ -322,17 +317,19 @@ boolean getyesno(char default_answer) { strncpy(yes, PSTR("y"), 7); strncpy(no, PSTR("n"), 7); } - flush_input(); - getstring(buf,6,TRUE); /* echo keyboard input */ - strlwr(buf); - if (!strncmp(buf, no, strlen(no))) - return(FALSE); - if (!strncmp(buf, yes, strlen(yes))) - return(TRUE); - if (buf[0] == 'n') - return(FALSE); - if (buf[0] == 'y') - return(TRUE); + if (!batchmode) /* return default answer in batchmode */ + { flush_input(); + getstring(buf,6,TRUE); /* echo keyboard input */ + strlwr(buf); + if (!strncmp(buf, no, strlen(no))) + return(FALSE); + if (!strncmp(buf, yes, strlen(yes))) + return(TRUE); + if (buf[0] == 'n') + return(FALSE); + if (buf[0] == 'y') + return(TRUE); + } return(default_answer == 'y' ? TRUE : FALSE); } /* getyesno */ @@ -382,37 +379,6 @@ char *buildfilename(char *result, char * return(result); } /* buildfilename */ -int build_path(char *path, char *fileName, char *origPath) -/* Build a path for fileName based on origPath */ -{ int i, lastSlash = 0; - -#ifdef BSLASH - for (i=0; origPath[i]; i++) - if (origPath[i] == ':' || origPath[i] == '\\') - lastSlash = i + 1; -#else -#ifdef VMS - for (i=0; origPath[i]; i++) - if (origPath[i] == ']') - lastSlash = i + 1; -#else - for (i=0; origPath[i]; i++) - if (origPath[i] == '/') - lastSlash = i + 1; -#endif -#endif - - /* Sanity check: Make sure the path is of legal length */ - if (i>=MAX_PATH) - { fprintf(pgpout,PSTR("Path '%s' too long\n"), origPath); - return(-1); /* error return */ - } - - strncpy(path,origPath,lastSlash); /* Add path component */ - strcpy(path+lastSlash,fileName); /* Append filename */ - return(0); /* normal return */ -} /* build_path */ - void file_to_canon(char *filename) { /* Convert filename to canonical form, with slashes as separators */ @@ -426,45 +392,6 @@ void file_to_canon(char *filename) } -void file_from_canon(char *filename) -{ /* Convert filename from canonical to local form */ - /* I think everyone can handle slashes */ - filename = filename; /* Suppress warning */ -} - - -FILE *fopenbin(char *name, char *mode) -{ -#if !defined(UNIX) - char mode_b[8]; - - strcpy(mode_b, mode); - strcat(mode_b, "b"); - mode = mode_b; -#endif - -#ifdef VMS - if (*mode == 'r') - return(fopen(name, mode, "ctx=stm")); - else - return(fopen(name, mode)); -#else - return(fopen(name, mode)); -#endif -} - -FILE *fopentxt(char *name, char *mode) -{ -#ifdef VMS - if (*mode == 'r') - return(fopen(name, mode, "ctx=stm")); - else - return(fopen(name, mode)); -#else - return(fopen(name, mode)); -#endif -} - int write_error(FILE *f) { fflush(f); @@ -530,6 +457,7 @@ int copyfile_to_canon (FILE *f, FILE *g, int count, status = 0; byte c, *tb1, *tb2; int i, nbytes; + int nspaces = 0; do /* read and write the whole file... */ { if (longcount < (word32) DISKBUFSIZE) @@ -552,9 +480,28 @@ int copyfile_to_canon (FILE *f, FILE *g, default: c = *tb1++; } - if (c == '\n') - *tb2++ = '\r'; - *tb2++ = c; + if (strip_spaces) + { + if (c == ' ') /* Don't output spaces yet */ + nspaces += 1; + else + { if (c == '\n') + { *tb2++ = '\r'; + nspaces = 0; /* Delete trailing spaces */ + } + if (nspaces) /* Put out spaces here */ + { do + *tb2++ = ' '; + while (--nspaces); + } + *tb2++ = c; + } + } + else + { if (c == '\n') + *tb2++ = '\r'; + *tb2++ = c; + } } nbytes = tb2 - textbuf2; if (fwrite(textbuf2,1,nbytes,g) != nbytes ) @@ -733,10 +680,10 @@ int readPhantomInput(char *filename) if ((outFilePtr = fopen(filename,FOPWBIN)) == NULL) return(-1); -#ifdef MSDOS +#if defined(MSDOS) || defined(OS2) /* Under DOS must set input stream to binary mode to avoid data mangling */ setmode(fileno(stdin), O_BINARY); -#endif /* MSDOS */ +#endif /* MSDOS || OS2 */ while ((bytesRead = fread (buffer, 1, 512, stdin)) > 0) if (fwrite (buffer, 1, bytesRead, outFilePtr) != bytesRead) { status = -1; @@ -745,9 +692,9 @@ int readPhantomInput(char *filename) if (write_error(outFilePtr)) status = -1; fclose (outFilePtr); -#ifdef MSDOS +#if defined(MSDOS) || defined(OS2) setmode(fileno(stdin), O_TEXT); /* Reset stream */ -#endif /* MSDOS */ +#endif /* MSDOS || OS2 */ return(status); } @@ -763,9 +710,9 @@ int writePhantomOutput(char *filename) /* this can't fail since we just created the file */ outFilePtr = fopen(filename,FOPRBIN); -#ifdef MSDOS +#if defined(MSDOS) || defined(OS2) setmode(fileno(stdout), O_BINARY); -#endif +#endif /* MSDOS || OS2 */ while ((bytesRead = fread (buffer, 1, 512, outFilePtr)) > 0) if (fwrite (buffer, 1, bytesRead, stdout) != bytesRead) { status = -1; @@ -777,9 +724,9 @@ int writePhantomOutput(char *filename) { status = -1; fprintf(pgpout, PSTR("\007Write error on stdout.\n")); } -#ifdef MSDOS +#if defined(MSDOS) || defined(OS2) setmode(fileno(stdout), O_TEXT); -#endif +#endif /* MSDOS || OS2 */ return(status); } @@ -821,7 +768,7 @@ int is_text_file (char *filename) if (c < '\007' || (c > '\r' && c < ' ' && c != '\032')) return(FALSE); /* not a text file */ } - if (strcmp(language, "ru")) + if (strcmp(language, "ru") == 0) return(TRUE); /* assume binary if more than 1/4 bytes have 8th bit set */ return(bit8 < n / 4); @@ -872,7 +819,7 @@ void settmpdir(char *path) } strcpy(tmpdir, path); p = tmpdir + strlen(tmpdir)-1; - if (*p != '/' && *p != '\\' && *p != ']') + if (*p != '/' && *p != '\\' && *p != ']' && *p != ':') { /* append path separator, either / or \ */ if ((p = strchr(tmpdir, '/')) == NULL && (p = strchr(tmpdir, '\\')) == NULL) @@ -960,10 +907,17 @@ again: fd = (fp == NULL ? -1 : 0); #endif - if (fd == -1 && !(flags & TMP_TMPDIR)) - { - flags |= TMP_TMPDIR; - goto again; + if (fd == -1) + { if (!(flags & TMP_TMPDIR)) + { flags |= TMP_TMPDIR; + goto again; + } +#ifdef UNIX + else if (tmpdir[0] == '\0') + { strcpy(tmpdir, "/tmp/"); + goto again; + } +#endif } if (fd == -1) { fprintf(pgpout, PSTR("\n\007Cannot create temporary file '%s'\n"), tmpf[i].path); @@ -1035,13 +989,25 @@ char *savetemp(char *name, char *newname while (file_exists(newname)) { + if (batchmode && !force_flag) + { fprintf(pgpout,PSTR("\n\007Output file '%s' already exists.\n"),newname); + return NULL; + } if (is_regular_file(newname)) - { fprintf(pgpout,PSTR("\n\007Output file '%s' already exists. Overwrite (y/N)? "), + { + if (force_flag) + { /* remove without asking */ + remove(newname); + break; + } + fprintf(pgpout,PSTR("\n\007Output file '%s' already exists. Overwrite (y/N)? "), newname); overwrite = getyesno('n'); } else { fprintf(pgpout,PSTR("\n\007Output file '%s' already exists.\n"),newname); + if (force_flag) /* never remove special file */ + return NULL; overwrite = FALSE; } @@ -1073,7 +1039,9 @@ char *savetemp(char *name, char *newname int savetempbak(char *tmpname, char *destname) { char bakpath[MAX_PATH]; +#ifdef UNIX int mode = -1; +#endif if (is_tempfile(destname)) remove(destname);