1 /* vi: set sw=4 ts=4: */
3 * split - split a file into pieces
4 * Copyright (c) 2007 Bernhard Reutner-Fischer
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 //config: bool "split (5.4 kb)"
12 //config: Split a file into pieces.
14 //config:config FEATURE_SPLIT_FANCY
15 //config: bool "Fancy extensions"
17 //config: depends on SPLIT
19 //config: Add support for features not required by SUSv3.
20 //config: Supports additional suffixes 'b' for 512 bytes,
21 //config: 'g' for 1GiB for the -b option.
23 //applet:IF_SPLIT(APPLET(split, BB_DIR_USR_BIN, BB_SUID_DROP))
25 //kbuild:lib-$(CONFIG_SPLIT) += split.o
27 /* BB_AUDIT: SUSv3 compliant
29 * http://www.opengroup.org/onlinepubs/009695399/utilities/split.html
32 //usage:#define split_trivial_usage
33 //usage: "[OPTIONS] [INPUT [PREFIX]]"
34 //usage:#define split_full_usage "\n\n"
35 //usage: " -b N[k|m] Split by N (kilo|mega)bytes"
36 //usage: "\n -l N Split by N lines"
37 //usage: "\n -a N Use N letters as suffix"
39 //usage:#define split_example_usage
40 //usage: "$ split TODO foo\n"
41 //usage: "$ cat TODO | split -a 2 -l 2 TODO_\n"
44 #include "common_bufsiz.h"
46 #if ENABLE_FEATURE_SPLIT_FANCY
47 static const struct suffix_mult split_suffixes[] = {
51 { "g", 1024*1024*1024 },
56 /* Increment the suffix part of the filename.
57 * Returns NULL if we are out of filenames.
59 static char *next_file(char *old, unsigned suffix_len)
61 size_t end = strlen(old);
81 #define read_buffer bb_common_bufsiz1
82 enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
84 #define SPLIT_OPT_l (1<<0)
85 #define SPLIT_OPT_b (1<<1)
86 #define SPLIT_OPT_a (1<<2)
88 int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
89 int split_main(int argc UNUSED_PARAM, char **argv)
91 unsigned suffix_len = 2;
98 ssize_t bytes_read, to_write;
101 setup_common_bufsiz();
103 opt = getopt32(argv, "^"
105 "\0" "?2"/*max 2 args*/,
106 &count_p, &count_p, &suffix_len
109 if (opt & SPLIT_OPT_l)
110 cnt = XATOOFF(count_p);
111 if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
112 cnt = xatoull_sfx(count_p,
113 IF_FEATURE_SPLIT_FANCY(split_suffixes)
114 IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes)
123 fd = xopen_stdin(argv[0]);
124 xmove_fd(fd, STDIN_FILENO);
126 argv[0] = (char *) bb_msg_standard_input;
129 if (NAME_MAX < strlen(sfx) + suffix_len)
130 bb_error_msg_and_die("suffix too long");
133 char *char_p = xzalloc(suffix_len + 1);
134 memset(char_p, 'a', suffix_len);
135 pfx = xasprintf("%s%s", sfx, char_p);
136 if (ENABLE_FEATURE_CLEAN_UP)
141 bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
145 bb_simple_perror_msg_and_die(argv[0]);
150 bb_error_msg_and_die("suffixes exhausted");
151 xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
152 pfx = next_file(pfx, suffix_len);
156 if (opt & SPLIT_OPT_b) {
158 to_write = (bytes_read < remaining) ? bytes_read : remaining;
159 remaining -= to_write;
162 /* can be sped up by using _memrchr_
163 * and writing many lines at once... */
164 char *end = memchr(src, '\n', bytes_read);
167 to_write = end - src + 1;
169 to_write = bytes_read;
173 xwrite(STDOUT_FILENO, src, to_write);
174 bytes_read -= to_write;
176 } while (bytes_read);