blob: 11e640442589eeafc0f1db9a09e13487b7018387 [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"
Denis Vlasenko91dd2752007-03-26 20:15:40 +000025
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000026static const struct suffix_mult split_suffices[] = {
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000027#if ENABLE_FEATURE_SPLIT_FANCY
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000028 { "b", 512 },
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000029#endif
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000030 { "k", 1024 },
31 { "m", 1024*1024 },
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000032#if ENABLE_FEATURE_SPLIT_FANCY
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000033 { "g", 1024*1024*1024 },
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000034#endif
Denys Vlasenko043b1e52009-09-06 12:47:55 +020035 { "", 0 }
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000036};
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000037
38/* Increment the suffix part of the filename.
Denis Vlasenko91dd2752007-03-26 20:15:40 +000039 * Returns NULL if we are out of filenames.
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000040 */
Denis Vlasenko91dd2752007-03-26 20:15:40 +000041static char *next_file(char *old, unsigned suffix_len)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000042{
Denis Vlasenko91dd2752007-03-26 20:15:40 +000043 size_t end = strlen(old);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000044 unsigned i = 1;
45 char *curr;
46
Denys Vlasenko940c7202011-03-02 04:07:14 +010047 while (1) {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000048 curr = old + end - i;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000049 if (*curr < 'z') {
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000050 *curr += 1;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000051 break;
52 }
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000053 i++;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000054 if (i > suffix_len) {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000055 return NULL;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000056 }
57 *curr = 'a';
Denys Vlasenko940c7202011-03-02 04:07:14 +010058 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +000059
60 return old;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000061}
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000062
Denis Vlasenko91dd2752007-03-26 20:15:40 +000063#define read_buffer bb_common_bufsiz1
Denis Vlasenko74324c82007-06-04 10:16:52 +000064enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
Denis Vlasenko91dd2752007-03-26 20:15:40 +000065
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000066#define SPLIT_OPT_l (1<<0)
67#define SPLIT_OPT_b (1<<1)
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000068#define SPLIT_OPT_a (1<<2)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000069
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000070int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000071int split_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000072{
Denis Vlasenko91dd2752007-03-26 20:15:40 +000073 unsigned suffix_len = 2;
74 char *pfx;
75 char *count_p;
76 const char *sfx;
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000077 off_t cnt = 1000;
78 off_t remaining = 0;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000079 unsigned opt;
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000080 ssize_t bytes_read, to_write;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000081 char *src;
82
Denis Vlasenko1d426652008-03-17 09:09:09 +000083 opt_complementary = "?2:a+"; /* max 2 args; -a N */
84 opt = getopt32(argv, "l:b:a:", &count_p, &count_p, &suffix_len);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000085
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000086 if (opt & SPLIT_OPT_l)
Denis Vlasenko1d426652008-03-17 09:09:09 +000087 cnt = XATOOFF(count_p);
88 if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
89 cnt = xatoull_sfx(count_p, split_suffices);
Denis Vlasenko91dd2752007-03-26 20:15:40 +000090 sfx = "x";
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000091
92 argv += optind;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000093 if (argv[0]) {
Denys Vlasenko5b6fe342009-11-26 05:43:16 +010094 int fd;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000095 if (argv[1])
96 sfx = argv[1];
Denys Vlasenko7ff68362010-08-23 03:06:46 +020097 fd = xopen_stdin(argv[0]);
Denys Vlasenko5b6fe342009-11-26 05:43:16 +010098 xmove_fd(fd, STDIN_FILENO);
Denis Vlasenko91dd2752007-03-26 20:15:40 +000099 } else {
100 argv[0] = (char *) bb_msg_standard_input;
101 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000102
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000103 if (NAME_MAX < strlen(sfx) + suffix_len)
104 bb_error_msg_and_die("suffix too long");
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000105
106 {
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000107 char *char_p = xzalloc(suffix_len + 1);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000108 memset(char_p, 'a', suffix_len);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000109 pfx = xasprintf("%s%s", sfx, char_p);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000110 if (ENABLE_FEATURE_CLEAN_UP)
111 free(char_p);
112 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000113
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000114 while (1) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000115 bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000116 if (!bytes_read)
117 break;
118 if (bytes_read < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000119 bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000120 src = read_buffer;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000121 do {
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000122 if (!remaining) {
123 if (!pfx)
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +0000124 bb_error_msg_and_die("suffixes exhausted");
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000125 xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
126 pfx = next_file(pfx, suffix_len);
127 remaining = cnt;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000128 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000129
130 if (opt & SPLIT_OPT_b) {
131 /* split by bytes */
132 to_write = (bytes_read < remaining) ? bytes_read : remaining;
133 remaining -= to_write;
134 } else {
135 /* split by lines */
136 /* can be sped up by using _memrchr_
137 * and writing many lines at once... */
138 char *end = memchr(src, '\n', bytes_read);
139 if (end) {
140 --remaining;
141 to_write = end - src + 1;
142 } else {
143 to_write = bytes_read;
144 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000145 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000146
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000147 xwrite(STDOUT_FILENO, src, to_write);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000148 bytes_read -= to_write;
149 src += to_write;
150 } while (bytes_read);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000151 }
Bernhard Reutner-Fischer7c2db5c2007-11-16 12:39:16 +0000152 return EXIT_SUCCESS;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000153}