blob: 1e1673efbe2c4b9fa51611e13c25752488ad90c4 [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-Fischer0a537a02007-03-28 16:48:40 +000026#if ENABLE_FEATURE_SPLIT_FANCY
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +020027static const struct suffix_mult split_suffixes[] = {
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000028 { "b", 512 },
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000029 { "k", 1024 },
30 { "m", 1024*1024 },
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000031 { "g", 1024*1024*1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +020032 { "", 0 }
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000033};
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +020034#endif
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000035
36/* Increment the suffix part of the filename.
Denis Vlasenko91dd2752007-03-26 20:15:40 +000037 * Returns NULL if we are out of filenames.
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000038 */
Denis Vlasenko91dd2752007-03-26 20:15:40 +000039static char *next_file(char *old, unsigned suffix_len)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000040{
Denis Vlasenko91dd2752007-03-26 20:15:40 +000041 size_t end = strlen(old);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000042 unsigned i = 1;
43 char *curr;
44
Denys Vlasenko940c7202011-03-02 04:07:14 +010045 while (1) {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000046 curr = old + end - i;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000047 if (*curr < 'z') {
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000048 *curr += 1;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000049 break;
50 }
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000051 i++;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000052 if (i > suffix_len) {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000053 return NULL;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000054 }
55 *curr = 'a';
Denys Vlasenko940c7202011-03-02 04:07:14 +010056 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +000057
58 return old;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000059}
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000060
Denis Vlasenko91dd2752007-03-26 20:15:40 +000061#define read_buffer bb_common_bufsiz1
Denis Vlasenko74324c82007-06-04 10:16:52 +000062enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
Denis Vlasenko91dd2752007-03-26 20:15:40 +000063
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000064#define SPLIT_OPT_l (1<<0)
65#define SPLIT_OPT_b (1<<1)
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000066#define SPLIT_OPT_a (1<<2)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000067
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000068int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000069int split_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000070{
Denis Vlasenko91dd2752007-03-26 20:15:40 +000071 unsigned suffix_len = 2;
72 char *pfx;
73 char *count_p;
74 const char *sfx;
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000075 off_t cnt = 1000;
76 off_t remaining = 0;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000077 unsigned opt;
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000078 ssize_t bytes_read, to_write;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000079 char *src;
80
Denis Vlasenko1d426652008-03-17 09:09:09 +000081 opt_complementary = "?2:a+"; /* max 2 args; -a N */
82 opt = getopt32(argv, "l:b:a:", &count_p, &count_p, &suffix_len);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000083
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000084 if (opt & SPLIT_OPT_l)
Denis Vlasenko1d426652008-03-17 09:09:09 +000085 cnt = XATOOFF(count_p);
86 if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +020087 cnt = xatoull_sfx(count_p,
88 IF_FEATURE_SPLIT_FANCY(split_suffixes)
89 IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes)
90 );
Denis Vlasenko91dd2752007-03-26 20:15:40 +000091 sfx = "x";
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000092
93 argv += optind;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000094 if (argv[0]) {
Denys Vlasenko5b6fe342009-11-26 05:43:16 +010095 int fd;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000096 if (argv[1])
97 sfx = argv[1];
Denys Vlasenko7ff68362010-08-23 03:06:46 +020098 fd = xopen_stdin(argv[0]);
Denys Vlasenko5b6fe342009-11-26 05:43:16 +010099 xmove_fd(fd, STDIN_FILENO);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000100 } else {
101 argv[0] = (char *) bb_msg_standard_input;
102 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000103
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000104 if (NAME_MAX < strlen(sfx) + suffix_len)
105 bb_error_msg_and_die("suffix too long");
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000106
107 {
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000108 char *char_p = xzalloc(suffix_len + 1);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000109 memset(char_p, 'a', suffix_len);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000110 pfx = xasprintf("%s%s", sfx, char_p);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000111 if (ENABLE_FEATURE_CLEAN_UP)
112 free(char_p);
113 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000114
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000115 while (1) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000116 bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000117 if (!bytes_read)
118 break;
119 if (bytes_read < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000120 bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000121 src = read_buffer;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000122 do {
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000123 if (!remaining) {
124 if (!pfx)
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +0000125 bb_error_msg_and_die("suffixes exhausted");
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000126 xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
127 pfx = next_file(pfx, suffix_len);
128 remaining = cnt;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000129 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000130
131 if (opt & SPLIT_OPT_b) {
132 /* split by bytes */
133 to_write = (bytes_read < remaining) ? bytes_read : remaining;
134 remaining -= to_write;
135 } else {
136 /* split by lines */
137 /* can be sped up by using _memrchr_
138 * and writing many lines at once... */
139 char *end = memchr(src, '\n', bytes_read);
140 if (end) {
141 --remaining;
142 to_write = end - src + 1;
143 } else {
144 to_write = bytes_read;
145 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000146 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000147
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000148 xwrite(STDOUT_FILENO, src, to_write);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000149 bytes_read -= to_write;
150 src += to_write;
151 } while (bytes_read);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000152 }
Bernhard Reutner-Fischer7c2db5c2007-11-16 12:39:16 +0000153 return EXIT_SUCCESS;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000154}