blob: 10035e9ab00b2890f099f866758042690c001739 [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
4 * Copyright (c) 2007 Bernhard Fischer
5 *
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 */
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 */
12#include "busybox.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
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000023 { NULL, 0 }
24};
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
52enum { READ_BUFFER_SIZE = sizeof(bb_common_bufsiz1) - 1 };
53
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
58int split_main(int argc, char **argv);
59int split_main(int argc, char **argv)
60{
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
71 opt_complementary = "?2";
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000072 opt = getopt32(argc, argv, "l:b:a:", &count_p, &count_p, &sfx);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000073
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000074 if (opt & SPLIT_OPT_l)
Denis Vlasenko91dd2752007-03-26 20:15:40 +000075 cnt = xatoul(count_p);
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000076 if (opt & SPLIT_OPT_b)
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000077 cnt = xatoul_sfx(count_p, split_suffices);
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000078 if (opt & SPLIT_OPT_a)
Denis Vlasenko91dd2752007-03-26 20:15:40 +000079 suffix_len = xatou(sfx);
Denis Vlasenko91dd2752007-03-26 20:15:40 +000080 sfx = "x";
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +000081
82 argv += optind;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000083 if (argv[0]) {
84 if (argv[1])
85 sfx = argv[1];
86 xmove_fd(xopen(argv[0], O_RDONLY), 0);
87 } else {
88 argv[0] = (char *) bb_msg_standard_input;
89 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000090
Denis Vlasenko91dd2752007-03-26 20:15:40 +000091 if (NAME_MAX < strlen(sfx) + suffix_len)
92 bb_error_msg_and_die("suffix too long");
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000093
94 {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000095 char *char_p = xzalloc(suffix_len + 1);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000096 memset(char_p, 'a', suffix_len);
Denis Vlasenko91dd2752007-03-26 20:15:40 +000097 pfx = xasprintf("%s%s", sfx, char_p);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000098 if (ENABLE_FEATURE_CLEAN_UP)
99 free(char_p);
100 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000101
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000102 while (1) {
103 bytes_read = safe_read(0, read_buffer, READ_BUFFER_SIZE);
104 if (!bytes_read)
105 break;
106 if (bytes_read < 0)
107 bb_perror_msg_and_die("%s", argv[0]);
108 src = read_buffer;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000109 do {
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000110 if (!remaining) {
111 if (!pfx)
Bernhard Reutner-Fischer0a537a02007-03-28 16:48:40 +0000112 bb_error_msg_and_die("suffixes exhausted");
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000113 xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
114 pfx = next_file(pfx, suffix_len);
115 remaining = cnt;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000116 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000117
118 if (opt & SPLIT_OPT_b) {
119 /* split by bytes */
120 to_write = (bytes_read < remaining) ? bytes_read : remaining;
121 remaining -= to_write;
122 } else {
123 /* split by lines */
124 /* can be sped up by using _memrchr_
125 * and writing many lines at once... */
126 char *end = memchr(src, '\n', bytes_read);
127 if (end) {
128 --remaining;
129 to_write = end - src + 1;
130 } else {
131 to_write = bytes_read;
132 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000133 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000134
135 xwrite(1, src, to_write);
136 bytes_read -= to_write;
137 src += to_write;
138 } while (bytes_read);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000139 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000140 return 0;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000141}