1992-11-08 02:50:43 +00:00
|
|
|
|
/* split.c -- split a file into pieces.
|
2003-02-19 14:28:50 +00:00
|
|
|
|
Copyright (C) 88, 91, 1995-2003 Free Software Foundation, Inc.
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
1997-01-25 06:11:48 +00:00
|
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
|
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
/* By tege@sics.se, with rms.
|
|
|
|
|
|
|
|
|
|
|
|
To do:
|
|
|
|
|
|
* Implement -t CHAR or -t REGEX to specify break characters other
|
|
|
|
|
|
than newline. */
|
|
|
|
|
|
|
1993-10-21 22:08:53 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
#include <sys/types.h>
|
1995-10-31 12:52:29 +00:00
|
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
|
#include "system.h"
|
2000-05-20 22:06:38 +00:00
|
|
|
|
#include "closeout.h"
|
2002-02-12 15:44:16 +00:00
|
|
|
|
#include "dirname.h"
|
1994-12-16 05:42:47 +00:00
|
|
|
|
#include "error.h"
|
2003-02-19 14:28:50 +00:00
|
|
|
|
#include "full-read.h"
|
2001-08-31 07:46:28 +00:00
|
|
|
|
#include "full-write.h"
|
2002-02-16 08:04:12 +00:00
|
|
|
|
#include "posixver.h"
|
1998-04-11 18:24:09 +00:00
|
|
|
|
#include "safe-read.h"
|
1999-03-04 05:36:10 +00:00
|
|
|
|
#include "xstrtol.h"
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1999-04-03 05:01:48 +00:00
|
|
|
|
/* The official name of this program (e.g., no `g' prefix). */
|
|
|
|
|
|
#define PROGRAM_NAME "split"
|
|
|
|
|
|
|
2001-08-13 10:33:28 +00:00
|
|
|
|
#define AUTHORS N_ ("Torbjorn Granlund and Richard M. Stallman")
|
1999-04-03 05:22:05 +00:00
|
|
|
|
|
2002-02-12 15:46:34 +00:00
|
|
|
|
#define DEFAULT_SUFFIX_LENGTH 2
|
|
|
|
|
|
|
1993-04-29 05:26:22 +00:00
|
|
|
|
/* The name this program was run with. */
|
1992-11-08 02:50:43 +00:00
|
|
|
|
char *program_name;
|
|
|
|
|
|
|
|
|
|
|
|
/* Base name of output files. */
|
2002-02-12 15:44:16 +00:00
|
|
|
|
static char const *outbase;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2002-02-12 15:44:16 +00:00
|
|
|
|
/* Name of output files. */
|
|
|
|
|
|
static char *outfile;
|
2002-02-12 15:46:34 +00:00
|
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
|
/* Pointer to the end of the prefix in OUTFILE.
|
|
|
|
|
|
Suffixes are inserted here. */
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static char *outfile_mid;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2002-02-12 15:44:16 +00:00
|
|
|
|
/* Length of OUTFILE's suffix. */
|
2002-02-12 15:46:34 +00:00
|
|
|
|
static size_t suffix_length = DEFAULT_SUFFIX_LENGTH;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
/* Name of input file. May be "-". */
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static char *infile;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
/* Descriptor on which input file is open. */
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static int input_desc;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
/* Descriptor on which output file is open. */
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static int output_desc;
|
1993-04-29 05:26:22 +00:00
|
|
|
|
|
1995-11-27 03:09:18 +00:00
|
|
|
|
/* If nonzero, print a diagnostic on standard error just before each
|
1995-11-07 04:56:08 +00:00
|
|
|
|
output file is opened. */
|
|
|
|
|
|
static int verbose;
|
|
|
|
|
|
|
1993-04-29 05:26:22 +00:00
|
|
|
|
static struct option const longopts[] =
|
|
|
|
|
|
{
|
|
|
|
|
|
{"bytes", required_argument, NULL, 'b'},
|
|
|
|
|
|
{"lines", required_argument, NULL, 'l'},
|
|
|
|
|
|
{"line-bytes", required_argument, NULL, 'C'},
|
2002-02-12 15:44:16 +00:00
|
|
|
|
{"suffix-length", required_argument, NULL, 'a'},
|
2003-03-11 20:48:36 +00:00
|
|
|
|
{"verbose", no_argument, &verbose, 0},
|
1999-04-04 15:44:26 +00:00
|
|
|
|
{GETOPT_HELP_OPTION_DECL},
|
|
|
|
|
|
{GETOPT_VERSION_OPTION_DECL},
|
1993-04-29 05:26:22 +00:00
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
1999-01-14 18:25:16 +00:00
|
|
|
|
void
|
1996-10-13 17:53:47 +00:00
|
|
|
|
usage (int status)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
1993-10-23 15:37:19 +00:00
|
|
|
|
if (status != 0)
|
1995-08-07 14:57:29 +00:00
|
|
|
|
fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
1993-10-23 15:37:19 +00:00
|
|
|
|
program_name);
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
1995-08-07 14:57:29 +00:00
|
|
|
|
printf (_("\
|
1993-10-23 15:37:19 +00:00
|
|
|
|
Usage: %s [OPTION] [INPUT [PREFIX]]\n\
|
1995-08-07 14:57:29 +00:00
|
|
|
|
"),
|
1993-10-23 15:37:19 +00:00
|
|
|
|
program_name);
|
2001-11-11 14:47:28 +00:00
|
|
|
|
fputs (_("\
|
1995-05-13 18:34:54 +00:00
|
|
|
|
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\
|
1993-10-23 15:37:19 +00:00
|
|
|
|
\n\
|
2001-11-23 19:58:23 +00:00
|
|
|
|
"), stdout);
|
|
|
|
|
|
fputs (_("\
|
2001-11-04 09:43:16 +00:00
|
|
|
|
Mandatory arguments to long options are mandatory for short options too.\n\
|
2001-11-23 19:58:23 +00:00
|
|
|
|
"), stdout);
|
2002-02-12 15:46:34 +00:00
|
|
|
|
fprintf (stdout, _("\
|
|
|
|
|
|
-a, --suffix-length=N use suffixes of length N (default %d)\n\
|
1993-10-24 20:00:39 +00:00
|
|
|
|
-b, --bytes=SIZE put SIZE bytes per output file\n\
|
1996-11-23 22:06:55 +00:00
|
|
|
|
-C, --line-bytes=SIZE put at most SIZE bytes of lines per output file\n\
|
1993-10-24 20:00:39 +00:00
|
|
|
|
-l, --lines=NUMBER put NUMBER lines per output file\n\
|
2002-02-12 15:46:34 +00:00
|
|
|
|
"), DEFAULT_SUFFIX_LENGTH);
|
Add more support for POSIX 1003.1-2001, which requires removal for
support of obsolete "-N" option syntax in expand, head, fold,
split, tail, unexpand, uniq, and which prohibits options with
optional arguments in od and pr.
(usage): Document this.
(main): Check for obsolete options.
(shortopts): New constant.
(main): Use -1, not EOF, for getopt_long.
2002-02-02 09:40:50 +00:00
|
|
|
|
fputs (_("\
|
1995-11-07 04:56:08 +00:00
|
|
|
|
--verbose print a diagnostic to standard error just\n\
|
1997-12-29 22:09:28 +00:00
|
|
|
|
before each output file is opened\n\
|
2001-11-23 19:58:23 +00:00
|
|
|
|
"), stdout);
|
2001-12-01 17:41:25 +00:00
|
|
|
|
fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|
|
|
|
|
fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
2001-11-23 19:58:23 +00:00
|
|
|
|
fputs (_("\
|
1993-10-23 15:37:19 +00:00
|
|
|
|
\n\
|
|
|
|
|
|
SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.\n\
|
2001-11-11 14:47:28 +00:00
|
|
|
|
"), stdout);
|
2002-07-02 09:06:33 +00:00
|
|
|
|
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
|
1993-10-23 15:37:19 +00:00
|
|
|
|
}
|
1996-03-24 14:58:01 +00:00
|
|
|
|
exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2002-02-12 15:44:16 +00:00
|
|
|
|
/* Compute the next sequential output file name and store it into the
|
|
|
|
|
|
string `outfile'. */
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
|
|
|
|
|
static void
|
1995-10-31 12:36:43 +00:00
|
|
|
|
next_file_name (void)
|
1995-10-31 12:34:48 +00:00
|
|
|
|
{
|
2002-02-12 15:44:16 +00:00
|
|
|
|
if (! outfile)
|
1998-01-18 11:18:08 +00:00
|
|
|
|
{
|
2002-02-12 15:44:16 +00:00
|
|
|
|
/* Allocate and initialize the first file name. */
|
|
|
|
|
|
|
|
|
|
|
|
size_t outbase_length = strlen (outbase);
|
|
|
|
|
|
size_t outfile_length = outbase_length + suffix_length;
|
|
|
|
|
|
if (outfile_length + 1 < outbase_length)
|
|
|
|
|
|
xalloc_die ();
|
|
|
|
|
|
outfile = xmalloc (outfile_length + 1);
|
|
|
|
|
|
outfile_mid = outfile + outbase_length;
|
|
|
|
|
|
memcpy (outfile, outbase, outbase_length);
|
|
|
|
|
|
memset (outfile_mid, 'a', suffix_length);
|
|
|
|
|
|
outfile[outfile_length] = 0;
|
|
|
|
|
|
|
|
|
|
|
|
#if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
|
|
|
|
|
|
/* POSIX requires that if the output file name is too long for
|
|
|
|
|
|
its directory, `split' must fail without creating any files.
|
|
|
|
|
|
This must be checked for explicitly on operating systems that
|
|
|
|
|
|
silently truncate file names. */
|
|
|
|
|
|
{
|
|
|
|
|
|
char *dir = dir_name (outfile);
|
|
|
|
|
|
long name_max = pathconf (dir, _PC_NAME_MAX);
|
|
|
|
|
|
if (0 <= name_max && name_max < base_len (base_name (outfile)))
|
|
|
|
|
|
error (EXIT_FAILURE, ENAMETOOLONG, "%s", outfile);
|
|
|
|
|
|
free (dir);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
1998-01-18 11:18:08 +00:00
|
|
|
|
}
|
2002-02-12 15:44:16 +00:00
|
|
|
|
else
|
1995-10-31 12:34:48 +00:00
|
|
|
|
{
|
2002-02-12 15:44:16 +00:00
|
|
|
|
/* Increment the suffix in place, if possible. */
|
|
|
|
|
|
|
|
|
|
|
|
char *p;
|
|
|
|
|
|
for (p = outfile_mid + suffix_length; outfile_mid < p; *--p = 'a')
|
|
|
|
|
|
if (p[-1]++ != 'z')
|
|
|
|
|
|
return;
|
|
|
|
|
|
error (EXIT_FAILURE, 0, _("Output file suffixes exhausted"));
|
1995-10-31 12:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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
|
2002-10-19 16:34:25 +00:00
|
|
|
|
cwrite (int new_file_flag, const char *bp, size_t bytes)
|
1995-10-31 12:34:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (new_file_flag)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (output_desc >= 0 && close (output_desc) < 0)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", outfile);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
|
|
|
|
|
next_file_name ();
|
1995-11-07 04:56:08 +00:00
|
|
|
|
if (verbose)
|
|
|
|
|
|
fprintf (stderr, _("creating file `%s'\n"), outfile);
|
1999-01-01 22:49:44 +00:00
|
|
|
|
output_desc = open (outfile,
|
|
|
|
|
|
O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
if (output_desc < 0)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", outfile);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
}
|
2002-10-19 16:34:25 +00:00
|
|
|
|
if (full_write (output_desc, bp, bytes) != bytes)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", outfile);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2003-04-08 09:39:08 +00:00
|
|
|
|
/* Split into pieces of exactly N_BYTES bytes.
|
1992-11-08 02:50:43 +00:00
|
|
|
|
Use buffer BUF, whose size is BUFSIZE. */
|
|
|
|
|
|
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static void
|
2003-04-08 09:30:09 +00:00
|
|
|
|
bytes_split (size_t n_bytes, char *buf, size_t bufsize)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t n_read;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
int new_file_flag = 1;
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t to_read;
|
2003-04-08 09:30:09 +00:00
|
|
|
|
size_t to_write = n_bytes;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
char *bp_out;
|
|
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
2003-02-19 14:28:50 +00:00
|
|
|
|
n_read = full_read (input_desc, buf, bufsize);
|
2002-10-19 16:34:25 +00:00
|
|
|
|
if (n_read == SAFE_READ_ERROR)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
bp_out = buf;
|
|
|
|
|
|
to_read = n_read;
|
|
|
|
|
|
for (;;)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (to_read < to_write)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (to_read) /* do not write 0 bytes! */
|
|
|
|
|
|
{
|
|
|
|
|
|
cwrite (new_file_flag, bp_out, to_read);
|
|
|
|
|
|
to_write -= to_read;
|
|
|
|
|
|
new_file_flag = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2003-02-19 14:28:50 +00:00
|
|
|
|
|
|
|
|
|
|
cwrite (new_file_flag, bp_out, to_write);
|
|
|
|
|
|
bp_out += to_write;
|
|
|
|
|
|
to_read -= to_write;
|
|
|
|
|
|
new_file_flag = 1;
|
2003-04-08 09:30:09 +00:00
|
|
|
|
to_write = n_bytes;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
while (n_read == bufsize);
|
|
|
|
|
|
}
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
|
/* Split into pieces of exactly NLINES lines.
|
|
|
|
|
|
Use buffer BUF, whose size is BUFSIZE. */
|
|
|
|
|
|
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static void
|
2002-10-19 16:34:25 +00:00
|
|
|
|
lines_split (size_t nlines, char *buf, size_t bufsize)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t n_read;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
char *bp, *bp_out, *eob;
|
|
|
|
|
|
int new_file_flag = 1;
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t n = 0;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
2003-02-19 14:28:50 +00:00
|
|
|
|
n_read = full_read (input_desc, buf, bufsize);
|
2002-10-19 16:34:25 +00:00
|
|
|
|
if (n_read == SAFE_READ_ERROR)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
bp = bp_out = buf;
|
|
|
|
|
|
eob = bp + n_read;
|
|
|
|
|
|
*eob = '\n';
|
|
|
|
|
|
for (;;)
|
|
|
|
|
|
{
|
2002-10-19 16:34:25 +00:00
|
|
|
|
bp = memchr (bp, '\n', eob - bp + 1);
|
|
|
|
|
|
if (bp == eob)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (eob != bp_out) /* do not write 0 bytes! */
|
|
|
|
|
|
{
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t len = eob - bp_out;
|
|
|
|
|
|
cwrite (new_file_flag, bp_out, len);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
new_file_flag = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2003-02-19 14:28:50 +00:00
|
|
|
|
|
|
|
|
|
|
++bp;
|
|
|
|
|
|
if (++n >= nlines)
|
|
|
|
|
|
{
|
|
|
|
|
|
cwrite (new_file_flag, bp_out, bp - bp_out);
|
|
|
|
|
|
bp_out = bp;
|
|
|
|
|
|
new_file_flag = 1;
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
while (n_read == bufsize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Split into pieces that are as large as possible while still not more
|
2003-04-08 09:39:08 +00:00
|
|
|
|
than N_BYTES bytes, and are split on line boundaries except
|
|
|
|
|
|
where lines longer than N_BYTES bytes occur. */
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static void
|
2003-04-08 09:30:09 +00:00
|
|
|
|
line_bytes_split (size_t n_bytes)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t n_read;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
char *bp;
|
|
|
|
|
|
int eof = 0;
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t n_buffered = 0;
|
2003-04-08 09:30:09 +00:00
|
|
|
|
char *buf = (char *) xmalloc (n_bytes);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Fill up the full buffer size from the input file. */
|
|
|
|
|
|
|
2003-04-08 09:30:09 +00:00
|
|
|
|
n_read = full_read (input_desc, buf + n_buffered, n_bytes - n_buffered);
|
2002-10-19 16:34:25 +00:00
|
|
|
|
if (n_read == SAFE_READ_ERROR)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
n_buffered += n_read;
|
2003-04-08 09:30:09 +00:00
|
|
|
|
if (n_buffered != n_bytes)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
eof = 1;
|
|
|
|
|
|
|
|
|
|
|
|
/* Find where to end this chunk. */
|
|
|
|
|
|
bp = buf + n_buffered;
|
2003-04-08 09:30:09 +00:00
|
|
|
|
if (n_buffered == n_bytes)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
while (bp > buf && bp[-1] != '\n')
|
|
|
|
|
|
bp--;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* If chunk has no newlines, use all the chunk. */
|
|
|
|
|
|
if (bp == buf)
|
|
|
|
|
|
bp = buf + n_buffered;
|
|
|
|
|
|
|
|
|
|
|
|
/* Output the chars as one output file. */
|
|
|
|
|
|
cwrite (1, buf, bp - buf);
|
|
|
|
|
|
|
|
|
|
|
|
/* Discard the chars we just output; move rest of chunk
|
1995-01-28 13:06:29 +00:00
|
|
|
|
down to be the start of the next chunk. Source and
|
|
|
|
|
|
destination probably overlap. */
|
1992-11-08 02:50:43 +00:00
|
|
|
|
n_buffered -= bp - buf;
|
|
|
|
|
|
if (n_buffered > 0)
|
1995-01-28 13:06:29 +00:00
|
|
|
|
memmove (buf, bp, n_buffered);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
while (!eof);
|
|
|
|
|
|
free (buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2002-09-28 16:50:34 +00:00
|
|
|
|
#define FAIL_ONLY_ONE_WAY() \
|
|
|
|
|
|
do \
|
|
|
|
|
|
{ \
|
|
|
|
|
|
error (0, 0, _("cannot split in more than one way")); \
|
|
|
|
|
|
usage (EXIT_FAILURE); \
|
|
|
|
|
|
} \
|
|
|
|
|
|
while (0)
|
|
|
|
|
|
|
1996-03-21 22:41:04 +00:00
|
|
|
|
int
|
1995-10-31 12:36:43 +00:00
|
|
|
|
main (int argc, char **argv)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
1995-10-31 12:34:48 +00:00
|
|
|
|
struct stat stat_buf;
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t num; /* numeric argument from command line */
|
1995-10-31 12:34:48 +00:00
|
|
|
|
enum
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
1995-10-31 12:34:48 +00:00
|
|
|
|
type_undef, type_bytes, type_byteslines, type_lines, type_digits
|
|
|
|
|
|
} split_type = type_undef;
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t in_blk_size; /* optimal block size of input file device */
|
1995-10-31 12:34:48 +00:00
|
|
|
|
char *buf; /* file i/o buffer */
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t accum = 0;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
int c;
|
|
|
|
|
|
int digits_optind = 0;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
program_name = argv[0];
|
1996-03-09 20:19:13 +00:00
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
|
|
textdomain (PACKAGE);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2000-05-20 22:06:38 +00:00
|
|
|
|
atexit (close_stdout);
|
|
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
/* Parse command line options. */
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
infile = "-";
|
|
|
|
|
|
outbase = "x";
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
while (1)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
1995-10-31 12:34:48 +00:00
|
|
|
|
/* This is the argv-index of the option we will read next. */
|
|
|
|
|
|
int this_optind = optind ? optind : 1;
|
1995-10-31 12:52:29 +00:00
|
|
|
|
long int tmp_long;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
2002-02-16 08:04:12 +00:00
|
|
|
|
c = getopt_long (argc, argv, "0123456789C:a:b:l:", longopts, NULL);
|
Add more support for POSIX 1003.1-2001, which requires removal for
support of obsolete "-N" option syntax in expand, head, fold,
split, tail, unexpand, uniq, and which prohibits options with
optional arguments in od and pr.
(usage): Document this.
(main): Check for obsolete options.
(shortopts): New constant.
(main): Use -1, not EOF, for getopt_long.
2002-02-02 09:40:50 +00:00
|
|
|
|
if (c == -1)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
break;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
|
|
{
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2002-02-12 15:44:16 +00:00
|
|
|
|
case 'a':
|
2002-09-28 16:50:34 +00:00
|
|
|
|
{
|
|
|
|
|
|
unsigned long tmp;
|
|
|
|
|
|
if (xstrtoul (optarg, NULL, 10, &tmp, "") != LONGINT_OK
|
|
|
|
|
|
|| SIZE_MAX < tmp)
|
|
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("%s: invalid suffix length"), optarg);
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
|
|
|
|
|
suffix_length = tmp;
|
|
|
|
|
|
}
|
2002-02-12 15:44:16 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
case 'b':
|
|
|
|
|
|
if (split_type != type_undef)
|
2002-09-28 16:50:34 +00:00
|
|
|
|
FAIL_ONLY_ONE_WAY ();
|
1995-10-31 12:34:48 +00:00
|
|
|
|
split_type = type_bytes;
|
1995-10-31 12:52:29 +00:00
|
|
|
|
if (xstrtol (optarg, NULL, 10, &tmp_long, "bkm") != LONGINT_OK
|
|
|
|
|
|
|| tmp_long < 0 || tmp_long > INT_MAX)
|
1996-10-13 17:53:47 +00:00
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("%s: invalid number of bytes"), optarg);
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
2002-10-19 16:34:25 +00:00
|
|
|
|
accum = /* FIXME: */ (int) tmp_long;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
|
|
if (split_type != type_undef)
|
2002-09-28 16:50:34 +00:00
|
|
|
|
FAIL_ONLY_ONE_WAY ();
|
1995-10-31 12:34:48 +00:00
|
|
|
|
split_type = type_lines;
|
1995-10-31 12:52:29 +00:00
|
|
|
|
if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
|
|
|
|
|
|
|| tmp_long < 0 || tmp_long > INT_MAX)
|
1996-10-13 17:53:47 +00:00
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("%s: invalid number of lines"), optarg);
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
2002-10-19 16:34:25 +00:00
|
|
|
|
accum = /* FIXME */ (int) tmp_long;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
|
|
if (split_type != type_undef)
|
2002-09-28 16:50:34 +00:00
|
|
|
|
FAIL_ONLY_ONE_WAY ();
|
1995-10-31 12:34:48 +00:00
|
|
|
|
split_type = type_byteslines;
|
1995-10-31 12:52:29 +00:00
|
|
|
|
if (xstrtol (optarg, NULL, 10, &tmp_long, "bkm") != LONGINT_OK
|
|
|
|
|
|
|| tmp_long < 0 || tmp_long > INT_MAX)
|
1996-10-13 17:53:47 +00:00
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("%s: invalid number of bytes"), optarg);
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
2002-10-19 16:34:25 +00:00
|
|
|
|
accum = /* FIXME */ (int) tmp_long;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
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)
|
2002-09-28 16:50:34 +00:00
|
|
|
|
FAIL_ONLY_ONE_WAY ();
|
1995-10-31 12:34:48 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
1999-04-04 15:44:26 +00:00
|
|
|
|
case_GETOPT_HELP_CHAR;
|
|
|
|
|
|
|
|
|
|
|
|
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
|
|
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
default:
|
1996-10-13 17:53:47 +00:00
|
|
|
|
usage (EXIT_FAILURE);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2002-02-16 08:04:12 +00:00
|
|
|
|
if (digits_optind && 200112 <= posix2_version ())
|
|
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("`-%d' option is obsolete; use `-l %d'"), accum, accum);
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
Add more support for POSIX 1003.1-2001, which requires removal for
support of obsolete "-N" option syntax in expand, head, fold,
split, tail, unexpand, uniq, and which prohibits options with
optional arguments in od and pr.
(usage): Document this.
(main): Check for obsolete options.
(shortopts): New constant.
(main): Use -1, not EOF, for getopt_long.
2002-02-02 09:40:50 +00:00
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
/* Handle default case. */
|
|
|
|
|
|
if (split_type == type_undef)
|
|
|
|
|
|
{
|
|
|
|
|
|
split_type = type_lines;
|
|
|
|
|
|
accum = 1000;
|
|
|
|
|
|
}
|
1995-05-20 11:52:36 +00:00
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
if (accum < 1)
|
1996-10-13 17:53:47 +00:00
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("invalid number"));
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
1995-10-31 12:34:48 +00:00
|
|
|
|
num = accum;
|
1995-05-20 11:52:36 +00:00
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
/* Get out the filename arguments. */
|
|
|
|
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
|
|
infile = argv[optind++];
|
|
|
|
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
|
|
outbase = argv[optind++];
|
|
|
|
|
|
|
|
|
|
|
|
if (optind < argc)
|
1996-10-13 17:53:47 +00:00
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("too many arguments"));
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
|
|
|
|
|
/* Open the input file. */
|
1998-04-12 09:27:45 +00:00
|
|
|
|
if (STREQ (infile, "-"))
|
1995-10-31 12:34:48 +00:00
|
|
|
|
input_desc = 0;
|
|
|
|
|
|
else
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
1995-10-31 12:34:48 +00:00
|
|
|
|
input_desc = open (infile, O_RDONLY);
|
|
|
|
|
|
if (input_desc < 0)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
1999-01-01 22:49:44 +00:00
|
|
|
|
/* Binary I/O is safer when bytecounts are used. */
|
|
|
|
|
|
SET_BINARY (input_desc);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
/* No output file is open now. */
|
|
|
|
|
|
output_desc = -1;
|
|
|
|
|
|
|
|
|
|
|
|
/* Get the optimal block size of input device and make a buffer. */
|
|
|
|
|
|
|
|
|
|
|
|
if (fstat (input_desc, &stat_buf) < 0)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
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)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
if (output_desc >= 0 && close (output_desc) < 0)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", outfile);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
1996-03-24 14:58:01 +00:00
|
|
|
|
exit (EXIT_SUCCESS);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|