Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * split - split a file into pieces |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 4 | * Copyright (c) 2007 Bernhard Reutner-Fischer |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 7 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 8 | //config:config SPLIT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 9 | //config: bool "split (5 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 10 | //config: default y |
| 11 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 12 | //config: Split a file into pieces. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 13 | //config: |
| 14 | //config:config FEATURE_SPLIT_FANCY |
| 15 | //config: bool "Fancy extensions" |
| 16 | //config: default y |
| 17 | //config: depends on SPLIT |
| 18 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 19 | //config: Add support for features not required by SUSv3. |
| 20 | //config: Supports additional suffixes 'b' for 512 bytes, |
| 21 | //config: 'g' for 1GiB for the -b option. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 22 | |
| 23 | //applet:IF_SPLIT(APPLET(split, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 24 | |
| 25 | //kbuild:lib-$(CONFIG_SPLIT) += split.o |
| 26 | |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 27 | /* BB_AUDIT: SUSv3 compliant |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 28 | * SUSv3 requirements: |
| 29 | * http://www.opengroup.org/onlinepubs/009695399/utilities/split.html |
| 30 | */ |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 31 | |
| 32 | //usage:#define split_trivial_usage |
| 33 | //usage: "[OPTIONS] [INPUT [PREFIX]]" |
| 34 | //usage:#define split_full_usage "\n\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 35 | //usage: " -b N[k|m] Split by N (kilo|mega)bytes" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 36 | //usage: "\n -l N Split by N lines" |
| 37 | //usage: "\n -a N Use N letters as suffix" |
| 38 | //usage: |
| 39 | //usage:#define split_example_usage |
| 40 | //usage: "$ split TODO foo\n" |
| 41 | //usage: "$ cat TODO | split -a 2 -l 2 TODO_\n" |
| 42 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 43 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 44 | #include "common_bufsiz.h" |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 45 | |
Bernhard Reutner-Fischer | 0a537a0 | 2007-03-28 16:48:40 +0000 | [diff] [blame] | 46 | #if ENABLE_FEATURE_SPLIT_FANCY |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 47 | static const struct suffix_mult split_suffixes[] ALIGN_SUFFIX = { |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 48 | { "b", 512 }, |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 49 | { "k", 1024 }, |
| 50 | { "m", 1024*1024 }, |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 51 | { "g", 1024*1024*1024 }, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 52 | { "", 0 } |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 53 | }; |
Denys Vlasenko | c72b43c | 2013-07-13 23:49:45 +0200 | [diff] [blame] | 54 | #endif |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 55 | |
| 56 | /* Increment the suffix part of the filename. |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 57 | * Returns NULL if we are out of filenames. |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 58 | */ |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 59 | static char *next_file(char *old, unsigned suffix_len) |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 60 | { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 61 | size_t end = strlen(old); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 62 | unsigned i = 1; |
| 63 | char *curr; |
| 64 | |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 65 | while (1) { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 66 | curr = old + end - i; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 67 | if (*curr < 'z') { |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 68 | *curr += 1; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 69 | break; |
| 70 | } |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 71 | i++; |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 72 | if (i > suffix_len) { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 73 | return NULL; |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 74 | } |
| 75 | *curr = 'a'; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 76 | } |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 77 | |
| 78 | return old; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 79 | } |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 81 | #define read_buffer bb_common_bufsiz1 |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 82 | enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 }; |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 83 | |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 84 | #define SPLIT_OPT_l (1<<0) |
| 85 | #define SPLIT_OPT_b (1<<1) |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 86 | #define SPLIT_OPT_a (1<<2) |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 87 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 88 | int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 89 | int split_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 90 | { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 91 | unsigned suffix_len = 2; |
| 92 | char *pfx; |
| 93 | char *count_p; |
| 94 | const char *sfx; |
Bernhard Reutner-Fischer | 0a537a0 | 2007-03-28 16:48:40 +0000 | [diff] [blame] | 95 | off_t cnt = 1000; |
| 96 | off_t remaining = 0; |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 97 | unsigned opt; |
Bernhard Reutner-Fischer | 0a537a0 | 2007-03-28 16:48:40 +0000 | [diff] [blame] | 98 | ssize_t bytes_read, to_write; |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 99 | char *src; |
| 100 | |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 101 | setup_common_bufsiz(); |
| 102 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 103 | opt = getopt32(argv, "^" |
| 104 | "l:b:a:+" /* -a N */ |
| 105 | "\0" "?2"/*max 2 args*/, |
| 106 | &count_p, &count_p, &suffix_len |
| 107 | ); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 108 | |
Bernhard Reutner-Fischer | 576de34 | 2007-03-26 18:19:29 +0000 | [diff] [blame] | 109 | if (opt & SPLIT_OPT_l) |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 110 | cnt = XATOOFF(count_p); |
| 111 | if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF |
Denys Vlasenko | c72b43c | 2013-07-13 23:49:45 +0200 | [diff] [blame] | 112 | cnt = xatoull_sfx(count_p, |
| 113 | IF_FEATURE_SPLIT_FANCY(split_suffixes) |
| 114 | IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes) |
| 115 | ); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 116 | sfx = "x"; |
Bernhard Reutner-Fischer | 0a537a0 | 2007-03-28 16:48:40 +0000 | [diff] [blame] | 117 | |
| 118 | argv += optind; |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 119 | if (argv[0]) { |
Denys Vlasenko | 5b6fe34 | 2009-11-26 05:43:16 +0100 | [diff] [blame] | 120 | int fd; |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 121 | if (argv[1]) |
| 122 | sfx = argv[1]; |
Denys Vlasenko | 7ff6836 | 2010-08-23 03:06:46 +0200 | [diff] [blame] | 123 | fd = xopen_stdin(argv[0]); |
Denys Vlasenko | 5b6fe34 | 2009-11-26 05:43:16 +0100 | [diff] [blame] | 124 | xmove_fd(fd, STDIN_FILENO); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 125 | } else { |
| 126 | argv[0] = (char *) bb_msg_standard_input; |
| 127 | } |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 128 | |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 129 | if (NAME_MAX < strlen(sfx) + suffix_len) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 130 | bb_simple_error_msg_and_die("suffix too long"); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 131 | |
| 132 | { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 133 | char *char_p = xzalloc(suffix_len + 1); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 134 | memset(char_p, 'a', suffix_len); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 135 | pfx = xasprintf("%s%s", sfx, char_p); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 136 | if (ENABLE_FEATURE_CLEAN_UP) |
| 137 | free(char_p); |
| 138 | } |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 139 | |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 140 | while (1) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 141 | bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 142 | if (!bytes_read) |
| 143 | break; |
| 144 | if (bytes_read < 0) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 145 | bb_simple_perror_msg_and_die(argv[0]); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 146 | src = read_buffer; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 147 | do { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 148 | if (!remaining) { |
| 149 | if (!pfx) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 150 | bb_simple_error_msg_and_die("suffixes exhausted"); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 151 | xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1); |
| 152 | pfx = next_file(pfx, suffix_len); |
| 153 | remaining = cnt; |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 154 | } |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 155 | |
| 156 | if (opt & SPLIT_OPT_b) { |
| 157 | /* split by bytes */ |
| 158 | to_write = (bytes_read < remaining) ? bytes_read : remaining; |
| 159 | remaining -= to_write; |
| 160 | } else { |
| 161 | /* split by lines */ |
| 162 | /* can be sped up by using _memrchr_ |
| 163 | * and writing many lines at once... */ |
| 164 | char *end = memchr(src, '\n', bytes_read); |
| 165 | if (end) { |
| 166 | --remaining; |
| 167 | to_write = end - src + 1; |
| 168 | } else { |
| 169 | to_write = bytes_read; |
| 170 | } |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 171 | } |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 172 | |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 173 | xwrite(STDOUT_FILENO, src, to_write); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 174 | bytes_read -= to_write; |
| 175 | src += to_write; |
| 176 | } while (bytes_read); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 177 | } |
Bernhard Reutner-Fischer | 7c2db5c | 2007-11-16 12:39:16 +0000 | [diff] [blame] | 178 | return EXIT_SUCCESS; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 179 | } |