1 /* vi: set sw=4 ts=4: */
2 /*
3  * split - split a file into pieces
4  * Copyright (c) 2007 Bernhard Reutner-Fischer
5  *
6  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7  */
8 //config:config SPLIT
9 //config:	bool "split"
10 //config:	default y
11 //config:	help
12 //config:	  split a file into pieces.
13 //config:
14 //config:config FEATURE_SPLIT_FANCY
15 //config:	bool "Fancy extensions"
16 //config:	default y
17 //config:	depends on SPLIT
18 //config:	help
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.
22 
23 //applet:IF_SPLIT(APPLET(split, BB_DIR_USR_BIN, BB_SUID_DROP))
24 
25 //kbuild:lib-$(CONFIG_SPLIT) += split.o
26 
27 /* BB_AUDIT: SUSv3 compliant
28  * SUSv3 requirements:
29  * http://www.opengroup.org/onlinepubs/009695399/utilities/split.html
30  */
31 
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"
38 //usage:
39 //usage:#define split_example_usage
40 //usage:       "$ split TODO foo\n"
41 //usage:       "$ cat TODO | split -a 2 -l 2 TODO_\n"
42 
43 #include "libbb.h"
44 #include "common_bufsiz.h"
45 
46 #if ENABLE_FEATURE_SPLIT_FANCY
47 static const struct suffix_mult split_suffixes[] = {
48 	{ "b", 512 },
49 	{ "k", 1024 },
50 	{ "m", 1024*1024 },
51 	{ "g", 1024*1024*1024 },
52 	{ "", 0 }
53 };
54 #endif
55 
56 /* Increment the suffix part of the filename.
57  * Returns NULL if we are out of filenames.
58  */
next_file(char * old,unsigned suffix_len)59 static char *next_file(char *old, unsigned suffix_len)
60 {
61 	size_t end = strlen(old);
62 	unsigned i = 1;
63 	char *curr;
64 
65 	while (1) {
66 		curr = old + end - i;
67 		if (*curr < 'z') {
68 			*curr += 1;
69 			break;
70 		}
71 		i++;
72 		if (i > suffix_len) {
73 			return NULL;
74 		}
75 		*curr = 'a';
76 	}
77 
78 	return old;
79 }
80 
81 #define read_buffer bb_common_bufsiz1
82 enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
83 
84 #define SPLIT_OPT_l (1<<0)
85 #define SPLIT_OPT_b (1<<1)
86 #define SPLIT_OPT_a (1<<2)
87 
88 int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
split_main(int argc UNUSED_PARAM,char ** argv)89 int split_main(int argc UNUSED_PARAM, char **argv)
90 {
91 	unsigned suffix_len = 2;
92 	char *pfx;
93 	char *count_p;
94 	const char *sfx;
95 	off_t cnt = 1000;
96 	off_t remaining = 0;
97 	unsigned opt;
98 	ssize_t bytes_read, to_write;
99 	char *src;
100 
101 	setup_common_bufsiz();
102 
103 	opt_complementary = "?2"; /* max 2 args; -a N */
104 	opt = getopt32(argv, "l:b:a:+", &count_p, &count_p, &suffix_len);
105 
106 	if (opt & SPLIT_OPT_l)
107 		cnt = XATOOFF(count_p);
108 	if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
109 		cnt = xatoull_sfx(count_p,
110 				IF_FEATURE_SPLIT_FANCY(split_suffixes)
111 				IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes)
112 		);
113 	sfx = "x";
114 
115 	argv += optind;
116 	if (argv[0]) {
117 		int fd;
118 		if (argv[1])
119 			sfx = argv[1];
120 		fd = xopen_stdin(argv[0]);
121 		xmove_fd(fd, STDIN_FILENO);
122 	} else {
123 		argv[0] = (char *) bb_msg_standard_input;
124 	}
125 
126 	if (NAME_MAX < strlen(sfx) + suffix_len)
127 		bb_error_msg_and_die("suffix too long");
128 
129 	{
130 		char *char_p = xzalloc(suffix_len + 1);
131 		memset(char_p, 'a', suffix_len);
132 		pfx = xasprintf("%s%s", sfx, char_p);
133 		if (ENABLE_FEATURE_CLEAN_UP)
134 			free(char_p);
135 	}
136 
137 	while (1) {
138 		bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
139 		if (!bytes_read)
140 			break;
141 		if (bytes_read < 0)
142 			bb_simple_perror_msg_and_die(argv[0]);
143 		src = read_buffer;
144 		do {
145 			if (!remaining) {
146 				if (!pfx)
147 					bb_error_msg_and_die("suffixes exhausted");
148 				xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
149 				pfx = next_file(pfx, suffix_len);
150 				remaining = cnt;
151 			}
152 
153 			if (opt & SPLIT_OPT_b) {
154 				/* split by bytes */
155 				to_write = (bytes_read < remaining) ? bytes_read : remaining;
156 				remaining -= to_write;
157 			} else {
158 				/* split by lines */
159 				/* can be sped up by using _memrchr_
160 				 * and writing many lines at once... */
161 				char *end = memchr(src, '\n', bytes_read);
162 				if (end) {
163 					--remaining;
164 					to_write = end - src + 1;
165 				} else {
166 					to_write = bytes_read;
167 				}
168 			}
169 
170 			xwrite(STDOUT_FILENO, src, to_write);
171 			bytes_read -= to_write;
172 			src += to_write;
173 		} while (bytes_read);
174 	}
175 	return EXIT_SUCCESS;
176 }
177