bug-coreutils
[Top][All Lists]
Advanced

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

coreutils int fixes for touch, tsort, tty, uname


From: Paul Eggert
Subject: coreutils int fixes for touch, tsort, tty, uname
Date: Tue, 03 Aug 2004 16:16:31 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

tsort has some real fixes here, on 64-bit hosts where it can stuff
more than 2**31 bytes into RAM and sort it.  "touch" has a theoretical
fix: it avoids messing up when given more than 2**31 options.  The
other fixes are cosmetic.

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

        * src/touch.c (no_create, use_ref, posix_date, amtime_now,
        touch, main): Use bool for booleans.
        (main): Avoid integer overflow when given more than INT_MAX
        options.
        * src/tsort.c (struct item, n_strings): Use size_t for sizes.
        (have_read_stdin, count_items, scan_zeros, detect_loop,
        recurse_tree, walk_tree, tsort, main):
        Use bool for booleans.
        (exit_status): Remove.
        (tsort): Return a success flag instead of storing into a global.
        (main): Use it.
        * src/tty.c (silent, main): Use bool for booleans.
        (main): 0 -> STDIN_FILENO.
        * src/uname.c (print_element): Use bool for booleans.

Index: src/touch.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/touch.c,v
retrieving revision 1.122
diff -p -u -r1.122 touch.c
--- src/touch.c 21 Jun 2004 15:03:35 -0000      1.122
+++ src/touch.c 19 Jul 2004 21:40:05 -0000
@@ -53,21 +53,21 @@ char *program_name;
 /* Which timestamps to change. */
 static int change_times;
 
-/* (-c) If nonzero, don't create if not already there. */
-static int no_create;
+/* (-c) If true, don't create if not already there.  */
+static bool no_create;
 
-/* (-r) If nonzero, use times from a reference file. */
-static int use_ref;
+/* (-r) If true, use times from a reference file.  */
+static bool use_ref;
 
-/* (-t) If nonzero, date supplied on command line in POSIX format. */
-static int posix_date;
+/* (-t) If true, use date supplied on command line in POSIX format.  */
+static bool posix_date;
 
-/* If nonzero, the only thing we have to do is change both the
+/* If true, the only thing we have to do is change both the
    modification and access time to the current time, so we don't
    have to own the file, just be able to read and write it.
    On some systems, we can do this if we own the file, even though
    we have neither read nor write access to it.  */
-static int amtime_now;
+static bool amtime_now;
 
 /* New access and modification times to use when setting time.  */
 static struct timespec newtime[2];
@@ -118,12 +118,12 @@ get_reldate (struct timespec *result,
 }
 
 /* Update the time of file FILE according to the options given.
-   Return 0 if successful, 1 if an error occurs. */
+   Return true if successful.  */
 
-static int
+static bool
 touch (const char *file)
 {
-  int status;
+  bool ok;
   struct stat sbuf;
   int fd = -1;
   int open_errno = 0;
@@ -155,27 +155,27 @@ touch (const char *file)
          else
            {
              if (no_create && errno == ENOENT)
-               return 0;
+               return true;
              error (0, errno, _("failed to get attributes of %s"),
                     quote (file));
            }
          if (fd != -1)
            close (fd);
-         return 1;
+         return false;
        }
     }
 
   if (fd != -1 && close (fd) < 0)
     {
       error (0, errno, _("creating %s"), quote (file));
-      return 1;
+      return false;
     }
 
   if (amtime_now)
     {
       /* Pass NULL to utime so it will not fail if we just have
         write access to the file, but don't own it.  */
-      status = utime (file, NULL);
+      ok = (utime (file, NULL) == 0);
     }
   else
     {
@@ -194,10 +194,10 @@ touch (const char *file)
          timespec[1].tv_nsec = TIMESPEC_NS (sbuf.st_mtim);
        }
 
-      status = utimens (file, timespec);
+      ok = (utimens (file, timespec) == 0);
     }
 
-  if (status)
+  if (!ok)
     {
       if (open_errno)
        {
@@ -210,13 +210,13 @@ touch (const char *file)
       else
        {
          if (no_create && errno == ENOENT)
-           return 0;
+           return true;
          error (0, errno, _("setting times of %s"), quote (file));
        }
-      return 1;
+      return false;
     }
 
-  return 0;
+  return true;
 }
 
 void
@@ -264,8 +264,8 @@ int
 main (int argc, char **argv)
 {
   int c;
-  int date_set = 0;
-  int err = 0;
+  bool date_set = false;
+  bool ok = true;
   char const *flex_date = NULL;
 
   initialize_main (&argc, &argv);
@@ -276,7 +276,7 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  change_times = no_create = use_ref = posix_date = 0;
+  change_times = no_create = use_ref = posix_date = false;
 
   while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
     {
@@ -290,12 +290,12 @@ main (int argc, char **argv)
          break;
 
        case 'c':
-         no_create++;
+         no_create = true;
          break;
 
        case 'd':
          flex_date = optarg;
-         date_set++;
+         date_set = true;
          break;
 
        case 'f':
@@ -306,18 +306,18 @@ main (int argc, char **argv)
          break;
 
        case 'r':
-         use_ref++;
+         use_ref = true;
          ref_file = optarg;
          break;
 
        case 't':
-         posix_date++;
+         posix_date = true;
          if (! posixtime (&newtime[0].tv_sec, optarg,
                           PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS))
            error (EXIT_FAILURE, 0, _("invalid date format %s"), quote 
(optarg));
          newtime[0].tv_nsec = 0;
          newtime[1] = newtime[0];
-         date_set++;
+         date_set = true;
          break;
 
        case TIME_OPTION:       /* --time */
@@ -360,7 +360,7 @@ main (int argc, char **argv)
          if (change_times & CH_MTIME)
            get_reldate (&newtime[1], flex_date, &newtime[1]);
        }
-      date_set++;
+      date_set = true;
     }
   else
     {
@@ -392,13 +392,13 @@ main (int argc, char **argv)
            }
 
          optind++;
-         date_set++;
+         date_set = true;
        }
     }
   if (!date_set)
     {
       if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
-       amtime_now = 1;
+       amtime_now = true;
       else
        {
          if (gettime (&newtime[0]) != 0)
@@ -414,7 +414,7 @@ main (int argc, char **argv)
     }
 
   for (; optind < argc; ++optind)
-    err |= touch (argv[optind]);
+    ok &= touch (argv[optind]);
 
-  exit (err == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
Index: src/tsort.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/tsort.c,v
retrieving revision 1.41
diff -p -u -r1.41 tsort.c
--- src/tsort.c 21 Jun 2004 15:03:35 -0000      1.41
+++ src/tsort.c 20 Jul 2004 05:35:50 -0000
@@ -54,8 +54,8 @@ struct item
 {
   const char *str;
   struct item *left, *right;
-  int balance;
-  int count;
+  int balance; /* -1, 0, or +1 */
+  size_t count;
   struct item *qlink;
   struct successor *top;
 };
@@ -63,11 +63,8 @@ struct item
 /* The name this program was run with. */
 char *program_name;
 
-/* Nonzero if any of the input files are the standard input. */
-static int have_read_stdin;
-
-/* Nonzero if a nonfatal error has occurred.  */
-static int exit_status;
+/* True if any of the input files are the standard input. */
+static bool have_read_stdin;
 
 /* The head of the sorted list.  */
 static struct item *head = NULL;
@@ -79,7 +76,7 @@ static struct item *zeros = NULL;
 static struct item *loop = NULL;
 
 /* The number of strings to sort.  */
-static int n_strings = 0;
+static size_t n_strings = 0;
 
 static struct option const long_options[] =
 {
@@ -294,14 +291,14 @@ record_relation (struct item *j, struct 
     }
 }
 
-static int
+static bool
 count_items (struct item *unused ATTRIBUTE_UNUSED)
 {
   n_strings++;
-  return 0;
+  return false;
 }
 
-static int
+static bool
 scan_zeros (struct item *k)
 {
   /* Ignore strings that have already been printed.  */
@@ -315,12 +312,12 @@ scan_zeros (struct item *k)
       zeros = k;
     }
 
-  return 0;
+  return false;
 }
 
 /* Try to detect the loop.  If we have detected that K is part of a
    loop, print the loop on standard error, remove a relation to break
-   the loop, and return non-zero.
+   the loop, and return true.
 
    The loop detection strategy is as follows: Realise that what we're
    dealing with is essentially a directed graph.  If we find an item
@@ -336,7 +333,7 @@ scan_zeros (struct item *k)
    loop has completely been constructed.  If the loop was found, the
    global variable LOOP will be NULL.  */
 
-static int
+static bool
 detect_loop (struct item *k)
 {
   if (k->count > 0)
@@ -390,7 +387,7 @@ detect_loop (struct item *k)
 
                      /* Since we have found the loop, stop walking
                          the tree.  */
-                     return 1;
+                     return true;
                    }
                  else
                    {
@@ -405,14 +402,14 @@ detect_loop (struct item *k)
        }
     }
 
-  return 0;
+  return false;
 }
 
 /* Recurse (sub)tree rooted at ROOT, calling ACTION for each node.
-   Stop when ACTION returns non-zero.  */
+   Stop when ACTION returns true.  */
 
-static int
-recurse_tree (struct item *root, int (*action) (struct item *))
+static bool
+recurse_tree (struct item *root, bool (*action) (struct item *))
 {
   if (root->left == NULL && root->right == NULL)
     return (*action) (root);
@@ -420,32 +417,33 @@ recurse_tree (struct item *root, int (*a
     {
       if (root->left != NULL)
        if (recurse_tree (root->left, action))
-         return 1;
+         return true;
       if ((*action) (root))
-       return 1;
+       return true;
       if (root->right != NULL)
        if (recurse_tree (root->right, action))
-         return 1;
+         return true;
     }
 
-  return 0;
+  return false;
 }
 
 /* Walk the tree specified by the head ROOT, calling ACTION for
    each node.  */
 
 static void
-walk_tree (struct item *root, int (*action) (struct item *))
+walk_tree (struct item *root, bool (*action) (struct item *))
 {
   if (root->right)
     recurse_tree (root->right, action);
 }
 
-/* Do a topological sort on FILE.   */
+/* Do a topological sort on FILE.   Return true if successful.  */
 
-static void
+static bool
 tsort (const char *file)
 {
+  bool ok = true;
   struct item *root;
   struct item *j = NULL;
   struct item *k = NULL;
@@ -458,7 +456,7 @@ tsort (const char *file)
   if (STREQ (file, "-"))
     {
       fp = stdin;
-      have_read_stdin = 1;
+      have_read_stdin = true;
     }
   else
     {
@@ -528,12 +526,11 @@ tsort (const char *file)
        }
 
       /* T8.  End of process.  */
-      assert (n_strings >= 0);
       if (n_strings > 0)
        {
          /* The input contains a loop.  */
          error (0, 0, _("%s: input contains a loop:"), file);
-         exit_status = 1;
+         ok = false;
 
          /* Print the loop and remove a relation to break it.  */
          do
@@ -541,11 +538,14 @@ tsort (const char *file)
          while (loop);
        }
     }
+
+  return ok;
 }
 
 int
 main (int argc, char **argv)
 {
+  bool ok;
   int opt;
 
   initialize_main (&argc, &argv);
@@ -556,8 +556,6 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  exit_status = 0;
-
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
 
@@ -570,7 +568,7 @@ main (int argc, char **argv)
        usage (EXIT_FAILURE);
       }
 
-  have_read_stdin = 0;
+  have_read_stdin = false;
 
   if (1 < argc - optind)
     {
@@ -578,10 +576,10 @@ main (int argc, char **argv)
       usage (EXIT_FAILURE);
     }
 
-  tsort (optind == argc ? "-" : argv[optind]);
+  ok = tsort (optind == argc ? "-" : argv[optind]);
 
   if (have_read_stdin && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, _("standard input"));
 
-  exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
Index: src/tty.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/tty.c,v
retrieving revision 1.55
diff -p -u -r1.55 tty.c
--- src/tty.c   21 Jun 2004 15:03:35 -0000      1.55
+++ src/tty.c   3 Aug 2004 23:10:54 -0000
@@ -46,8 +46,8 @@ enum
 /* The name under which this program was run. */
 char *program_name;
 
-/* If nonzero, return an exit status but produce no output. */
-static int silent;
+/* If true, return an exit status but produce no output. */
+static bool silent;
 
 static struct option const longopts[] =
 {
@@ -94,7 +94,7 @@ main (int argc, char **argv)
   initialize_exit_failure (TTY_WRITE_ERROR);
   atexit (close_stdout);
 
-  silent = 0;
+  silent = false;
 
   while ((optc = getopt_long (argc, argv, "s", longopts, NULL)) != -1)
     {
@@ -104,7 +104,7 @@ main (int argc, char **argv)
          break;
 
        case 's':
-         silent = 1;
+         silent = true;
          break;
 
        case_GETOPT_HELP_CHAR;
@@ -119,7 +119,7 @@ main (int argc, char **argv)
   if (optind < argc)
     error (0, 0, _("extra operand %s"), quote (argv[optind]));
 
-  tty = ttyname (0);
+  tty = ttyname (STDIN_FILENO);
   if (!silent)
     {
       if (tty)
@@ -128,5 +128,5 @@ main (int argc, char **argv)
        puts (_("not a tty"));
     }
 
-  exit (isatty (0) ? EXIT_SUCCESS : EXIT_FAIL);
+  exit (isatty (STDIN_FILENO) ? EXIT_SUCCESS : EXIT_FAIL);
 }
Index: src/uname.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/uname.c,v
retrieving revision 1.59
diff -p -u -r1.59 uname.c
--- src/uname.c 21 Jun 2004 15:03:35 -0000      1.59
+++ src/uname.c 24 Jul 2004 19:41:47 -0000
@@ -136,9 +136,10 @@ Print certain system information.  With 
 static void
 print_element (char const *element)
 {
-  static int printed;
-  if (printed++)
+  static bool printed;
+  if (printed)
     putchar (' ');
+  printed = true;
   fputs (element, stdout);
 }
 
@@ -149,7 +150,7 @@ main (int argc, char **argv)
   static char const unknown[] = "unknown";
 
   /* Mask indicating which elements to print. */
-  unsigned toprint = 0;
+  unsigned int toprint = 0;
 
   initialize_main (&argc, &argv);
   program_name = argv[0];




reply via email to

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