From 85008f04cabff49391306d036dcf99bb7633774b Mon Sep 17 00:00:00 2001 From: gdr Date: Wed, 8 Feb 1995 05:15:36 +0000 Subject: [PATCH] Soenke's initial checkins for v1.14, dated 30 Jan 95. The temp file is now created in the same directory as the original file, then renamed after the conversion is done. Some small doc changes were made as well, mostly typos and addresses. --- usr.orca.bin/udl/README | 22 ++++-- usr.orca.bin/udl/common.c | 143 ++++++++++++++++++++++------------ usr.orca.bin/udl/common.h | 9 ++- usr.orca.bin/udl/describe.udl | 12 +-- usr.orca.bin/udl/udl.1 | 16 ++-- usr.orca.bin/udl/udlgs.c | 15 ++-- usr.orca.bin/udl/udlunix.c | 13 ++-- usr.orca.bin/udl/udluse.c | 12 +-- 8 files changed, 147 insertions(+), 95 deletions(-) diff --git a/usr.orca.bin/udl/README b/usr.orca.bin/udl/README index 6c996cf..ea8debf 100644 --- a/usr.orca.bin/udl/README +++ b/usr.orca.bin/udl/README @@ -2,9 +2,9 @@ udl - Convert EOL formats freely between MS-DOS (CR/LF), Unix/Amiga (LF), and Apple (CR). - (c) 1993-1994 Soenke Behrens + (c) 1993-1995 Soenke Behrens, Devin Glyn Reade - Version 1.13: $Id: README,v 1.2 1995/02/08 05:05:36 gdr Exp $ + Version 1.14: $Id: README,v 1.3 1995/02/08 05:15:24 gdr Exp $ ============================================================================= Udl converts text files between CR, LF and CR/LF (Apple, Unix and MS-DOS). @@ -50,6 +50,12 @@ v1.13 implementation, a non-TXT or non-SRC file) is encountered unless the -v option is specified. +v1.14 + (Soenke Behrens, January 1995) + udl creates a temporary file in the directory of the + source file, not in /tmp. This reduces the likelihood of + data loss in the event of a system crash or powerdown. + ========= Compiling: ========= @@ -69,6 +75,13 @@ Also, udl.c assumes that getopt() is declared in and that the function strdup() exists in . You might want to change these includes if that's not the case. +======= +Authors +======= + +Original code by Soenke Behrens, sbehrens@contech.demon.co.uk +Modifications and version-merging by Devin Reade, gdr@myrias.ab.ca + ========== Legalities: ========== @@ -77,8 +90,3 @@ This program contains material from the Orca/C Run-Time Libraries, copyright 1987-1994 by Byte Works, Inc. Used with permission. ============================================================================= -Enjoy, - -Soenke -schaf@meadow.muc.de -December 1993 diff --git a/usr.orca.bin/udl/common.c b/usr.orca.bin/udl/common.c index b9bb7e7..7805f80 100644 --- a/usr.orca.bin/udl/common.c +++ b/usr.orca.bin/udl/common.c @@ -4,9 +4,9 @@ * * Routines common to both the Unix and Apple IIgs versions. * - * $Id: common.c,v 1.2 1995/02/08 05:05:38 gdr Exp $ + * $Id: common.c,v 1.3 1995/02/08 05:15:26 gdr Exp $ * - * Copyright (c) 1993-1994 Soenke Behrens + * Copyright (c) 1993-1995 Soenke Behrens, Devin Glyn Reade */ #include "common.h" @@ -597,54 +597,6 @@ void my_fwrite (unsigned char *buffer, FILE *outfile, int howmuch) { return; } -/* - * copy_file() ... copies a file to another, creates the destination file. - * - * Inputs: - * char *from Name of file to copy from - * char *to Name of file to create and copy to - * - * Output: - * None - */ - -void copy_file (char *from, char *to) { - FILE *fp1, *fp2; - unsigned char *buffer; - size_t length; - - if((buffer = malloc(BUFFERSIZE)) == NULL) { - fprintf(stderr,"%s: memory allocation failure\n",program_name); - exit (EXIT_FAILURE); - } - - fp1 = tryopen (from,"rb"); - fp2 = tryopen (to,"wb"); - - while (!feof (fp1)) { - length = fread(buffer,1,BUFFERSIZE,fp1); - if (ferror(fp1)) { - fprintf(stderr,"%s: Error while trying to read %s\n", - program_name, from); - free (buffer); - exit (EXIT_FAILURE); - } - - if (fwrite(buffer,1,length,fp2) != length) { - fprintf(stderr,"%s: Error while trying to write out " - "file %s\n",program_name,to); - free (buffer); - exit (EXIT_FAILURE); - } - } - - if (fclose (fp1) == EOF || fclose (fp2) == EOF) { - perror ("closing files"); - free (buffer); - exit (EXIT_FAILURE); - } -} - /* * cleanup() ... called in case of an exit(). Frees memory I allocated. * @@ -662,6 +614,7 @@ void cleanup (void) { free (current_file); free (in_buffer); free (out_buffer); + free (tempfile); if (pathList) { p = pathList; while(*p) free(*p++); @@ -848,4 +801,94 @@ void add_to_pathList(char *thisdir, char *file) { return; } +/* mktemp() construct a unique file name + * + * 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') + { + free (st); + if ((st = strdup("TXXXXXXX")) == NULL) + { + fprintf(stderr,"%s: memory allocation failure\n", program_name); + exit (EXIT_FAILURE); + } + } + + /* Replace all "X" with part if ID string */ + for(p1 = st + strlen(st) - 1,p2 = &id[14]; + 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') + { + *p1 = 'A'; + p1--; + } else { + *p1++; + break; + } + + /* Make sure the file name does not already exist */ + if (stat(st,&tstat) == 0) + { + free (st); + st = mktemp (base); + } + + 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); + + for (i = 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; +} + /* End Of File */ diff --git a/usr.orca.bin/udl/common.h b/usr.orca.bin/udl/common.h index 5a13080..5f06326 100644 --- a/usr.orca.bin/udl/common.h +++ b/usr.orca.bin/udl/common.h @@ -4,9 +4,9 @@ * * Header file for routines common to both the Unix and Apple IIgs versions. * - * $Id: common.h,v 1.2 1995/02/08 05:05:40 gdr Exp $ + * $Id: common.h,v 1.3 1995/02/08 05:15:28 gdr Exp $ * - * Copyright (c) 1993-1994 Soenke Behrens + * Copyright (c) 1993-1995 Soenke Behrens, Devin Glyn Reade */ #include @@ -23,7 +23,7 @@ #define BUFFERSIZE 0x2000 #define PATHLIST_QUANTUM 20 -#define UDL_VERSION "Version 1.13" +#define UDL_VERSION "Version 1.14" #define STACKSIZE 2048 #define BYTES_PER_DEPTH 40 #define BASESIZE 700 @@ -86,11 +86,12 @@ extern enum file_format get_file_format (unsigned char *buffer); extern FILE *tryopen (char *file, char *mode); extern int my_fread (FILE *infile, int howmuch); extern void my_fwrite (unsigned char *buffer, FILE *outfile, int howmuch); -extern void copy_file (char *from, char *to); extern void cleanup (void); extern void usage (void); extern void build_file_list(char *file, short recurse); extern void add_to_pathList(char *thisdir, char *file); +extern char *mktemp(const char *base); +extern char *get_path(const char *name); extern int needsgno(void); diff --git a/usr.orca.bin/udl/describe.udl b/usr.orca.bin/udl/describe.udl index 0b8b294..e0dbd5a 100644 --- a/usr.orca.bin/udl/describe.udl +++ b/usr.orca.bin/udl/describe.udl @@ -1,9 +1,9 @@ Name: udl -Version: 1.13 -Author: Soenke Behrens -Contact: soenke.behrens@conner.com +Version: 1.14 +Author: Soenke Behrens, Devin Glyn Reade +Contact: sbehrens@contech.demon.co.uk, gdr@myrias.ab.ca Where: /usr/local/bin -FTP: cco.caltech.edu, grind.isca.uiowa.edu +FTP: ftp.cco.caltech.edu, grind.isca.uiowa.edu - Converts text between the CR, LF and CR/LF forms. Also available for -Unix machines, reasonably fast yet secure. + Converts text between the CR, LF and CR/LF forms. Also available for Unix +machines, reasonably fast yet secure. diff --git a/usr.orca.bin/udl/udl.1 b/usr.orca.bin/udl/udl.1 index 4569ecd..31011d2 100644 --- a/usr.orca.bin/udl/udl.1 +++ b/usr.orca.bin/udl/udl.1 @@ -1,13 +1,13 @@ -.\" Copyright (c) 1993-1994 Soenke Behrens -.\" $Id: udl.1,v 1.2 1995/02/08 05:05:42 gdr Exp $ -.TH UDL 1 "Commands and Applications" "22 November 1994" "Version 1.13" +.\" Copyright (c) 1993-1995 Soenke Behrens, Devin Glyn Reade +.\" $Id: udl.1,v 1.5 1995/02/08 05:47:53 gdr Exp $ +.TH UDL 1 "Commands and Applications" "28 January 1995" "Version 1.14" .SH NAME udl - convert text files between different architectures .SH SYNOPSIS .BR udl .BR -u | m | g [ -.B -Rv +.B -Rvp ] .I file1 [ @@ -38,7 +38,7 @@ flag is used. .PP .B udl creates a temporary file the size of the file it is currently working on -while it's doing its thing. +during conversion. .PP When running under Byteworks' ORCA shell, the Orca shell wildcards .BR = @@ -90,12 +90,10 @@ the stack actually overflows. .LP If you find any other bugs, please send a report to the address given below. .SH AUTHOR -Soenke Behrens, soenke.behrens@conner.com +Soenke Behrens, sbehrens@contech.demon.co.uk .br Version 1.13 updated by Devin Reade, gdr@myrias.ab.ca .SH VERSION This is .B udl -version 1.13. -.br -It's 1.5 times faster than 1.1 for Unix<->Apple conversions. +version 1.14. diff --git a/usr.orca.bin/udl/udlgs.c b/usr.orca.bin/udl/udlgs.c index 029daa9..bfa193e 100644 --- a/usr.orca.bin/udl/udlgs.c +++ b/usr.orca.bin/udl/udlgs.c @@ -4,9 +4,9 @@ * * Apple IIgs specific routines. * - * $Id: udlgs.c,v 1.2 1995/02/08 05:05:46 gdr Exp $ + * $Id: udlgs.c,v 1.3 1995/02/08 05:15:33 gdr Exp $ * - * Copyright (c) 1993-1994 Soenke Behrens + * Copyright (c) 1993-1995 Soenke Behrens, Devin Glyn Reade */ #include @@ -258,7 +258,8 @@ int main(int argc,char *argv[]) { } infile = tryopen(current_file,"rwb"); - outfile = tryopen(tempfile = tmpnam(NULL),"wb"); + tempfile = mktemp(strcat(get_path(current_file), "udltmpXX")); + outfile = tryopen(tempfile,"wb"); if (careful) { converted = TRUE; /* always */ @@ -290,13 +291,13 @@ int main(int argc,char *argv[]) { } if (rename (tempfile,current_file) != 0) { - copy_file (tempfile,current_file); - remove (tempfile); - tempfile = NULL; + perror ("cannot rename temporary file"); + exit (EXIT_FAILURE); } } else remove (tempfile); + free (tempfile); tempfile = NULL; SetGSOSType (current_file, theType, theAuxType); p++; } /* end while */ @@ -338,7 +339,7 @@ int CheckGSOSType(char *name) { program_name,name,toolerror()); exit (EXIT_FAILURE); } - + if ((fir.fileType != TXT) && (fir.fileType != SRC)) { if (verbose && (fir.fileType != DIRECTORY)) fprintf(stderr,"%s: %s is not of type TXT or " diff --git a/usr.orca.bin/udl/udlunix.c b/usr.orca.bin/udl/udlunix.c index dfde231..3b0fff9 100644 --- a/usr.orca.bin/udl/udlunix.c +++ b/usr.orca.bin/udl/udlunix.c @@ -4,9 +4,9 @@ * * Unix specific routines. * - * $Id: udlunix.c,v 1.2 1995/02/08 05:05:48 gdr Exp $ + * $Id: udlunix.c,v 1.3 1995/02/08 05:15:35 gdr Exp $ * - * Copyright (c) 1993-1994 Soenke Behrens + * Copyright (c) 1993-1995 Soenke Behrens, Devin Glyn Reade */ #define MAIN 1 @@ -157,7 +157,8 @@ int main(int argc,char *argv[]) { } infile = tryopen(current_file,"rwb"); - outfile = tryopen(tempfile = tmpnam(NULL),"wb"); + tempfile = mktemp(strcat(get_path(current_file), "udltmpXX")); + outfile = tryopen(tempfile,"wb"); if (careful) { converted = TRUE; /* always */ @@ -189,13 +190,13 @@ int main(int argc,char *argv[]) { } if (rename (tempfile,current_file) != 0) { - copy_file (tempfile,current_file); - remove (tempfile); - tempfile = NULL; + perror ("cannot rename temporary file"); + exit (EXIT_FAILURE); } } else remove (tempfile); + free (tempfile); tempfile = NULL; free(current_file); current_file = NULL; diff --git a/usr.orca.bin/udl/udluse.c b/usr.orca.bin/udl/udluse.c index 0efdcbc..2a1e53c 100644 --- a/usr.orca.bin/udl/udluse.c +++ b/usr.orca.bin/udl/udluse.c @@ -4,14 +4,14 @@ * * Usage strings. * - * $Id: udluse.c,v 1.2 1995/02/08 05:05:49 gdr Exp $ + * $Id: udluse.c,v 1.3 1995/02/08 05:15:36 gdr Exp $ * - * Copyright (c) 1993-1994 Soenke Behrens + * Copyright (c) 1993-1995 Soenke Behrens, Devin Glyn Reade */ char use1 [] = -"udl 1.13 by Soenke Behrens\n" -"Usage: udl -u|g|m [-Rv] file1 [file2 ...]\n\n" +"udl 1.14 by Soenke Behrens, Devin Glyn Reade\n" +"Usage: udl -u|g|m [-Rvp] file1 [file2 ...]\n\n" "Options:\n" " -u Convert file to use LF as EOL character.\n" " -g Convert file to use CR as EOL character.\n" @@ -19,8 +19,8 @@ char use1 [] = " -R Recurse through subdirectories.\n" " -p Be pedantic.\n" " -v Be verbose about it.\n\n" -"udl creates a temporary file on 14:, the original file(s) are over-\n" -"written when it is done.\n"; +"udl creates a temporary file in the directory of the original file,\n" +"the original file is overwritten after conversion.\n"; char use2 [] = "\nFiles may contain ORCA/Shell style wildcards.\n";