Mail Archives: djgpp-workers/2000/11/27/03:10:03
On Sun, 26 Nov 2000, DJ Delorie wrote:
>
> > There are examples of software where additional -v simply increases
> > verbosity level. First example which comes into mind is bzip2.
>
> I use this method a lot. I'm not sure if getopt supports that
> gracefully. I don't mind if people want to volunteer to switch
> programs to getopt (as long as it's the default getopt and not a
> custom one) but I don't personally use it. I never know which getopt
> is going to be available.
>
Below is patch against current CVS version of dtou.c
- change to use getopt(). Some targets may not have getopt() in
libc (for example MINGW doesn't have it). But it's in
libiberty.a.
- change to use verbosity level instead of 2 variables (verbose
and vverbose)
- fix for broken detection when file are changed. Previous updates
often errorously misdetected file as changed. Try:
utod foo.txt
dtou -vv foo.txt foo.txt
for some file (replace foo.txt with name of file)
- I removed notice about myself in copyright notes as I don't
think it's reasonable to keep it now
- I added removal of old backup file (if one exist) before
renaming
Andris
*** djgpp/src/utils/dtou.c~1 Sun Nov 26 07:01:35 2000
--- djgpp/src/utils/dtou.c Mon Nov 27 09:47:11 2000
***************
*** 1,7 ****
/* Copyright (C) 2000 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
- /* Modified by A.Pavenis to work also in different Unix clones */
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
--- 1,6 ----
***************
*** 50,59 ****
static int
! dtou(char *fname, int make_backup, int repair_mode, int strip_mode, int verbose, int vverbose, int preserve_timestamp)
{
int i, k, sf, df, l, l2 = 0, is_CR = 0, is_nCR = 0, is_CR_sequence = 0;
int CntlZ_flag = 0, CR_flag = 0, nCR_flag = 0, LF_flag = 0, exit_status = NO_ERROR;
int buf_counter, nbufs, LF_counter, must_rewind, position, offset, whence;
char buf[BUF_SIZE];
char bfname[FILENAME_MAX], tfname[FILENAME_MAX], *bn;
--- 49,59 ----
static int
! dtou(char *fname, int make_backup, int repair_mode, int strip_mode, int verbose, int preserve_timestamp)
{
int i, k, sf, df, l, l2 = 0, is_CR = 0, is_nCR = 0, is_CR_sequence = 0;
int CntlZ_flag = 0, CR_flag = 0, nCR_flag = 0, LF_flag = 0, exit_status = NO_ERROR;
+ int bytes_in=0, bytes_out=0;
int buf_counter, nbufs, LF_counter, must_rewind, position, offset, whence;
char buf[BUF_SIZE];
char bfname[FILENAME_MAX], tfname[FILENAME_MAX], *bn;
***************
*** 110,115 ****
--- 110,116 ----
}
while ((l = read (sf, buf, BUF_SIZE)) > 0)
{
+ bytes_in+=l;
for (i = k = 0; i < l; i++)
{
if (strip_mode)
***************
*** 188,193 ****
--- 189,195 ----
}
l2 = (k > 0 ? write (df, buf, k) : 0);
+ bytes_out += k;
if (l2 < 0 || CntlZ_flag) break;
if (l2 != k) { exit_status = IO_ERROR; break; }
}
***************
*** 202,228 ****
if (l >= 0 && l2 >= 0 && exit_status == NO_ERROR)
{
! int file_has_changed = CR_flag || nCR_flag || CntlZ_flag || LF_flag;
! if (verbose)
printf ("File: %s successfully processed.\n",fname);
! if (vverbose)
printf ("File: %s\n",fname);
! if (CR_flag && vverbose)
printf ("At least one CR/LF to LF transformation occurred.\n");
! if (nCR_flag && vverbose)
printf ("Warning: At least one CR sequence stripped from a LF.\n");
! if (CntlZ_flag && vverbose)
printf ("Warning: At least one Cntl-Z has been found. File truncated at line %i.\n", LF_counter);
! if (LF_flag && vverbose)
printf ("Warning: At least one LF without a preceeding CR has been found.\n");
! if (vverbose && !file_has_changed)
printf ("File unchanged.\n");
if (make_backup && file_has_changed)
! rename (fname, bfname);
else
remove (fname);
rename (tfname, fname);
--- 204,233 ----
if (l >= 0 && l2 >= 0 && exit_status == NO_ERROR)
{
! int file_has_changed = (bytes_in!=bytes_out) || CntlZ_flag;
! if (verbose==1)
printf ("File: %s successfully processed.\n",fname);
! if (verbose>1)
printf ("File: %s\n",fname);
! if (CR_flag && verbose>1)
printf ("At least one CR/LF to LF transformation occurred.\n");
! if (nCR_flag && verbose>1)
printf ("Warning: At least one CR sequence stripped from a LF.\n");
! if (CntlZ_flag && verbose>1)
printf ("Warning: At least one Cntl-Z has been found. File truncated at line %i.\n", LF_counter);
! if (LF_flag && verbose>1)
printf ("Warning: At least one LF without a preceeding CR has been found.\n");
! if (verbose>1 && !file_has_changed)
printf ("File unchanged.\n");
if (make_backup && file_has_changed)
! {
! remove (bfname);
! rename (fname, bfname);
! }
else
remove (fname);
rename (tfname, fname);
***************
*** 234,240 ****
else
{
remove (tfname);
! if (verbose || vverbose)
printf ("File: %s. An I/O error occurred\n",fname);
}
--- 239,245 ----
else
{
remove (tfname);
! if (verbose)
printf ("File: %s. An I/O error occurred\n",fname);
}
***************
*** 244,250 ****
static void
usage(char *progname)
{
! printf ("Usage: %s [-b] [-h] [-r] [-s] [-t] [-v] [-vv] files...\n\n", progname);
printf ("Options are:\n");
printf (" -b: A backup of the original file is made using `.d2u' as backup\n");
printf (" extension, if the file has been modified.\n");
--- 249,255 ----
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 `.d2u' as backup\n");
printf (" extension, if the file has been modified.\n");
***************
*** 276,283 ****
main(int argc, char **argv)
{
int exit_status = NO_ERROR, i, make_backup, repair_mode;
! int strip_mode, verbose, vverbose, preserve_timestamp;
! char* progname = BaseName(argv[0]);
if (argc < 2)
{
--- 281,288 ----
main(int argc, char **argv)
{
int exit_status = NO_ERROR, i, make_backup, repair_mode;
! int c, strip_mode, verbose, preserve_timestamp;
! char* progname = argv[0];
if (argc < 2)
{
***************
*** 285,298 ****
exit(NO_ERROR);
}
/* Default for backward compatibility. */
! make_backup = repair_mode = strip_mode = verbose = vverbose = 0;
preserve_timestamp = 1;
i = 1;
! while ((argc > i) && (argv[i][0] == '-') && argv[i][1])
{
! switch (argv[i][1])
{
case 'b':
make_backup = 1;
--- 290,305 ----
exit(NO_ERROR);
}
+ opterr=0;
+
/* Default for backward compatibility. */
! make_backup = repair_mode = strip_mode = verbose = 0;
preserve_timestamp = 1;
i = 1;
! while ((c = getopt(argc, argv, "?bhrstv"))!=-1)
{
! switch (c)
{
case 'b':
make_backup = 1;
***************
*** 313,339 ****
preserve_timestamp = 0;
break;
case 'v':
! if (argv[i][2] == 'v')
! {
! vverbose = 1;
! verbose = 0;
! }
! else
! {
! verbose = 1;
! vverbose = 0;
! }
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 += dtou (argv[i], make_backup, repair_mode, strip_mode, verbose, vverbose, preserve_timestamp);
return exit_status;
}
--- 320,345 ----
preserve_timestamp = 0;
break;
case 'v':
! verbose++;
break;
! case '?':
! if (optopt=='?')
! {
! usage (progname);
! exit(NO_ERROR);
! }
! else
! {
! fprintf (stderr, "%s: invalid option -- %c\n", progname, optopt);
! fprintf (stderr, "Try `%s -h' for more information.\n", progname);
! exit (IO_ERROR);
! }
break;
}
! i=optind;
}
for (; i < argc; i++)
! exit_status += dtou (argv[i], make_backup, repair_mode, strip_mode, verbose, preserve_timestamp);
return exit_status;
}
- Raw text -