Mail Archives: djgpp-workers/2001/09/13/16:51:20
Some months ago I submitted a patch to implement some new options
for utod. These options are similar to that options that have also
been implemented in dtou. For some reason that patch seems to have
desapered. I have updated it to todays CVS-libc. The patch will
modify wc204.txi, utils.tex and utod.c. Hope it is still of interest.
Regards,
Guerrero, Juan M.
diff -acprNC3 djgpp.orig/src/docs/kb/wc204.txi djgpp/src/docs/kb/wc204.txi
*** djgpp.orig/src/docs/kb/wc204.txi Sun Aug 26 12:41:18 2001
--- djgpp/src/docs/kb/wc204.txi Thu Sep 13 19:54:40 2001
*************** operation, backup file creation, and con
*** 138,143 ****
--- 138,149 ----
preservation. Its source can now be compiled on a Unix or GNU/Linux
system.
+ @pindex utod AT r{, new command-line options}
+ The @code{utod} program accepts new command-line options for verbose
+ operation, backup file creation, and control of file timestamp
+ preservation. Its source can now be compiled on a Unix or GNU/Linux
+ system.
+
@cindex header files and GCC
The protection against multiple redefinitions of various
types like @code{size_t} has been rewriten to make DJGPP
diff -acprNC3 djgpp.orig/src/utils/utils.tex djgpp/src/utils/utils.tex
*** djgpp.orig/src/utils/utils.tex Tue Jul 3 19:15:38 2001
--- djgpp/src/utils/utils.tex Thu Sep 13 19:52:38 2001
*************** no backup of the original file will be d
*** 435,445 ****
--- 435,505 ----
@node utod, gxx, dtou, Top
@chapter utod
+ Usage: @code{utod} [@code{-b}] [@code{-h} | @code{-?}] [@code{-t}]
+ [@code{-v}] [@code{-v -v}|@code{-vv}] @file{files}
+
Each file specified on the command line is converted from unix's NL text
file mode to dos's CR/LF text file mode.
All djgpp wildcards are supported. Timestamps of the files are preserved.
+ @code{utod} will pass an exit status of 0 to the calling context if all the
+ files have been successfully processed and an exit status greater than 0 if
+ not. In this case, the exit status is equal to the amount of unsuccessfully
+ processed files. A file has not been successfully processed if some kind of
+ I/O error occurred.
+
+ @strong{Options:}
+
+ @table @code
+
+ @item -b
+
+ Creates a backup of the original file if the file has been modified.
+ @file{.u2d} is used as backup suffix. On systems with LFN support, the
+ backup suffix will be appended to the file name. If no LFN support is
+ available the backup suffix will overwrite the original file suffix.
+ If an old backup file exists it will be deleted.
+
+ @item -h
+
+ Displays a help text and exits.
+
+ @item -t
+
+ Timestamp. With this option the timestamp of a modified file will not be
+ preserved. The timestamp of an unmodified file will always be preserved.
+
+ @item -v
+
+ Verbose mode. Prints a single line showing the file name and if file
+ processing has been successful or not. The only case that a file is
+ considered as unsuccessfully processed is if an I/O error has occurred.
+
+ @item -vv
+
+ Very verbose mode. Prints the file name and shows the kind of modifications
+ that have been done to the file. The possible output looks like:
+
+ @example
+ File: foo.c
+ File unchanged.
+ At least one LF to CR/LF transformation occurred.
+ @end example
+
+ Of course, not all of the above lines will appear all together. The first
+ line showing the file name will always be printed. If the file has not been
+ modified at all, then only the next line will be printed. This will be the
+ case if the file does not contain any LF at all or if it is already a dos
+ text file. If the file has been modified line 3 will be printed.
+
+ @end table
+
+ The program is backward compatible with previous program versions if no options
+ are given at all. In this case, UNIX-style EOL (LF) are transformed into
+ MSDOS-style EOL (CR/LF), the timestamp will not be alterated and no backup
+ of the original file will be done.
+
@c -----------------------------------------------------------------------------
@node gxx, redir, utod, Top
@chapter gxx
diff -acprNC3 djgpp.orig/src/utils/utod.c djgpp/src/utils/utod.c
*** djgpp.orig/src/utils/utod.c Fri Feb 23 18:10:44 2001
--- djgpp/src/utils/utod.c Thu Sep 13 19:46:26 2001
***************
*** 12,27 ****
#include <utime.h>
#ifndef O_BINARY
! #define O_BINARY 0
#endif
static int
! utod(char *fname)
{
! int i, k, sf, df, l, l2=0, err=0, iscr=0;
! char buf[16384], buf2[32768];
! char tfname[FILENAME_MAX], *bn, *w;
struct stat st;
struct utimbuf tim1;
--- 12,56 ----
#include <utime.h>
#ifndef O_BINARY
! # define O_BINARY 0
#endif
+ #if defined(MSDOS) || defined(_Windows)
+ # define IS_DIR_SEPARATOR(path) ((path) == '/' || (path) == '\\' || (path) == ':')
+ #else
+ # define IS_DIR_SEPARATOR(path) ((path) == '/')
+ #endif
+
+ /* Buffer size. */
+ #define SRCBUF_SIZE 16384 /* Suorce buffer. */
+ #define DESBUF_SIZE 32768 /* Destination buffer. */
+
+ /* Control characters. */
+ #define LF 0x0A
+ #define CR 0x0D
+
+ /* Exit codes. */
+ #define NO_ERROR 0x00
+ #define IO_ERROR 0x01 /* Some I/O error occurred. */
+
+
+ static char *
+ BaseName (char *name)
+ {
+ char *bn, *w;
+ for (bn = w = name; *w; w++)
+ if (IS_DIR_SEPARATOR (*w))
+ bn = w + 1;
+ return bn;
+ }
+
static int
! utod (char *fname, int make_backup, int verbosity, int preserve_timestamp)
{
! int i, k, sf, df, l, l2 = 0, exit_status = NO_ERROR, file_has_changed = 0, is_CR = 0;
! char src_buffer[SRCBUF_SIZE], dest_buffer[DESBUF_SIZE];
! char backup_fname[FILENAME_MAX], temp_fname[FILENAME_MAX], *bn;
struct stat st;
struct utimbuf tim1;
*************** utod(char *fname)
*** 30,96 ****
if (sf < 0)
{
perror(fname);
! return 1;
}
tim1.actime = st.st_atime;
tim1.modtime = st.st_mtime;
! strcpy (tfname, fname);
! for (bn=w=tfname; *w; w++)
! if (*w=='/' || *w=='\\' || *w==':')
! bn = w+1;
! if (bn) *bn=0;
! strcat (tfname,"utod.tm$");
! df = open(tfname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
if (df < 0)
{
! perror(tfname);
close(sf);
! return 1;
}
! while ((l=read(sf, buf, 16384)) > 0)
{
! for (i=k=0; i<l; i++)
{
! if (buf[i]==10 && !iscr) buf2[k++]=13;
! iscr=(buf[i]==13 ? 1 : 0);
! buf2[k++]=buf[i];
}
! l2=write(df, buf2, k);
! if (l2<0) break;
! if (l2!=k) { err=1; break; }
}
! if (l<0) perror (fname);
! if (l2<0) perror (tfname);
! if (err) fprintf (stderr,"Cannot process file %s\n",fname);
close(sf);
close(df);
! if (l>=0 && l2>=0 && err==0)
{
! remove(fname);
! rename(tfname, fname);
! utime(fname, &tim1);
chown(fname, st.st_uid, st.st_gid);
chmod(fname, st.st_mode);
}
else
{
! remove(tfname);
}
! return 0;
}
int
main(int argc, char **argv)
{
! int rv = 0;
! for (argc--, argv++; argc; argc--, argv++)
! rv += utod(*argv);
! return rv;
}
--- 59,221 ----
if (sf < 0)
{
perror(fname);
! return IO_ERROR;
}
tim1.actime = st.st_atime;
tim1.modtime = st.st_mtime;
! strcpy (temp_fname, fname);
! bn = BaseName (temp_fname);
! *bn = 0;
! strcat (temp_fname, "utod.tm$");
! if (make_backup)
! {
! strcpy (backup_fname, fname);
! if (pathconf ((backup_fname), _PC_NAME_MAX) <= 12)
! for (i = strlen (backup_fname); i > -1; i--)
! if (backup_fname[i] == '.')
! {
! backup_fname[i] = '\0';
! break;
! }
! strcat (backup_fname, ".u2d");
! }
! df = open(temp_fname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
if (df < 0)
{
! perror(temp_fname);
close(sf);
! return IO_ERROR;
}
! while ((l = read(sf, src_buffer, SRCBUF_SIZE)) > 0)
{
! for (i = k = 0; i < l; i++)
{
! if (src_buffer[i] == LF && !is_CR)
! {
! dest_buffer[k++] = CR;
! file_has_changed = 1;
! }
! is_CR = (src_buffer[i] == CR ? 1 : 0);
! dest_buffer[k++] = src_buffer[i];
}
! l2 = write(df, dest_buffer, k);
! if (l2 < 0) break;
! if (l2 != k) { exit_status = IO_ERROR; break; }
}
! if (l < 0) perror(fname);
! if (l2 < 0) perror(temp_fname);
! if (exit_status != NO_ERROR)
! fprintf(stderr,"Cannot process file %s\n",fname);
close(sf);
close(df);
! if (l >= 0 && l2 >= 0 && exit_status == NO_ERROR)
{
! if (verbosity == 1)
! printf("File: %s successfully processed.\n",fname);
! if (verbosity == 2)
! printf("File: %s\n",fname);
!
! if ((verbosity == 2) && file_has_changed)
! printf("At least one LF to CR/LF transformation occurred.\n");
! if ((verbosity == 2) && !file_has_changed)
! printf("File unchanged.\n");
!
! if (make_backup && file_has_changed)
! {
! remove(backup_fname);
! rename(fname, backup_fname);
! }
! else
! remove(fname);
! rename(temp_fname, fname);
chown(fname, st.st_uid, st.st_gid);
chmod(fname, st.st_mode);
+ if (preserve_timestamp || !file_has_changed)
+ utime(fname, &tim1);
}
else
{
! remove (temp_fname);
! if (verbosity)
! printf("File: %s. An I/O error occurred\n",fname);
}
! return exit_status;
! }
!
! static void
! usage (char *progname)
! {
! printf("Usage: %s [options] files...\n\n", progname);
! printf("Options are:\n");
! printf(" -b: A backup of the original file is made using `.u2d' as backup\n");
! printf(" extension, if the file has been modified.\n");
! printf(" -h or -?: Display this help and exit.\n");
! printf(" -t: The timestamp of the modified file will not be preserved.\n");
! printf(" -v: Show if file processing has been successful or not.\n");
! printf(" -v -v or -vv: Show the kind of modifications that have been done to the file.\n");
! printf("The program is backward compatible with previous program versions if no options\n");
! printf("are given at all. In this case, UNIX-style EOL (LF) are transformed into MSDOS-\n");
! printf("style EOL (CRLF), the timestamp will not be alterated and no backup file of the\n");
! printf("original file will be created.\n");
}
int
main(int argc, char **argv)
{
! int exit_status = NO_ERROR, i, make_backup;
! int verbosity, preserve_timestamp;
! char* progname = BaseName(argv[0]);
!
! if (argc < 2)
! {
! usage(progname);
! exit(NO_ERROR);
! }
!
! /* Default for backward compatibility. */
! make_backup = verbosity = 0;
! preserve_timestamp = 1;
!
! i = 1;
! while ((argc > i) && (argv[i][0] == '-') && argv[i][1])
! {
! switch (argv[i][1])
! {
! case 'b':
! make_backup = 1;
! break;
! case 'h': case '?':
! usage(progname);
! exit(NO_ERROR);
! break;
! case 't':
! preserve_timestamp = 0;
! break;
! case 'v':
! if (verbosity == 1)
! verbosity++;
! else if (argv[i][2] == 'v')
! verbosity++;
! else
! verbosity = 1;
! break;
! default:
! fprintf(stderr, "%s: invalid option -- %s\n", progname, &argv[i][1]);
! fprintf(stderr, "Try `%s -h' for more information.\n", progname);
! exit(IO_ERROR);
! break;
! }
! i++;
! }
!
! for (; i < argc; i++)
! exit_status += utod(argv[i], make_backup, verbosity, preserve_timestamp);
! return exit_status;
}
- Raw text -