bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

coreutils minor cleanups for csplit, cut, date, env, expr


From: Paul Eggert
Subject: coreutils minor cleanups for csplit, cut, date, env, expr
Date: Mon, 02 Aug 2004 23:05:20 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

Continuing on our int cleanup, I installed the following patches.  I
don't think this fixes any bugs, except perhaps the (somewhat
unrelated, sorry) portability fix to env.c's main, which currently declares
an unused envp parameter that in theory leads to undefined behavior.

2004-08-02  Paul Eggert  <address@hidden>

        * src/csplit.c (struct line): Use size_t for sizes.
        (main): Remove unnecessary cast.
        * src/cut.c (cut_fields): Use to_uchar rather than a cast.
        * src/cut.c (cut_file, main): Use bool for booleans.
        * src/date.c (show_date, rfc_format, batch_convert, main): Likewise.
        * src/env.c (main): Likewise.
        * src/expr.c (nextarg): Likewise.
        * src/env.c (main): Remove unused and nonstandard envp arg.

Index: src/csplit.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/csplit.c,v
retrieving revision 1.128
diff -p -u -r1.128 csplit.c
--- src/csplit.c        21 Jun 2004 15:03:35 -0000      1.128
+++ src/csplit.c        15 Jul 2004 18:35:27 -0000
@@ -91,9 +91,9 @@ struct cstring
    These structures are linked together if needed. */
 struct line
 {
-  unsigned int used;           /* Number of offsets used in this struct. */
-  unsigned int insert_index;   /* Next offset to use when inserting line. */
-  unsigned int retrieve_index; /* Next index to use when retrieving line. */
+  size_t used;                 /* Number of offsets used in this struct. */
+  size_t insert_index;         /* Next offset to use when inserting line. */
+  size_t retrieve_index;       /* Next index to use when retrieving line. */
   struct cstring starts[CTRL_SIZE]; /* Lines in the data area. */
   struct line *next;           /* Next in linked list. */
 };
@@ -1353,7 +1353,7 @@ main (int argc, char **argv)
        if (xstrtoul (optarg, NULL, 10, &val, "") != LONGINT_OK
            || val > INT_MAX)
          error (EXIT_FAILURE, 0, _("%s: invalid number"), optarg);
-       digits = (int) val;
+       digits = val;
        break;
 
       case 's':
Index: src/cut.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/cut.c,v
retrieving revision 1.113
diff -p -u -r1.113 cut.c
--- src/cut.c   2 Aug 2004 05:25:33 -0000       1.113
+++ src/cut.c   2 Aug 2004 05:40:30 -0000
@@ -604,7 +604,7 @@ cut_fields (FILE *stream)
          /* If the first field extends to the end of line (it is not
             delimited) and we are printing all non-delimited lines,
             print this one.  */
-         if ((unsigned char) field_1_buffer[n_bytes - 1] != delim)
+         if (to_uchar (field_1_buffer[n_bytes - 1]) != delim)
            {
              if (suppress_non_delimited)
                {
@@ -688,9 +688,9 @@ cut_stream (FILE *stream)
 }
 
 /* Process file FILE to standard output.
-   Return 0 if successful, 1 if not. */
+   Return true if successful.  */
 
-static int
+static bool
 cut_file (char *file)
 {
   FILE *stream;
@@ -706,7 +706,7 @@ cut_file (char *file)
       if (stream == NULL)
        {
          error (0, errno, "%s", file);
-         return 1;
+         return false;
        }
     }
 
@@ -715,22 +715,23 @@ cut_file (char *file)
   if (ferror (stream))
     {
       error (0, errno, "%s", file);
-      return 1;
+      return false;
     }
   if (STREQ (file, "-"))
     clearerr (stream);         /* Also clear EOF. */
   else if (fclose (stream) == EOF)
     {
       error (0, errno, "%s", file);
-      return 1;
+      return false;
     }
-  return 0;
+  return true;
 }
 
 int
 main (int argc, char **argv)
 {
-  int optc, exit_status = 0;
+  int optc;
+  bool ok;
   bool delim_specified = false;
   char *spec_list_string IF_LINT(= NULL);
 
@@ -850,10 +851,10 @@ main (int argc, char **argv)
     }
 
   if (optind == argc)
-    exit_status |= cut_file ("-");
+    ok = cut_file ("-");
   else
-    for (; optind < argc; optind++)
-      exit_status |= cut_file (argv[optind]);
+    for (ok = true; optind < argc; optind++)
+      ok &= cut_file (argv[optind]);
 
   if (range_start_ht)
     hash_free (range_start_ht);
@@ -861,8 +862,8 @@ main (int argc, char **argv)
   if (have_read_stdin && fclose (stdin) == EOF)
     {
       error (0, errno, "-");
-      exit_status = 1;
+      ok = false;
     }
 
-  exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
Index: src/date.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/date.c,v
retrieving revision 1.137
diff -p -u -r1.137 date.c
--- src/date.c  21 Jun 2004 15:03:35 -0000      1.137
+++ src/date.c  20 Jul 2004 05:42:12 -0000
@@ -43,7 +43,7 @@
 
 int putenv ();
 
-static int show_date (const char *format, struct timespec when);
+static bool show_date (const char *format, struct timespec when);
 
 enum Time_spec
 {
@@ -76,8 +76,8 @@ char *program_name;
 /* If nonzero, display an ISO 8601 format date/time string */
 static int iso_8601_format = 0;
 
-/* If non-zero, display time in RFC-(2)822 format for mail or news. */
-static int rfc_format = 0;
+/* If true, display time in RFC-(2)822 format for mail or news. */
+static bool rfc_format = false;
 
 #define COMMON_SHORT_OPTIONS "Rd:f:r:s:u"
 
@@ -223,12 +223,12 @@ the following modifiers between `%' and 
 /* Parse each line in INPUT_FILENAME as with --date and display each
    resulting time and date.  If the file cannot be opened, tell why
    then exit.  Issue a diagnostic for any lines that cannot be parsed.
-   If any line cannot be parsed, return nonzero;  otherwise return zero.  */
+   Return true if successful.  */
 
-static int
+static bool
 batch_convert (const char *input_filename, const char *format)
 {
-  int status;
+  bool ok;
   FILE *in_stream;
   char *line;
   size_t buflen;
@@ -250,7 +250,7 @@ batch_convert (const char *input_filenam
 
   line = NULL;
   buflen = 0;
-  status = 0;
+  ok = true;
   while (1)
     {
       ssize_t line_length = getline (&line, &buflen, in_stream);
@@ -265,11 +265,11 @@ batch_convert (const char *input_filenam
          if (line[line_length - 1] == '\n')
            line[line_length - 1] = '\0';
          error (0, 0, _("invalid date `%s'"), line);
-         status = 1;
+         ok = false;
        }
       else
        {
-         status |= show_date (format, when);
+         ok &= show_date (format, when);
        }
     }
 
@@ -279,7 +279,7 @@ batch_convert (const char *input_filenam
   if (line != NULL)
     free (line);
 
-  return status;
+  return ok;
 }
 
 int
@@ -289,13 +289,13 @@ main (int argc, char **argv)
   const char *datestr = NULL;
   const char *set_datestr = NULL;
   struct timespec when;
-  int set_date = 0;
+  bool set_date = false;
   char *format;
   char *batch_file = NULL;
   char *reference = NULL;
   struct stat refstats;
   int n_args;
-  int status;
+  bool ok;
   int option_specified_date;
   char const *short_options = (posix2_version () < 200112
                               ? COMMON_SHORT_OPTIONS "I::"
@@ -331,11 +331,11 @@ main (int argc, char **argv)
        reference = optarg;
        break;
       case 'R':
-       rfc_format = 1;
+       rfc_format = true;
        break;
       case 's':
        set_datestr = optarg;
-       set_date = 1;
+       set_date = true;
        break;
       case 'u':
        /* POSIX says that `date -u' is equivalent to setting the TZ
@@ -401,14 +401,11 @@ argument must be a format string beginni
     datestr = set_datestr;
 
   if (batch_file != NULL)
-    {
-      status = batch_convert (batch_file,
-                             (n_args == 1 ? argv[optind] + 1 : NULL));
-    }
+    ok = batch_convert (batch_file, (n_args == 1 ? argv[optind] + 1 : NULL));
   else
     {
       bool valid_date = true;
-      status = 0;
+      ok = true;
 
       if (!option_specified_date && !set_date)
        {
@@ -416,7 +413,7 @@ argument must be a format string beginni
            {
              /* Prepare to set system clock to the specified date/time
                 given in the POSIX-format.  */
-             set_date = 1;
+             set_date = true;
              datestr = argv[optind];
              valid_date = posixtime (&when.tv_sec,
                                      datestr,
@@ -462,22 +459,22 @@ argument must be a format string beginni
          if (settime (&when) != 0)
            {
              error (0, errno, _("cannot set date"));
-             status = 1;
+             ok = false;
            }
        }
 
-      status |= show_date (format, when);
+      ok &= show_date (format, when);
     }
 
-  exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
 /* Display the date and/or time in WHEN according to the format specified
    in FORMAT, followed by a newline.  If FORMAT is NULL, use the
    standard output format (ctime style but with a timezone inserted).
-   Return zero if successful.  */
+   Return true if successful.  */
 
-static int
+static bool
 show_date (const char *format, struct timespec when)
 {
   struct tm *tm;
@@ -515,7 +512,7 @@ show_date (const char *format, struct ti
   else if (*format == '\0')
     {
       printf ("\n");
-      return 0;
+      return true;
     }
 
   tm = localtime (&when.tv_sec);
@@ -527,12 +524,12 @@ show_date (const char *format, struct ti
              ? imaxtostr (when.tv_sec, buf)
              : umaxtostr (when.tv_sec, buf)));
       puts (buf);
-      return 1;
+      return false;
     }
 
   while (1)
     {
-      int done;
+      bool done;
       out = x2nrealloc (out, &out_length, sizeof *out);
 
       /* Mark the first byte of the buffer so we can detect the case
@@ -556,5 +553,5 @@ show_date (const char *format, struct ti
 
   printf ("%s\n", out);
   free (out);
-  return 0;
+  return true;
 }
Index: src/env.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/env.c,v
retrieving revision 1.58
diff -p -u -r1.58 env.c
--- src/env.c   1 Jun 2004 13:07:01 -0000       1.58
+++ src/env.c   2 Aug 2004 23:39:08 -0000
@@ -136,10 +136,10 @@ A mere - implies -i.  If no COMMAND, pri
 }
 
 int
-main (register int argc, register char **argv, char **envp)
+main (register int argc, register char **argv)
 {
   int optc;
-  int ignore_environment = 0;
+  bool ignore_environment = false;
 
   initialize_main (&argc, &argv);
   program_name = argv[0];
@@ -157,7 +157,7 @@ main (register int argc, register char *
        case 0:
          break;
        case 'i':
-         ignore_environment = 1;
+         ignore_environment = true;
          break;
        case 'u':
          break;
@@ -169,7 +169,7 @@ main (register int argc, register char *
     }
 
   if (optind < argc && STREQ (argv[optind], "-"))
-    ignore_environment = 1;
+    ignore_environment = true;
 
   if (ignore_environment)
     {
Index: src/expr.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/expr.c,v
retrieving revision 1.94
diff -p -u -r1.94 expr.c
--- src/expr.c  21 Jun 2004 15:03:35 -0000      1.94
+++ src/expr.c  17 Jul 2004 06:08:27 -0000
@@ -358,14 +358,14 @@ toarith (VALUE *v)
     }
 }
 
-/* Return nonzero and advance if the next token matches STR exactly.
+/* Return true and advance if the next token matches STR exactly.
    STR must not be NULL.  */
 
 static bool
 nextarg (char const *str)
 {
   if (*args == NULL)
-    return 0;
+    return false;
   else
     {
       bool r = STREQ (*args, str);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]