gno/usr.orca.bin/udl/common.c

895 lines
20 KiB
C
Raw Normal View History

1994-12-13 18:08:34 +00:00
/*
* udl - Convert EOL formats freely between MS-DOS (CR/LF), Unix/Amiga (LF),
* and Apple (CR).
*
* Routines common to both the Unix and Apple IIgs versions.
*
1996-02-04 01:34:31 +00:00
* $Id: common.c,v 1.9 1996/02/04 01:34:26 gdr Exp $
1994-12-13 18:08:34 +00:00
*
* Copyright (c) 1993-1995 Soenke Behrens, Devin Reade
1994-12-13 18:08:34 +00:00
*/
#ifdef GNO
#pragma noroot
#endif
1994-12-13 18:08:34 +00:00
#include "common.h"
extern char *strdup(const char *);
/*
* convert_gs() ... convert files to use CR as EOL
*
* Inputs:
* FILE *infile File to read from
* FILE *outfile File to write to
*
* Outputs:
* None
*/
void convert_gs(FILE *infile, FILE *outfile) {
unsigned char a;
unsigned char *in_bufpos;
unsigned char *out_bufpos;
unsigned char *in_bufend;
unsigned char *out_bufend;
size_t file_remain;
in_bufpos = in_buffer;
out_bufpos = out_buffer;
(void) fseek(infile,0L,SEEK_END);
file_remain = ftell(infile);
rewind(infile);
in_bufend = in_buffer + my_fread(infile,BUFFERSIZE);
out_bufend = out_buffer + BUFFERSIZE;
while (file_remain != 0) {
a = *in_bufpos;
in_bufpos++;
if (in_bufpos >= in_bufend) {
file_remain -= in_bufend - in_buffer;
in_bufend = in_buffer + my_fread(infile,BUFFERSIZE);
in_bufpos = in_buffer;
}
1996-02-04 01:34:31 +00:00
if(a == LF) {
*out_bufpos = CR;
1994-12-13 18:08:34 +00:00
out_bufpos++;
if (out_bufpos == out_bufend) {
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,BUFFERSIZE);
out_bufpos = out_buffer;
1994-12-13 18:08:34 +00:00
}
1996-02-04 01:34:31 +00:00
} else if(a == CR) {
*out_bufpos = CR;
1994-12-13 18:08:34 +00:00
out_bufpos++;
if (out_bufpos == out_bufend) {
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,BUFFERSIZE);
out_bufpos = out_buffer;
1994-12-13 18:08:34 +00:00
}
1996-02-04 01:34:31 +00:00
if (*in_bufpos == LF && file_remain != 0) {
in_bufpos++;
if (in_bufpos >= in_bufend) {
file_remain -= in_bufend - in_buffer;
in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
in_bufpos = in_buffer;
}
1994-12-13 18:08:34 +00:00
}
} else {
*out_bufpos = a;
out_bufpos++;
if (out_bufpos == out_bufend) {
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,BUFFERSIZE);
out_bufpos = out_buffer;
1994-12-13 18:08:34 +00:00
}
}
}
/* Check for remainder in output buffer */
if (out_bufpos != out_buffer)
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,(int)(out_bufpos - out_buffer));
1994-12-13 18:08:34 +00:00
}
/*
* convert_messy() ... convert files to use CR/LF as EOL
*
* Inputs:
* FILE *infile File to read from
* FILE *outfile File to write to
*
* Outputs:
* None
*/
void convert_messy (FILE *infile, FILE *outfile) {
unsigned char a;
unsigned char *in_bufpos;
unsigned char *out_bufpos;
unsigned char *in_bufend;
unsigned char *out_bufend;
size_t file_remain;
in_bufpos = in_buffer;
out_bufpos = out_buffer;
(void) fseek(infile,0L,SEEK_END);
file_remain = ftell(infile);
rewind(infile);
in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
out_bufend = out_buffer + BUFFERSIZE;
while (file_remain != 0) {
a = *in_bufpos;
in_bufpos++;
if (in_bufpos >= in_bufend) {
file_remain -= in_bufend - in_buffer;
in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
in_bufpos = in_buffer;
}
1996-02-04 01:34:31 +00:00
if(a == LF) {
*out_bufpos = CR;
1994-12-13 18:08:34 +00:00
out_bufpos++;
if (out_bufpos == out_bufend) {
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,BUFFERSIZE);
out_bufpos = out_buffer;
1994-12-13 18:08:34 +00:00
}
1996-02-04 01:34:31 +00:00
*out_bufpos = LF;
1994-12-13 18:08:34 +00:00
out_bufpos++;
if (out_bufpos == out_bufend) {
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,BUFFERSIZE);
out_bufpos = out_buffer;
1994-12-13 18:08:34 +00:00
}
1996-02-04 01:34:31 +00:00
} else if(a == CR) {
*out_bufpos = CR;
1994-12-13 18:08:34 +00:00
out_bufpos++;
if (out_bufpos == out_bufend) {
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,BUFFERSIZE);
out_bufpos = out_buffer;
1994-12-13 18:08:34 +00:00
}
1996-02-04 01:34:31 +00:00
*out_bufpos = LF;
1994-12-13 18:08:34 +00:00
out_bufpos++;
if (out_bufpos == out_bufend) {
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,BUFFERSIZE);
out_bufpos = out_buffer;
1994-12-13 18:08:34 +00:00
}
1996-02-04 01:34:31 +00:00
if (*in_bufpos == LF && file_remain != 0) {
in_bufpos++;
1994-12-13 18:08:34 +00:00
1996-02-04 01:34:31 +00:00
if (in_bufpos >= in_bufend) {
file_remain -= in_bufend - in_buffer;
in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
in_bufpos = in_buffer;
}
1994-12-13 18:08:34 +00:00
}
} else {
*out_bufpos = a;
out_bufpos++;
if (out_bufpos == out_bufend) {
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,BUFFERSIZE);
out_bufpos = out_buffer;
1994-12-13 18:08:34 +00:00
}
}
}
1996-02-04 01:34:31 +00:00
/* Check for remainder in output buffer */
1994-12-13 18:08:34 +00:00
if (out_bufpos != out_buffer)
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,(int)(out_bufpos - out_buffer));
1994-12-13 18:08:34 +00:00
}
/*
* convert_tunix() ... convert files to use LF as EOL
*
* Inputs:
* FILE *infile File to read from
* FILE *outfile File to write to
*
* Outputs:
* None
*/
void convert_tunix (FILE *infile, FILE *outfile) {
unsigned char a;
unsigned char *in_bufpos;
unsigned char *out_bufpos;
unsigned char *in_bufend;
unsigned char *out_bufend;
size_t file_remain;
in_bufpos = in_buffer;
out_bufpos = out_buffer;
(void) fseek(infile,0L,SEEK_END);
file_remain = ftell(infile);
rewind(infile);
in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
out_bufend = out_buffer + BUFFERSIZE;
while (file_remain != 0) {
a = *in_bufpos;
in_bufpos++;
if (in_bufpos >= in_bufend) {
file_remain -= in_bufend - in_buffer;
in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
in_bufpos = in_buffer;
}
1996-02-04 01:34:31 +00:00
if(a == CR) {
*out_bufpos = LF;
1994-12-13 18:08:34 +00:00
out_bufpos++;
if (out_bufpos == out_bufend) {
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,BUFFERSIZE);
out_bufpos = out_buffer;
1994-12-13 18:08:34 +00:00
}
1996-02-04 01:34:31 +00:00
if (*in_bufpos == LF && file_remain != 0) {
in_bufpos++;
if (in_bufpos >= in_bufend) {
file_remain -= in_bufend - in_buffer;
in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
in_bufpos = in_buffer;
}
1994-12-13 18:08:34 +00:00
}
} else {
*out_bufpos = a;
out_bufpos++;
if (out_bufpos == out_bufend) {
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,BUFFERSIZE);
out_bufpos = out_buffer;
1994-12-13 18:08:34 +00:00
}
}
}
/* Check for remainder in output buffer */
if (out_bufpos != out_buffer)
1996-02-04 01:34:31 +00:00
my_fwrite(out_buffer,outfile,(int)(out_bufpos - out_buffer));
1994-12-13 18:08:34 +00:00
}
/*
* convert_fast_gs() ... convert files to use CR as EOL
* Do not care about differing EOL chars in the same file,
* do not allow '\0' bytes, and replace in-vitro if possible.
*
* Inputs:
* FILE *infile File to read from
* FILE *outfile File to write to
*
* Outputs:
* int FALSE if no conversion took place, TRUE otherwise
*/
int convert_fast_gs(FILE *infile, FILE *outfile) {
unsigned char a;
unsigned char *in_bufpos;
unsigned char *in_bufend;
size_t file_remain;
enum file_format infile_type;
in_bufpos = in_buffer;
(void) fseek(infile,0L,SEEK_END);
file_remain = ftell(infile);
rewind(infile);
in_bufend = in_buffer + my_fread(infile,BUFFERSIZE);
*in_bufend = '\0';
infile_type = get_file_format (in_buffer);
switch (infile_type) {
case apple:
if (verbose)
printf("%s: %s is already in Apple format, skipping.\n",
1996-02-04 01:34:31 +00:00
program_name,current_file);
1994-12-13 18:08:34 +00:00
return (FALSE);
break;
case tunix:
/* Replace "in-vitro", so out_buffer isn't used */
while (file_remain != 0) {
a = *in_bufpos;
1996-02-04 01:34:31 +00:00
if (a == LF)
*in_bufpos++ = CR;
1994-12-13 18:08:34 +00:00
else if (a == '\0') { /* End of buffer reached */
1996-02-04 01:34:31 +00:00
/* Write changed buffer out */
my_fwrite(in_buffer,outfile,(int)(in_bufend - in_buffer));
/* And reload it */
file_remain -= in_bufend - in_buffer;
in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
*in_bufend = '\0';
in_bufpos = in_buffer;
1994-12-13 18:08:34 +00:00
} else in_bufpos++;
}
return (TRUE);
break;
case dos:
/* This I couldn't speed up, so use the existing thing */
convert_gs (infile, outfile);
return (TRUE);
break;
case binary:
return (FALSE);
break;
default:
fprintf(stderr,"%s: Fatal internal error\n",program_name);
exit (EXIT_FAILURE);
break;
} /* switch */
}
/*
* convert_fast_messy() ... convert files to use CR/LF as EOL
* Just check if it's already in DOS format.
*
* Inputs:
* FILE *infile File to read from
* FILE *outfile File to write to
*
* Outputs:
* int FALSE if no conversion took place, TRUE otherwise
*/
int convert_fast_messy (FILE *infile, FILE *outfile) {
unsigned char *in_bufend;
enum file_format infile_type;
in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
*in_bufend = '\0';
infile_type = get_file_format (in_buffer);
switch (infile_type) {
case dos:
if (verbose)
printf("%s: %s is already in MS-DOS format, skipping.\n",
1996-02-04 01:34:31 +00:00
program_name,current_file);
1994-12-13 18:08:34 +00:00
return (FALSE);
break;
case tunix: /* drop through */
case apple:
/* Wasn't able to speed this up, call old routine */
convert_messy (infile, outfile);
return (TRUE);
break;
case binary:
return (FALSE);
break;
default:
fprintf(stderr,"%s: Fatal internal error\n",program_name);
exit (EXIT_FAILURE);
break;
} /* switch */
}
/*
* convert_fast_tunix() ... convert files to use LF as EOL
* Do not care about differing EOL chars in the same file,
* do not allow '\0' bytes, and replace in-vitro if possible.
*
* Inputs:
* FILE *infile File to read from
* FILE *outfile File to write to
*
* Outputs:
* int FALSE if no conversion took place, TRUE otherwise
*/
int convert_fast_tunix (FILE *infile, FILE *outfile) {
unsigned char a;
unsigned char *in_bufpos;
unsigned char *in_bufend;
size_t file_remain;
enum file_format infile_type;
in_bufpos = in_buffer;
(void) fseek(infile,0L,SEEK_END);
file_remain = ftell(infile);
rewind(infile);
in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
*in_bufend = '\0';
infile_type = get_file_format (in_buffer);
switch (infile_type) {
case tunix:
if (verbose)
printf("%s: %s is already in Unix format, skipping.\n",
1996-02-04 01:34:31 +00:00
program_name,current_file);
1994-12-13 18:08:34 +00:00
return (FALSE);
break;
case apple:
/* Replace "in-vitro", so out_buffer isn't used */
while (file_remain != 0) {
a = *in_bufpos;
1996-02-04 01:34:31 +00:00
if (a == CR)
*in_bufpos++ = LF;
1994-12-13 18:08:34 +00:00
else if (a == '\0'){ /* End of buffer reached */
1996-02-04 01:34:31 +00:00
/* Write changed buffer out */
my_fwrite(in_buffer,outfile,(int)(in_bufend - in_buffer));
1994-12-13 18:08:34 +00:00
1996-02-04 01:34:31 +00:00
/* And reload */
file_remain -= in_bufend - in_buffer;
in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
*in_bufend = '\0';
in_bufpos = in_buffer;
1994-12-13 18:08:34 +00:00
} else in_bufpos++;
}
return (TRUE);
break;
case dos:
/* Couldn't speed it up, so use old routine */
convert_tunix (infile, outfile);
return (TRUE);
break;
case binary:
return (FALSE);
break;
default:
fprintf(stderr,"%s: Fatal internal error\n", program_name);
exit (EXIT_FAILURE);
break;
} /* switch */
}
/*
* get_file_format() ... look at a buffer and find out what the EOL
* character is. If no EOL character is found, print an error message
* and exit.
*
* Inputs:
* unsigned char *buffer Buffer to search through, terminated
* by '\0'.
*
* Output:
* enum file_format tunix, dos, apple, or binary
*/
enum file_format get_file_format (unsigned char *buffer) {
unsigned char c;
1996-02-04 01:34:31 +00:00
enum file_format result = unknown;
1994-12-13 18:08:34 +00:00
while ((c = *buffer++) != '\0') {
1996-02-04 01:34:31 +00:00
if (c == LF) {
1994-12-13 18:08:34 +00:00
result = tunix;
break;
1996-02-04 01:34:31 +00:00
} else if (c == CR) {
if (*buffer == LF)
result = dos;
1994-12-13 18:08:34 +00:00
else
1996-02-04 01:34:31 +00:00
result = apple;
1994-12-13 18:08:34 +00:00
break;
}
}
1996-02-04 01:34:31 +00:00
if (result == unknown) {
1994-12-13 18:08:34 +00:00
if (verbose)
printf("%s: No EOL found on the first %d bytes "
1996-02-04 01:34:31 +00:00
"of %s. Might be a binary file. File skipped\n",
program_name,BUFFERSIZE, current_file);
1994-12-13 18:08:34 +00:00
result = binary;
}
return (result);
}
/*
* tryopen() ... try to open a file, exit if unsuccesful
*
* Inputs:
* char *name Name of file to be opened
* char *mode Mode string for fopen() call
*
* Output:
* FILE * File identifier of successful fopen()
*/
FILE *tryopen (char *name, char *mode) {
FILE *tmp;
if ((tmp = fopen(name,mode)) == NULL) {
fprintf(stderr,"%s: Unable to open file %s\n",program_name, name);
exit (EXIT_FAILURE);
} else
return (tmp);
}
/*
* my_fread() ... read data into global buffer and exit if I fail
*
* Inputs:
* FILE *infile File to read from
* int howmuch Number of bytes to read
*
* Output:
* int Number of bytes actually read
*/
int my_fread(FILE *infile, int howmuch) {
int result;
1996-02-04 01:34:31 +00:00
result = (int)fread(in_buffer, 1, howmuch, infile);
1994-12-13 18:08:34 +00:00
if (ferror(infile)) {
fprintf(stderr,"%s: Error while reading data\n",program_name);
exit (EXIT_FAILURE);
}
return (result);
}
/*
* my_fwrite() ... write data from global buffer to file
*
* Inputs:
* unsigned char *buffer Buffer to write out
* FILE *outfile File to write to
* int howmuch Number of bytes to write
*
* Output:
* None
*/
void my_fwrite (unsigned char *buffer, FILE *outfile, int howmuch) {
fwrite(buffer, 1, howmuch, outfile);
if (ferror(outfile)) {
fprintf(stderr,"%s: Error while writing data\n",program_name);
exit (EXIT_FAILURE);
}
return;
}
/*
* cleanup() ... called in case of an exit(). Frees memory I allocated.
*
* Inputs:
* None
*
* Output:
* None
*/
void cleanup (void) {
char **p;
free (program_name);
free (current_file);
free (in_buffer);
free (out_buffer);
free (tempfile);
1994-12-13 18:08:34 +00:00
if (pathList) {
p = pathList;
while(*p) free(*p++);
free (pathList);
}
if (tempfile)
remove (tempfile);
}
/*
* usage() ... print out a usage string gotten from udluse.c
*
* Inputs:
* None
*
* Outputs:
* None
*/
void usage (void) {
extern char use1[]; /* from udluse.c */
#ifdef GNO
1994-12-13 18:08:34 +00:00
extern char use2[];
#endif
1994-12-13 18:08:34 +00:00
fprintf(stderr,"%s",use1);
#ifdef GNO
if(!needsgno())
fprintf(stderr,"%s",use2);
#endif
return;
}
/*
* build_file_list() build the list of files to process
*
* Precondition:
* file is the file name to be added to the pathList
* recurse if non-zero, directories will be recursed
*
* Postcondition:
* pathList will be a NULL-terminated array of strings. Each
* string is a partial pathname (relative to rootdir) of a file
* to convert.
*
* Note: This is a recursive routine that uses up (3 * sizeof(char *))
* bytes of stack with each level of subdirectories.
*/
void build_file_list(char *file, short recurse) {
1994-12-13 18:08:34 +00:00
char *thisdir;
DIR *dir;
struct dirent *entry;
1996-02-04 01:34:31 +00:00
#ifdef __DJGPP__ /* chdir() doesn't accept dir names ending in '\\' */
if(file[strlen(file)-1] == '\\')
file[strlen(file)-1] = '/';
#endif
/* check for stack overflow */
recursionDepth++;
#ifdef OVERFLOW_CHECK
if ((recursionDepth * BYTES_PER_DEPTH + BASESIZE) > STACKSIZE) {
fprintf(stderr,"%s: Exceeded permitted nesting depth (%d levels)\n"
1996-02-04 01:34:31 +00:00
"Aborted.\n",program_name,recursionDepth);
exit(EXIT_FAILURE);
}
#endif
1994-12-13 18:08:34 +00:00
if (stat(file,&tstat)!=0) {
fprintf(stderr,"%s: Couldn't stat %s. File skipped\n",program_name,file);
--recursionDepth;
1994-12-13 18:08:34 +00:00
return;
}
if (recurse && S_ISDIR(tstat.st_mode)) {
char tstr[2];
1996-02-04 01:34:31 +00:00
1994-12-13 18:08:34 +00:00
/*
* It is a directory. recurse through it.
*/
/* save our state */
tstr[0] = dirbrk;
tstr[1] = '\0';
if (*currentDirectory) {
thisdir = strdup(currentDirectory);
} else {
thisdir = malloc(1);
if (thisdir != NULL) *thisdir='\0';
}
if (thisdir == NULL) {
perror("Couldn't duplicate current directory");
exit (EXIT_FAILURE);
}
if (*currentDirectory) strcat(currentDirectory,tstr);
strcat(currentDirectory,file);
if (currentDirectory[strlen(currentDirectory)-1] == dirbrk)
currentDirectory[strlen(currentDirectory)-1] = '\0';
1994-12-13 18:08:34 +00:00
/* recurse */
if ((dir = opendir(file)) == NULL) {
fprintf(stderr,"%s: Couldn't open %s. Directory skipped.\n",
1996-02-04 01:34:31 +00:00
program_name,currentDirectory);
1994-12-13 18:08:34 +00:00
} else {
if (chdir(file) !=0) {
1996-02-04 01:34:31 +00:00
fprintf(stderr,"couldn't cd to %s\n",currentDirectory);
exit (EXIT_FAILURE);
1994-12-13 18:08:34 +00:00
}
#ifdef READDIR_RETURNS_DOT
entry = readdir(dir); /* for "." */
1996-02-04 01:34:31 +00:00
entry = readdir(dir); /* for ".." */
#endif
1994-12-13 18:08:34 +00:00
while ((entry = readdir(dir))!=NULL) {
1996-02-04 01:34:31 +00:00
/* ignore hidden files */
#ifdef BROKEN_DIRENT_STRUCT
1996-02-04 01:34:31 +00:00
if (*(entry->d_name)!='.') build_file_list((entry->d_name)-2,1);
#else
1996-02-04 01:34:31 +00:00
if (*(entry->d_name)!='.') build_file_list(entry->d_name,1);
#endif
1994-12-13 18:08:34 +00:00
}
if (*thisdir) {
1996-02-04 01:34:31 +00:00
if ((chdir(rootdir)!=0) || (chdir(thisdir)!=0)) {
fprintf(stderr,"couldn't cd to %s\n",thisdir);
exit (EXIT_FAILURE);
}
1994-12-13 18:08:34 +00:00
} else {
1996-02-04 01:34:31 +00:00
if (chdir(rootdir)!=0) {
fprintf(stderr,"couldn't cd to calling directory\n");
exit (EXIT_FAILURE);
}
1994-12-13 18:08:34 +00:00
}
1996-02-04 01:34:31 +00:00
1994-12-13 18:08:34 +00:00
}
1996-02-04 01:34:31 +00:00
1994-12-13 18:08:34 +00:00
/* restore our state */
strcpy(currentDirectory,thisdir);
free(thisdir);
1996-02-04 01:34:31 +00:00
1994-12-13 18:08:34 +00:00
} else if (S_ISREG(tstat.st_mode)) {
/* It is a normal file. Add it to the pathList */
add_to_pathList(currentDirectory, file);
}
--recursionDepth;
1994-12-13 18:08:34 +00:00
return;
}
void add_to_pathList(char *thisdir, char *file) {
char **p;
/* expand the pathList if necessary */
if (pathSlotsUsed >= pathSlots) {
pathSlots += PATHLIST_QUANTUM;
#if BROKEN_REALLOC
if ((pathList==NULL) &&
1996-02-04 01:34:31 +00:00
((pathList = malloc((pathSlots+1) * sizeof(char *)))==NULL)) {
1994-12-13 18:08:34 +00:00
fprintf(stderr,"%s: Couldn't expand pathList\n",program_name);
exit (EXIT_FAILURE);
} else {
if ((p = realloc(pathList, (pathSlots+1) * sizeof(char *)))==NULL) {
1996-02-04 01:34:31 +00:00
fprintf(stderr,"%s: Couldn't expand pathList\n",program_name);
exit (EXIT_FAILURE);
1994-12-13 18:08:34 +00:00
}
pathList = p;
}
#else
if ((p = realloc(pathList,(pathSlots+1) * sizeof(char *)))==NULL) {
fprintf(stderr,"%s: Couldn't expand pathList\n",program_name);
exit (EXIT_FAILURE);
} else pathList = p;
#endif
}
/* add in the current directory and filename to the pathList */
pathList[pathSlotsUsed] = malloc(strlen(thisdir)+strlen(file)+2);
if (pathList[pathSlotsUsed] == NULL) {
fprintf(stderr,"%s: Couldn't duplicate filename %s%c%s\n",program_name,
1996-02-04 01:34:31 +00:00
thisdir,dirbrk,file);
1994-12-13 18:08:34 +00:00
exit (EXIT_FAILURE);
}
if (*thisdir) {
sprintf(pathList[pathSlotsUsed],"%s%c%s",thisdir,dirbrk,file);
} else {
strcpy(pathList[pathSlotsUsed],file);
}
pathSlotsUsed++;
pathList[pathSlotsUsed] = NULL;
return;
}
/* Mktemp() construct a unique file name
*
* This routine is slightly different than the Unix standard one,
* thus the capitalization.
*
* Inputs:
* base Template to construct the name upon. It should
* be in the format "nameXXXXXX" where all "X" are replaced
* in such a way that the resulting name is unique. There
* should be at least one, at most 15 "X" in the base name.
* base may contain a full or partial path.
*
* Outputs:
* Mktemp() returns a pointer to a dynamically allocated string
* containing a unique file name.
*
*/
char *Mktemp(const char *base)
{
static char id[16] = "AAAAAAAAAAAAAAA";
char *p1,*p2,*st;
if ((st = malloc(strlen(base) + 1)) == NULL)
{
fprintf(stderr,"%s: memory allocation failure\n", program_name);
exit (EXIT_FAILURE);
}
st = strcpy(st,base);
if (*st == '\0')
{
1996-02-04 01:34:31 +00:00
free (st);
if ((st = strdup("TXXXXXXX")) == NULL)
{
fprintf(stderr,"%s: memory allocation failure\n", program_name);
exit (EXIT_FAILURE);
}
}
/* Replace all "X" with part of ID string */
for(p1 = st + strlen(st) - 1,p2 = &id[14];
1996-02-04 01:34:31 +00:00
p1 >= st && p2 >= id && *p1 == 'X';
p1--,p2--)
*p1 = *p2;
/* Update ID string to "count" one further */
for(p1 = &id[14];p1 >= id;)
if(*p1 == 'Z')
{
1996-02-04 01:34:31 +00:00
*p1 = 'A';
p1--;
} else {
1996-02-04 01:34:31 +00:00
*p1 += 1;
break;
}
/* Make sure the file name does not already exist */
#ifdef GNO
if (needsgno() == TRUE) {
#endif
if (stat(st,&tstat) == 0)
{
1996-02-04 01:34:31 +00:00
free (st);
st = Mktemp (base);
}
#ifdef GNO
} else { /* ORCA/Shell doesn't like stat one little bit */
FILE *fp;
if ((fp = fopen(st,"r")) != NULL)
{
1996-02-04 01:34:31 +00:00
fclose(fp);
free (st);
st = Mktemp (base);
} else if ((fp = fopen(st,"a")) == NULL) {
1996-02-04 01:34:31 +00:00
free(st);
st = Mktemp (base);
} else {
1996-02-04 01:34:31 +00:00
fclose(fp);
}
}
#endif
return st;
}
/* get_path() ... extract path from filename
*
* Inputs:
* name A file name containing a full, partial or no path.
*
* Outputs:
* Pointer to a string in static memory containing the path
* to the given file, or an empty string if "name" contained
* no path. The string can hold MAXPATHLEN characters.
*/
char *get_path (const char *name)
{
int i;
strcpy(filebuffer, name);
1996-02-04 01:34:31 +00:00
for (i = (int)strlen(filebuffer) - 1; i > 0 && filebuffer[i] != dirbrk; i--)
; /* empty loop to find end of path in name */
if (i != 0)
++i;
filebuffer[i] = '\0';
return filebuffer;
}
1994-12-13 18:08:34 +00:00
/* End Of File */