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 |
| 4 | * Copyright (c) 2007 Bernhard Fischer |
| 5 | * |
| 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 7 | */ |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 8 | /* BB_AUDIT: SUSv3 compliant |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 9 | * SUSv3 requirements: |
| 10 | * http://www.opengroup.org/onlinepubs/009695399/utilities/split.html |
| 11 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 13 | |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 14 | static const struct suffix_mult split_suffices[] = { |
Bernhard Reutner-Fischer | 0a537a0 | 2007-03-28 16:48:40 +0000 | [diff] [blame] | 15 | #if ENABLE_FEATURE_SPLIT_FANCY |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 16 | { "b", 512 }, |
Bernhard Reutner-Fischer | 0a537a0 | 2007-03-28 16:48:40 +0000 | [diff] [blame] | 17 | #endif |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 18 | { "k", 1024 }, |
| 19 | { "m", 1024*1024 }, |
Bernhard Reutner-Fischer | 0a537a0 | 2007-03-28 16:48:40 +0000 | [diff] [blame] | 20 | #if ENABLE_FEATURE_SPLIT_FANCY |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 21 | { "g", 1024*1024*1024 }, |
Bernhard Reutner-Fischer | 0a537a0 | 2007-03-28 16:48:40 +0000 | [diff] [blame] | 22 | #endif |
Denis Vlasenko | f868963 | 2007-07-27 15:06:25 +0000 | [diff] [blame] | 23 | { } |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 24 | }; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 25 | |
| 26 | /* Increment the suffix part of the filename. |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 27 | * Returns NULL if we are out of filenames. |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 28 | */ |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 29 | static char *next_file(char *old, unsigned suffix_len) |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 30 | { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 31 | size_t end = strlen(old); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 32 | unsigned i = 1; |
| 33 | char *curr; |
| 34 | |
| 35 | do { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 36 | curr = old + end - i; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 37 | if (*curr < 'z') { |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 38 | *curr += 1; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 39 | break; |
| 40 | } |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 41 | i++; |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 42 | if (i > suffix_len) { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 43 | return NULL; |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 44 | } |
| 45 | *curr = 'a'; |
Bernhard Reutner-Fischer | 576de34 | 2007-03-26 18:19:29 +0000 | [diff] [blame] | 46 | } while (1); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 47 | |
| 48 | return old; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 49 | } |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 51 | #define read_buffer bb_common_bufsiz1 |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 52 | enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 }; |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 53 | |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 54 | #define SPLIT_OPT_l (1<<0) |
| 55 | #define SPLIT_OPT_b (1<<1) |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 56 | #define SPLIT_OPT_a (1<<2) |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 57 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 58 | int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 59 | int split_main(int argc UNUSED_PARAM, char **argv) |
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 | unsigned suffix_len = 2; |
| 62 | char *pfx; |
| 63 | char *count_p; |
| 64 | const char *sfx; |
Bernhard Reutner-Fischer | 0a537a0 | 2007-03-28 16:48:40 +0000 | [diff] [blame] | 65 | off_t cnt = 1000; |
| 66 | off_t remaining = 0; |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 67 | unsigned opt; |
Bernhard Reutner-Fischer | 0a537a0 | 2007-03-28 16:48:40 +0000 | [diff] [blame] | 68 | ssize_t bytes_read, to_write; |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 69 | char *src; |
| 70 | |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 71 | opt_complementary = "?2:a+"; /* max 2 args; -a N */ |
| 72 | opt = getopt32(argv, "l:b:a:", &count_p, &count_p, &suffix_len); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 73 | |
Bernhard Reutner-Fischer | 576de34 | 2007-03-26 18:19:29 +0000 | [diff] [blame] | 74 | if (opt & SPLIT_OPT_l) |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 75 | cnt = XATOOFF(count_p); |
| 76 | if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF |
| 77 | cnt = xatoull_sfx(count_p, split_suffices); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 78 | sfx = "x"; |
Bernhard Reutner-Fischer | 0a537a0 | 2007-03-28 16:48:40 +0000 | [diff] [blame] | 79 | |
| 80 | argv += optind; |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 81 | if (argv[0]) { |
| 82 | if (argv[1]) |
| 83 | sfx = argv[1]; |
| 84 | xmove_fd(xopen(argv[0], O_RDONLY), 0); |
| 85 | } else { |
| 86 | argv[0] = (char *) bb_msg_standard_input; |
| 87 | } |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 89 | if (NAME_MAX < strlen(sfx) + suffix_len) |
| 90 | bb_error_msg_and_die("suffix too long"); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 91 | |
| 92 | { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 93 | char *char_p = xzalloc(suffix_len + 1); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 94 | memset(char_p, 'a', suffix_len); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 95 | pfx = xasprintf("%s%s", sfx, char_p); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 96 | if (ENABLE_FEATURE_CLEAN_UP) |
| 97 | free(char_p); |
| 98 | } |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 99 | |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 100 | while (1) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 101 | bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 102 | if (!bytes_read) |
| 103 | break; |
| 104 | if (bytes_read < 0) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 105 | bb_simple_perror_msg_and_die(argv[0]); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 106 | src = read_buffer; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 107 | do { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 108 | if (!remaining) { |
| 109 | if (!pfx) |
Bernhard Reutner-Fischer | 0a537a0 | 2007-03-28 16:48:40 +0000 | [diff] [blame] | 110 | bb_error_msg_and_die("suffixes exhausted"); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 111 | xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1); |
| 112 | pfx = next_file(pfx, suffix_len); |
| 113 | remaining = cnt; |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 114 | } |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 115 | |
| 116 | if (opt & SPLIT_OPT_b) { |
| 117 | /* split by bytes */ |
| 118 | to_write = (bytes_read < remaining) ? bytes_read : remaining; |
| 119 | remaining -= to_write; |
| 120 | } else { |
| 121 | /* split by lines */ |
| 122 | /* can be sped up by using _memrchr_ |
| 123 | * and writing many lines at once... */ |
| 124 | char *end = memchr(src, '\n', bytes_read); |
| 125 | if (end) { |
| 126 | --remaining; |
| 127 | to_write = end - src + 1; |
| 128 | } else { |
| 129 | to_write = bytes_read; |
| 130 | } |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 131 | } |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 132 | |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 133 | xwrite(STDOUT_FILENO, src, to_write); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 134 | bytes_read -= to_write; |
| 135 | src += to_write; |
| 136 | } while (bytes_read); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 137 | } |
Bernhard Reutner-Fischer | 7c2db5c | 2007-11-16 12:39:16 +0000 | [diff] [blame] | 138 | return EXIT_SUCCESS; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 139 | } |