(struct item, n_strings): Use size_t for sizes.
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 3 Aug 2004 23:13:20 +0000 (23:13 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 3 Aug 2004 23:13:20 +0000 (23:13 +0000)
(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/tsort.c

index 07a0770..5d12a3d 100644 (file)
@@ -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 item *k)
     }
 }
 
-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 (*action) (struct item *))
     {
       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);
 }