summaryrefslogtreecommitdiff
path: root/coreutils/split.c (plain)
blob: 1e1673efbe2c4b9fa51611e13c25752488ad90c4
1/* vi: set sw=4 ts=4: */
2/*
3 * split - split a file into pieces
4 * Copyright (c) 2007 Bernhard Reutner-Fischer
5 *
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7 */
8/* BB_AUDIT: SUSv3 compliant
9 * SUSv3 requirements:
10 * http://www.opengroup.org/onlinepubs/009695399/utilities/split.html
11 */
12
13//usage:#define split_trivial_usage
14//usage: "[OPTIONS] [INPUT [PREFIX]]"
15//usage:#define split_full_usage "\n\n"
16//usage: " -b N[k|m] Split by N (kilo|mega)bytes"
17//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
24#include "libbb.h"
25
26#if ENABLE_FEATURE_SPLIT_FANCY
27static const struct suffix_mult split_suffixes[] = {
28 { "b", 512 },
29 { "k", 1024 },
30 { "m", 1024*1024 },
31 { "g", 1024*1024*1024 },
32 { "", 0 }
33};
34#endif
35
36/* Increment the suffix part of the filename.
37 * Returns NULL if we are out of filenames.
38 */
39static char *next_file(char *old, unsigned suffix_len)
40{
41 size_t end = strlen(old);
42 unsigned i = 1;
43 char *curr;
44
45 while (1) {
46 curr = old + end - i;
47 if (*curr < 'z') {
48 *curr += 1;
49 break;
50 }
51 i++;
52 if (i > suffix_len) {
53 return NULL;
54 }
55 *curr = 'a';
56 }
57
58 return old;
59}
60
61#define read_buffer bb_common_bufsiz1
62enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
63
64#define SPLIT_OPT_l (1<<0)
65#define SPLIT_OPT_b (1<<1)
66#define SPLIT_OPT_a (1<<2)
67
68int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
69int split_main(int argc UNUSED_PARAM, char **argv)
70{
71 unsigned suffix_len = 2;
72 char *pfx;
73 char *count_p;
74 const char *sfx;
75 off_t cnt = 1000;
76 off_t remaining = 0;
77 unsigned opt;
78 ssize_t bytes_read, to_write;
79 char *src;
80
81 opt_complementary = "?2:a+"; /* max 2 args; -a N */
82 opt = getopt32(argv, "l:b:a:", &count_p, &count_p, &suffix_len);
83
84 if (opt & SPLIT_OPT_l)
85 cnt = XATOOFF(count_p);
86 if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
87 cnt = xatoull_sfx(count_p,
88 IF_FEATURE_SPLIT_FANCY(split_suffixes)
89 IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes)
90 );
91 sfx = "x";
92
93 argv += optind;
94 if (argv[0]) {
95 int fd;
96 if (argv[1])
97 sfx = argv[1];
98 fd = xopen_stdin(argv[0]);
99 xmove_fd(fd, STDIN_FILENO);
100 } else {
101 argv[0] = (char *) bb_msg_standard_input;
102 }
103
104 if (NAME_MAX < strlen(sfx) + suffix_len)
105 bb_error_msg_and_die("suffix too long");
106
107 {
108 char *char_p = xzalloc(suffix_len + 1);
109 memset(char_p, 'a', suffix_len);
110 pfx = xasprintf("%s%s", sfx, char_p);
111 if (ENABLE_FEATURE_CLEAN_UP)
112 free(char_p);
113 }
114
115 while (1) {
116 bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
117 if (!bytes_read)
118 break;
119 if (bytes_read < 0)
120 bb_simple_perror_msg_and_die(argv[0]);
121 src = read_buffer;
122 do {
123 if (!remaining) {
124 if (!pfx)
125 bb_error_msg_and_die("suffixes exhausted");
126 xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
127 pfx = next_file(pfx, suffix_len);
128 remaining = cnt;
129 }
130
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 }
146 }
147
148 xwrite(STDOUT_FILENO, src, to_write);
149 bytes_read -= to_write;
150 src += to_write;
151 } while (bytes_read);
152 }
153 return EXIT_SUCCESS;
154}
155