(usage): Say that
[platform/upstream/coreutils.git] / src / split.c
index b0800c3..a02e64a 100644 (file)
@@ -1,5 +1,5 @@
 /* split.c -- split a file into pieces.
-   Copyright (C) 88, 91, 95, 1996 Free Software Foundation, Inc.
+   Copyright (C) 88, 91, 1995-2001 Free Software Foundation, Inc.
 
    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
@@ -12,8 +12,8 @@
    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, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 \f
 /* By tege@sics.se, with rms.
 
 #include <getopt.h>
 #include <sys/types.h>
 
-#if HAVE_LIMITS_H
-# include <limits.h>
-#endif
-
-#ifndef UINT_MAX
-# define UINT_MAX ((unsigned int) ~(unsigned int) 0)
-#endif
-
-#ifndef INT_MAX
-# define INT_MAX ((int) (UINT_MAX >> 1))
-#endif
-
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
+#include "full-write.h"
+#include "safe-read.h"
 #include "xstrtol.h"
 
-char *xmalloc ();
-int full_write ();
-int safe_read ();
+/* The official name of this program (e.g., no `g' prefix).  */
+#define PROGRAM_NAME "split"
+
+#define AUTHORS N_ ("Torbjorn Granlund and Richard M. Stallman")
 
 /* The name this program was run with. */
 char *program_name;
@@ -69,12 +61,6 @@ static int input_desc;
 /* Descriptor on which output file is open.  */
 static int output_desc;
 
-/* If nonzero, display usage information and exit.  */
-static int show_help;
-
-/* If nonzero, print the version on standard output then exit.  */
-static int show_version;
-
 /* If nonzero, print a diagnostic on standard error just before each
    output file is opened. */
 static int verbose;
@@ -85,12 +71,12 @@ static struct option const longopts[] =
   {"lines", required_argument, NULL, 'l'},
   {"line-bytes", required_argument, NULL, 'C'},
   {"verbose", no_argument, NULL, 2},
-  {"help", no_argument, &show_help, 1},
-  {"version", no_argument, &show_version, 1},
+  {GETOPT_HELP_OPTION_DECL},
+  {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
 };
 
-static void
+void
 usage (int status)
 {
   if (status != 0)
@@ -106,18 +92,19 @@ Usage: %s [OPTION] [INPUT [PREFIX]]\n\
 Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default\n\
 PREFIX is `x'.  With no INPUT, or when INPUT is -, read standard input.\n\
 \n\
+Mandatory arguments to long options are mandatory for short options too.\n\
   -b, --bytes=SIZE        put SIZE bytes per output file\n\
   -C, --line-bytes=SIZE   put at most SIZE bytes of lines per output file\n\
   -l, --lines=NUMBER      put NUMBER lines per output file\n\
   -NUMBER                 same as -l NUMBER\n\
       --verbose           print a diagnostic to standard error just\n\
-                           before each output file is opened\n\
+                            before each output file is opened\n\
       --help              display this help and exit\n\
       --version           output version information and exit\n\
 \n\
 SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.\n\
 "));
-      puts (_("\nReport bugs to textutils-bugs@gnu.ai.mit.edu"));
+      puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
     }
   exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
@@ -128,40 +115,30 @@ SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.\n\
 static void
 next_file_name (void)
 {
-  int x;
-  char *ne;
-  unsigned int i;
+  static unsigned n_digits = 2;
+  char *p;
 
-  static int first_call = 1;
+  /* Change any suffix of `z's to `a's.  */
+  for (p = outfile_end - 1; *p == 'z'; p--)
+    {
+      *p = 'a';
+    }
 
-  /* Status for outfile name generation.  */
-  static unsigned outfile_count = 0;
-  static unsigned outfile_name_limit = 25 * 26;
-  static unsigned outfile_name_generation = 1;
+  /* Increment the rightmost non-`z' character that was present before the
+     above z/a substitutions.  There is guaranteed to be such a character.  */
+  ++(*p);
 
-  if (!first_call)
-    outfile_count++;
-  first_call = 0;
-  if (outfile_count < outfile_name_limit)
+  /* If the result of that increment operation yielded a `z' and there
+     are only `z's to the left of it, then append two more `a' characters
+     to the end and add 1 (-1 + 2) to the number of digits (we're taking
+     out this `z' and adding two `a's).  */
+  if (*p == 'z' && p == outfile_mid)
     {
-      for (ne = outfile_end - 1; ; ne--)
-       {
-         x = *ne;
-         if (x != 'z')
-           break;
-         *ne = 'a';
-       }
-      *ne = x + 1;
-      return;
+      ++n_digits;
+      ++outfile_mid;
+      *outfile_end++ = 'a';
+      *outfile_end++ = 'a';
     }
-
-  outfile_count = 0;
-  outfile_name_limit *= 26;
-  outfile_name_generation++;
-  *outfile_mid++ = 'z';
-  for (i = 0; i <= outfile_name_generation; i++)
-    outfile_mid[i] = 'a';
-  outfile_end += 2;
 }
 
 /* Write BYTES bytes at BP to an output file.
@@ -179,11 +156,12 @@ cwrite (int new_file_flag, const char *bp, int bytes)
       next_file_name ();
       if (verbose)
        fprintf (stderr, _("creating file `%s'\n"), outfile);
-      output_desc = open (outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+      output_desc = open (outfile,
+                         O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
       if (output_desc < 0)
        error (EXIT_FAILURE, errno, "%s", outfile);
     }
-  if (full_write (output_desc, bp, bytes) < 0)
+  if (full_write (output_desc, bp, bytes) != bytes)
     error (EXIT_FAILURE, errno, "%s", outfile);
 }
 
@@ -371,6 +349,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   /* Parse command line options.  */
 
   infile = "-";
@@ -466,20 +446,15 @@ main (int argc, char **argv)
          verbose = 1;
          break;
 
+       case_GETOPT_HELP_CHAR;
+
+       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
+
        default:
          usage (EXIT_FAILURE);
        }
     }
 
-  if (show_version)
-    {
-      printf ("split (%s) %s\n", GNU_PACKAGE, VERSION);
-      exit (EXIT_SUCCESS);
-    }
-
-  if (show_help)
-    usage (0);
-
   /* Handle default case.  */
   if (split_type == type_undef)
     {
@@ -509,7 +484,7 @@ main (int argc, char **argv)
     }
 
   /* Open the input file.  */
-  if (!strcmp (infile, "-"))
+  if (STREQ (infile, "-"))
     input_desc = 0;
   else
     {
@@ -517,6 +492,8 @@ main (int argc, char **argv)
       if (input_desc < 0)
        error (EXIT_FAILURE, errno, "%s", infile);
     }
+  /* Binary I/O is safer when bytecounts are used.  */
+  SET_BINARY (input_desc);
 
   /* No output file is open now.  */
   output_desc = -1;