dd: clarify meaning of multiplication factors; put xM in order
[platform/upstream/coreutils.git] / src / tsort.c
index 6f16baf..703b855 100644 (file)
@@ -1,10 +1,10 @@
 /* tsort - topological sort.
-   Copyright (C) 1998-2005 Free Software Foundation, Inc.
+   Copyright (C) 1998-2005, 2007-2008 Free Software Foundation, Inc.
 
-   This program is free software; you can redistribute it and/or modify
+   This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -12,8 +12,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* Written by Mark Kettenis <kettenis@phys.uva.nl>.  */
 
@@ -37,7 +36,7 @@
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "tsort"
 
-#define AUTHORS "Mark Kettenis"
+#define AUTHORS proper_name ("Mark Kettenis")
 
 /* Token delimiters when reading from a file.  */
 #define DELIM " \t\n"
@@ -60,12 +59,6 @@ struct item
   struct successor *top;
 };
 
-/* The name this program was run with. */
-char *program_name;
-
-/* 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;
 
@@ -94,7 +87,7 @@ With no FILE, or when FILE is -, read standard input.\n\
 "), program_name);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
-      printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
+      emit_bug_reporting_address ();
     }
 
   exit (status);
@@ -442,30 +435,21 @@ tsort (const char *file)
   struct item *root;
   struct item *j = NULL;
   struct item *k = NULL;
-  FILE *fp;
   token_buffer tokenbuffer;
+  bool is_stdin = STREQ (file, "-");
 
   /* Intialize the head of the tree will hold the strings we're sorting.  */
   root = new_item (NULL);
 
-  if (STREQ (file, "-"))
-    {
-      fp = stdin;
-      have_read_stdin = true;
-    }
-  else
-    {
-      fp = fopen (file, "r");
-      if (fp == NULL)
-       error (EXIT_FAILURE, errno, "%s", file);
-    }
+  if (!is_stdin && ! freopen (file, "r", stdin))
+    error (EXIT_FAILURE, errno, "%s", file);
 
   init_tokenbuffer (&tokenbuffer);
 
   while (1)
     {
       /* T2. Next Relation.  */
-      size_t len = readtoken (fp, DELIM, sizeof (DELIM) - 1, &tokenbuffer);
+      size_t len = readtoken (stdin, DELIM, sizeof (DELIM) - 1, &tokenbuffer);
       if (len == (size_t) -1)
        break;
 
@@ -499,7 +483,7 @@ tsort (const char *file)
          struct successor *p = head->top;
 
          /* T5. Output front of queue.  */
-         printf ("%s\n", head->str);
+         puts (head->str);
          head->str = NULL;     /* Avoid printing the same string twice.  */
          n_strings--;
 
@@ -534,6 +518,10 @@ tsort (const char *file)
        }
     }
 
+  if (fclose (stdin) != 0)
+    error (EXIT_FAILURE, errno, "%s",
+          is_stdin ? _("standard input") : quote (file));
+
   return ok;
 }
 
@@ -543,7 +531,7 @@ main (int argc, char **argv)
   bool ok;
 
   initialize_main (&argc, &argv);
-  program_name = argv[0];
+  set_program_name (argv[0]);
   setlocale (LC_ALL, "");
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
@@ -555,8 +543,6 @@ main (int argc, char **argv)
   if (getopt_long (argc, argv, "", NULL, NULL) != -1)
     usage (EXIT_FAILURE);
 
-  have_read_stdin = false;
-
   if (1 < argc - optind)
     {
       error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
@@ -565,8 +551,5 @@ main (int argc, char **argv)
 
   ok = tsort (optind == argc ? "-" : argv[optind]);
 
-  if (have_read_stdin && fclose (stdin) == EOF)
-    error (EXIT_FAILURE, errno, _("standard input"));
-
   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }