1994-12-13 18:08:34 +00:00
|
|
|
/*
|
|
|
|
* udl - Convert EOL formats freely between MS-DOS (CR/LF), Unix/Amiga (LF),
|
|
|
|
* and Apple (CR).
|
|
|
|
*
|
|
|
|
* Unix specific routines.
|
|
|
|
*
|
1996-02-04 01:34:31 +00:00
|
|
|
* $Id: udlunix.c,v 1.9 1996/02/04 01:34:30 gdr Exp $
|
1994-12-13 18:08:34 +00:00
|
|
|
*
|
1995-02-08 06:12:49 +00:00
|
|
|
* Copyright (c) 1993-1995 Soenke Behrens, Devin Reade
|
1994-12-13 18:08:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
extern char *strdup(const char *);
|
|
|
|
|
|
|
|
int main(int argc,char *argv[]) {
|
|
|
|
FILE *infile, *outfile;
|
|
|
|
int Tunix = FALSE;
|
|
|
|
int Messy = FALSE;
|
|
|
|
int GS = FALSE;
|
|
|
|
int careful = FALSE;
|
|
|
|
int converted;
|
|
|
|
int c;
|
|
|
|
int R_flag = FALSE;
|
|
|
|
|
|
|
|
verbose = FALSE;
|
|
|
|
program_name = NULL;
|
|
|
|
tempfile = NULL;
|
|
|
|
current_file = NULL;
|
|
|
|
in_buffer = out_buffer = NULL;
|
|
|
|
pathSlotsUsed = 0;
|
|
|
|
pathSlots = 0;
|
|
|
|
pathList = NULL;
|
|
|
|
*currentDirectory = '\0';
|
1995-02-08 05:05:49 +00:00
|
|
|
recursionDepth=0;
|
1994-12-13 18:08:34 +00:00
|
|
|
|
|
|
|
/* In case of exit(), free the mem I allocated */
|
|
|
|
#ifdef HAS_ATEXIT
|
|
|
|
atexit (cleanup);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((program_name = strdup (argv[0])) == NULL) {
|
|
|
|
fprintf(stderr,"%s: memory allocation failure\n",argv[0]);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < 3) {
|
|
|
|
usage();
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get and process arguments */
|
|
|
|
|
|
|
|
opterr = 1;
|
|
|
|
while ((c = getopt (argc, argv, "pvugmR")) != EOF) {
|
|
|
|
switch (c) {
|
|
|
|
case 'v':
|
|
|
|
verbose = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
careful = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
if (Tunix == TRUE || Messy == TRUE || GS == TRUE) {
|
1995-02-08 05:05:49 +00:00
|
|
|
fprintf(stderr, "%s: You may not "
|
|
|
|
"specify more than one conversion option\n",program_name);
|
|
|
|
exit (EXIT_FAILURE);
|
1994-12-13 18:08:34 +00:00
|
|
|
}
|
|
|
|
Tunix = TRUE;
|
|
|
|
break;
|
1995-02-08 05:05:49 +00:00
|
|
|
|
1994-12-13 18:08:34 +00:00
|
|
|
case 'm':
|
|
|
|
if (Tunix == TRUE || Messy == TRUE || GS == TRUE) {
|
1995-02-08 05:05:49 +00:00
|
|
|
fprintf(stderr, "%s: You may not specify more than one "
|
|
|
|
"conversion option\n",program_name);
|
|
|
|
exit (EXIT_FAILURE);
|
1994-12-13 18:08:34 +00:00
|
|
|
}
|
|
|
|
Messy = TRUE;
|
|
|
|
break;
|
1995-02-08 05:05:49 +00:00
|
|
|
|
1994-12-13 18:08:34 +00:00
|
|
|
case 'g':
|
|
|
|
if (Tunix == TRUE || Messy == TRUE || GS == TRUE) {
|
1995-02-08 05:05:49 +00:00
|
|
|
fprintf(stderr, "%s: You may not specify more than one "
|
|
|
|
"conversion option\n",program_name);
|
|
|
|
exit (EXIT_FAILURE);
|
1994-12-13 18:08:34 +00:00
|
|
|
}
|
|
|
|
GS = TRUE;
|
|
|
|
break;
|
1995-02-08 05:05:49 +00:00
|
|
|
|
1994-12-13 18:08:34 +00:00
|
|
|
case 'R':
|
|
|
|
R_flag++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
usage();
|
|
|
|
exit (EXIT_FAILURE);
|
1995-02-08 05:05:49 +00:00
|
|
|
|
1994-12-13 18:08:34 +00:00
|
|
|
default:
|
|
|
|
fprintf (stderr, "%s: Internal getopt error\n", program_name);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no files specified */
|
|
|
|
if (optind == argc) {
|
|
|
|
usage();
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tunix == FALSE && GS == FALSE && Messy == FALSE) {
|
|
|
|
fprintf(stderr,"%s: You have to specify a destination "
|
1995-02-08 05:05:49 +00:00
|
|
|
"format.\n",program_name);
|
1994-12-13 18:08:34 +00:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose == TRUE) {
|
|
|
|
printf ("%s %s\n",program_name,UDL_VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now process the files I got */
|
1995-02-08 06:12:49 +00:00
|
|
|
|
1994-12-13 18:08:34 +00:00
|
|
|
if ((in_buffer = malloc(BUFFERSIZE+1)) == NULL ||
|
|
|
|
(out_buffer = malloc(BUFFERSIZE+1)) == NULL) {
|
|
|
|
fprintf(stderr,"%s: Unable to buffer files\n",program_name);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save the directory we're in */
|
|
|
|
if (getwd(rootdir)==NULL) {
|
|
|
|
fprintf(stderr,"%s: Couldn't stat .\n",program_name);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the directory separator character. */
|
|
|
|
dirbrk = '/';
|
|
|
|
|
|
|
|
/* build the list of files to process */
|
|
|
|
for (; optind<argc; optind++) {
|
|
|
|
build_file_list(argv[optind],R_flag);
|
|
|
|
chdir(rootdir);
|
|
|
|
*currentDirectory = '\0';
|
|
|
|
}
|
|
|
|
|
1995-02-08 06:12:49 +00:00
|
|
|
/* files were all directories and no -R flag given */
|
|
|
|
if (!pathList) {
|
|
|
|
if (verbose) printf("%s: no files to process\n",program_name);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1994-12-13 18:08:34 +00:00
|
|
|
for (c=0; pathList[c]; c++) {
|
|
|
|
if((current_file = strdup(pathList[c])) == NULL) {
|
|
|
|
fprintf(stderr,"%s: memory allocation failure\n", program_name);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
1996-02-04 01:34:31 +00:00
|
|
|
|
1994-12-13 18:08:34 +00:00
|
|
|
if (verbose == TRUE) {
|
|
|
|
printf("%s: Working on %s\n",program_name,
|
1995-02-08 05:05:49 +00:00
|
|
|
current_file);
|
1994-12-13 18:08:34 +00:00
|
|
|
}
|
1996-02-04 01:34:31 +00:00
|
|
|
|
|
|
|
infile = tryopen(current_file,"r+b");
|
1996-01-22 01:01:36 +00:00
|
|
|
tempfile = Mktemp(strcat(get_path(current_file), "udltmpXX"));
|
1995-02-08 05:15:36 +00:00
|
|
|
outfile = tryopen(tempfile,"wb");
|
1996-02-04 01:34:31 +00:00
|
|
|
|
1994-12-13 18:08:34 +00:00
|
|
|
if (careful) {
|
|
|
|
converted = TRUE; /* always */
|
|
|
|
|
|
|
|
if (GS)
|
1995-02-08 05:05:49 +00:00
|
|
|
convert_gs(infile,outfile);
|
1994-12-13 18:08:34 +00:00
|
|
|
else if (Tunix)
|
1995-02-08 05:05:49 +00:00
|
|
|
convert_tunix(infile,outfile);
|
1994-12-13 18:08:34 +00:00
|
|
|
else
|
1995-02-08 05:05:49 +00:00
|
|
|
convert_messy(infile,outfile);
|
1994-12-13 18:08:34 +00:00
|
|
|
} else {
|
|
|
|
if (GS)
|
1995-02-08 05:05:49 +00:00
|
|
|
converted = convert_fast_gs(infile,outfile);
|
1994-12-13 18:08:34 +00:00
|
|
|
else if (Tunix)
|
1995-02-08 05:05:49 +00:00
|
|
|
converted = convert_fast_tunix(infile,outfile);
|
1994-12-13 18:08:34 +00:00
|
|
|
else
|
1995-02-08 05:05:49 +00:00
|
|
|
converted = convert_fast_messy(infile,outfile);
|
1994-12-13 18:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fclose (infile) == EOF || fclose (outfile) == EOF) {
|
|
|
|
perror ("closing files");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (converted) { /* Temp file contains converted data */
|
|
|
|
if (remove (current_file) != 0) {
|
1995-02-08 05:05:49 +00:00
|
|
|
perror ("removing original file");
|
|
|
|
exit (EXIT_FAILURE);
|
1994-12-13 18:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rename (tempfile,current_file) != 0) {
|
1995-02-08 05:15:36 +00:00
|
|
|
perror ("cannot rename temporary file");
|
|
|
|
exit (EXIT_FAILURE);
|
1994-12-13 18:08:34 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
remove (tempfile);
|
|
|
|
|
1995-02-08 05:15:36 +00:00
|
|
|
free (tempfile); tempfile = NULL;
|
1994-12-13 18:08:34 +00:00
|
|
|
free(current_file);
|
|
|
|
current_file = NULL;
|
|
|
|
|
|
|
|
} /* for (c=0; pathList[c]; c++) */
|
|
|
|
|
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
}
|
1995-02-13 19:47:38 +00:00
|
|
|
|
|
|
|
/* End Of File */
|