blob: 07eb97e0cc1c1d223b26de48e0487865e4e238b9 [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-Fischercd43dfd2007-03-26 10:46:31 +000015 { "b", 512 },
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000016 { "k", 1024 },
17 { "m", 1024*1024 },
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000018 { "g", 1024*1024*1024 },
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000019 { NULL, 0 }
20};
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000021
22/* Increment the suffix part of the filename.
Denis Vlasenko91dd2752007-03-26 20:15:40 +000023 * Returns NULL if we are out of filenames.
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000024 */
Denis Vlasenko91dd2752007-03-26 20:15:40 +000025static char *next_file(char *old, unsigned suffix_len)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000026{
Denis Vlasenko91dd2752007-03-26 20:15:40 +000027 size_t end = strlen(old);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000028 unsigned i = 1;
29 char *curr;
30
31 do {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000032 curr = old + end - i;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000033 if (*curr < 'z') {
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000034 *curr += 1;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000035 break;
36 }
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000037 i++;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000038 if (i > suffix_len) {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000039 return NULL;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000040 }
41 *curr = 'a';
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000042 } while (1);
Denis Vlasenko91dd2752007-03-26 20:15:40 +000043
44 return old;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000045}
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000046
Denis Vlasenko91dd2752007-03-26 20:15:40 +000047#define read_buffer bb_common_bufsiz1
48enum { READ_BUFFER_SIZE = sizeof(bb_common_bufsiz1) - 1 };
49
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000050#define SPLIT_OPT_l (1<<0)
51#define SPLIT_OPT_b (1<<1)
Bernhard Reutner-Fischercd43dfd2007-03-26 10:46:31 +000052#define SPLIT_OPT_a (1<<2)
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000053
54int split_main(int argc, char **argv);
55int split_main(int argc, char **argv)
56{
Denis Vlasenko91dd2752007-03-26 20:15:40 +000057 unsigned suffix_len = 2;
58 char *pfx;
59 char *count_p;
60 const char *sfx;
61 unsigned long cnt = 1000;
62 unsigned long remaining = 0;
63 unsigned opt;
64 int bytes_read, to_write;
65 char *src;
66
67 opt_complementary = "?2";
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000068 opt = getopt32(argc, argv, "l:b:a:", &count_p, &count_p, &sfx);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000069
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000070 if (opt & SPLIT_OPT_l)
Denis Vlasenko91dd2752007-03-26 20:15:40 +000071 cnt = xatoul(count_p);
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000072 if (opt & SPLIT_OPT_b)
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000073 cnt = xatoul_sfx(count_p, split_suffices);
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000074 if (opt & SPLIT_OPT_a)
Denis Vlasenko91dd2752007-03-26 20:15:40 +000075 suffix_len = xatou(sfx);
Bernhard Reutner-Fischer576de342007-03-26 18:19:29 +000076 argv += optind;
Denis Vlasenko91dd2752007-03-26 20:15:40 +000077 sfx = "x";
78 if (argv[0]) {
79 if (argv[1])
80 sfx = argv[1];
81 xmove_fd(xopen(argv[0], O_RDONLY), 0);
82 } else {
83 argv[0] = (char *) bb_msg_standard_input;
84 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000085
Denis Vlasenko91dd2752007-03-26 20:15:40 +000086 if (NAME_MAX < strlen(sfx) + suffix_len)
87 bb_error_msg_and_die("suffix too long");
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000088
89 {
Denis Vlasenko91dd2752007-03-26 20:15:40 +000090 char *char_p = xzalloc(suffix_len + 1);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000091 memset(char_p, 'a', suffix_len);
Denis Vlasenko91dd2752007-03-26 20:15:40 +000092 pfx = xasprintf("%s%s", sfx, char_p);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +000093 if (ENABLE_FEATURE_CLEAN_UP)
94 free(char_p);
95 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +000096
Denis Vlasenko91dd2752007-03-26 20:15:40 +000097 while (1) {
98 bytes_read = safe_read(0, read_buffer, READ_BUFFER_SIZE);
99 if (!bytes_read)
100 break;
101 if (bytes_read < 0)
102 bb_perror_msg_and_die("%s", argv[0]);
103 src = read_buffer;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000104 do {
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000105 if (!remaining) {
106 if (!pfx)
107 bb_error_msg_and_die("suffices exhausted");
108 xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
109 pfx = next_file(pfx, suffix_len);
110 remaining = cnt;
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000111 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000112
113 if (opt & SPLIT_OPT_b) {
114 /* split by bytes */
115 to_write = (bytes_read < remaining) ? bytes_read : remaining;
116 remaining -= to_write;
117 } else {
118 /* split by lines */
119 /* can be sped up by using _memrchr_
120 * and writing many lines at once... */
121 char *end = memchr(src, '\n', bytes_read);
122 if (end) {
123 --remaining;
124 to_write = end - src + 1;
125 } else {
126 to_write = bytes_read;
127 }
Bernhard Reutner-Fischerb813eaf2007-03-26 14:28:12 +0000128 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000129
130 xwrite(1, src, to_write);
131 bytes_read -= to_write;
132 src += to_write;
133 } while (bytes_read);
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000134 }
Denis Vlasenko91dd2752007-03-26 20:15:40 +0000135 return 0;
Bernhard Reutner-Fischer10f8f5f2007-03-25 21:50:18 +0000136}