blob: 3fcfd95f234e7d1c9dbeba8d417b7e4822ffcbd8 [file] [log] [blame]
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +00001/* vi: set sw=4 ts=4: */
2/*
3 * split - split a file into pieces
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00004 * Copyright (c) 2007 Bernhard Reutner-Fischer
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +00005 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +00007 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01008//config:config SPLIT
Denys Vlasenkob097a842018-12-28 03:20:17 +01009//config: bool "split (5 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010010//config: default y
11//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020012//config: Split a file into pieces.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010013//config:
14//config:config FEATURE_SPLIT_FANCY
15//config: bool "Fancy extensions"
16//config: default y
17//config: depends on SPLIT
18//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020019//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.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010022
23//applet:IF_SPLIT(APPLET(split, BB_DIR_USR_BIN, BB_SUID_DROP))
24
25//kbuild:lib-$(CONFIG_SPLIT) += split.o
26
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000027/* BB_AUDIT: SUSv3 compliant
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000028 * SUSv3 requirements:
29 * http://www.opengroup.org/onlinepubs/009695399/utilities/split.html
30 */
Pere Orga34425382011-03-31 14:43:25 +020031
32//usage:#define split_trivial_usage
33//usage: "[OPTIONS] [INPUT [PREFIX]]"
34//usage:#define split_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020035//usage: " -b N[k|m] Split by N (kilo|mega)bytes"
Pere Orga34425382011-03-31 14:43:25 +020036//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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000043#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020044#include "common_bufsiz.h"
Denis Vlasenko91dd2752007-03-26 20:15:40 +000045
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000046#if ENABLE_FEATURE_SPLIT_FANCY
Denys Vlasenko965b7952020-11-30 13:03:03 +010047static const struct suffix_mult split_suffixes[] ALIGN_SUFFIX = {
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000048 { "b", 512 },
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000049 { "k", 1024 },
50 { "m", 1024*1024 },
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000051 { "g", 1024*1024*1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +020052 { "", 0 }
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000053};
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +020054#endif
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000055
56/* Increment the suffix part of the filename.
Denis Vlasenko91dd2752007-03-26 20:15:40 +000057 * Returns NULL if we are out of filenames.
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000058 */
Denis Vlasenko91dd2752007-03-26 20:15:40 +000059static char *next_file(char *old, unsigned suffix_len)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000060{
Denis Vlasenko91dd2752007-03-26 20:15:40 +000061 size_t end = strlen(old);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000062 unsigned i = 1;
63 char *curr;
64
Denys Vlasenko940c7202011-03-02 04:07:14 +010065 while (1) {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000066 curr = old + end - i;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000067 if (*curr < 'z') {
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000068 *curr += 1;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000069 break;
70 }
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000071 i++;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000072 if (i > suffix_len) {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000073 return NULL;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000074 }
75 *curr = 'a';
Denys Vlasenko940c7202011-03-02 04:07:14 +010076 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +000077
78 return old;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000079}
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000080
Denis Vlasenko91dd2752007-03-26 20:15:40 +000081#define read_buffer bb_common_bufsiz1
Denis Vlasenko74324c82007-06-04 10:16:52 +000082enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
Denis Vlasenko91dd2752007-03-26 20:15:40 +000083
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000084#define SPLIT_OPT_l (1<<0)
85#define SPLIT_OPT_b (1<<1)
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000086#define SPLIT_OPT_a (1<<2)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000087
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000088int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000089int split_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000090{
Denis Vlasenko91dd2752007-03-26 20:15:40 +000091 unsigned suffix_len = 2;
92 char *pfx;
93 char *count_p;
94 const char *sfx;
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000095 off_t cnt = 1000;
96 off_t remaining = 0;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000097 unsigned opt;
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000098 ssize_t bytes_read, to_write;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000099 char *src;
100
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200101 setup_common_bufsiz();
102
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200103 opt = getopt32(argv, "^"
104 "l:b:a:+" /* -a N */
105 "\0" "?2"/*max 2 args*/,
106 &count_p, &count_p, &suffix_len
107 );
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000108
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +0000109 if (opt & SPLIT_OPT_l)
Denis Vlasenko1d426652008-03-17 09:09:09 +0000110 cnt = XATOOFF(count_p);
111 if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +0200112 cnt = xatoull_sfx(count_p,
113 IF_FEATURE_SPLIT_FANCY(split_suffixes)
114 IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes)
115 );
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000116 sfx = "x";
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +0000117
118 argv += optind;
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000119 if (argv[0]) {
Denys Vlasenko5b6fe342009-11-26 05:43:16 +0100120 int fd;
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000121 if (argv[1])
122 sfx = argv[1];
Denys Vlasenko7ff68362010-08-23 03:06:46 +0200123 fd = xopen_stdin(argv[0]);
Denys Vlasenko5b6fe342009-11-26 05:43:16 +0100124 xmove_fd(fd, STDIN_FILENO);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000125 } else {
126 argv[0] = (char *) bb_msg_standard_input;
127 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000128
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000129 if (NAME_MAX < strlen(sfx) + suffix_len)
James Byrne69374872019-07-02 11:35:03 +0200130 bb_simple_error_msg_and_die("suffix too long");
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000131
132 {
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000133 char *char_p = xzalloc(suffix_len + 1);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000134 memset(char_p, 'a', suffix_len);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000135 pfx = xasprintf("%s%s", sfx, char_p);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000136 if (ENABLE_FEATURE_CLEAN_UP)
137 free(char_p);
138 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000139
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000140 while (1) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000141 bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000142 if (!bytes_read)
143 break;
144 if (bytes_read < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000145 bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000146 src = read_buffer;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000147 do {
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000148 if (!remaining) {
149 if (!pfx)
James Byrne69374872019-07-02 11:35:03 +0200150 bb_simple_error_msg_and_die("suffixes exhausted");
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000151 xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
152 pfx = next_file(pfx, suffix_len);
153 remaining = cnt;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000154 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000155
156 if (opt & SPLIT_OPT_b) {
157 /* split by bytes */
158 to_write = (bytes_read < remaining) ? bytes_read : remaining;
159 remaining -= to_write;
160 } else {
161 /* split by lines */
162 /* can be sped up by using _memrchr_
163 * and writing many lines at once... */
164 char *end = memchr(src, '\n', bytes_read);
165 if (end) {
166 --remaining;
167 to_write = end - src + 1;
168 } else {
169 to_write = bytes_read;
170 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000171 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000172
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000173 xwrite(STDOUT_FILENO, src, to_write);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000174 bytes_read -= to_write;
175 src += to_write;
176 } while (bytes_read);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000177 }
Bernhard Reutner-Fischer7c2db5c2007-11-16 12:39:16 +0000178 return EXIT_SUCCESS;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000179}