/* split.c -- split a file into pieces.
- Copyright (C) 88, 91, 95, 96, 1997, 1998 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
#include <sys/types.h>
#include "system.h"
+#include "closeout.h"
#include "error.h"
-#include "xstrtol.h"
+#include "full-write.h"
#include "safe-read.h"
+#include "xstrtol.h"
-int full_write ();
+/* 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;
/* 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;
{"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)
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\
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);
}
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
+ atexit (close_stdout);
+
/* Parse command line options. */
infile = "-";
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)
{
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;