/* split.c -- split a file into pieces.
Copyright (C) 1988, 1991 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
the Free Software Foundation; either version 2, or (at your option)
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
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* By tege@sics.se, with rms.
* Implement -t CHAR or -t REGEX to specify break characters other
void line_bytes_split ();
/* Name under which this program was invoked. */
/* Base name of output files. */
/* Pointer to the end of the prefix in OUTFILE.
Suffixes are inserted here. */
/* Pointer to the end of OUTFILE. */
/* Status for outfile name generation. */
unsigned outfile_count
= -1;
unsigned outfile_name_limit
= 25 * 26;
unsigned outfile_name_generation
= 1;
/* Name of input file. May be "-". */
/* Descriptor on which input file is open. */
/* Descriptor on which output file is open. */
fprintf (stderr
, "%s: %s\n", program_name
, reason
);
Usage: %s [-lines] [-l lines] [-b bytes[bkm]] [-C bytes[bkm]]\n\
[--lines=lines] [--bytes=bytes[bkm]] [--line-bytes=bytes[bkm]]\n\
[infile [outfile-prefix]]\n",
struct option longopts
[] =
{"line-bytes", 1, NULL
, 'C'},
int num
; /* numeric argument from command line */
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 */
/* Parse command line options. */
/* 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 (split_type
!= type_undef
)
usage ("cannot split in more than one way");
if (convint (optarg
, &accum
) == -1)
usage ("invalid number of bytes");
if (split_type
!= type_undef
)
usage ("cannot split in more than one way");
usage ("invalid number of lines");
if (split_type
!= type_undef
)
usage ("cannot split in more than one way");
split_type
= type_byteslines
;
if (convint (optarg
, &accum
) == -1)
usage ("invalid number of bytes");
if (split_type
!= type_undef
&& split_type
!= type_digits
)
usage ("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';
/* Handle default case. */
if (split_type
== type_undef
)
usage ("invalid number");
/* Get out the filename arguments. */
outbase
= argv
[optind
++];
usage ("too many arguments");
/* Open the input file. */
if (!strcmp (infile
, "-"))
input_desc
= open (infile
, O_RDONLY
);
error (1, errno
, "%s", infile
);
/* No output file is open now. */
/* 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;
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);
lines_split (num
, buf
, in_blk_size
);
bytes_split (num
, buf
, in_blk_size
);
if (close (input_desc
) < 0)
error (1, errno
, "%s", infile
);
if (output_desc
>= 0 && close (output_desc
) < 0)
error (1, errno
, "%s", outfile
);
/* Return nonzero if the string STR is composed entirely of decimal digits. */
/* Put the value of the number in STR into *VAL.
STR can specify a positive integer, optionally ending in `k'
to mean kilo or `m' to mean mega.
Return 0 if STR is valid, -1 if not. */
int arglen
= strlen (str
);
*val
= atoi (str
) * multiplier
;
/* Split into pieces of exactly NCHARS bytes.
Use buffer BUF, whose size is BUFSIZE. */
bytes_split (nchars
, buf
, bufsize
)
n_read
= stdread (buf
, bufsize
);
error (1, errno
, "%s", infile
);
if (to_read
) /* do not write 0 bytes! */
cwrite (new_file_flag
, bp_out
, to_read
);
cwrite (new_file_flag
, bp_out
, to_write
);
while (n_read
== bufsize
);
/* Split into pieces of exactly NLINES lines.
Use buffer BUF, whose size is BUFSIZE. */
lines_split (nlines
, buf
, bufsize
)
n_read
= stdread (buf
, bufsize
);
error (1, errno
, "%s", infile
);
; /* this semicolon takes most of the time */
if (eob
!= bp_out
) /* do not write 0 bytes! */
cwrite (new_file_flag
, bp_out
, eob
- bp_out
);
cwrite (new_file_flag
, bp_out
, bp
- bp_out
);
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. */
line_bytes_split (nchars
)
char *buf
= (char *) xmalloc (nchars
);
/* Fill up the full buffer size from the input file. */
n_read
= stdread (buf
+ n_buffered
, nchars
- n_buffered
);
error (1, errno
, "%s", infile
);
if (n_buffered
!= nchars
)
/* Find where to end this chunk. */
if (n_buffered
== nchars
)
while (bp
> buf
&& bp
[-1] != '\n')
/* If chunk has no newlines, use all the chunk. */
/* Output the chars as one output file. */
cwrite (1, buf
, bp
- buf
);
/* Discard the chars we just output; move rest of chunk
down to be the start of the next chunk. */
bcopy (bp
, buf
, n_buffered
);
/* 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. */
cwrite (new_file_flag
, bp
, bytes
)
if (output_desc
>= 0 && close (output_desc
) < 0)
error (1, errno
, "%s", outfile
);
output_desc
= open (outfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
error (1, errno
, "%s", outfile
);
if (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. */
n_read
= read (input_desc
, buf
, to_be_read
);
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 (outfile_count
< outfile_name_limit
)
for (ne
= outfile_end
- 1; ; ne
--)
outfile_name_limit
*= 26;
outfile_name_generation
++;
for (x
= 0; x
<= outfile_name_generation
; x
++)