1992-11-08 02:50:43 +00:00
|
|
|
|
/* split.c -- split a file into pieces.
|
2013-01-01 03:54:51 +01:00
|
|
|
|
Copyright (C) 1988-2013 Free Software Foundation, Inc.
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2007-07-23 14:35:58 +02:00
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
1992-11-08 02:50:43 +00:00
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2007-07-23 14:35:58 +02:00
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
(at your option) any later version.
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
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
|
2007-07-23 14:35:58 +02:00
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
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>
|
|
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
#include <assert.h>
|
1992-11-08 02:50:43 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <getopt.h>
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
#include <signal.h>
|
1992-11-08 02:50:43 +00:00
|
|
|
|
#include <sys/types.h>
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
#include <sys/wait.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"
|
2005-07-03 07:20:33 +00:00
|
|
|
|
#include "fd-reopen.h"
|
|
|
|
|
|
#include "fcntl--.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"
|
2011-06-11 16:28:34 +01:00
|
|
|
|
#include "ioblksize.h"
|
2004-06-21 15:03:35 +00:00
|
|
|
|
#include "quote.h"
|
2010-01-08 03:42:27 -08:00
|
|
|
|
#include "safe-read.h"
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
#include "sig2str.h"
|
2008-10-12 14:50:02 +02:00
|
|
|
|
#include "xfreopen.h"
|
1999-03-04 05:36:10 +00:00
|
|
|
|
#include "xstrtol.h"
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2012-01-08 15:08:30 +01:00
|
|
|
|
/* The official name of this program (e.g., no 'g' prefix). */
|
1999-04-03 05:01:48 +00:00
|
|
|
|
#define PROGRAM_NAME "split"
|
|
|
|
|
|
|
use gnulib's proper_name_utf8 function, but *not* proper_name
* bootstrap.conf (gnulib_modules): Add propername.
(XGETTEXT_OPTIONS): Add options to tell xgettext about the functions.
* src/cat.c, src/cp.c, src/df.c, src/du.c, src/split.c:
Mark Torbjörn Granlund's name.
* src/ptx.c: Mark François Pinard's name.
Use "TRANSLATORS:" comment marker, rather than "Note to translators:".
* src/system.h: Include propername.h.
(proper_name): Define away.
* src/Makefile.am (cat_LDADD, df_LDADD, du_LDADD, ptx_LDADD, split_LDADD):
Initialize, so we can...
(cat_LDADD, cp_LDADD, df_LDADD, du_LDADD, ptx_LDADD, split_LDADD):
...Use "+=" to append $(LIBICONV) for each program that uses
proper_name_utf8.
2008-05-19 16:14:13 +02:00
|
|
|
|
#define AUTHORS \
|
|
|
|
|
|
proper_name_utf8 ("Torbjorn Granlund", "Torbj\303\266rn Granlund"), \
|
2008-05-19 16:43:32 +02:00
|
|
|
|
proper_name ("Richard M. Stallman")
|
1999-04-03 05:22:05 +00:00
|
|
|
|
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
/* Shell command to filter through, instead of creating files. */
|
|
|
|
|
|
static char const *filter_command;
|
|
|
|
|
|
|
|
|
|
|
|
/* Process ID of the filter. */
|
|
|
|
|
|
static int filter_pid;
|
|
|
|
|
|
|
|
|
|
|
|
/* Array of open pipes. */
|
|
|
|
|
|
static int *open_pipes;
|
|
|
|
|
|
static size_t open_pipes_alloc;
|
|
|
|
|
|
static size_t n_open_pipes;
|
|
|
|
|
|
|
|
|
|
|
|
/* Blocked signals. */
|
|
|
|
|
|
static sigset_t oldblocked;
|
|
|
|
|
|
static sigset_t newblocked;
|
|
|
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
|
/* 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
|
|
|
|
|
2012-03-01 20:37:41 +01:00
|
|
|
|
/* Generate new suffix when suffixes are exhausted. */
|
|
|
|
|
|
static bool suffix_auto = true;
|
|
|
|
|
|
|
2002-02-12 15:44:16 +00:00
|
|
|
|
/* Length of OUTFILE's suffix. */
|
2010-01-08 03:42:27 -08:00
|
|
|
|
static size_t suffix_length;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2003-08-09 09:16:39 +00:00
|
|
|
|
/* Alphabet of characters to use in suffix. */
|
|
|
|
|
|
static char const *suffix_alphabet = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
|
|
2012-01-29 15:20:31 +01:00
|
|
|
|
/* Numerical suffix start value. */
|
|
|
|
|
|
static const char *numeric_suffix_start;
|
|
|
|
|
|
|
2012-02-19 13:52:47 +01:00
|
|
|
|
/* Additional suffix to append to output file names. */
|
|
|
|
|
|
static char const *additional_suffix;
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-06-22 09:32:34 +01:00
|
|
|
|
/* stat buf for input file. */
|
|
|
|
|
|
static struct stat in_stat_buf;
|
|
|
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
|
/* Descriptor on which output file is open. */
|
2010-01-08 03:42:27 -08:00
|
|
|
|
static int output_desc = -1;
|
1993-04-29 05:26:22 +00:00
|
|
|
|
|
2004-09-21 22:13:53 +00:00
|
|
|
|
/* If true, print a diagnostic on standard error just before each
|
1995-11-07 04:56:08 +00:00
|
|
|
|
output file is opened. */
|
2004-09-21 22:13:53 +00:00
|
|
|
|
static bool verbose;
|
|
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
/* If true, don't generate zero length output files. */
|
|
|
|
|
|
static bool elide_empty_files;
|
|
|
|
|
|
|
|
|
|
|
|
/* If true, in round robin mode, immediately copy
|
|
|
|
|
|
input to output, which is much slower, so disabled by default. */
|
|
|
|
|
|
static bool unbuffered;
|
|
|
|
|
|
|
2010-12-30 01:36:59 +00:00
|
|
|
|
/* The split mode to use. */
|
|
|
|
|
|
enum Split_type
|
|
|
|
|
|
{
|
|
|
|
|
|
type_undef, type_bytes, type_byteslines, type_lines, type_digits,
|
|
|
|
|
|
type_chunk_bytes, type_chunk_lines, type_rr
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2004-09-21 22:13:53 +00:00
|
|
|
|
/* For long options that have no equivalent short option, use a
|
|
|
|
|
|
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
|
|
|
|
|
|
enum
|
|
|
|
|
|
{
|
2010-12-08 08:33:15 +00:00
|
|
|
|
VERBOSE_OPTION = CHAR_MAX + 1,
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
FILTER_OPTION,
|
2012-02-19 13:52:47 +01:00
|
|
|
|
IO_BLKSIZE_OPTION,
|
|
|
|
|
|
ADDITIONAL_SUFFIX_OPTION
|
2004-09-21 22:13:53 +00:00
|
|
|
|
};
|
1995-11-07 04:56:08 +00:00
|
|
|
|
|
remove redundant const directives
In 1463824d8e7f72c31f1d803d7cfe2b608ccafc5c, I added some
missing "const" directives, as well as some new, redundant ones.
This removes the redundant ones. Pointed out by Eric Blake.
* base64.c, cat.c, chcon.c, chgrp.c, chmod.c, chown.c, comm.c:
* cp.c, csplit.c, cut.c, date.c, dd.c, df.c, dircolors.c, du.c:
* env.c, expand.c, fmt.c, fold.c, groups.c, head.c, id.c:
* install.c, join.c, kill.c, ln.c, ls.c, md5sum.c, mkdir.c:
* mkfifo.c, mknod.c, mktemp.c, mv.c, nice.c, nl.c, od.c:
* paste.c, pathchk.c, pinky.c, pr.c, ptx.c, readlink.c, rm.c:
* rmdir.c, runcon.c, seq.c, shred.c, shuf.c, sort.c, split.c:
* stat.c, stty.c, su.c, sum.c, tac.c, tail.c, tee.c, timeout.c:
* touch.c, tr.c, truncate.c, tty.c, uname.c, unexpand.c, uniq.c:
* wc.c, who.c: Remove redundant const directives.
* maint.mk (sc_const_long_option): Don't require redundant "const".
2008-06-16 14:55:06 +02:00
|
|
|
|
static struct option const longopts[] =
|
1993-04-29 05:26:22 +00:00
|
|
|
|
{
|
|
|
|
|
|
{"bytes", required_argument, NULL, 'b'},
|
|
|
|
|
|
{"lines", required_argument, NULL, 'l'},
|
|
|
|
|
|
{"line-bytes", required_argument, NULL, 'C'},
|
2010-01-08 03:42:27 -08:00
|
|
|
|
{"number", required_argument, NULL, 'n'},
|
|
|
|
|
|
{"elide-empty-files", no_argument, NULL, 'e'},
|
|
|
|
|
|
{"unbuffered", no_argument, NULL, 'u'},
|
2002-02-12 15:44:16 +00:00
|
|
|
|
{"suffix-length", required_argument, NULL, 'a'},
|
2012-02-19 13:52:47 +01:00
|
|
|
|
{"additional-suffix", required_argument, NULL,
|
|
|
|
|
|
ADDITIONAL_SUFFIX_OPTION},
|
2012-01-29 15:20:31 +01:00
|
|
|
|
{"numeric-suffixes", optional_argument, NULL, 'd'},
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
{"filter", required_argument, NULL, FILTER_OPTION},
|
2004-09-21 22:13:53 +00:00
|
|
|
|
{"verbose", no_argument, NULL, VERBOSE_OPTION},
|
2010-12-08 08:33:15 +00:00
|
|
|
|
{"-io-blksize", required_argument, NULL,
|
2011-04-30 07:48:08 +02:00
|
|
|
|
IO_BLKSIZE_OPTION}, /* do not document */
|
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}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
/* Return true if the errno value, ERR, is ignorable. */
|
|
|
|
|
|
static inline bool
|
|
|
|
|
|
ignorable (int err)
|
|
|
|
|
|
{
|
|
|
|
|
|
return filter_command && err == EPIPE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
static void
|
2010-12-30 01:36:59 +00:00
|
|
|
|
set_suffix_length (uintmax_t n_units, enum Split_type split_type)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
{
|
|
|
|
|
|
#define DEFAULT_SUFFIX_LENGTH 2
|
|
|
|
|
|
|
|
|
|
|
|
size_t suffix_needed = 0;
|
2010-12-30 01:36:59 +00:00
|
|
|
|
|
2012-03-01 20:37:41 +01:00
|
|
|
|
/* The suffix auto length feature is incompatible with
|
|
|
|
|
|
a user specified start value as the generated suffixes
|
|
|
|
|
|
are not all consecutive. */
|
|
|
|
|
|
if (numeric_suffix_start)
|
|
|
|
|
|
suffix_auto = false;
|
|
|
|
|
|
|
2010-12-30 01:36:59 +00:00
|
|
|
|
/* Auto-calculate the suffix length if the number of files is given. */
|
|
|
|
|
|
if (split_type == type_chunk_bytes || split_type == type_chunk_lines
|
|
|
|
|
|
|| split_type == type_rr)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t alphabet_len = strlen (suffix_alphabet);
|
|
|
|
|
|
bool alphabet_slop = (n_units % alphabet_len) != 0;
|
|
|
|
|
|
while (n_units /= alphabet_len)
|
|
|
|
|
|
suffix_needed++;
|
|
|
|
|
|
suffix_needed += alphabet_slop;
|
2012-03-01 20:37:41 +01:00
|
|
|
|
suffix_auto = false;
|
2010-12-30 01:36:59 +00:00
|
|
|
|
}
|
2010-01-08 03:42:27 -08:00
|
|
|
|
|
|
|
|
|
|
if (suffix_length) /* set by user */
|
|
|
|
|
|
{
|
|
|
|
|
|
if (suffix_length < suffix_needed)
|
|
|
|
|
|
{
|
|
|
|
|
|
error (EXIT_FAILURE, 0,
|
|
|
|
|
|
_("the suffix length needs to be at least %zu"),
|
|
|
|
|
|
suffix_needed);
|
|
|
|
|
|
}
|
2012-03-01 20:37:41 +01:00
|
|
|
|
suffix_auto = false;
|
2010-01-08 03:42:27 -08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
suffix_length = MAX (DEFAULT_SUFFIX_LENGTH, suffix_needed);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2004-01-21 23:45:13 +00:00
|
|
|
|
if (status != EXIT_SUCCESS)
|
2012-01-07 16:54:26 +01:00
|
|
|
|
emit_try_help ();
|
1993-10-23 15:37:19 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
1995-08-07 14:57:29 +00:00
|
|
|
|
printf (_("\
|
2008-06-28 09:53:03 +02:00
|
|
|
|
Usage: %s [OPTION]... [INPUT [PREFIX]]\n\
|
1995-08-07 14:57:29 +00:00
|
|
|
|
"),
|
2009-08-22 18:56:06 +02:00
|
|
|
|
program_name);
|
2011-04-30 07:48:08 +02:00
|
|
|
|
fputs (_("\
|
1995-05-13 18:34:54 +00:00
|
|
|
|
Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default\n\
|
2012-01-08 14:08:03 +01:00
|
|
|
|
size is 1000 lines, and default PREFIX is 'x'. With no INPUT, or when INPUT\n\
|
2004-12-20 20:00:44 +00:00
|
|
|
|
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, _("\
|
2012-02-19 13:52:47 +01:00
|
|
|
|
-a, --suffix-length=N generate suffixes of length N (default %d)\n\
|
|
|
|
|
|
--additional-suffix=SUFFIX append an additional SUFFIX to file names.\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\
|
2012-01-29 15:20:31 +01:00
|
|
|
|
-d, --numeric-suffixes[=FROM] use numeric suffixes instead of alphabetic.\n\
|
|
|
|
|
|
FROM changes the start value (default 0).\n\
|
2012-01-08 14:08:03 +01:00
|
|
|
|
-e, --elide-empty-files do not generate empty output files with '-n'\n\
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
--filter=COMMAND write to shell COMMAND; file name is $FILE\n\
|
1993-10-24 20:00:39 +00:00
|
|
|
|
-l, --lines=NUMBER put NUMBER lines per output file\n\
|
2010-01-08 03:42:27 -08:00
|
|
|
|
-n, --number=CHUNKS generate CHUNKS output files. See below\n\
|
2012-01-08 14:08:03 +01:00
|
|
|
|
-u, --unbuffered immediately copy input to output with '-n r/...'\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 (_("\
|
2008-02-06 08:25:24 +01:00
|
|
|
|
--verbose print a diagnostic just before each\n\
|
|
|
|
|
|
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);
|
2009-09-10 12:33:41 +01:00
|
|
|
|
emit_size_note ();
|
2011-04-30 07:48:08 +02:00
|
|
|
|
fputs (_("\n\
|
2010-01-08 03:42:27 -08:00
|
|
|
|
CHUNKS may be:\n\
|
|
|
|
|
|
N split into N files based on size of input\n\
|
|
|
|
|
|
K/N output Kth of N to stdout\n\
|
|
|
|
|
|
l/N split into N files without splitting lines\n\
|
|
|
|
|
|
l/K/N output Kth of N to stdout without splitting lines\n\
|
2012-01-08 14:08:03 +01:00
|
|
|
|
r/N like 'l' but use round robin distribution\n\
|
2010-01-08 03:42:27 -08:00
|
|
|
|
r/K/N likewise but only output Kth of N to stdout\n\
|
|
|
|
|
|
"), stdout);
|
2009-09-18 23:06:21 +01:00
|
|
|
|
emit_ancillary_info ();
|
1993-10-23 15:37:19 +00:00
|
|
|
|
}
|
2004-01-21 23:45:13 +00:00
|
|
|
|
exit (status);
|
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
|
2012-01-08 15:08:30 +01:00
|
|
|
|
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
|
|
|
|
{
|
2003-08-09 09:16:39 +00:00
|
|
|
|
/* Index in suffix_alphabet of each character in the suffix. */
|
|
|
|
|
|
static size_t *sufindex;
|
2012-03-01 20:37:41 +01:00
|
|
|
|
static size_t outbase_length;
|
|
|
|
|
|
static size_t outfile_length;
|
|
|
|
|
|
static size_t addsuf_length;
|
2003-08-09 09:16:39 +00:00
|
|
|
|
|
2002-02-12 15:44:16 +00:00
|
|
|
|
if (! outfile)
|
1998-01-18 11:18:08 +00:00
|
|
|
|
{
|
2012-03-01 20:37:41 +01:00
|
|
|
|
bool widen;
|
|
|
|
|
|
|
|
|
|
|
|
new_name:
|
|
|
|
|
|
widen = !! outfile_length;
|
|
|
|
|
|
|
|
|
|
|
|
if (! widen)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Allocate and initialize the first file name. */
|
|
|
|
|
|
|
|
|
|
|
|
outbase_length = strlen (outbase);
|
|
|
|
|
|
addsuf_length = additional_suffix ? strlen (additional_suffix) : 0;
|
|
|
|
|
|
outfile_length = outbase_length + suffix_length + addsuf_length;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Reallocate and initialize a new wider file name.
|
|
|
|
|
|
We do this by subsuming the unchanging part of
|
|
|
|
|
|
the generated suffix into the prefix (base), and
|
|
|
|
|
|
reinitializing the now one longer suffix. */
|
|
|
|
|
|
|
|
|
|
|
|
outfile_length += 2;
|
|
|
|
|
|
suffix_length++;
|
|
|
|
|
|
}
|
2002-02-12 15:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
if (outfile_length + 1 < outbase_length)
|
2009-08-22 18:56:06 +02:00
|
|
|
|
xalloc_die ();
|
2012-03-01 20:37:41 +01:00
|
|
|
|
outfile = xrealloc (outfile, outfile_length + 1);
|
|
|
|
|
|
|
|
|
|
|
|
if (! widen)
|
|
|
|
|
|
memcpy (outfile, outbase, outbase_length);
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Append the last alphabet character to the file name prefix. */
|
|
|
|
|
|
outfile[outbase_length] = suffix_alphabet[sufindex[0]];
|
|
|
|
|
|
outbase_length++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2002-02-12 15:44:16 +00:00
|
|
|
|
outfile_mid = outfile + outbase_length;
|
2003-08-09 09:16:39 +00:00
|
|
|
|
memset (outfile_mid, suffix_alphabet[0], suffix_length);
|
2012-02-19 13:52:47 +01:00
|
|
|
|
if (additional_suffix)
|
|
|
|
|
|
memcpy (outfile_mid + suffix_length, additional_suffix, addsuf_length);
|
2002-02-12 15:44:16 +00:00
|
|
|
|
outfile[outfile_length] = 0;
|
2012-03-01 20:37:41 +01:00
|
|
|
|
|
|
|
|
|
|
free (sufindex);
|
2003-11-04 06:25:45 +00:00
|
|
|
|
sufindex = xcalloc (suffix_length, sizeof *sufindex);
|
2002-02-12 15:44:16 +00:00
|
|
|
|
|
2012-01-29 15:20:31 +01:00
|
|
|
|
if (numeric_suffix_start)
|
|
|
|
|
|
{
|
2012-03-01 20:37:41 +01:00
|
|
|
|
assert (! widen);
|
|
|
|
|
|
|
2012-01-29 15:20:31 +01:00
|
|
|
|
/* Update the output file name. */
|
|
|
|
|
|
size_t i = strlen (numeric_suffix_start);
|
|
|
|
|
|
memcpy (outfile_mid + suffix_length - i, numeric_suffix_start, i);
|
|
|
|
|
|
|
|
|
|
|
|
/* Update the suffix index. */
|
|
|
|
|
|
size_t *sufindex_end = sufindex + suffix_length;
|
|
|
|
|
|
while (i-- != 0)
|
|
|
|
|
|
*--sufindex_end = numeric_suffix_start[i] - '0';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2002-02-12 15:44:16 +00:00
|
|
|
|
#if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
|
|
|
|
|
|
/* POSIX requires that if the output file name is too long for
|
2012-01-08 15:08:30 +01:00
|
|
|
|
its directory, 'split' must fail without creating any files.
|
2009-08-22 18:56:06 +02:00
|
|
|
|
This must be checked for explicitly on operating systems that
|
|
|
|
|
|
silently truncate file names. */
|
2002-02-12 15:44:16 +00:00
|
|
|
|
{
|
2009-08-22 18:56:06 +02:00
|
|
|
|
char *dir = dir_name (outfile);
|
|
|
|
|
|
long name_max = pathconf (dir, _PC_NAME_MAX);
|
|
|
|
|
|
if (0 <= name_max && name_max < base_len (last_component (outfile)))
|
|
|
|
|
|
error (EXIT_FAILURE, ENAMETOOLONG, "%s", outfile);
|
|
|
|
|
|
free (dir);
|
2002-02-12 15:44:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
#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. */
|
|
|
|
|
|
|
2003-08-09 09:16:39 +00:00
|
|
|
|
size_t i = suffix_length;
|
|
|
|
|
|
while (i-- != 0)
|
2009-08-22 18:56:06 +02:00
|
|
|
|
{
|
|
|
|
|
|
sufindex[i]++;
|
2012-03-01 20:37:41 +01:00
|
|
|
|
if (suffix_auto && i == 0 && ! suffix_alphabet[sufindex[0] + 1])
|
|
|
|
|
|
goto new_name;
|
2009-08-22 18:56:06 +02:00
|
|
|
|
outfile_mid[i] = suffix_alphabet[sufindex[i]];
|
|
|
|
|
|
if (outfile_mid[i])
|
|
|
|
|
|
return;
|
|
|
|
|
|
sufindex[i] = 0;
|
|
|
|
|
|
outfile_mid[i] = suffix_alphabet[sufindex[i]];
|
|
|
|
|
|
}
|
2008-04-04 11:13:13 -04:00
|
|
|
|
error (EXIT_FAILURE, 0, _("output file suffixes exhausted"));
|
1995-10-31 12:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
/* Create or truncate a file. */
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2011-04-30 07:48:08 +02:00
|
|
|
|
create (const char *name)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
{
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
if (!filter_command)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (verbose)
|
|
|
|
|
|
fprintf (stdout, _("creating file %s\n"), quote (name));
|
2012-06-22 09:32:34 +01:00
|
|
|
|
|
|
|
|
|
|
int fd = open (name, O_WRONLY | O_CREAT | O_BINARY, MODE_RW_UGO);
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
|
return fd;
|
|
|
|
|
|
struct stat out_stat_buf;
|
|
|
|
|
|
if (fstat (fd, &out_stat_buf) != 0)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, _("failed to stat %s"), quote (name));
|
|
|
|
|
|
if (SAME_INODE (in_stat_buf, out_stat_buf))
|
|
|
|
|
|
error (EXIT_FAILURE, 0, _("%s would overwrite input; aborting"),
|
|
|
|
|
|
quote (name));
|
|
|
|
|
|
if (ftruncate (fd, 0) != 0)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, _("%s: error truncating"), quote (name));
|
|
|
|
|
|
|
|
|
|
|
|
return fd;
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
int fd_pair[2];
|
|
|
|
|
|
pid_t child_pid;
|
|
|
|
|
|
char const *shell_prog = getenv ("SHELL");
|
|
|
|
|
|
if (shell_prog == NULL)
|
|
|
|
|
|
shell_prog = "/bin/sh";
|
|
|
|
|
|
if (setenv ("FILE", name, 1) != 0)
|
|
|
|
|
|
error (EXIT_FAILURE, errno,
|
|
|
|
|
|
_("failed to set FILE environment variable"));
|
|
|
|
|
|
if (verbose)
|
|
|
|
|
|
fprintf (stdout, _("executing with FILE=%s\n"), quote (name));
|
|
|
|
|
|
if (pipe (fd_pair) != 0)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, _("failed to create pipe"));
|
|
|
|
|
|
child_pid = fork ();
|
|
|
|
|
|
if (child_pid == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* This is the child process. If an error occurs here, the
|
|
|
|
|
|
parent will eventually learn about it after doing a wait,
|
|
|
|
|
|
at which time it will emit its own error message. */
|
|
|
|
|
|
int j;
|
|
|
|
|
|
/* We have to close any pipes that were opened during an
|
|
|
|
|
|
earlier call, otherwise this process will be holding a
|
|
|
|
|
|
write-pipe that will prevent the earlier process from
|
|
|
|
|
|
reading an EOF on the corresponding read-pipe. */
|
|
|
|
|
|
for (j = 0; j < n_open_pipes; ++j)
|
|
|
|
|
|
if (close (open_pipes[j]) != 0)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, _("closing prior pipe"));
|
|
|
|
|
|
if (close (fd_pair[1]))
|
|
|
|
|
|
error (EXIT_FAILURE, errno, _("closing output pipe"));
|
|
|
|
|
|
if (fd_pair[0] != STDIN_FILENO)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dup2 (fd_pair[0], STDIN_FILENO) != STDIN_FILENO)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, _("moving input pipe"));
|
|
|
|
|
|
if (close (fd_pair[0]) != 0)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, _("closing input pipe"));
|
|
|
|
|
|
}
|
|
|
|
|
|
sigprocmask (SIG_SETMASK, &oldblocked, NULL);
|
|
|
|
|
|
execl (shell_prog, last_component (shell_prog), "-c",
|
|
|
|
|
|
filter_command, (char *) NULL);
|
|
|
|
|
|
error (EXIT_FAILURE, errno, _("failed to run command: \"%s -c %s\""),
|
|
|
|
|
|
shell_prog, filter_command);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (child_pid == -1)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, _("fork system call failed"));
|
|
|
|
|
|
if (close (fd_pair[0]) != 0)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, _("failed to close input pipe"));
|
|
|
|
|
|
filter_pid = child_pid;
|
|
|
|
|
|
if (n_open_pipes == open_pipes_alloc)
|
|
|
|
|
|
open_pipes = x2nrealloc (open_pipes, &open_pipes_alloc,
|
|
|
|
|
|
sizeof *open_pipes);
|
|
|
|
|
|
open_pipes[n_open_pipes++] = fd_pair[1];
|
|
|
|
|
|
return fd_pair[1];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Close the output file, and do any associated cleanup.
|
|
|
|
|
|
If FP and FD are both specified, they refer to the same open file;
|
|
|
|
|
|
in this case FP is closed, but FD is still used in cleanup. */
|
|
|
|
|
|
static void
|
|
|
|
|
|
closeout (FILE *fp, int fd, pid_t pid, char const *name)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (fp != NULL && fclose (fp) != 0 && ! ignorable (errno))
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", name);
|
|
|
|
|
|
if (fd >= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (fp == NULL && close (fd) < 0)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", name);
|
|
|
|
|
|
int j;
|
|
|
|
|
|
for (j = 0; j < n_open_pipes; ++j)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (open_pipes[j] == fd)
|
|
|
|
|
|
{
|
|
|
|
|
|
open_pipes[j] = open_pipes[--n_open_pipes];
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (pid > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
int wstatus = 0;
|
|
|
|
|
|
if (waitpid (pid, &wstatus, 0) == -1 && errno != ECHILD)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, _("waiting for child process"));
|
|
|
|
|
|
if (WIFSIGNALED (wstatus))
|
|
|
|
|
|
{
|
|
|
|
|
|
int sig = WTERMSIG (wstatus);
|
|
|
|
|
|
if (sig != SIGPIPE)
|
|
|
|
|
|
{
|
|
|
|
|
|
char signame[MAX (SIG2STR_MAX, INT_BUFSIZE_BOUND (int))];
|
|
|
|
|
|
if (sig2str (sig, signame) != 0)
|
|
|
|
|
|
sprintf (signame, "%d", sig);
|
|
|
|
|
|
error (sig + 128, 0,
|
2011-09-01 15:07:51 +01:00
|
|
|
|
_("with FILE=%s, signal %s from command: %s"),
|
|
|
|
|
|
name, signame, filter_command);
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (WIFEXITED (wstatus))
|
|
|
|
|
|
{
|
|
|
|
|
|
int ex = WEXITSTATUS (wstatus);
|
|
|
|
|
|
if (ex != 0)
|
|
|
|
|
|
error (ex, 0, _("with FILE=%s, exit %d from command: %s"),
|
|
|
|
|
|
name, ex, filter_command);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
/* shouldn't happen. */
|
|
|
|
|
|
error (EXIT_FAILURE, 0,
|
|
|
|
|
|
_("unknown status from command (0x%X)"), wstatus);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2010-01-08 03:42:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
/* Write BYTES bytes at BP to an output file.
|
2004-08-03 19:08:01 +00:00
|
|
|
|
If NEW_FILE_FLAG is true, open the next output file.
|
1995-10-31 12:34:48 +00:00
|
|
|
|
Otherwise add to the same output file already in use. */
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2004-08-03 19:08:01 +00:00
|
|
|
|
cwrite (bool new_file_flag, const char *bp, size_t bytes)
|
1995-10-31 12:34:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (new_file_flag)
|
|
|
|
|
|
{
|
2010-12-08 08:33:15 +00:00
|
|
|
|
if (!bp && bytes == 0 && elide_empty_files)
|
|
|
|
|
|
return;
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
closeout (NULL, output_desc, filter_pid, outfile);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
next_file_name ();
|
2010-01-08 03:42:27 -08:00
|
|
|
|
if ((output_desc = create (outfile)) < 0)
|
2009-08-22 18:56:06 +02:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", outfile);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
}
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
if (full_write (output_desc, bp, bytes) != bytes && ! ignorable (errno))
|
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
|
2010-01-08 03:42:27 -08:00
|
|
|
|
bytes_split (uintmax_t n_bytes, char *buf, size_t bufsize, uintmax_t max_files)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t n_read;
|
2004-08-03 19:08:01 +00:00
|
|
|
|
bool new_file_flag = true;
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t to_read;
|
2003-04-09 14:37:47 +00:00
|
|
|
|
uintmax_t to_write = n_bytes;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
char *bp_out;
|
2010-01-08 03:42:27 -08:00
|
|
|
|
uintmax_t opened = 0;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
2005-07-03 07:20:33 +00:00
|
|
|
|
n_read = full_read (STDIN_FILENO, buf, bufsize);
|
2010-10-07 13:12:36 +01:00
|
|
|
|
if (n_read < bufsize && errno)
|
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;
|
maint: replace each "for (;;)" with "while (true)"
Run this command:
git ls-files | grep '\.[ch]$' \
| xargs perl -pi -e 's/for \(;;\)/while (true)/g'
...except for randint.c, which does not include stdbool.h.
In that case, use "while (1)".
* gl/lib/randint.c (randint_genmax): Use "while (1)" for infloops.
* src/cat.c (simple_cat, cat): Use "while (true)" for infloops.
* gl/lib/randread.c (readsource, readisaac): Likewise.
* src/copy.c (copy_reg): Likewise.
* src/csplit.c (record_line_starts, process_regexp): Likewise.
* src/cut.c (set_fields): Likewise.
* src/dd.c (iread, parse_symbols): Likewise.
* src/df.c (find_mount_point, main): Likewise.
* src/du.c (main): Likewise.
* src/expand.c (expand): Likewise.
* src/factor.c (factor_using_division, do_stdin): Likewise.
* src/fmt.c (get_space): Likewise.
* src/ls.c (decode_switches): Likewise.
* src/od.c (main): Likewise.
* src/pr.c (main, read_line): Likewise.
* src/shred.c (dopass, genpattern): Likewise.
* src/sort.c (initbuf, fillbuf, getmonth, keycompare): Likewise.
* src/split.c (bytes_split, lines_split): Likewise.
* src/tac.c (tac_seekable): Likewise.
* src/test.c (and, or): Likewise.
* src/tr.c (squeeze_filter, main): Likewise.
* src/tsort.c (search_item): Likewise.
* src/unexpand.c (unexpand): Likewise.
* src/uniq.c (main): Likewise.
* src/yes.c (main): Likewise.
2010-05-01 14:24:35 +02:00
|
|
|
|
while (true)
|
2009-08-22 18:56:06 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (to_read < to_write)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (to_read) /* do not write 0 bytes! */
|
|
|
|
|
|
{
|
|
|
|
|
|
cwrite (new_file_flag, bp_out, to_read);
|
2010-01-08 03:42:27 -08:00
|
|
|
|
opened += new_file_flag;
|
2009-08-22 18:56:06 +02:00
|
|
|
|
to_write -= to_read;
|
|
|
|
|
|
new_file_flag = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t w = to_write;
|
|
|
|
|
|
cwrite (new_file_flag, bp_out, w);
|
2010-01-08 03:42:27 -08:00
|
|
|
|
opened += new_file_flag;
|
|
|
|
|
|
new_file_flag = !max_files || (opened < max_files);
|
2011-05-20 01:26:41 +01:00
|
|
|
|
if (!new_file_flag && ignorable (errno))
|
|
|
|
|
|
{
|
|
|
|
|
|
/* If filter no longer accepting input, stop reading. */
|
|
|
|
|
|
n_read = 0;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2009-08-22 18:56:06 +02:00
|
|
|
|
bp_out += w;
|
|
|
|
|
|
to_read -= w;
|
|
|
|
|
|
to_write = n_bytes;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
while (n_read == bufsize);
|
2010-01-08 03:42:27 -08:00
|
|
|
|
|
|
|
|
|
|
/* Ensure NUMBER files are created, which truncates
|
|
|
|
|
|
any existing files or notifies any consumers on fifos.
|
|
|
|
|
|
FIXME: Should we do this before EXIT_FAILURE? */
|
2010-12-08 08:33:15 +00:00
|
|
|
|
while (opened++ < max_files)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
cwrite (true, NULL, 0);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
2003-04-09 13:25:48 +00:00
|
|
|
|
/* Split into pieces of exactly N_LINES lines.
|
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-09 14:37:47 +00:00
|
|
|
|
lines_split (uintmax_t n_lines, 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;
|
2004-08-03 19:08:01 +00:00
|
|
|
|
bool new_file_flag = true;
|
2003-04-09 14:37:47 +00:00
|
|
|
|
uintmax_t n = 0;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
2005-07-03 07:20:33 +00:00
|
|
|
|
n_read = full_read (STDIN_FILENO, buf, bufsize);
|
2010-10-07 13:12:36 +01:00
|
|
|
|
if (n_read < bufsize && errno)
|
2009-08-22 18:56:06 +02: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';
|
maint: replace each "for (;;)" with "while (true)"
Run this command:
git ls-files | grep '\.[ch]$' \
| xargs perl -pi -e 's/for \(;;\)/while (true)/g'
...except for randint.c, which does not include stdbool.h.
In that case, use "while (1)".
* gl/lib/randint.c (randint_genmax): Use "while (1)" for infloops.
* src/cat.c (simple_cat, cat): Use "while (true)" for infloops.
* gl/lib/randread.c (readsource, readisaac): Likewise.
* src/copy.c (copy_reg): Likewise.
* src/csplit.c (record_line_starts, process_regexp): Likewise.
* src/cut.c (set_fields): Likewise.
* src/dd.c (iread, parse_symbols): Likewise.
* src/df.c (find_mount_point, main): Likewise.
* src/du.c (main): Likewise.
* src/expand.c (expand): Likewise.
* src/factor.c (factor_using_division, do_stdin): Likewise.
* src/fmt.c (get_space): Likewise.
* src/ls.c (decode_switches): Likewise.
* src/od.c (main): Likewise.
* src/pr.c (main, read_line): Likewise.
* src/shred.c (dopass, genpattern): Likewise.
* src/sort.c (initbuf, fillbuf, getmonth, keycompare): Likewise.
* src/split.c (bytes_split, lines_split): Likewise.
* src/tac.c (tac_seekable): Likewise.
* src/test.c (and, or): Likewise.
* src/tr.c (squeeze_filter, main): Likewise.
* src/tsort.c (search_item): Likewise.
* src/unexpand.c (unexpand): Likewise.
* src/uniq.c (main): Likewise.
* src/yes.c (main): Likewise.
2010-05-01 14:24:35 +02:00
|
|
|
|
while (true)
|
2009-08-22 18:56:06 +02:00
|
|
|
|
{
|
|
|
|
|
|
bp = memchr (bp, '\n', eob - bp + 1);
|
|
|
|
|
|
if (bp == eob)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (eob != bp_out) /* do not write 0 bytes! */
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t len = eob - bp_out;
|
|
|
|
|
|
cwrite (new_file_flag, bp_out, len);
|
|
|
|
|
|
new_file_flag = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
++bp;
|
|
|
|
|
|
if (++n >= n_lines)
|
|
|
|
|
|
{
|
|
|
|
|
|
cwrite (new_file_flag, bp_out, bp - bp_out);
|
|
|
|
|
|
bp_out = bp;
|
|
|
|
|
|
new_file_flag = true;
|
|
|
|
|
|
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
|
2003-04-09 14:37:47 +00:00
|
|
|
|
where lines longer than N_BYTES bytes occur.
|
2003-04-09 20:48:29 +00:00
|
|
|
|
FIXME: Allow N_BYTES to be any uintmax_t value, and don't require a
|
|
|
|
|
|
buffer of size N_BYTES, in case N_BYTES is very large. */
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1992-11-08 20:19:58 +00:00
|
|
|
|
static void
|
2003-04-09 20:48:29 +00:00
|
|
|
|
line_bytes_split (size_t n_bytes)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
char *bp;
|
2004-08-03 19:08:01 +00:00
|
|
|
|
bool eof = false;
|
2002-10-19 16:34:25 +00:00
|
|
|
|
size_t n_buffered = 0;
|
2003-04-11 10:51:56 +00:00
|
|
|
|
char *buf = xmalloc (n_bytes);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Fill up the full buffer size from the input file. */
|
|
|
|
|
|
|
2010-10-07 13:12:36 +01:00
|
|
|
|
size_t to_read = n_bytes - n_buffered;
|
|
|
|
|
|
size_t n_read = full_read (STDIN_FILENO, buf + n_buffered, to_read);
|
|
|
|
|
|
if (n_read < to_read && errno)
|
2009-08-22 18:56:06 +02: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)
|
2009-08-22 18:56:06 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (n_buffered == 0)
|
|
|
|
|
|
break;
|
|
|
|
|
|
eof = true;
|
|
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
/* Find where to end this chunk. */
|
|
|
|
|
|
bp = buf + n_buffered;
|
2003-04-08 09:30:09 +00:00
|
|
|
|
if (n_buffered == n_bytes)
|
2009-08-22 18:56:06 +02:00
|
|
|
|
{
|
|
|
|
|
|
while (bp > buf && bp[-1] != '\n')
|
|
|
|
|
|
bp--;
|
|
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
/* If chunk has no newlines, use all the chunk. */
|
|
|
|
|
|
if (bp == buf)
|
2009-08-22 18:56:06 +02:00
|
|
|
|
bp = buf + n_buffered;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
/* Output the chars as one output file. */
|
2004-08-03 19:08:01 +00:00
|
|
|
|
cwrite (true, buf, bp - buf);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
/* Discard the chars we just output; move rest of chunk
|
2009-08-22 18:56:06 +02: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)
|
2009-08-22 18:56:06 +02:00
|
|
|
|
memmove (buf, bp, n_buffered);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
while (!eof);
|
|
|
|
|
|
free (buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
/* -n l/[K/]N: Write lines to files of approximately file size / N.
|
|
|
|
|
|
The file is partitioned into file size / N sized portions, with the
|
|
|
|
|
|
last assigned any excess. If a line _starts_ within a partition
|
|
|
|
|
|
it is written completely to the corresponding file. Since lines
|
|
|
|
|
|
are not split even if they overlap a partition, the files written
|
|
|
|
|
|
can be larger or smaller than the partition size, and even empty
|
|
|
|
|
|
if a line is so long as to completely overlap the partition. */
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
|
lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
|
|
|
|
|
|
off_t file_size)
|
|
|
|
|
|
{
|
|
|
|
|
|
assert (n && k <= n && n <= file_size);
|
|
|
|
|
|
|
|
|
|
|
|
const off_t chunk_size = file_size / n;
|
|
|
|
|
|
uintmax_t chunk_no = 1;
|
|
|
|
|
|
off_t chunk_end = chunk_size - 1;
|
|
|
|
|
|
off_t n_written = 0;
|
|
|
|
|
|
bool new_file_flag = true;
|
2011-05-25 13:05:37 +01:00
|
|
|
|
bool chunk_truncated = false;
|
2010-01-08 03:42:27 -08:00
|
|
|
|
|
|
|
|
|
|
if (k > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Start reading 1 byte before kth chunk of file. */
|
|
|
|
|
|
off_t start = (k - 1) * chunk_size - 1;
|
|
|
|
|
|
if (lseek (STDIN_FILENO, start, SEEK_CUR) < 0)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
|
|
|
|
|
n_written = start;
|
|
|
|
|
|
chunk_no = k - 1;
|
|
|
|
|
|
chunk_end = chunk_no * chunk_size - 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (n_written < file_size)
|
|
|
|
|
|
{
|
|
|
|
|
|
char *bp = buf, *eob;
|
|
|
|
|
|
size_t n_read = full_read (STDIN_FILENO, buf, bufsize);
|
|
|
|
|
|
if (n_read < bufsize && errno)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
|
|
|
|
|
else if (n_read == 0)
|
|
|
|
|
|
break; /* eof. */
|
2012-01-05 09:26:32 +01:00
|
|
|
|
n_read = MIN (n_read, file_size - n_written);
|
2011-05-25 13:05:37 +01:00
|
|
|
|
chunk_truncated = false;
|
2010-01-08 03:42:27 -08:00
|
|
|
|
eob = buf + n_read;
|
|
|
|
|
|
|
|
|
|
|
|
while (bp != eob)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t to_write;
|
|
|
|
|
|
bool next = false;
|
|
|
|
|
|
|
|
|
|
|
|
/* Begin looking for '\n' at last byte of chunk. */
|
|
|
|
|
|
off_t skip = MIN (n_read, MAX (0, chunk_end - n_written));
|
|
|
|
|
|
char *bp_out = memchr (bp + skip, '\n', n_read - skip);
|
|
|
|
|
|
if (bp_out++)
|
|
|
|
|
|
next = true;
|
|
|
|
|
|
else
|
|
|
|
|
|
bp_out = eob;
|
|
|
|
|
|
to_write = bp_out - bp;
|
|
|
|
|
|
|
|
|
|
|
|
if (k == chunk_no)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* We don't use the stdout buffer here since we're writing
|
|
|
|
|
|
large chunks from an existing file, so it's more efficient
|
|
|
|
|
|
to write out directly. */
|
2011-05-20 01:10:58 +01:00
|
|
|
|
if (full_write (STDOUT_FILENO, bp, to_write) != to_write)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", _("write error"));
|
|
|
|
|
|
}
|
2011-05-24 09:59:08 +01:00
|
|
|
|
else if (! k)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
cwrite (new_file_flag, bp, to_write);
|
|
|
|
|
|
n_written += to_write;
|
|
|
|
|
|
bp += to_write;
|
|
|
|
|
|
n_read -= to_write;
|
|
|
|
|
|
new_file_flag = next;
|
|
|
|
|
|
|
|
|
|
|
|
/* A line could have been so long that it skipped
|
|
|
|
|
|
entire chunks. So create empty files in that case. */
|
|
|
|
|
|
while (next || chunk_end <= n_written - 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!next && bp == eob)
|
2011-05-24 09:59:08 +01:00
|
|
|
|
{
|
|
|
|
|
|
/* replenish buf, before going to next chunk. */
|
2011-05-25 13:05:37 +01:00
|
|
|
|
chunk_truncated = true;
|
2011-05-24 09:59:08 +01:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
2010-01-08 03:42:27 -08:00
|
|
|
|
chunk_no++;
|
|
|
|
|
|
if (k && chunk_no > k)
|
|
|
|
|
|
return;
|
|
|
|
|
|
if (chunk_no == n)
|
|
|
|
|
|
chunk_end = file_size - 1; /* >= chunk_size. */
|
|
|
|
|
|
else
|
|
|
|
|
|
chunk_end += chunk_size;
|
2010-12-08 08:33:15 +00:00
|
|
|
|
if (chunk_end <= n_written - 1)
|
2011-05-24 09:59:08 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (! k)
|
|
|
|
|
|
cwrite (true, NULL, 0);
|
|
|
|
|
|
}
|
2010-01-08 03:42:27 -08:00
|
|
|
|
else
|
|
|
|
|
|
next = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-05-25 13:05:37 +01:00
|
|
|
|
if (chunk_truncated)
|
|
|
|
|
|
chunk_no++;
|
|
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
/* Ensure NUMBER files are created, which truncates
|
|
|
|
|
|
any existing files or notifies any consumers on fifos.
|
|
|
|
|
|
FIXME: Should we do this before EXIT_FAILURE? */
|
2010-12-08 08:33:15 +00:00
|
|
|
|
while (!k && chunk_no++ <= n)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
cwrite (true, NULL, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* -n K/N: Extract Kth of N chunks. */
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
|
bytes_chunk_extract (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
|
|
|
|
|
|
off_t file_size)
|
|
|
|
|
|
{
|
|
|
|
|
|
off_t start;
|
|
|
|
|
|
off_t end;
|
|
|
|
|
|
|
|
|
|
|
|
assert (k && n && k <= n && n <= file_size);
|
|
|
|
|
|
|
|
|
|
|
|
start = (k - 1) * (file_size / n);
|
|
|
|
|
|
end = (k == n) ? file_size : k * (file_size / n);
|
|
|
|
|
|
|
|
|
|
|
|
if (lseek (STDIN_FILENO, start, SEEK_CUR) < 0)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
|
|
|
|
|
|
|
|
|
|
|
while (start < end)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t n_read = full_read (STDIN_FILENO, buf, bufsize);
|
|
|
|
|
|
if (n_read < bufsize && errno)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
|
|
|
|
|
else if (n_read == 0)
|
|
|
|
|
|
break; /* eof. */
|
2012-01-05 09:26:32 +01:00
|
|
|
|
n_read = MIN (n_read, end - start);
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
if (full_write (STDOUT_FILENO, buf, n_read) != n_read
|
|
|
|
|
|
&& ! ignorable (errno))
|
2010-01-08 03:42:27 -08:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", quote ("-"));
|
|
|
|
|
|
start += n_read;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct of_info
|
|
|
|
|
|
{
|
|
|
|
|
|
char *of_name;
|
|
|
|
|
|
int ofd;
|
2011-04-30 07:48:08 +02:00
|
|
|
|
FILE *ofile;
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
int opid;
|
2010-01-08 03:42:27 -08:00
|
|
|
|
} of_t;
|
|
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
|
|
{
|
|
|
|
|
|
OFD_NEW = -1,
|
|
|
|
|
|
OFD_APPEND = -2
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Rotate file descriptors when we're writing to more output files than we
|
|
|
|
|
|
have available file descriptors.
|
|
|
|
|
|
Return whether we came under file resource pressure.
|
|
|
|
|
|
If so, it's probably best to close each file when finished with it. */
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
|
ofile_open (of_t *files, size_t i_check, size_t nfiles)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool file_limit = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (files[i_check].ofd <= OFD_NEW)
|
|
|
|
|
|
{
|
|
|
|
|
|
int fd;
|
|
|
|
|
|
size_t i_reopen = i_check ? i_check - 1 : nfiles - 1;
|
|
|
|
|
|
|
|
|
|
|
|
/* Another process could have opened a file in between the calls to
|
|
|
|
|
|
close and open, so we should keep trying until open succeeds or
|
|
|
|
|
|
we've closed all of our files. */
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (files[i_check].ofd == OFD_NEW)
|
|
|
|
|
|
fd = create (files[i_check].of_name);
|
|
|
|
|
|
else /* OFD_APPEND */
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Attempt to append to previously opened file.
|
|
|
|
|
|
We use O_NONBLOCK to support writing to fifos,
|
|
|
|
|
|
where the other end has closed because of our
|
|
|
|
|
|
previous close. In that case we'll immediately
|
|
|
|
|
|
get an error, rather than waiting indefinitely.
|
|
|
|
|
|
In specialised cases the consumer can keep reading
|
|
|
|
|
|
from the fifo, terminating on conditions in the data
|
maint: quote 'like this' or "like this", not `like this'
* doc/coreutils.texi (Formatting the file names):
coreutils now quotes 'like this'.
* man/help2man:
* src/timeout.c (usage): Quote 'like this' in diagnostics.
* HACKING, Makefile.am, NEWS, README, README-hacking, TODO, cfg.mk:
* doc/Makefile.am, doc/coreutils.texi, m4/jm-macros.m4:
* man/Makefile.am, man/help2man, src/Makefile.am, src/copy.h:
* src/extract-magic, src/ls.c, src/pinky.c, src/pr.c, src/sort.c:
* src/split.c, src/timeout.c, src/who.c, tests/dd/skip-seek-past-file:
* tests/pr/pr-tests: Quote 'like this' in commentary.
* cfg.mk (old_NEWS_hash): Update due to changed old NEWS.
2012-01-22 15:26:00 -08:00
|
|
|
|
itself, or perhaps never in the case of 'tail -f'.
|
2011-05-20 01:10:58 +01:00
|
|
|
|
I.E. for fifos it is valid to attempt this reopen.
|
|
|
|
|
|
|
|
|
|
|
|
We don't handle the filter_command case here, as create()
|
|
|
|
|
|
will exit if there are not enough files in that case.
|
|
|
|
|
|
I.E. we don't support restarting filters, as that would
|
|
|
|
|
|
put too much burden on users specifying --filter commands. */
|
2011-04-30 07:48:08 +02:00
|
|
|
|
fd = open (files[i_check].of_name,
|
|
|
|
|
|
O_WRONLY | O_BINARY | O_APPEND | O_NONBLOCK);
|
|
|
|
|
|
}
|
2010-01-08 03:42:27 -08:00
|
|
|
|
|
|
|
|
|
|
if (-1 < fd)
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
if (!(errno == EMFILE || errno == ENFILE))
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", files[i_check].of_name);
|
|
|
|
|
|
|
|
|
|
|
|
file_limit = true;
|
|
|
|
|
|
|
|
|
|
|
|
/* Search backwards for an open file to close. */
|
|
|
|
|
|
while (files[i_reopen].ofd < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
i_reopen = i_reopen ? i_reopen - 1 : nfiles - 1;
|
|
|
|
|
|
/* No more open files to close, exit with E[NM]FILE. */
|
|
|
|
|
|
if (i_reopen == i_check)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", files[i_check].of_name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-05-20 01:10:58 +01:00
|
|
|
|
if (fclose (files[i_reopen].ofile) != 0)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", files[i_reopen].of_name);
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
files[i_reopen].ofile = NULL;
|
2010-01-08 03:42:27 -08:00
|
|
|
|
files[i_reopen].ofd = OFD_APPEND;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
files[i_check].ofd = fd;
|
|
|
|
|
|
if (!(files[i_check].ofile = fdopen (fd, "a")))
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", files[i_check].of_name);
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
files[i_check].opid = filter_pid;
|
|
|
|
|
|
filter_pid = 0;
|
2010-01-08 03:42:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return file_limit;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* -n r/[K/]N: Divide file into N chunks in round robin fashion.
|
|
|
|
|
|
When K == 0, we try to keep the files open in parallel.
|
|
|
|
|
|
If we run out of file resources, then we revert
|
|
|
|
|
|
to opening and closing each file for each line. */
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
|
lines_rr (uintmax_t k, uintmax_t n, char *buf, size_t bufsize)
|
|
|
|
|
|
{
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
bool wrapped = false;
|
2011-05-20 01:26:41 +01:00
|
|
|
|
bool wrote = false;
|
2010-01-08 03:42:27 -08:00
|
|
|
|
bool file_limit;
|
|
|
|
|
|
size_t i_file;
|
2011-01-24 09:38:40 +01:00
|
|
|
|
of_t *files IF_LINT (= NULL);
|
2010-01-08 03:42:27 -08:00
|
|
|
|
uintmax_t line_no;
|
|
|
|
|
|
|
|
|
|
|
|
if (k)
|
|
|
|
|
|
line_no = 1;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SIZE_MAX < n)
|
|
|
|
|
|
error (exit_failure, 0, "%s", _("memory exhausted"));
|
|
|
|
|
|
files = xnmalloc (n, sizeof *files);
|
|
|
|
|
|
|
|
|
|
|
|
/* Generate output file names. */
|
|
|
|
|
|
for (i_file = 0; i_file < n; i_file++)
|
|
|
|
|
|
{
|
|
|
|
|
|
next_file_name ();
|
|
|
|
|
|
files[i_file].of_name = xstrdup (outfile);
|
|
|
|
|
|
files[i_file].ofd = OFD_NEW;
|
|
|
|
|
|
files[i_file].ofile = NULL;
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
files[i_file].opid = 0;
|
2010-01-08 03:42:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
i_file = 0;
|
|
|
|
|
|
file_limit = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
char *bp = buf, *eob;
|
|
|
|
|
|
/* Use safe_read() rather than full_read() here
|
|
|
|
|
|
so that we process available data immediately. */
|
|
|
|
|
|
size_t n_read = safe_read (STDIN_FILENO, buf, bufsize);
|
|
|
|
|
|
if (n_read == SAFE_READ_ERROR)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
|
|
|
|
|
else if (n_read == 0)
|
|
|
|
|
|
break; /* eof. */
|
|
|
|
|
|
eob = buf + n_read;
|
|
|
|
|
|
|
|
|
|
|
|
while (bp != eob)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t to_write;
|
|
|
|
|
|
bool next = false;
|
|
|
|
|
|
|
|
|
|
|
|
/* Find end of line. */
|
|
|
|
|
|
char *bp_out = memchr (bp, '\n', eob - bp);
|
|
|
|
|
|
if (bp_out)
|
|
|
|
|
|
{
|
|
|
|
|
|
bp_out++;
|
|
|
|
|
|
next = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
bp_out = eob;
|
|
|
|
|
|
to_write = bp_out - bp;
|
|
|
|
|
|
|
|
|
|
|
|
if (k)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (line_no == k && unbuffered)
|
|
|
|
|
|
{
|
2011-05-20 01:10:58 +01:00
|
|
|
|
if (full_write (STDOUT_FILENO, bp, to_write) != to_write)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", _("write error"));
|
|
|
|
|
|
}
|
2011-05-20 01:10:58 +01:00
|
|
|
|
else if (line_no == k && fwrite (bp, to_write, 1, stdout) != 1)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
{
|
|
|
|
|
|
clearerr (stdout); /* To silence close_stdout(). */
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", _("write error"));
|
|
|
|
|
|
}
|
|
|
|
|
|
if (next)
|
|
|
|
|
|
line_no = (line_no == n) ? 1 : line_no + 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Secure file descriptor. */
|
|
|
|
|
|
file_limit |= ofile_open (files, i_file, n);
|
|
|
|
|
|
if (unbuffered)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Note writing to fd, rather than flushing the FILE gives
|
|
|
|
|
|
an 8% performance benefit, due to reduced data copying. */
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
if (full_write (files[i_file].ofd, bp, to_write) != to_write
|
|
|
|
|
|
&& ! ignorable (errno))
|
2010-01-08 03:42:27 -08:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", files[i_file].of_name);
|
|
|
|
|
|
}
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
else if (fwrite (bp, to_write, 1, files[i_file].ofile) != 1
|
|
|
|
|
|
&& ! ignorable (errno))
|
2010-01-08 03:42:27 -08:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", files[i_file].of_name);
|
2011-05-20 01:26:41 +01:00
|
|
|
|
if (! ignorable (errno))
|
|
|
|
|
|
wrote = true;
|
|
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
if (file_limit)
|
|
|
|
|
|
{
|
2011-05-20 01:10:58 +01:00
|
|
|
|
if (fclose (files[i_file].ofile) != 0)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", files[i_file].of_name);
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
files[i_file].ofile = NULL;
|
2010-01-08 03:42:27 -08:00
|
|
|
|
files[i_file].ofd = OFD_APPEND;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (next && ++i_file == n)
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
{
|
|
|
|
|
|
wrapped = true;
|
2011-05-20 01:26:41 +01:00
|
|
|
|
/* If no filters are accepting input, stop reading. */
|
|
|
|
|
|
if (! wrote)
|
|
|
|
|
|
goto no_filters;
|
|
|
|
|
|
wrote = false;
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
i_file = 0;
|
|
|
|
|
|
}
|
2010-01-08 03:42:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bp = bp_out;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-05-20 01:26:41 +01:00
|
|
|
|
no_filters:
|
2010-01-08 03:42:27 -08:00
|
|
|
|
/* Ensure all files created, so that any existing files are truncated,
|
|
|
|
|
|
and to signal any waiting fifo consumers.
|
|
|
|
|
|
Also, close any open file descriptors.
|
|
|
|
|
|
FIXME: Should we do this before EXIT_FAILURE? */
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
if (!k)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
{
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
int ceiling = (wrapped ? n : i_file);
|
|
|
|
|
|
for (i_file = 0; i_file < n; i_file++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (i_file >= ceiling && !elide_empty_files)
|
|
|
|
|
|
file_limit |= ofile_open (files, i_file, n);
|
|
|
|
|
|
if (files[i_file].ofd >= 0)
|
|
|
|
|
|
closeout (files[i_file].ofile, files[i_file].ofd,
|
|
|
|
|
|
files[i_file].opid, files[i_file].of_name);
|
|
|
|
|
|
files[i_file].ofd = OFD_APPEND;
|
|
|
|
|
|
}
|
2010-01-08 03:42:27 -08:00
|
|
|
|
}
|
2012-08-02 19:31:36 +02:00
|
|
|
|
IF_LINT (free (files));
|
2010-01-08 03:42:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
/* Parse K/N syntax of chunk options. */
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
|
parse_chunk (uintmax_t *k_units, uintmax_t *n_units, char *slash)
|
|
|
|
|
|
{
|
|
|
|
|
|
*slash = '\0';
|
2011-04-30 07:48:08 +02:00
|
|
|
|
if (xstrtoumax (slash + 1, NULL, 10, n_units, "") != LONGINT_OK
|
2010-01-08 03:42:27 -08:00
|
|
|
|
|| *n_units == 0)
|
2011-04-30 07:48:08 +02:00
|
|
|
|
error (EXIT_FAILURE, 0, _("%s: invalid number of chunks"), slash + 1);
|
2010-01-08 03:42:27 -08:00
|
|
|
|
if (slash != optarg /* a leading number is specified. */
|
|
|
|
|
|
&& (xstrtoumax (optarg, NULL, 10, k_units, "") != LONGINT_OK
|
|
|
|
|
|
|| *k_units == 0 || *n_units < *k_units))
|
|
|
|
|
|
error (EXIT_FAILURE, 0, _("%s: invalid chunk number"), optarg);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2010-12-30 01:36:59 +00:00
|
|
|
|
enum Split_type split_type = type_undef;
|
2010-12-08 08:33:15 +00:00
|
|
|
|
size_t in_blk_size = 0; /* optimal block size of input file device */
|
2004-04-15 09:10:54 +00:00
|
|
|
|
size_t page_size = getpagesize ();
|
2010-01-08 03:42:27 -08:00
|
|
|
|
uintmax_t k_units = 0;
|
2003-04-09 14:37:47 +00:00
|
|
|
|
uintmax_t n_units;
|
2010-01-08 03:42:27 -08:00
|
|
|
|
|
The following commands and options now support the standard size
suffixes kB, M, MB, G, GB, and so on for T, P, Y, Z, and Y:
head -c, head -n, od -j, od -N, od -S, split -b, split -C,
tail -c, tail -n.
* doc/coreutils.texi (od invocation, head invocation, tail invocation):
Document support for new size suffixes.
(head invocation, tail invocation):
Document that -n uses the same suffixes as -c.
(tail invocation): More-clearly document what leading "+" does.
* src/head.c (usage, string_to_integer): Support new suffixes.
* src/od.c (usage, main): Likewise.
* src/split.c (usage, main): Likewise.
* src/tail.c (usage, parse_options): Likewise.
Prompted by a patch from Evan Hunt.
2007-05-03 13:45:26 +02:00
|
|
|
|
static char const multipliers[] = "bEGKkMmPTYZ0";
|
1995-10-31 12:34:48 +00:00
|
|
|
|
int c;
|
|
|
|
|
|
int digits_optind = 0;
|
2012-05-09 23:53:16 -07:00
|
|
|
|
off_t file_size IF_LINT (= 0);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2003-06-17 18:13:23 +00:00
|
|
|
|
initialize_main (&argc, &argv);
|
2008-06-03 08:34:09 +02:00
|
|
|
|
set_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
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
infile = bad_cast ("-");
|
2008-10-28 22:06:44 +01:00
|
|
|
|
outbase = bad_cast ("x");
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
while (true)
|
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;
|
2010-01-08 03:42:27 -08:00
|
|
|
|
char *slash;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
c = getopt_long (argc, argv, "0123456789C:a:b:del:n:u",
|
|
|
|
|
|
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)
|
2009-08-22 18:56:06 +02:00
|
|
|
|
break;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
|
|
|
|
|
switch (c)
|
2009-08-22 18:56:06 +02:00
|
|
|
|
{
|
|
|
|
|
|
case 'a':
|
|
|
|
|
|
{
|
|
|
|
|
|
unsigned long tmp;
|
|
|
|
|
|
if (xstrtoul (optarg, NULL, 10, &tmp, "") != LONGINT_OK
|
|
|
|
|
|
|| SIZE_MAX / sizeof (size_t) < tmp)
|
|
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("%s: invalid suffix length"), optarg);
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
|
|
|
|
|
suffix_length = tmp;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2012-02-19 13:52:47 +01:00
|
|
|
|
case ADDITIONAL_SUFFIX_OPTION:
|
|
|
|
|
|
if (last_component (optarg) != optarg)
|
|
|
|
|
|
{
|
|
|
|
|
|
error (0, 0,
|
|
|
|
|
|
_("invalid suffix %s, contains directory separator"),
|
|
|
|
|
|
quote (optarg));
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
|
|
|
|
|
additional_suffix = optarg;
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
|
case 'b':
|
|
|
|
|
|
if (split_type != type_undef)
|
|
|
|
|
|
FAIL_ONLY_ONE_WAY ();
|
|
|
|
|
|
split_type = type_bytes;
|
|
|
|
|
|
if (xstrtoumax (optarg, NULL, 10, &n_units, multipliers) != LONGINT_OK
|
|
|
|
|
|
|| n_units == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("%s: invalid number of bytes"), optarg);
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
2010-11-07 03:09:38 +00:00
|
|
|
|
/* If input is a pipe, we could get more data than is possible
|
|
|
|
|
|
to write to a single file, so indicate that immediately
|
|
|
|
|
|
rather than having possibly future invocations fail. */
|
|
|
|
|
|
if (OFF_T_MAX < n_units)
|
|
|
|
|
|
error (EXIT_FAILURE, EFBIG,
|
|
|
|
|
|
_("%s: invalid number of bytes"), optarg);
|
|
|
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
|
|
if (split_type != type_undef)
|
|
|
|
|
|
FAIL_ONLY_ONE_WAY ();
|
|
|
|
|
|
split_type = type_lines;
|
|
|
|
|
|
if (xstrtoumax (optarg, NULL, 10, &n_units, "") != LONGINT_OK
|
|
|
|
|
|
|| n_units == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("%s: invalid number of lines"), optarg);
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
|
|
if (split_type != type_undef)
|
|
|
|
|
|
FAIL_ONLY_ONE_WAY ();
|
|
|
|
|
|
split_type = type_byteslines;
|
|
|
|
|
|
if (xstrtoumax (optarg, NULL, 10, &n_units, multipliers) != LONGINT_OK
|
|
|
|
|
|
|| n_units == 0 || SIZE_MAX < n_units)
|
|
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("%s: invalid number of bytes"), optarg);
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
2010-11-07 03:09:38 +00:00
|
|
|
|
if (OFF_T_MAX < n_units)
|
|
|
|
|
|
error (EXIT_FAILURE, EFBIG,
|
|
|
|
|
|
_("%s: invalid number of bytes"), optarg);
|
2009-08-22 18:56:06 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
case 'n':
|
|
|
|
|
|
if (split_type != type_undef)
|
|
|
|
|
|
FAIL_ONLY_ONE_WAY ();
|
|
|
|
|
|
/* skip any whitespace */
|
|
|
|
|
|
while (isspace (to_uchar (*optarg)))
|
|
|
|
|
|
optarg++;
|
2011-03-31 17:59:16 +02:00
|
|
|
|
if (STRNCMP_LIT (optarg, "r/") == 0)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
{
|
|
|
|
|
|
split_type = type_rr;
|
|
|
|
|
|
optarg += 2;
|
|
|
|
|
|
}
|
2011-03-31 17:59:16 +02:00
|
|
|
|
else if (STRNCMP_LIT (optarg, "l/") == 0)
|
2010-01-08 03:42:27 -08:00
|
|
|
|
{
|
|
|
|
|
|
split_type = type_chunk_lines;
|
|
|
|
|
|
optarg += 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
split_type = type_chunk_bytes;
|
|
|
|
|
|
if ((slash = strchr (optarg, '/')))
|
|
|
|
|
|
parse_chunk (&k_units, &n_units, slash);
|
|
|
|
|
|
else if (xstrtoumax (optarg, NULL, 10, &n_units, "") != LONGINT_OK
|
|
|
|
|
|
|| n_units == 0)
|
|
|
|
|
|
error (EXIT_FAILURE, 0, _("%s: invalid number of chunks"), optarg);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
|
|
unbuffered = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
|
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;
|
|
|
|
|
|
n_units = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (split_type != type_undef && split_type != type_digits)
|
|
|
|
|
|
FAIL_ONLY_ONE_WAY ();
|
|
|
|
|
|
if (digits_optind != 0 && digits_optind != this_optind)
|
|
|
|
|
|
n_units = 0; /* More than one number given; ignore other. */
|
|
|
|
|
|
digits_optind = this_optind;
|
|
|
|
|
|
if (!DECIMAL_DIGIT_ACCUMULATE (n_units, c - '0', uintmax_t))
|
|
|
|
|
|
{
|
|
|
|
|
|
char buffer[INT_BUFSIZE_BOUND (uintmax_t)];
|
|
|
|
|
|
error (EXIT_FAILURE, 0,
|
|
|
|
|
|
_("line count option -%s%c... is too large"),
|
|
|
|
|
|
umaxtostr (n_units, buffer), c);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
|
|
suffix_alphabet = "0123456789";
|
2012-01-29 15:20:31 +01:00
|
|
|
|
if (optarg)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (strlen (optarg) != strspn (optarg, suffix_alphabet))
|
|
|
|
|
|
{
|
|
|
|
|
|
error (0, 0,
|
|
|
|
|
|
_("%s: invalid start value for numerical suffix"),
|
|
|
|
|
|
optarg);
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Skip any leading zero. */
|
|
|
|
|
|
while (*optarg == '0' && *(optarg + 1) != '\0')
|
|
|
|
|
|
optarg++;
|
|
|
|
|
|
numeric_suffix_start = optarg;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2009-08-22 18:56:06 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
case 'e':
|
|
|
|
|
|
elide_empty_files = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
case FILTER_OPTION:
|
|
|
|
|
|
filter_command = optarg;
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2010-12-08 08:33:15 +00:00
|
|
|
|
case IO_BLKSIZE_OPTION:
|
|
|
|
|
|
{
|
|
|
|
|
|
uintmax_t tmp_blk_size;
|
|
|
|
|
|
if (xstrtoumax (optarg, NULL, 10, &tmp_blk_size,
|
|
|
|
|
|
multipliers) != LONGINT_OK
|
|
|
|
|
|
|| tmp_blk_size == 0 || SIZE_MAX - page_size < tmp_blk_size)
|
|
|
|
|
|
error (0, 0, _("%s: invalid IO block size"), optarg);
|
|
|
|
|
|
else
|
|
|
|
|
|
in_blk_size = tmp_blk_size;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
|
case VERBOSE_OPTION:
|
|
|
|
|
|
verbose = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case_GETOPT_HELP_CHAR;
|
|
|
|
|
|
|
|
|
|
|
|
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-05-20 01:18:28 +01:00
|
|
|
|
if (k_units != 0 && filter_command)
|
|
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("--filter does not process a chunk extracted to stdout"));
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
/* Handle default case. */
|
|
|
|
|
|
if (split_type == type_undef)
|
|
|
|
|
|
{
|
|
|
|
|
|
split_type = type_lines;
|
2003-04-08 09:50:50 +00:00
|
|
|
|
n_units = 1000;
|
1995-10-31 12:34:48 +00:00
|
|
|
|
}
|
1995-05-20 11:52:36 +00:00
|
|
|
|
|
2003-04-09 14:37:47 +00:00
|
|
|
|
if (n_units == 0)
|
1996-10-13 17:53:47 +00:00
|
|
|
|
{
|
2010-01-08 03:42:27 -08:00
|
|
|
|
error (0, 0, _("%s: invalid number of lines"), "0");
|
1996-10-13 17:53:47 +00:00
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
1995-05-20 11:52:36 +00:00
|
|
|
|
|
2010-12-30 01:36:59 +00:00
|
|
|
|
set_suffix_length (n_units, split_type);
|
2010-01-08 03:42:27 -08: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
|
|
|
|
{
|
2004-06-21 15:03:35 +00:00
|
|
|
|
error (0, 0, _("extra operand %s"), quote (argv[optind]));
|
1996-10-13 17:53:47 +00:00
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
2012-01-29 15:20:31 +01:00
|
|
|
|
/* Check that the suffix length is large enough for the numerical
|
|
|
|
|
|
suffix start value. */
|
|
|
|
|
|
if (numeric_suffix_start && strlen (numeric_suffix_start) > suffix_length)
|
|
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("numerical suffix start value is too large "
|
|
|
|
|
|
"for the suffix length"));
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
/* Open the input file. */
|
2005-07-03 07:20:33 +00:00
|
|
|
|
if (! STREQ (infile, "-")
|
|
|
|
|
|
&& fd_reopen (STDIN_FILENO, infile, O_RDONLY, 0) < 0)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, _("cannot open %s for reading"),
|
2009-08-22 18:56:06 +02:00
|
|
|
|
quote (infile));
|
2005-07-03 07:20:33 +00:00
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
/* Binary I/O is safer when byte counts are used. */
|
2005-07-11 18:24:42 +00:00
|
|
|
|
if (O_BINARY && ! isatty (STDIN_FILENO))
|
2008-10-12 14:50:02 +02:00
|
|
|
|
xfreopen (NULL, "rb", stdin);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
/* Get the optimal block size of input device and make a buffer. */
|
|
|
|
|
|
|
2012-06-22 09:32:34 +01:00
|
|
|
|
if (fstat (STDIN_FILENO, &in_stat_buf) != 0)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
2010-12-08 08:33:15 +00:00
|
|
|
|
if (in_blk_size == 0)
|
2012-06-22 09:32:34 +01:00
|
|
|
|
in_blk_size = io_blksize (in_stat_buf);
|
2012-05-07 09:32:00 +02:00
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
if (split_type == type_chunk_bytes || split_type == type_chunk_lines)
|
|
|
|
|
|
{
|
|
|
|
|
|
off_t input_offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
|
2012-06-22 09:32:34 +01:00
|
|
|
|
if (usable_st_size (&in_stat_buf))
|
|
|
|
|
|
file_size = in_stat_buf.st_size;
|
2012-05-09 23:53:16 -07:00
|
|
|
|
else if (0 <= input_offset)
|
|
|
|
|
|
{
|
|
|
|
|
|
file_size = lseek (STDIN_FILENO, 0, SEEK_END);
|
|
|
|
|
|
input_offset = (file_size < 0
|
|
|
|
|
|
? file_size
|
|
|
|
|
|
: lseek (STDIN_FILENO, input_offset, SEEK_SET));
|
|
|
|
|
|
}
|
2010-01-08 03:42:27 -08:00
|
|
|
|
if (input_offset < 0)
|
|
|
|
|
|
error (EXIT_FAILURE, 0, _("%s: cannot determine file size"),
|
|
|
|
|
|
quote (infile));
|
|
|
|
|
|
file_size -= input_offset;
|
|
|
|
|
|
/* Overflow, and sanity checking. */
|
|
|
|
|
|
if (OFF_T_MAX < n_units)
|
|
|
|
|
|
{
|
|
|
|
|
|
char buffer[INT_BUFSIZE_BOUND (uintmax_t)];
|
|
|
|
|
|
error (EXIT_FAILURE, EFBIG, _("%s: invalid number of chunks"),
|
|
|
|
|
|
umaxtostr (n_units, buffer));
|
|
|
|
|
|
}
|
|
|
|
|
|
/* increase file_size to n_units here, so that we still process
|
|
|
|
|
|
any input data, and create empty files for the rest. */
|
|
|
|
|
|
file_size = MAX (file_size, n_units);
|
|
|
|
|
|
}
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
2012-08-02 19:31:36 +02:00
|
|
|
|
void *b = xmalloc (in_blk_size + 1 + page_size - 1);
|
|
|
|
|
|
char *buf = ptr_align (b, page_size);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
/* When filtering, closure of one pipe must not terminate the process,
|
|
|
|
|
|
as there may still be other streams expecting input from us. */
|
|
|
|
|
|
if (filter_command)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct sigaction act;
|
2011-05-19 23:23:23 +01:00
|
|
|
|
sigemptyset (&newblocked);
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
sigaction (SIGPIPE, NULL, &act);
|
|
|
|
|
|
if (act.sa_handler != SIG_IGN)
|
|
|
|
|
|
sigaddset (&newblocked, SIGPIPE);
|
2011-05-19 23:23:23 +01:00
|
|
|
|
sigprocmask (SIG_BLOCK, &newblocked, &oldblocked);
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
switch (split_type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case type_digits:
|
|
|
|
|
|
case type_lines:
|
2003-04-09 14:37:47 +00:00
|
|
|
|
lines_split (n_units, buf, in_blk_size);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case type_bytes:
|
2010-01-08 03:42:27 -08:00
|
|
|
|
bytes_split (n_units, buf, in_blk_size, 0);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case type_byteslines:
|
2003-04-09 14:37:47 +00:00
|
|
|
|
line_bytes_split (n_units);
|
1995-10-31 12:34:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
2010-01-08 03:42:27 -08:00
|
|
|
|
case type_chunk_bytes:
|
|
|
|
|
|
if (k_units == 0)
|
|
|
|
|
|
bytes_split (file_size / n_units, buf, in_blk_size, n_units);
|
|
|
|
|
|
else
|
|
|
|
|
|
bytes_chunk_extract (k_units, n_units, buf, in_blk_size, file_size);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case type_chunk_lines:
|
|
|
|
|
|
lines_chunk_split (k_units, n_units, buf, in_blk_size, file_size);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case type_rr:
|
maint: quote 'like this' or "like this", not `like this'
* doc/coreutils.texi (Formatting the file names):
coreutils now quotes 'like this'.
* man/help2man:
* src/timeout.c (usage): Quote 'like this' in diagnostics.
* HACKING, Makefile.am, NEWS, README, README-hacking, TODO, cfg.mk:
* doc/Makefile.am, doc/coreutils.texi, m4/jm-macros.m4:
* man/Makefile.am, man/help2man, src/Makefile.am, src/copy.h:
* src/extract-magic, src/ls.c, src/pinky.c, src/pr.c, src/sort.c:
* src/split.c, src/timeout.c, src/who.c, tests/dd/skip-seek-past-file:
* tests/pr/pr-tests: Quote 'like this' in commentary.
* cfg.mk (old_NEWS_hash): Update due to changed old NEWS.
2012-01-22 15:26:00 -08:00
|
|
|
|
/* Note, this is like 'sed -n ${k}~${n}p' when k > 0,
|
2010-01-08 03:42:27 -08:00
|
|
|
|
but the functionality is provided for symmetry. */
|
|
|
|
|
|
lines_rr (k_units, n_units, buf, in_blk_size);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
1995-10-31 12:34:48 +00:00
|
|
|
|
default:
|
|
|
|
|
|
abort ();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-02 19:31:36 +02:00
|
|
|
|
IF_LINT (free (b));
|
|
|
|
|
|
|
2005-07-03 07:20:33 +00:00
|
|
|
|
if (close (STDIN_FILENO) != 0)
|
1996-03-24 16:59:11 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infile);
|
split: accept new output --filter=CMD option
* src/split.c: Include <signal.h>, <sys/wait.h> and "sig2str.h".
(FILTER_OPTION): New anonymous enum member.
(filter_command, filter_pid): New globals.
(open_pipes, open_pipes_alloc, n_open_pipes): Likewise.
(oldblocked, newblocked): Likewise.
(longopts): Add "filter".
(usage): Document --filter.
(create): Extend to create a pipe and fork "sh -c CMD".
(closeout): Adapt to close a pipe and wait for child process.
(cwrite): Call closeout, not just close.
(lines_chunk_split): FIXME
(bytes_chunk_extract): FIXME
(opid, ofile_open, lines_rr, main): FIXME
(ignorable): New function, to encapsulate EPIPE test.
2011-04-29 12:23:27 +02:00
|
|
|
|
closeout (NULL, output_desc, filter_pid, 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
|
|
|
|
}
|