2007-03-25 21:50:18 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* split - split a file into pieces
|
|
|
|
* Copyright (c) 2007 Bernhard Fischer
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
2007-03-26 14:28:12 +00:00
|
|
|
/* BB_AUDIT: SUSv3 compliant
|
2007-03-25 21:50:18 +00:00
|
|
|
* SUSv3 requirements:
|
|
|
|
* http://www.opengroup.org/onlinepubs/009695399/utilities/split.html
|
|
|
|
*/
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2007-03-26 20:15:40 +00:00
|
|
|
|
2007-03-26 10:46:31 +00:00
|
|
|
static const struct suffix_mult split_suffices[] = {
|
2007-03-28 16:48:40 +00:00
|
|
|
#if ENABLE_FEATURE_SPLIT_FANCY
|
2007-03-26 10:46:31 +00:00
|
|
|
{ "b", 512 },
|
2007-03-28 16:48:40 +00:00
|
|
|
#endif
|
2007-03-26 10:46:31 +00:00
|
|
|
{ "k", 1024 },
|
|
|
|
{ "m", 1024*1024 },
|
2007-03-28 16:48:40 +00:00
|
|
|
#if ENABLE_FEATURE_SPLIT_FANCY
|
2007-03-26 10:46:31 +00:00
|
|
|
{ "g", 1024*1024*1024 },
|
2007-03-28 16:48:40 +00:00
|
|
|
#endif
|
2007-07-27 15:06:25 +00:00
|
|
|
{ }
|
2007-03-26 10:46:31 +00:00
|
|
|
};
|
2007-03-25 21:50:18 +00:00
|
|
|
|
|
|
|
/* Increment the suffix part of the filename.
|
2007-03-26 20:15:40 +00:00
|
|
|
* Returns NULL if we are out of filenames.
|
2007-03-25 21:50:18 +00:00
|
|
|
*/
|
2007-03-26 20:15:40 +00:00
|
|
|
static char *next_file(char *old, unsigned suffix_len)
|
2007-03-25 21:50:18 +00:00
|
|
|
{
|
2007-03-26 20:15:40 +00:00
|
|
|
size_t end = strlen(old);
|
2007-03-25 21:50:18 +00:00
|
|
|
unsigned i = 1;
|
|
|
|
char *curr;
|
|
|
|
|
|
|
|
do {
|
2007-03-26 20:15:40 +00:00
|
|
|
curr = old + end - i;
|
2007-03-25 21:50:18 +00:00
|
|
|
if (*curr < 'z') {
|
2007-03-26 10:46:31 +00:00
|
|
|
*curr += 1;
|
2007-03-25 21:50:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
2007-03-26 14:28:12 +00:00
|
|
|
if (i > suffix_len) {
|
2007-03-26 20:15:40 +00:00
|
|
|
return NULL;
|
2007-03-26 14:28:12 +00:00
|
|
|
}
|
|
|
|
*curr = 'a';
|
2007-03-26 18:19:29 +00:00
|
|
|
} while (1);
|
2007-03-26 20:15:40 +00:00
|
|
|
|
|
|
|
return old;
|
2007-03-25 21:50:18 +00:00
|
|
|
}
|
2007-03-26 14:28:12 +00:00
|
|
|
|
2007-03-26 20:15:40 +00:00
|
|
|
#define read_buffer bb_common_bufsiz1
|
2007-06-04 10:16:52 +00:00
|
|
|
enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
|
2007-03-26 20:15:40 +00:00
|
|
|
|
2007-03-25 21:50:18 +00:00
|
|
|
#define SPLIT_OPT_l (1<<0)
|
|
|
|
#define SPLIT_OPT_b (1<<1)
|
2007-03-26 10:46:31 +00:00
|
|
|
#define SPLIT_OPT_a (1<<2)
|
2007-03-25 21:50:18 +00:00
|
|
|
|
|
|
|
int split_main(int argc, char **argv);
|
|
|
|
int split_main(int argc, char **argv)
|
|
|
|
{
|
2007-03-26 20:15:40 +00:00
|
|
|
unsigned suffix_len = 2;
|
|
|
|
char *pfx;
|
|
|
|
char *count_p;
|
|
|
|
const char *sfx;
|
2007-03-28 16:48:40 +00:00
|
|
|
off_t cnt = 1000;
|
|
|
|
off_t remaining = 0;
|
2007-03-26 20:15:40 +00:00
|
|
|
unsigned opt;
|
2007-03-28 16:48:40 +00:00
|
|
|
ssize_t bytes_read, to_write;
|
2007-03-26 20:15:40 +00:00
|
|
|
char *src;
|
|
|
|
|
|
|
|
opt_complementary = "?2";
|
2007-08-18 15:32:12 +00:00
|
|
|
opt = getopt32(argv, "l:b:a:", &count_p, &count_p, &sfx);
|
2007-03-25 21:50:18 +00:00
|
|
|
|
2007-03-26 18:19:29 +00:00
|
|
|
if (opt & SPLIT_OPT_l)
|
2007-03-26 20:15:40 +00:00
|
|
|
cnt = xatoul(count_p);
|
2007-03-26 18:19:29 +00:00
|
|
|
if (opt & SPLIT_OPT_b)
|
2007-03-26 14:28:12 +00:00
|
|
|
cnt = xatoul_sfx(count_p, split_suffices);
|
2007-03-26 18:19:29 +00:00
|
|
|
if (opt & SPLIT_OPT_a)
|
2007-03-26 20:15:40 +00:00
|
|
|
suffix_len = xatou(sfx);
|
|
|
|
sfx = "x";
|
2007-03-28 16:48:40 +00:00
|
|
|
|
|
|
|
argv += optind;
|
2007-03-26 20:15:40 +00:00
|
|
|
if (argv[0]) {
|
|
|
|
if (argv[1])
|
|
|
|
sfx = argv[1];
|
|
|
|
xmove_fd(xopen(argv[0], O_RDONLY), 0);
|
|
|
|
} else {
|
|
|
|
argv[0] = (char *) bb_msg_standard_input;
|
|
|
|
}
|
2007-03-26 14:28:12 +00:00
|
|
|
|
2007-03-26 20:15:40 +00:00
|
|
|
if (NAME_MAX < strlen(sfx) + suffix_len)
|
|
|
|
bb_error_msg_and_die("suffix too long");
|
2007-03-25 21:50:18 +00:00
|
|
|
|
|
|
|
{
|
2007-03-26 20:15:40 +00:00
|
|
|
char *char_p = xzalloc(suffix_len + 1);
|
2007-03-25 21:50:18 +00:00
|
|
|
memset(char_p, 'a', suffix_len);
|
2007-03-26 20:15:40 +00:00
|
|
|
pfx = xasprintf("%s%s", sfx, char_p);
|
2007-03-25 21:50:18 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
free(char_p);
|
|
|
|
}
|
2007-03-26 14:28:12 +00:00
|
|
|
|
2007-03-26 20:15:40 +00:00
|
|
|
while (1) {
|
|
|
|
bytes_read = safe_read(0, read_buffer, READ_BUFFER_SIZE);
|
|
|
|
if (!bytes_read)
|
|
|
|
break;
|
|
|
|
if (bytes_read < 0)
|
|
|
|
bb_perror_msg_and_die("%s", argv[0]);
|
|
|
|
src = read_buffer;
|
2007-03-25 21:50:18 +00:00
|
|
|
do {
|
2007-03-26 20:15:40 +00:00
|
|
|
if (!remaining) {
|
|
|
|
if (!pfx)
|
2007-03-28 16:48:40 +00:00
|
|
|
bb_error_msg_and_die("suffixes exhausted");
|
2007-03-26 20:15:40 +00:00
|
|
|
xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
|
|
|
|
pfx = next_file(pfx, suffix_len);
|
|
|
|
remaining = cnt;
|
2007-03-26 14:28:12 +00:00
|
|
|
}
|
2007-03-26 20:15:40 +00:00
|
|
|
|
|
|
|
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 14:28:12 +00:00
|
|
|
}
|
2007-03-26 20:15:40 +00:00
|
|
|
|
|
|
|
xwrite(1, src, to_write);
|
|
|
|
bytes_read -= to_write;
|
|
|
|
src += to_write;
|
|
|
|
} while (bytes_read);
|
2007-03-25 21:50:18 +00:00
|
|
|
}
|
2007-03-26 20:15:40 +00:00
|
|
|
return 0;
|
2007-03-25 21:50:18 +00:00
|
|
|
}
|