Reorder functions to obviate forward dcls.
authorJim Meyering <jim@meyering.net>
Tue, 31 Oct 1995 12:34:48 +0000 (12:34 +0000)
committerJim Meyering <jim@meyering.net>
Tue, 31 Oct 1995 12:34:48 +0000 (12:34 +0000)
Remove forward dcls.

src/split.c

index 4464aa2..ca9a0b7 100644 (file)
@@ -34,15 +34,6 @@ char *xmalloc ();
 int full_write ();
 int safe_read ();
 
-static int convint ();
-static int isdigits ();
-static int stdread ();
-static void line_bytes_split ();
-static void bytes_split ();
-static void cwrite ();
-static void lines_split ();
-static void next_file_name ();
-
 /* The name this program was run with. */
 char *program_name;
 
@@ -115,184 +106,6 @@ SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.\n\
   exit (status);
 }
 
-void
-main (argc, argv)
-     int argc;
-     char *argv[];
-{
-  struct stat stat_buf;
-  int num;                     /* numeric argument from command line */
-  enum
-    {
-      type_undef, type_bytes, type_byteslines, type_lines, type_digits
-    } split_type = type_undef;
-  int in_blk_size;             /* optimal block size of input file device */
-  char *buf;                   /* file i/o buffer */
-  int accum = 0;
-  char *outbase;
-  int c;
-  int digits_optind = 0;
-
-  program_name = argv[0];
-
-  /* Parse command line options.  */
-
-  infile = "-";
-  outbase = "x";
-
-  while (1)
-    {
-      /* This is the argv-index of the option we will read next.  */
-      int this_optind = optind ? optind : 1;
-
-      c = getopt_long (argc, argv, "0123456789b:l:C:", longopts, (int *) 0);
-      if (c == EOF)
-       break;
-
-      switch (c)
-       {
-       case 0:
-         break;
-
-       case 'b':
-         if (split_type != type_undef)
-           usage (2, _("cannot split in more than one way"));
-         split_type = type_bytes;
-         if (convint (optarg, &accum) == -1)
-           usage (2, _("invalid number of bytes"));
-         break;
-
-       case 'l':
-         if (split_type != type_undef)
-           usage (2, _("cannot split in more than one way"));
-         split_type = type_lines;
-         if (!isdigits (optarg))
-           usage (2, _("invalid number of lines"));
-         accum = atoi (optarg);
-         break;
-
-       case 'C':
-         if (split_type != type_undef)
-           usage (2, _("cannot split in more than one way"));
-         split_type = type_byteslines;
-         if (convint (optarg, &accum) == -1)
-           usage (2, _("invalid number of bytes"));
-         break;
-
-       case '0':
-       case '1':
-       case '2':
-       case '3':
-       case '4':
-       case '5':
-       case '6':
-       case '7':
-       case '8':
-       case '9':
-         if (split_type != type_undef && split_type != type_digits)
-           usage (2, _("cannot split in more than one way"));
-         if (digits_optind != 0 && digits_optind != this_optind)
-           accum = 0;          /* More than one number given; ignore other. */
-         digits_optind = this_optind;
-         split_type = type_digits;
-         accum = accum * 10 + c - '0';
-         break;
-
-       default:
-         usage (2, (char *)0);
-       }
-    }
-
-  if (show_version)
-    {
-      printf ("split - %s\n", version_string);
-      exit (0);
-    }
-
-  if (show_help)
-    usage (0, (char *)0);
-
-  /* Handle default case.  */
-  if (split_type == type_undef)
-    {
-      split_type = type_lines;
-      accum = 1000;
-    }
-
-  if (accum < 1)
-    usage (2, _("invalid number"));
-  num = accum;
-
-  /* Get out the filename arguments.  */
-
-  if (optind < argc)
-    infile = argv[optind++];
-
-  if (optind < argc)
-    outbase = argv[optind++];
-
-  if (optind < argc)
-    usage (2, _("too many arguments"));
-
-  /* Open the input file.  */
-  if (!strcmp (infile, "-"))
-    input_desc = 0;
-  else
-    {
-      input_desc = open (infile, O_RDONLY);
-      if (input_desc < 0)
-       error (1, errno, "%s", infile);
-    }
-
-  /* No output file is open now.  */
-  output_desc = -1;
-
-  /* Copy the output file prefix so we can add suffixes to it.
-     26**29 is certainly enough output files!  */
-
-  outfile = xmalloc (strlen (outbase) + 30);
-  strcpy (outfile, outbase);
-  outfile_mid = outfile + strlen (outfile);
-  outfile_end = outfile_mid + 2;
-  memset (outfile_mid, 0, 30);
-  outfile_mid[0] = 'a';
-  outfile_mid[1] = 'a' - 1;  /* first call to next_file_name makes it an 'a' */
-
-  /* Get the optimal block size of input device and make a buffer.  */
-
-  if (fstat (input_desc, &stat_buf) < 0)
-    error (1, errno, "%s", infile);
-  in_blk_size = ST_BLKSIZE (stat_buf);
-
-  buf = xmalloc (in_blk_size + 1);
-
-  switch (split_type)
-    {
-    case type_digits:
-    case type_lines:
-      lines_split (num, buf, in_blk_size);
-      break;
-
-    case type_bytes:
-      bytes_split (num, buf, in_blk_size);
-      break;
-
-    case type_byteslines:
-      line_bytes_split (num);
-      break;
-
-    default:
-      abort ();
-    }
-
-  if (close (input_desc) < 0)
-    error (1, errno, "%s", infile);
-  if (output_desc >= 0 && close (output_desc) < 0)
-    error (1, errno, "%s", outfile);
-
-  exit (0);
-}
-
 /* Return nonzero if the string STR is composed entirely of decimal digits.  */
 
 static int
@@ -345,7 +158,98 @@ convint (str, val)
   *val = atoi (str) * multiplier;
   return 0;
 }
-\f
+
+/* Compute the next sequential output file name suffix and store it
+   into the string `outfile' at the position pointed to by `outfile_mid'.  */
+
+static void
+next_file_name ()
+{
+  int x;
+  char *ne;
+  unsigned int i;
+
+  static int first_call = 1;
+
+  /* Status for outfile name generation.  */
+  static unsigned outfile_count = 0;
+  static unsigned outfile_name_limit = 25 * 26;
+  static unsigned outfile_name_generation = 1;
+
+  if (!first_call)
+    outfile_count++;
+  first_call = 0;
+  if (outfile_count < outfile_name_limit)
+    {
+      for (ne = outfile_end - 1; ; ne--)
+       {
+         x = *ne;
+         if (x != 'z')
+           break;
+         *ne = 'a';
+       }
+      *ne = x + 1;
+      return;
+    }
+
+  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.
+   If NEW_FILE_FLAG is nonzero, open the next output file.
+   Otherwise add to the same output file already in use.  */
+
+static void
+cwrite (new_file_flag, bp, bytes)
+     int new_file_flag;
+     char *bp;
+     int bytes;
+{
+  if (new_file_flag)
+    {
+      if (output_desc >= 0 && close (output_desc) < 0)
+       error (1, errno, "%s", outfile);
+
+      next_file_name ();
+      output_desc = open (outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+      if (output_desc < 0)
+       error (1, errno, "%s", outfile);
+    }
+  if (full_write (output_desc, bp, bytes) < 0)
+    error (1, errno, "%s", outfile);
+}
+
+/* Read NCHARS bytes from the input file into BUF.
+   Return the number of bytes successfully read.
+   If this is less than NCHARS, do not call `stdread' again.  */
+
+static int
+stdread (buf, nchars)
+     char *buf;
+     int nchars;
+{
+  int n_read;
+  int to_be_read = nchars;
+
+  while (to_be_read)
+    {
+      n_read = safe_read (input_desc, buf, to_be_read);
+      if (n_read < 0)
+       return -1;
+      if (n_read == 0)
+       break;
+      to_be_read -= n_read;
+      buf += n_read;
+    }
+  return nchars - to_be_read;
+}
+
 /* Split into pieces of exactly NCHARS bytes.
    Use buffer BUF, whose size is BUFSIZE.  */
 
@@ -392,7 +296,7 @@ bytes_split (nchars, buf, bufsize)
     }
   while (n_read == bufsize);
 }
-\f
+
 /* Split into pieces of exactly NLINES lines.
    Use buffer BUF, whose size is BUFSIZE.  */
 
@@ -492,94 +396,184 @@ line_bytes_split (nchars)
   while (!eof);
   free (buf);
 }
-\f
-/* Write BYTES bytes at BP to an output file.
-   If NEW_FILE_FLAG is nonzero, open the next output file.
-   Otherwise add to the same output file already in use.  */
 
-static void
-cwrite (new_file_flag, bp, bytes)
-     int new_file_flag;
-     char *bp;
-     int bytes;
+void
+main (argc, argv)
+     int argc;
+     char *argv[];
 {
-  if (new_file_flag)
+  struct stat stat_buf;
+  int num;                     /* numeric argument from command line */
+  enum
     {
-      if (output_desc >= 0 && close (output_desc) < 0)
-       error (1, errno, "%s", outfile);
+      type_undef, type_bytes, type_byteslines, type_lines, type_digits
+    } split_type = type_undef;
+  int in_blk_size;             /* optimal block size of input file device */
+  char *buf;                   /* file i/o buffer */
+  int accum = 0;
+  char *outbase;
+  int c;
+  int digits_optind = 0;
 
-      next_file_name ();
-      output_desc = open (outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
-      if (output_desc < 0)
-       error (1, errno, "%s", outfile);
-    }
-  if (full_write (output_desc, bp, bytes) < 0)
-    error (1, errno, "%s", outfile);
-}
+  program_name = argv[0];
 
-/* Read NCHARS bytes from the input file into BUF.
-   Return the number of bytes successfully read.
-   If this is less than NCHARS, do not call `stdread' again.  */
+  /* Parse command line options.  */
 
-static int
-stdread (buf, nchars)
-     char *buf;
-     int nchars;
-{
-  int n_read;
-  int to_be_read = nchars;
+  infile = "-";
+  outbase = "x";
 
-  while (to_be_read)
+  while (1)
     {
-      n_read = safe_read (input_desc, buf, to_be_read);
-      if (n_read < 0)
-       return -1;
-      if (n_read == 0)
+      /* This is the argv-index of the option we will read next.  */
+      int this_optind = optind ? optind : 1;
+
+      c = getopt_long (argc, argv, "0123456789b:l:C:", longopts, (int *) 0);
+      if (c == EOF)
        break;
-      to_be_read -= n_read;
-      buf += n_read;
+
+      switch (c)
+       {
+       case 0:
+         break;
+
+       case 'b':
+         if (split_type != type_undef)
+           usage (2, _("cannot split in more than one way"));
+         split_type = type_bytes;
+         /* FIXME: use xstrtoul */
+         if (convint (optarg, &accum) == -1)
+           usage (2, _("invalid number of bytes"));
+         break;
+
+       case 'l':
+         if (split_type != type_undef)
+           usage (2, _("cannot split in more than one way"));
+         split_type = type_lines;
+         if (!isdigits (optarg))
+           usage (2, _("invalid number of lines"));
+         /* FIXME: use xstrtoul */
+         accum = atoi (optarg);
+         break;
+
+       case 'C':
+         if (split_type != type_undef)
+           usage (2, _("cannot split in more than one way"));
+         split_type = type_byteslines;
+         /* FIXME: use xstrtoul */
+         if (convint (optarg, &accum) == -1)
+           usage (2, _("invalid number of bytes"));
+         break;
+
+       case '0':
+       case '1':
+       case '2':
+       case '3':
+       case '4':
+       case '5':
+       case '6':
+       case '7':
+       case '8':
+       case '9':
+         if (split_type != type_undef && split_type != type_digits)
+           usage (2, _("cannot split in more than one way"));
+         if (digits_optind != 0 && digits_optind != this_optind)
+           accum = 0;          /* More than one number given; ignore other. */
+         digits_optind = this_optind;
+         split_type = type_digits;
+         accum = accum * 10 + c - '0';
+         break;
+
+       default:
+         usage (2, (char *)0);
+       }
     }
-  return nchars - to_be_read;
-}
 
-/* Compute the next sequential output file name suffix and store it
-   into the string `outfile' at the position pointed to by `outfile_mid'.  */
+  if (show_version)
+    {
+      printf ("split - %s\n", version_string);
+      exit (0);
+    }
 
-static void
-next_file_name ()
-{
-  int x;
-  char *ne;
-  unsigned int i;
+  if (show_help)
+    usage (0, (char *)0);
 
-  static int first_call = 1;
+  /* Handle default case.  */
+  if (split_type == type_undef)
+    {
+      split_type = type_lines;
+      accum = 1000;
+    }
 
-  /* Status for outfile name generation.  */
-  static unsigned outfile_count = 0;
-  static unsigned outfile_name_limit = 25 * 26;
-  static unsigned outfile_name_generation = 1;
+  if (accum < 1)
+    usage (2, _("invalid number"));
+  num = accum;
 
-  if (!first_call)
-    outfile_count++;
-  first_call = 0;
-  if (outfile_count < outfile_name_limit)
+  /* Get out the filename arguments.  */
+
+  if (optind < argc)
+    infile = argv[optind++];
+
+  if (optind < argc)
+    outbase = argv[optind++];
+
+  if (optind < argc)
+    usage (2, _("too many arguments"));
+
+  /* Open the input file.  */
+  if (!strcmp (infile, "-"))
+    input_desc = 0;
+  else
     {
-      for (ne = outfile_end - 1; ; ne--)
-       {
-         x = *ne;
-         if (x != 'z')
-           break;
-         *ne = 'a';
-       }
-      *ne = x + 1;
-      return;
+      input_desc = open (infile, O_RDONLY);
+      if (input_desc < 0)
+       error (1, errno, "%s", infile);
     }
 
-  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;
+  /* No output file is open now.  */
+  output_desc = -1;
+
+  /* Copy the output file prefix so we can add suffixes to it.
+     26**29 is certainly enough output files!  */
+
+  outfile = xmalloc (strlen (outbase) + 30);
+  strcpy (outfile, outbase);
+  outfile_mid = outfile + strlen (outfile);
+  outfile_end = outfile_mid + 2;
+  memset (outfile_mid, 0, 30);
+  outfile_mid[0] = 'a';
+  outfile_mid[1] = 'a' - 1;  /* first call to next_file_name makes it an 'a' */
+
+  /* Get the optimal block size of input device and make a buffer.  */
+
+  if (fstat (input_desc, &stat_buf) < 0)
+    error (1, errno, "%s", infile);
+  in_blk_size = ST_BLKSIZE (stat_buf);
+
+  buf = xmalloc (in_blk_size + 1);
+
+  switch (split_type)
+    {
+    case type_digits:
+    case type_lines:
+      lines_split (num, buf, in_blk_size);
+      break;
+
+    case type_bytes:
+      bytes_split (num, buf, in_blk_size);
+      break;
+
+    case type_byteslines:
+      line_bytes_split (num);
+      break;
+
+    default:
+      abort ();
+    }
+
+  if (close (input_desc) < 0)
+    error (1, errno, "%s", infile);
+  if (output_desc >= 0 && close (output_desc) < 0)
+    error (1, errno, "%s", outfile);
+
+  exit (0);
 }