Category : C Source Code
Archive   : PGP20SRC.ZIP
Filename : FILEIO.C

 
Output of file : FILEIO.C contained in archive : PGP20SRC.ZIP
/* fileio.c - I/O routines for PGP.
PGP: Pretty Good(tm) Privacy - public key cryptography for the masses.

(c) Copyright 1990-1992 by Philip Zimmermann. All rights reserved.
The author assumes no liability for damages resulting from the use
of this software, even if the damage results from defects in this
software. No warranty is expressed or implied.

All the source code Philip Zimmermann wrote for PGP is available for
free under the "Copyleft" General Public License from the Free
Software Foundation. A copy of that license agreement is included in
the source release package of PGP. Code developed by others for PGP
is also freely available. Other code that has been incorporated into
PGP from other sources was either originally published in the public
domain or was used with permission from the various authors. See the
PGP User's Guide for more complete information about licensing,
patent restrictions on certain algorithms, trademarks, copyrights,
and export controls.

Modified 16 Apr 92 - HAJK
Mods for support of VAX/VMS file system
*/

#include
#include
#include
#include
#if defined(UNIX)
#include
#if !defined(NeXT)
#include
#endif
#endif
#include "random.h"
#include "mpilib.h"
#include "mpiio.h"
#include "fileio.h"
#include "language.h"
#include "pgp.h"
#ifdef MSDOS
#include
#include
#endif

/* 1st character of temporary file extension */
#define TMP_EXT '$' /* extensions are '.$##' */

/* The PGPPATH environment variable */

char PGPPATH[] = "PGPPATH";

/* Disk buffers, used here and in crypto.c */
byte textbuf[DISKBUFSIZE];
static byte textbuf2[2*DISKBUFSIZE];

boolean file_exists(char *filename)
/* Returns TRUE iff file exists. */
{
#ifdef UNIX
#ifndef F_OK
#define F_OK 0
#endif
return(access(filename, F_OK) == 0);
#else
FILE *f;
/* open file f for read, in binary (not text) mode...*/
if ((f = fopen(filename,"rb")) == NULL)
return(FALSE);
fclose(f);
return(TRUE);
#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,"wb")) == NULL)
return(FALSE);
fclose(f);
remove(filename);
return(TRUE);
} /* file_ok_write */

boolean is_regular_file(char *filename)
{
#ifdef S_ISREG
struct stat st;
return(stat(filename, &st) != -1 && S_ISREG(st.st_mode));
#else
return TRUE;
#endif
}

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.
*/

long flength;
int count = 0;

fseek(f, 0L, SEEK_END);
flength = ftell(f);
rewind(f);

fill0(textbuf, sizeof(textbuf));
while (flength > 0L)
{ /* write zeros to the whole file... */
if (flength < (word32) DISKBUFSIZE)
count = (int)flength;
else
count = DISKBUFSIZE;
fwrite(textbuf,1,count,f);
flength -= count;
}
rewind(f); /* maybe this isn't necessary */
return(0); /* normal return */
} /* wipeout */


int wipefile(char *filename)
{ /* Completely overwrite and erase file, so that no sensitive
information is left on the disk.
*/
FILE *f;
/* open file f for read/write, in binary (not text) mode...*/
#ifdef VMS
if ((f = fopen(filename,"rb+","ctx=stm")) == NULL)
#else
if ((f = fopen(filename,"rb+")) == NULL)
#endif
return(-1); /* error - file can't be opened */
wipeout(f);
fclose(f);
return(0); /* normal return */
} /* wipefile */

char *file_tail (char *filename)
/* Returns the after-slash part of filename. Also skips backslashes,
* colons, and right brackets. */
{
char *bslashPos = strrchr(filename, '\\');/* Find last bslash in filename */
char *slashPos = strrchr(filename, '/');/* Find last slash in filename */
char *colonPos = strrchr(filename, ':');
char *rbrakPos = strrchr(filename, ']');

if (!slashPos || bslashPos > slashPos)
slashPos = bslashPos;
if (!slashPos || colonPos > slashPos)
slashPos = colonPos;
if (!slashPos || rbrakPos > slashPos)
slashPos = rbrakPos;
return( slashPos?(slashPos+1):filename );
}
/* Define BSLASH for machines that use backslash, FSLASH for machines
* that use forward slash as separators.
*/

#ifdef MSDOS
#define BSLASH
#endif
#ifdef ATARI
#define BSLASH
#endif
#ifdef UNIX
#define FSLASH
#define MULTIPLE_DOTS
#endif
#ifdef AMIGA
#define FSLASH
#define MULTIPLE_DOTS
#endif

boolean has_extension(char *filename, char *extension)
{ /* return TRUE if extension matches the end of filename */
int lf = strlen(filename);
int lx = strlen(extension);

if (lf <= lx)
return(FALSE);
return(!strcmp(filename + lf - lx, extension));
}

boolean is_tempfile(char *path)
{ /* return TRUE if path is a filename created by tempfile() */
char *p;

return((p = strrchr(path, '.')) != NULL &&
p[1] == TMP_EXT && strlen(p) == 4);
}

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) ||
has_extension(filename, PGP_EXTENSION) ||
has_extension(filename, SIG_EXTENSION) ||
is_tempfile(filename))
return(FALSE);
else
return(TRUE);
#else
#ifdef BSLASH
char *slashPos = strrchr(filename, '\\'); /* Find last slash in filename */

/* Look for the filename after the last slash if there is one */
return(strchr((slashPos != NULL ) ? slashPos : filename, '.') == NULL);
#else
#ifdef FSLASH
char *slashPos = strrchr(filename, '/'); /* Find last slash in filename */

/* Look for the filename after the last slash if there is one */
return(strchr((slashPos != NULL ) ? slashPos : filename, '.') == NULL);
#else
#ifdef VMS
char *slashPos = strrchr(filename,']'); /* Locate end of directory spec */

/* Look for last period in filename */
return(strrchr((slashPos != NULL) ? slashPos : filename, '.') == NULL );
#else
return( (strrchr(filename,'.')==NULL) ? TRUE : FALSE );
#endif /* VMS */
#endif /* FSLASH */
#endif /* BSLASH */
#endif /* MULTIPLE_DOTS */
} /* no_extension */


void drop_extension(char *filename)
{ /* deletes trailing ".xxx" file extension after the period. */
if (!no_extension(filename))
*strrchr(filename,'.') = '\0';
} /* drop_extension */


void default_extension(char *filename, char *extension)
{ /* append filename extension if there isn't one already. */
if (no_extension(filename))
strcat(filename,extension);
} /* default_extension */

#ifndef MAX_NAMELEN
#if defined(AMIGA) || defined(NeXT)
#define MAX_NAMELEN 255
#endif
#endif

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;

#ifdef MAX_NAMELEN /* overrides the use of pathconf() */
namemax = MAX_NAMELEN
#else
#ifdef _PC_NAME_MAX
strcpy(dir, path);
if ((p = strrchr(dir, '/')) == NULL)
strcpy(dir, ".");
else
{ if (p == dir)
++p;
*p = '\0';
}
if ((namemax = pathconf(dir, _PC_NAME_MAX)) <= ext_len)
return;
#else
namemax = 14;
#endif
#endif /* MAX_NAMELEN */

if ((p = strrchr(path, '/')) == NULL)
p = path;
else
++p;
if (strlen(p) > namemax - ext_len)
{
if (verbose)
fprintf(pgpout, "Truncating filename '%s' ", path);
p[namemax - ext_len] = '\0';
if (verbose)
fprintf(pgpout, "to '%s'\n", path);
}
#endif /* UNIX */
}

void force_extension(char *filename, char *extension)
{ /* change the filename extension. */
drop_extension(filename); /* out with the old */
truncate_name(filename, strlen(extension));
strcat(filename,extension); /* in with the new */
} /* force_extension */


boolean getyesno(char default_answer)
/* Get yes/no answer from user, returns TRUE for yes, FALSE for no.
First the translations are checked, if they don't match 'y' and 'n'
are tried.
*/
{ char buf[8];
static char yes[8], no[8];

if (yes[0] == '\0')
{ 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);
return(default_answer == 'y' ? TRUE : FALSE);
} /* getyesno */



void maybe_force_extension(char *filename, char *extension)
{ /* if user consents to it, change the filename extension. */
char newname[MAX_PATH];
if (!has_extension(filename,extension))
{ strcpy(newname,filename);
force_extension(newname,extension);
if (!file_exists(newname))
{ fprintf(pgpout,PSTR("\nShould '%s' be renamed to '%s' [Y/n]? "),
filename,newname);
if (getyesno('y'))
rename(filename,newname);
}
}
} /* maybe_force_extension */

char *buildfilename(char *result, char *fname)
/* Builds a filename with a complete path specifier from the environmental
variable PGPPATH.
*/
{ char *s = getenv(PGPPATH);

if ( s==NULL || strlen(s) > 50) /* undefined, or too long to use */
s="";
strcpy(result,s);
if (strlen(result) != 0)
#ifdef BSLASH
if (result[strlen(result)-1] != '\\')
strcat(result,"\\");
#else
#ifdef FSLASH
if (result[strlen(result)-1] != '/')
strcat(result,"/");
#else
#ifdef VMS
if (result[strlen(result)-1] != ']')
strcat(result,"]");
#endif
#endif
#endif /* Various OS-specific defines */
strcat(result,fname);
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 */
#ifdef BSLASH
while (*filename) {
if (*filename == '\\')
*filename = '/';
++filename;
}
#endif
}


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 copyfile(FILE *f, FILE *g, word32 longcount)
{ /* copy file f to file g, for longcount bytes */
int count, status = 0;
do /* read and write the whole file... */
{
if (longcount < (word32) DISKBUFSIZE)
count = (int)longcount;
else
count = DISKBUFSIZE;
count = fread(textbuf,1,count,f);
if (count>0)
{
if (CONVERSION != NO_CONV)
{ int i;
for (i = 0; i < count; i++)
textbuf[i] = (CONVERSION == EXT_CONV) ?
EXT_C(textbuf[i]) :
INT_C(textbuf[i]);
}
if (fwrite(textbuf,1,count,g) != count )
{ /* Problem: return error value */
status = -1;
break;
}
longcount -= count;
}
/* if text block was short, exit loop */
} while (count==DISKBUFSIZE);
burn(textbuf); /* burn sensitive data on stack */
return(status);
} /* copyfile */

int copyfilepos (FILE *f, FILE *g, word32 longcount, word32 fpos)
/* Like copyfile, but takes a position for file f. Returns with
* f and g pointing just past the copied data.
*/
{
fseek (f, fpos, SEEK_SET);
return copyfile (f, g, longcount);
}


#ifndef CANONICAL_TEXT
int copyfile_to_canon (FILE *f, FILE *g, word32 longcount)
{ /* copy file f to file g, for longcount bytes. Convert to
canonical form as we go. f is open in text mode. Canonical
form uses crlf's as line separators. */
int count, status = 0;
byte c, *tb1, *tb2;
int i, nbytes;
do /* read and write the whole file... */
{
if (longcount < (word32) DISKBUFSIZE)
count = (int)longcount;
else
count = DISKBUFSIZE;
count = fread(textbuf,1,count,f);
if (count>0)
{ /* Convert by adding CR before LF */
tb1 = textbuf;
tb2 = textbuf2;
for (i=0; i { switch (CONVERSION) {
case EXT_CONV:
c = EXT_C(*tb1++);
break;
case INT_CONV:
c = INT_C(*tb1++);
break;
default:
c = *tb1++;
}
if (c == '\n')
*tb2++ = '\r';
*tb2++ = c;
}
nbytes = tb2 - textbuf2;
if (fwrite(textbuf2,1,nbytes,g) != nbytes )
{ /* Problem: return error value */
status = -1;
break;
}
longcount -= count;
}
/* if text block was short, exit loop */
} while (count==DISKBUFSIZE);
burn(textbuf); /* burn sensitive data on stack */
burn(textbuf2);
return(status);
} /* copyfile_to_canon */
#endif


#ifndef CANONICAL_TEXT
int copyfile_from_canon (FILE *f, FILE *g, word32 longcount)
{ /* copy file f to file g, for longcount bytes. Convert from
canonical to local form as we go. g is open in text mode. Canonical
form uses crlf's as line separators. */
int count, status = 0;
byte c, *tb1, *tb2;
int i, nbytes;
do /* read and write the whole file... */
{
if (longcount < (word32) DISKBUFSIZE)
count = (int)longcount;
else
count = DISKBUFSIZE;
count = fread(textbuf,1,count,f);
if (count>0)
{ /* Convert by removing CR's */
tb1 = textbuf;
tb2 = textbuf2;
for (i=0; i { switch (CONVERSION) {
case EXT_CONV:
c = EXT_C(*tb1++);
break;
case INT_CONV:
c = INT_C(*tb1++);
break;
default:
c = *tb1++;
}
if (c != '\r')
*tb2++ = c;
}
nbytes = tb2 - textbuf2;
if (fwrite(textbuf2,1,nbytes,g) != nbytes )
{ /* Problem: return error value */
status = -1;
break;
}
longcount -= count;
}
/* if text block was short, exit loop */
} while (count==DISKBUFSIZE);
burn(textbuf); /* burn sensitive data on stack */
burn(textbuf2);
return(status);
} /* copyfile_from_canon */
#endif


int copyfiles_by_name(char *srcFile, char *destFile)
/* Copy srcFile to destFile */
{
FILE *f, *g;
int status = 0;
long fileLength;

#ifdef VMS
if (((f=fopen(srcFile,"rb","ctx=stm")) == NULL) ||
#else
if (((f=fopen(srcFile,"rb")) == NULL) ||
#endif
((g=fopen(destFile,"wb")) == NULL))
/* Can't open files */
return(-1);

/* Get file length and copy it */
fseek(f,0L,SEEK_END);
fileLength = ftell(f);
rewind(f);
status = copyfile(f,g,fileLength);
fclose(f);
fclose(g);
return(status);
} /* copyfiles_by_name */


int make_canonical(char *srcFile, char *destFile)
/* Copy srcFile to destFile, converting to canonical text form */
{
FILE *f, *g;
int status = 0;
long fileLength;

#ifdef CANONICAL_TEXT
if (((f=fopen(srcFile,"rb")) == NULL) ||
#else
if (((f=fopen(srcFile,"r")) == NULL) ||
#endif
((g=fopen(destFile,"wb")) == NULL))
/* Can't open files */
return(-1);

/* Get file length and copy it */
fseek(f,0L,SEEK_END);
fileLength = ftell(f);
rewind(f);
CONVERSION = INT_CONV;
#ifdef CANONICAL_TEXT
status = copyfile(f,g,fileLength);
#else
status = copyfile_to_canon(f,g,fileLength);
#endif
CONVERSION = NO_CONV;
fclose(f);
fclose(g);
return(status);
} /* make_canonical */


int rename2(char *srcFile, char *destFile)
/* Like rename() but will try to copy the file if the rename fails.
This is because under OS's with multiple physical volumes if the
source and destination are on different volumes the rename will fail */
{
FILE *f, *g;
int status = 0;
long fileLength;

if (rename(srcFile,destFile) == -1)
{ /* Rename failed, try a copy */
#ifdef VMS
if (((f=fopen(srcFile,"rb","ctx=stm")) == NULL) ||
#else
if (((f=fopen(srcFile,"rb")) == NULL) ||
#endif
((g=fopen(destFile,"wb")) == NULL))
/* Can't open files */
return(-1);

/* Get file length and copy it */
fseek(f,0L,SEEK_END);
fileLength = ftell(f);
rewind(f);
status = copyfile(f,g,fileLength);

/* Zap source file if the copy went OK, otherwise zap the (possibly
incomplete) destination file */
if (status >= 0)
{ wipeout(f); /* Zap source file */
fclose(f);
remove(srcFile);
fclose(g);
}
else
{ if (is_regular_file(destFile))
{ wipeout(g); /* Zap destination file */
fclose(g);
remove(destFile);
} else
fclose(g);
fclose(f);
}
}
return(status);
}


int readPhantomInput(char *filename)
/* read the data from stdin to the phantom input file */
{ FILE *outFilePtr;
byte buffer[ 512 ];
int bytesRead;

if ((outFilePtr = fopen(filename,"wb")) == NULL)
return(-1);

#ifdef MSDOS
/* Under DOS must set input stream to binary mode to avoid data mangling */
setmode(fileno(stdin), O_BINARY);
#endif /* MSDOS */
while ((bytesRead = fread (buffer, 1, 512, stdin)) > 0)
fwrite (buffer, 1, bytesRead, outFilePtr);
fclose (outFilePtr);
#ifdef MSDOS
setmode(fileno(stdin), O_TEXT); /* Reset stream */
#endif /* MSDOS */
return(0);
}


void writePhantomOutput(char *filename)
/* write the data from the phantom output file to stdout */
{ FILE *outFilePtr;
byte buffer[ 512 ];
int bytesRead;

/* this can't fail since we just created the file */
outFilePtr = fopen(filename,"rb");

#ifdef MSDOS
setmode(fileno(stdout), O_BINARY);
#endif
while ((bytesRead = fread (buffer, 1, 512, outFilePtr)) > 0)
fwrite (buffer, 1, bytesRead, stdout);
fclose (outFilePtr);
fflush(stdout);
#ifdef MSDOS
setmode(fileno(stdout), O_TEXT);
#endif

/* finally, delete the phantom file */
wipefile(filename);
remove(filename);
}

/* Return the size from the current position of file f to the end */
word32 fsize (FILE *f)
{
long fpos = ftell (f);
long fpos2;

fseek (f, 0L, SEEK_END);
fpos2 = ftell (f);
fseek (f, fpos, SEEK_SET);
return (word32)(fpos2 - fpos);
}

/* Return TRUE if file filename looks like a pure text file */
int is_text_file (char *filename)
{
FILE *f = fopen(filename,"rb");
int i, n, bit8 = 0;
unsigned char buf[512];
unsigned char *bufptr = buf;
unsigned char c;

if (!f)
return(FALSE); /* error opening it, so not a text file */
i = n = fread (buf, 1, sizeof(buf), f);
fclose(f);
if (n <= 0)
return(FALSE); /* empty file or error, not a text file */
if (compressSignature(buf) >= 0)
return(FALSE);
while (i--)
{ c = *bufptr++;
if (c & 0x80)
++bit8;
else /* allow BEL BS HT LF VT FF CR EOF control characters */
if (c < '\007' || (c > '\r' && c < ' ' && c != '\032'))
return(FALSE); /* not a text file */
}
if (strcmp(language, "ru"))
return(TRUE);
/* assume binary if more than 1/4 bytes have 8th bit set */
return(bit8 < n / 4);
} /* is_text_file */

void *xmalloc(unsigned size)
{ void *p;
if (size == 0)
++size;
if ((p = malloc(size)) == NULL)
{ fprintf(stderr, PSTR("\n\007Out of memory.\n"));
exitPGP(1);
}
return(p);
}

/*----------------------------------------------------------------------
* temporary file routines
*/


#define MAXTMPF 8

#define TMP_INUSE 2

static struct
{ char path[MAX_PATH];
int flags;
} tmpf[MAXTMPF];

extern char outdir[];
extern char basename[];

/*
* return a unique temporary file name
*/
char *tempfile(int flags)
{
int i;
int num = 0;

for (i = 0; i < MAXTMPF; ++i)
if (tmpf[i].flags == 0)
break;

if (i == MAXTMPF)
{ /* message only for debugging, no need for PSTR */
fprintf(stderr, "\n\007Out of temporary files\n");
return(NULL);
}

do
sprintf(tmpf[i].path, "%s%s.%c%02d",
(flags & TMP_TMPDIR ? tmpdir : outdir), basename, TMP_EXT, num);
while (file_exists(tmpf[i].path) && ++num < 100);

if (!file_ok_write(tmpf[i].path))
{ fprintf(pgpout, PSTR("\n\007Cannot create temporary file '%s'\n"), tmpf[i].path);
user_error();
}

if (num == 100)
{ fprintf(stderr, "\n\007tempfile: cannot find unique name\n");
return(NULL);
}

tmpf[i].flags = flags | TMP_INUSE;
if (verbose)
fprintf(stderr, "tempfile: created '%s'\n", tmpf[i].path);
return(tmpf[i].path);
} /* tempfile */

/*
* remove temporary file, wipe if necessary.
*/
void rmtemp(char *name)
{
int i;

for (i = 0; i < MAXTMPF; ++i)
if (tmpf[i].flags && strcmp(tmpf[i].path, name) == 0)
break;

if (i < MAXTMPF)
{ if (strlen(name) > 3 && name[strlen(name)-3] == TMP_EXT)
{ /* only remove file if name hasn't changed */
if (verbose)
fprintf(stderr, "rmtemp: removing '%s'\n", name);
if (tmpf[i].flags & TMP_WIPE)
wipefile(name);
remove(name);
tmpf[i].flags = 0;
} else if (verbose)
fprintf(stderr, "rmtemp: not removing '%s'\n", name);
}
} /* rmtemp */

/*
* make temporary file permanent, returns the new name.
*/
char *savetemp(char *name, char *newname)
{
int i, overwrite;
static char buf[MAX_PATH];

if (strcmp(name, newname) == 0)
return(name);

for (i = 0; i < MAXTMPF; ++i)
if (tmpf[i].flags && strcmp(tmpf[i].path, name) == 0)
break;

if (i < MAXTMPF)
{ if (strlen(name) < 4 || name[strlen(name)-3] != TMP_EXT)
{ if (verbose)
fprintf(stderr, "savetemp: not renaming '%s' to '%s'\n",
name, newname);
return(name); /* return original file name */
}
}

while (file_exists(newname))
{
if (is_regular_file(newname))
{ 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);
overwrite = FALSE;
}

if (!overwrite)
{ fprintf(pgpout, PSTR("\nEnter new file name: "));
getstring(buf, MAX_PATH - 1, TRUE);
if (buf[0] == '\0')
return(NULL);
newname = buf;
}
else
remove(newname);
}
if (verbose)
fprintf(stderr, "savetemp: renaming '%s' to '%s'\n", name, newname);
if (rename2(name, newname) < 0)
{ /* errorLvl = UNKNOWN_FILE_ERROR; */
return(NULL);
}
if (i < MAXTMPF)
tmpf[i].flags = 0;
return(newname);
} /* savetemp */

/*
* like savetemp(), only make backup of destname if it exists
*/
int savetempbak(char *tmpname, char *destname)
{
char bakpath[MAX_PATH];

if (strcmp(destname, SCRATCH_KEYRING_PATH) == 0 ||
is_tempfile(destname))
remove(destname);
else
{ if (file_exists(destname))
{
strcpy(bakpath, destname);
force_extension(bakpath, BAK_EXTENSION);
remove(bakpath);
if (rename(destname, bakpath) == -1)
return(-1);
}
}
if (savetemp(tmpname, destname) == NULL)
return(-1);
return(0);
}

/*
* remove all temporary files and wipe them if necessary
*/
void cleanup_tmpf(void)
{
int i;

for (i = 0; i < MAXTMPF; ++i)
if (tmpf[i].flags)
rmtemp(tmpf[i].path);
} /* cleanup_tmpf */


  3 Responses to “Category : C Source Code
Archive   : PGP20SRC.ZIP
Filename : FILEIO.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/