2007-03-26 03:20:18 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* split - split a file into pieces
|
2008-09-25 17:43:34 +05:30
|
|
|
* Copyright (c) 2007 Bernhard Reutner-Fischer
|
2007-03-26 03:20:18 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2007-03-26 03:20:18 +05:30
|
|
|
*/
|
2016-11-23 19:16:56 +05:30
|
|
|
//config:config SPLIT
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "split (5 kb)"
|
2016-11-23 19:16:56 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Split a file into pieces.
|
2016-11-23 19:16:56 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_SPLIT_FANCY
|
|
|
|
//config: bool "Fancy extensions"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on SPLIT
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Add support for features not required by SUSv3.
|
|
|
|
//config: Supports additional suffixes 'b' for 512 bytes,
|
|
|
|
//config: 'g' for 1GiB for the -b option.
|
2016-11-23 19:16:56 +05:30
|
|
|
|
|
|
|
//applet:IF_SPLIT(APPLET(split, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_SPLIT) += split.o
|
|
|
|
|
2007-03-26 19:58:12 +05:30
|
|
|
/* BB_AUDIT: SUSv3 compliant
|
2007-03-26 03:20:18 +05:30
|
|
|
* SUSv3 requirements:
|
|
|
|
* http://www.opengroup.org/onlinepubs/009695399/utilities/split.html
|
|
|
|
*/
|
2011-03-31 18:13:25 +05:30
|
|
|
|
|
|
|
//usage:#define split_trivial_usage
|
|
|
|
//usage: "[OPTIONS] [INPUT [PREFIX]]"
|
|
|
|
//usage:#define split_full_usage "\n\n"
|
2011-06-05 07:28:28 +05:30
|
|
|
//usage: " -b N[k|m] Split by N (kilo|mega)bytes"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: "\n -l N Split by N lines"
|
|
|
|
//usage: "\n -a N Use N letters as suffix"
|
|
|
|
//usage:
|
|
|
|
//usage:#define split_example_usage
|
|
|
|
//usage: "$ split TODO foo\n"
|
|
|
|
//usage: "$ cat TODO | split -a 2 -l 2 TODO_\n"
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2007-03-27 01:45:40 +05:30
|
|
|
|
2007-03-28 22:18:40 +05:30
|
|
|
#if ENABLE_FEATURE_SPLIT_FANCY
|
2013-07-14 03:19:45 +05:30
|
|
|
static const struct suffix_mult split_suffixes[] = {
|
2007-03-26 16:16:31 +05:30
|
|
|
{ "b", 512 },
|
|
|
|
{ "k", 1024 },
|
|
|
|
{ "m", 1024*1024 },
|
|
|
|
{ "g", 1024*1024*1024 },
|
2009-09-06 16:17:55 +05:30
|
|
|
{ "", 0 }
|
2007-03-26 16:16:31 +05:30
|
|
|
};
|
2013-07-14 03:19:45 +05:30
|
|
|
#endif
|
2007-03-26 03:20:18 +05:30
|
|
|
|
|
|
|
/* Increment the suffix part of the filename.
|
2007-03-27 01:45:40 +05:30
|
|
|
* Returns NULL if we are out of filenames.
|
2007-03-26 03:20:18 +05:30
|
|
|
*/
|
2007-03-27 01:45:40 +05:30
|
|
|
static char *next_file(char *old, unsigned suffix_len)
|
2007-03-26 03:20:18 +05:30
|
|
|
{
|
2007-03-27 01:45:40 +05:30
|
|
|
size_t end = strlen(old);
|
2007-03-26 03:20:18 +05:30
|
|
|
unsigned i = 1;
|
|
|
|
char *curr;
|
|
|
|
|
2011-03-02 08:37:14 +05:30
|
|
|
while (1) {
|
2007-03-27 01:45:40 +05:30
|
|
|
curr = old + end - i;
|
2007-03-26 03:20:18 +05:30
|
|
|
if (*curr < 'z') {
|
2007-03-26 16:16:31 +05:30
|
|
|
*curr += 1;
|
2007-03-26 03:20:18 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
2007-03-26 19:58:12 +05:30
|
|
|
if (i > suffix_len) {
|
2007-03-27 01:45:40 +05:30
|
|
|
return NULL;
|
2007-03-26 19:58:12 +05:30
|
|
|
}
|
|
|
|
*curr = 'a';
|
2011-03-02 08:37:14 +05:30
|
|
|
}
|
2007-03-27 01:45:40 +05:30
|
|
|
|
|
|
|
return old;
|
2007-03-26 03:20:18 +05:30
|
|
|
}
|
2007-03-26 19:58:12 +05:30
|
|
|
|
2007-03-27 01:45:40 +05:30
|
|
|
#define read_buffer bb_common_bufsiz1
|
2007-06-04 15:46:52 +05:30
|
|
|
enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
|
2007-03-27 01:45:40 +05:30
|
|
|
|
2007-03-26 03:20:18 +05:30
|
|
|
#define SPLIT_OPT_l (1<<0)
|
|
|
|
#define SPLIT_OPT_b (1<<1)
|
2007-03-26 16:16:31 +05:30
|
|
|
#define SPLIT_OPT_a (1<<2)
|
2007-03-26 03:20:18 +05:30
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int split_main(int argc UNUSED_PARAM, char **argv)
|
2007-03-26 03:20:18 +05:30
|
|
|
{
|
2007-03-27 01:45:40 +05:30
|
|
|
unsigned suffix_len = 2;
|
|
|
|
char *pfx;
|
|
|
|
char *count_p;
|
|
|
|
const char *sfx;
|
2007-03-28 22:18:40 +05:30
|
|
|
off_t cnt = 1000;
|
|
|
|
off_t remaining = 0;
|
2007-03-27 01:45:40 +05:30
|
|
|
unsigned opt;
|
2007-03-28 22:18:40 +05:30
|
|
|
ssize_t bytes_read, to_write;
|
2007-03-27 01:45:40 +05:30
|
|
|
char *src;
|
|
|
|
|
2016-04-21 22:08:51 +05:30
|
|
|
setup_common_bufsiz();
|
|
|
|
|
2017-08-09 01:25:02 +05:30
|
|
|
opt = getopt32(argv, "^"
|
|
|
|
"l:b:a:+" /* -a N */
|
|
|
|
"\0" "?2"/*max 2 args*/,
|
|
|
|
&count_p, &count_p, &suffix_len
|
|
|
|
);
|
2007-03-26 03:20:18 +05:30
|
|
|
|
2007-03-26 23:49:29 +05:30
|
|
|
if (opt & SPLIT_OPT_l)
|
2008-03-17 14:39:09 +05:30
|
|
|
cnt = XATOOFF(count_p);
|
|
|
|
if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
|
2013-07-14 03:19:45 +05:30
|
|
|
cnt = xatoull_sfx(count_p,
|
|
|
|
IF_FEATURE_SPLIT_FANCY(split_suffixes)
|
|
|
|
IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes)
|
|
|
|
);
|
2007-03-27 01:45:40 +05:30
|
|
|
sfx = "x";
|
2007-03-28 22:18:40 +05:30
|
|
|
|
|
|
|
argv += optind;
|
2007-03-27 01:45:40 +05:30
|
|
|
if (argv[0]) {
|
2009-11-26 10:13:16 +05:30
|
|
|
int fd;
|
2007-03-27 01:45:40 +05:30
|
|
|
if (argv[1])
|
|
|
|
sfx = argv[1];
|
2010-08-23 06:36:46 +05:30
|
|
|
fd = xopen_stdin(argv[0]);
|
2009-11-26 10:13:16 +05:30
|
|
|
xmove_fd(fd, STDIN_FILENO);
|
2007-03-27 01:45:40 +05:30
|
|
|
} else {
|
|
|
|
argv[0] = (char *) bb_msg_standard_input;
|
|
|
|
}
|
2007-03-26 19:58:12 +05:30
|
|
|
|
2007-03-27 01:45:40 +05:30
|
|
|
if (NAME_MAX < strlen(sfx) + suffix_len)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die("suffix too long");
|
2007-03-26 03:20:18 +05:30
|
|
|
|
|
|
|
{
|
2007-03-27 01:45:40 +05:30
|
|
|
char *char_p = xzalloc(suffix_len + 1);
|
2007-03-26 03:20:18 +05:30
|
|
|
memset(char_p, 'a', suffix_len);
|
2007-03-27 01:45:40 +05:30
|
|
|
pfx = xasprintf("%s%s", sfx, char_p);
|
2007-03-26 03:20:18 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
free(char_p);
|
|
|
|
}
|
2007-03-26 19:58:12 +05:30
|
|
|
|
2007-03-27 01:45:40 +05:30
|
|
|
while (1) {
|
2008-05-19 15:18:17 +05:30
|
|
|
bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
|
2007-03-27 01:45:40 +05:30
|
|
|
if (!bytes_read)
|
|
|
|
break;
|
|
|
|
if (bytes_read < 0)
|
2007-10-01 17:28:38 +05:30
|
|
|
bb_simple_perror_msg_and_die(argv[0]);
|
2007-03-27 01:45:40 +05:30
|
|
|
src = read_buffer;
|
2007-03-26 03:20:18 +05:30
|
|
|
do {
|
2007-03-27 01:45:40 +05:30
|
|
|
if (!remaining) {
|
|
|
|
if (!pfx)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die("suffixes exhausted");
|
2007-03-27 01:45:40 +05:30
|
|
|
xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
|
|
|
|
pfx = next_file(pfx, suffix_len);
|
|
|
|
remaining = cnt;
|
2007-03-26 19:58:12 +05:30
|
|
|
}
|
2007-03-27 01:45:40 +05:30
|
|
|
|
|
|
|
if (opt & SPLIT_OPT_b) {
|
|
|
|
/* split by bytes */
|
|
|
|
to_write = (bytes_read < remaining) ? bytes_read : remaining;
|
|
|
|
remaining -= to_write;
|
|
|
|
} else {
|
|
|
|
/* split by lines */
|
|
|
|
/* can be sped up by using _memrchr_
|
|
|
|
* and writing many lines at once... */
|
|
|
|
char *end = memchr(src, '\n', bytes_read);
|
|
|
|
if (end) {
|
|
|
|
--remaining;
|
|
|
|
to_write = end - src + 1;
|
|
|
|
} else {
|
|
|
|
to_write = bytes_read;
|
|
|
|
}
|
2007-03-26 19:58:12 +05:30
|
|
|
}
|
2007-03-27 01:45:40 +05:30
|
|
|
|
2008-05-19 15:18:17 +05:30
|
|
|
xwrite(STDOUT_FILENO, src, to_write);
|
2007-03-27 01:45:40 +05:30
|
|
|
bytes_read -= to_write;
|
|
|
|
src += to_write;
|
|
|
|
} while (bytes_read);
|
2007-03-26 03:20:18 +05:30
|
|
|
}
|
2007-11-16 18:09:16 +05:30
|
|
|
return EXIT_SUCCESS;
|
2007-03-26 03:20:18 +05:30
|
|
|
}
|