blob: e401bd388c16def60d073721c0f9e48af5f86c58 [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 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Denis Vlasenko91dd2752007-03-26 20:15:40 +000013
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000014static const struct suffix_mult split_suffices[] = {
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000015#if ENABLE_FEATURE_SPLIT_FANCY
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000016 { "b", 512 },
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000017#endif
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000018 { "k", 1024 },
19 { "m", 1024*1024 },
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000020#if ENABLE_FEATURE_SPLIT_FANCY
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000021 { "g", 1024*1024*1024 },
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000022#endif
Denys Vlasenko043b1e52009-09-06 12:47:55 +020023 { "", 0 }
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000024};
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000025
26/* Increment the suffix part of the filename.
Denis Vlasenko91dd2752007-03-26 20:15:40 +000027 * Returns NULL if we are out of filenames.
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000028 */
Denis Vlasenko91dd2752007-03-26 20:15:40 +000029static char *next_file(char *old, unsigned suffix_len)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000030{
Denis Vlasenko91dd2752007-03-26 20:15:40 +000031 size_t end = strlen(old);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000032 unsigned i = 1;
33 char *curr;
34
35 do {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000036 curr = old + end - i;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000037 if (*curr < 'z') {
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000038 *curr += 1;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000039 break;
40 }
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000041 i++;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000042 if (i > suffix_len) {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000043 return NULL;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000044 }
45 *curr = 'a';
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000046 } while (1);
Denis Vlasenko91dd2752007-03-26 20:15:40 +000047
48 return old;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000049}
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000050
Denis Vlasenko91dd2752007-03-26 20:15:40 +000051#define read_buffer bb_common_bufsiz1
Denis Vlasenko74324c82007-06-04 10:16:52 +000052enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
Denis Vlasenko91dd2752007-03-26 20:15:40 +000053
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000054#define SPLIT_OPT_l (1<<0)
55#define SPLIT_OPT_b (1<<1)
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000056#define SPLIT_OPT_a (1<<2)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000057
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000058int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000059int split_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000060{
Denis Vlasenko91dd2752007-03-26 20:15:40 +000061 unsigned suffix_len = 2;
62 char *pfx;
63 char *count_p;
64 const char *sfx;
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000065 off_t cnt = 1000;
66 off_t remaining = 0;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000067 unsigned opt;
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000068 ssize_t bytes_read, to_write;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000069 char *src;
70
Denis Vlasenko1d426652008-03-17 09:09:09 +000071 opt_complementary = "?2:a+"; /* max 2 args; -a N */
72 opt = getopt32(argv, "l:b:a:", &count_p, &count_p, &suffix_len);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000073
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000074 if (opt & SPLIT_OPT_l)
Denis Vlasenko1d426652008-03-17 09:09:09 +000075 cnt = XATOOFF(count_p);
76 if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
77 cnt = xatoull_sfx(count_p, split_suffices);
Denis Vlasenko91dd2752007-03-26 20:15:40 +000078 sfx = "x";
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000079
80 argv += optind;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000081 if (argv[0]) {
Denys Vlasenko5b6fe342009-11-26 05:43:16 +010082 int fd;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000083 if (argv[1])
84 sfx = argv[1];
Denys Vlasenko5b6fe342009-11-26 05:43:16 +010085 fd = open_or_warn_stdin(argv[0]);
86 if (fd == -1)
87 return EXIT_FAILURE;
88 xmove_fd(fd, STDIN_FILENO);
Denis Vlasenko91dd2752007-03-26 20:15:40 +000089 } else {
90 argv[0] = (char *) bb_msg_standard_input;
91 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000092
Denis Vlasenko91dd2752007-03-26 20:15:40 +000093 if (NAME_MAX < strlen(sfx) + suffix_len)
94 bb_error_msg_and_die("suffix too long");
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000095
96 {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000097 char *char_p = xzalloc(suffix_len + 1);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000098 memset(char_p, 'a', suffix_len);
Denis Vlasenko91dd2752007-03-26 20:15:40 +000099 pfx = xasprintf("%s%s", sfx, char_p);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000100 if (ENABLE_FEATURE_CLEAN_UP)
101 free(char_p);
102 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000103
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000104 while (1) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000105 bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000106 if (!bytes_read)
107 break;
108 if (bytes_read < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000109 bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000110 src = read_buffer;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000111 do {
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000112 if (!remaining) {
113 if (!pfx)
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +0000114 bb_error_msg_and_die("suffixes exhausted");
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000115 xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
116 pfx = next_file(pfx, suffix_len);
117 remaining = cnt;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000118 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000119
120 if (opt & SPLIT_OPT_b) {
121 /* split by bytes */
122 to_write = (bytes_read < remaining) ? bytes_read : remaining;
123 remaining -= to_write;
124 } else {
125 /* split by lines */
126 /* can be sped up by using _memrchr_
127 * and writing many lines at once... */
128 char *end = memchr(src, '\n', bytes_read);
129 if (end) {
130 --remaining;
131 to_write = end - src + 1;
132 } else {
133 to_write = bytes_read;
134 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000135 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000136
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000137 xwrite(STDOUT_FILENO, src, to_write);
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000138 bytes_read -= to_write;
139 src += to_write;
140 } while (bytes_read);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000141 }
Bernhard Reutner-Fischer7c2db5c2007-11-16 12:39:16 +0000142 return EXIT_SUCCESS;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000143}