1992-11-08 02:50:43 +00:00
|
|
|
|
/* split.c -- split a file into pieces.
|
1998-01-10 22:12:03 +00:00
|
|
|
|
Copyright (C) 88, 91, 95, 96, 1997, 1998 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"
|
1994-12-16 05:42:47 +00:00
|
|
|
|
#include "error.h"
|
1995-10-31 12:52:29 +00:00
|
|
|
|
#include "xstrtol.h"
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1994-01-09 03:47:21 +00:00
|
|
|
|
int full_write ();
|
|
|
|
|
|
int safe_read ();
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
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. */
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static char *outfile;
|
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
|
|
|
|
|
|
|
|
|
|
/* Pointer to the end of OUTFILE. */
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static char *outfile_end;
|
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, display usage information and exit. */
|
1993-10-05 18:29:39 +00:00
|
|
|
|
static int show_help;
|
1993-04-29 05:26:22 +00:00
|
|
|
|
|
1995-11-27 03:09:18 +00:00
|
|
|
|
/* If nonzero, print the version on standard output then exit. */
|
1993-10-05 18:29:39 +00:00
|
|
|
|
static int show_version;
|
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'},
|
1995-11-07 04:56:08 +00:00
|
|
|
|
{"verbose", no_argument, NULL, 2},
|
1993-10-05 18:29:39 +00:00
|
|
|
|
{"help", no_argument, &show_help, 1},
|
|
|
|
|
|
{"version", no_argument, &show_version, 1},
|
1993-04-29 05:26:22 +00:00
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static 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);
|
1995-08-07 14:57:29 +00:00
|
|
|
|
printf (_("\
|
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\
|
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\
|
1996-11-23 22:06:55 +00:00
|
|
|
|
-NUMBER same as -l NUMBER\n\
|
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\
|
1993-10-23 15:37:19 +00:00
|
|
|
|
--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\
|
1995-08-07 14:57:29 +00:00
|
|
|
|
"));
|
1997-10-07 23:55:52 +00:00
|
|
|
|
puts (_("\nReport bugs to <textutils-bugs@gnu.org>."));
|
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
|
|
|
|
}
|
|
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
/* 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
|
1995-10-31 12:36:43 +00:00
|
|
|
|
next_file_name (void)
|
1995-10-31 12:34:48 +00:00
|
|
|
|
{
|
1998-01-18 11:18:08 +00:00
|
|
|
|
static unsigned n_digits = 2;
|
|
|
|
|
|
char *p;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
1998-01-18 11:18:08 +00:00
|
|
|
|
/* Change any suffix of `z's to `a's. */
|
|
|
|
|
|
for (p = outfile_end - 1; *p == 'z'; p--)
|
|
|
|
|
|
{
|
|
|
|
|
|
*p = 'a';
|
|
|
|
|
|
}
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
1998-01-18 11:18:08 +00:00
|
|
|
|
/* Increment the rightmost non-`z' character that was present before the
|
|
|
|
|
|
above z/a substitutions. There is guaranteed to be such a character. */
|
|
|
|
|
|
++(*p);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
1998-01-18 11:18:08 +00:00
|
|
|
|
/* 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)
|
1995-10-31 12:34:48 +00:00
|
|
|
|
{
|
1998-01-18 11:18:08 +00:00
|
|
|
|
++n_digits;
|
|
|
|
|
|
++outfile_mid;
|
|
|
|
|
|
*outfile_end++ = 'a';
|
|
|
|
|
|
*outfile_end++ = 'a';
|
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
|
1995-10-31 12:39:18 +00:00
|
|
|
|
cwrite (int new_file_flag, const char *bp, int 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);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
output_desc = open (outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
if (full_write (output_desc, bp, bytes) < 0)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", outfile);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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
|
1995-10-31 12:36:43 +00:00
|
|
|
|
stdread (char *buf, int nchars)
|
1995-10-31 12:34:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
|
/* Split into pieces of exactly NCHARS bytes.
|
|
|
|
|
|
Use buffer BUF, whose size is BUFSIZE. */
|
|
|
|
|
|
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static void
|
1995-10-31 12:36:43 +00:00
|
|
|
|
bytes_split (int nchars, char *buf, int bufsize)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
int n_read;
|
|
|
|
|
|
int new_file_flag = 1;
|
|
|
|
|
|
int to_read;
|
|
|
|
|
|
int to_write = nchars;
|
|
|
|
|
|
char *bp_out;
|
|
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
|
|
|
|
|
n_read = stdread (buf, bufsize);
|
|
|
|
|
|
if (n_read < 0)
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
cwrite (new_file_flag, bp_out, to_write);
|
|
|
|
|
|
bp_out += to_write;
|
|
|
|
|
|
to_read -= to_write;
|
|
|
|
|
|
new_file_flag = 1;
|
|
|
|
|
|
to_write = nchars;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
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
|
1995-10-31 12:36:43 +00:00
|
|
|
|
lines_split (int nlines, char *buf, int bufsize)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
int n_read;
|
|
|
|
|
|
char *bp, *bp_out, *eob;
|
|
|
|
|
|
int new_file_flag = 1;
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
|
|
|
|
|
n_read = stdread (buf, bufsize);
|
|
|
|
|
|
if (n_read < 0)
|
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 (;;)
|
|
|
|
|
|
{
|
|
|
|
|
|
while (*bp++ != '\n')
|
|
|
|
|
|
; /* this semicolon takes most of the time */
|
|
|
|
|
|
if (bp > eob)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (eob != bp_out) /* do not write 0 bytes! */
|
|
|
|
|
|
{
|
|
|
|
|
|
cwrite (new_file_flag, bp_out, eob - bp_out);
|
|
|
|
|
|
new_file_flag = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
if (++n >= nlines)
|
|
|
|
|
|
{
|
|
|
|
|
|
cwrite (new_file_flag, bp_out, bp - bp_out);
|
|
|
|
|
|
bp_out = bp;
|
|
|
|
|
|
new_file_flag = 1;
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
while (n_read == bufsize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Split into pieces that are as large as possible while still not more
|
|
|
|
|
|
than NCHARS bytes, and are split on line boundaries except
|
|
|
|
|
|
where lines longer than NCHARS bytes occur. */
|
|
|
|
|
|
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static void
|
1995-10-31 12:36:43 +00:00
|
|
|
|
line_bytes_split (int nchars)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
int n_read;
|
|
|
|
|
|
char *bp;
|
|
|
|
|
|
int eof = 0;
|
|
|
|
|
|
int n_buffered = 0;
|
|
|
|
|
|
char *buf = (char *) xmalloc (nchars);
|
|
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Fill up the full buffer size from the input file. */
|
|
|
|
|
|
|
|
|
|
|
|
n_read = stdread (buf + n_buffered, nchars - n_buffered);
|
|
|
|
|
|
if (n_read < 0)
|
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;
|
|
|
|
|
|
if (n_buffered != nchars)
|
|
|
|
|
|
eof = 1;
|
|
|
|
|
|
|
|
|
|
|
|
/* Find where to end this chunk. */
|
|
|
|
|
|
bp = buf + n_buffered;
|
|
|
|
|
|
if (n_buffered == nchars)
|
|
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
int num; /* numeric argument from command line */
|
|
|
|
|
|
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;
|
|
|
|
|
|
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;
|
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
|
|
|
|
|
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
|
|
|
|
|
1995-11-07 04:56:08 +00:00
|
|
|
|
c = getopt_long (argc, argv, "0123456789vb:l:C:", longopts, (int *) 0);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
if (c == EOF)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
break;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
|
|
{
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
|
|
if (split_type != type_undef)
|
1996-10-13 17:53:47 +00:00
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("cannot split in more than one way"));
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
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);
|
|
|
|
|
|
}
|
1995-10-31 12:52:29 +00:00
|
|
|
|
accum = (int) tmp_long;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
|
|
if (split_type != type_undef)
|
1996-10-13 17:53:47 +00:00
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("cannot split in more than one way"));
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
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);
|
|
|
|
|
|
}
|
1995-10-31 12:52:29 +00:00
|
|
|
|
accum = (int) tmp_long;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
|
|
if (split_type != type_undef)
|
1996-10-13 17:53:47 +00:00
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("cannot split in more than one way"));
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
1995-10-31 12:52:29 +00:00
|
|
|
|
accum = (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)
|
1996-10-13 17:53:47 +00:00
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("cannot split in more than one way"));
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
1995-11-07 04:56:08 +00:00
|
|
|
|
case 2:
|
|
|
|
|
|
verbose = 1;
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
if (show_version)
|
|
|
|
|
|
{
|
1996-10-04 04:25:46 +00:00
|
|
|
|
printf ("split (%s) %s\n", GNU_PACKAGE, VERSION);
|
1996-03-24 14:58:01 +00:00
|
|
|
|
exit (EXIT_SUCCESS);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
if (show_help)
|
1996-10-13 17:53:47 +00:00
|
|
|
|
usage (0);
|
1992-11-08 02:50:43 +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. */
|
|
|
|
|
|
if (!strcmp (infile, "-"))
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
/* 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)
|
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
|
|
|
|
}
|