blob: 19d58a21b13e6e11051059d171c8b932fcb748b6 [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 */
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +00008/* BB_AUDIT: SUSv3 compliant
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +00009 * SUSv3 requirements:
10 * http://www.opengroup.org/onlinepubs/009695399/utilities/split.html
11 */
Pere Orga34425382011-03-31 14:43:25 +020012
13//usage:#define split_trivial_usage
14//usage: "[OPTIONS] [INPUT [PREFIX]]"
15//usage:#define split_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020016//usage: " -b N[k|m] Split by N (kilo|mega)bytes"
Pere Orga34425382011-03-31 14:43:25 +020017//usage: "\n -l N Split by N lines"
18//usage: "\n -a N Use N letters as suffix"
19//usage:
20//usage:#define split_example_usage
21//usage: "$ split TODO foo\n"
22//usage: "$ cat TODO | split -a 2 -l 2 TODO_\n"
23
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000024#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020025#include "common_bufsiz.h"
Denis Vlasenko91dd2752007-03-26 20:15:40 +000026
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000027#if ENABLE_FEATURE_SPLIT_FANCY
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +020028static const struct suffix_mult split_suffixes[] = {
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000029 { "b", 512 },
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000030 { "k", 1024 },
31 { "m", 1024*1024 },
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000032 { "g", 1024*1024*1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +020033 { "", 0 }
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000034};
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +020035#endif
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000036
37/* Increment the suffix part of the filename.
Denis Vlasenko91dd2752007-03-26 20:15:40 +000038 * Returns NULL if we are out of filenames.
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000039 */
Denis Vlasenko91dd2752007-03-26 20:15:40 +000040static char *next_file(char *old, unsigned suffix_len)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000041{
Denis Vlasenko91dd2752007-03-26 20:15:40 +000042 size_t end = strlen(old);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000043 unsigned i = 1;
44 char *curr;
45
Denys Vlasenko940c7202011-03-02 04:07:14 +010046 while (1) {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000047 curr = old + end - i;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000048 if (*curr < 'z') {
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000049 *curr += 1;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000050 break;
51 }
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000052 i++;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000053 if (i > suffix_len) {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000054 return NULL;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000055 }
56 *curr = 'a';
Denys Vlasenko940c7202011-03-02 04:07:14 +010057 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +000058
59 return old;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000060}
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000061
Denis Vlasenko91dd2752007-03-26 20:15:40 +000062#define read_buffer bb_common_bufsiz1
Denis Vlasenko74324c82007-06-04 10:16:52 +000063enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
Denis Vlasenko91dd2752007-03-26 20:15:40 +000064
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000065#define SPLIT_OPT_l (1<<0)
66#define SPLIT_OPT_b (1<<1)
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000067#define SPLIT_OPT_a (1<<2)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000068
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000069int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000070int split_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000071{
Denis Vlasenko91dd2752007-03-26 20:15:40 +000072 unsigned suffix_len = 2;
73 char *pfx;
74 char *count_p;
75 const char *sfx;
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000076 off_t cnt = 1000;
77 off_t remaining = 0;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000078 unsigned opt;
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000079 ssize_t bytes_read, to_write;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000080 char *src;
81
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020082 setup_common_bufsiz();
83
Denys Vlasenko237bedd2016-07-06 21:58:02 +020084 opt_complementary = "?2"; /* max 2 args; -a N */
85 opt = getopt32(argv, "l:b:a:+", &count_p, &count_p, &suffix_len);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000086
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000087 if (opt & SPLIT_OPT_l)
Denis Vlasenko1d426652008-03-17 09:09:09 +000088 cnt = XATOOFF(count_p);
89 if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +020090 cnt = xatoull_sfx(count_p,
91 IF_FEATURE_SPLIT_FANCY(split_suffixes)
92 IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes)
93 );
Denis Vlasenko91dd2752007-03-26 20:15:40 +000094 sfx = "x";
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000095
96 argv += optind;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000097 if (argv[0]) {
Denys Vlasenko5b6fe342009-11-26 05:43:16 +010098 int fd;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000099 if (argv[1])
100 sfx = argv[1];
Denys Vlasenko7ff68362010-08-23 03:06:46 +0200101 fd = xopen_stdin(argv[0]);
Denys Vlasenko5b6fe342009-11-26 05:43:16 +0100102 xmove_fd(fd, STDIN_FILENO);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000103 } else {
104 argv[0] = (char *) bb_msg_standard_input;
105 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000106
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000107 if (NAME_MAX < strlen(sfx) + suffix_len)
108 bb_error_msg_and_die("suffix too long");
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000109
110 {
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000111 char *char_p = xzalloc(suffix_len + 1);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000112 memset(char_p, 'a', suffix_len);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000113 pfx = xasprintf("%s%s", sfx, char_p);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000114 if (ENABLE_FEATURE_CLEAN_UP)
115 free(char_p);
116 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000117
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000118 while (1) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000119 bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000120 if (!bytes_read)
121 break;
122 if (bytes_read < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000123 bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000124 src = read_buffer;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000125 do {
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000126 if (!remaining) {
127 if (!pfx)
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +0000128 bb_error_msg_and_die("suffixes exhausted");
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000129 xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
130 pfx = next_file(pfx, suffix_len);
131 remaining = cnt;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000132 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000133
134 if (opt & SPLIT_OPT_b) {
135 /* split by bytes */
136 to_write = (bytes_read < remaining) ? bytes_read : remaining;
137 remaining -= to_write;
138 } else {
139 /* split by lines */
140 /* can be sped up by using _memrchr_
141 * and writing many lines at once... */
142 char *end = memchr(src, '\n', bytes_read);
143 if (end) {
144 --remaining;
145 to_write = end - src + 1;
146 } else {
147 to_write = bytes_read;
148 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000149 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000150
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000151 xwrite(STDOUT_FILENO, src, to_write);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000152 bytes_read -= to_write;
153 src += to_write;
154 } while (bytes_read);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000155 }
Bernhard Reutner-Fischer7c2db5c2007-11-16 12:39:16 +0000156 return EXIT_SUCCESS;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000157}