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 | */ |
| 12 | #include "busybox.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 | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 15 | { "b", 512 }, |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 16 | { "k", 1024 }, |
| 17 | { "m", 1024*1024 }, |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 18 | { "g", 1024*1024*1024 }, |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 19 | { NULL, 0 } |
| 20 | }; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 21 | |
| 22 | /* Increment the suffix part of the filename. |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 23 | * Returns NULL if we are out of filenames. |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 24 | */ |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 25 | static char *next_file(char *old, unsigned suffix_len) |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 26 | { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 27 | size_t end = strlen(old); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 28 | unsigned i = 1; |
| 29 | char *curr; |
| 30 | |
| 31 | do { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 32 | curr = old + end - i; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 33 | if (*curr < 'z') { |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 34 | *curr += 1; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 35 | break; |
| 36 | } |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 37 | i++; |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 38 | if (i > suffix_len) { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 39 | return NULL; |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 40 | } |
| 41 | *curr = 'a'; |
Bernhard Reutner-Fischer | 576de34 | 2007-03-26 18:19:29 +0000 | [diff] [blame] | 42 | } while (1); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 43 | |
| 44 | return old; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 45 | } |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 47 | #define read_buffer bb_common_bufsiz1 |
| 48 | enum { READ_BUFFER_SIZE = sizeof(bb_common_bufsiz1) - 1 }; |
| 49 | |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 50 | #define SPLIT_OPT_l (1<<0) |
| 51 | #define SPLIT_OPT_b (1<<1) |
Bernhard Reutner-Fischer | cd43dfd | 2007-03-26 10:46:31 +0000 | [diff] [blame] | 52 | #define SPLIT_OPT_a (1<<2) |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 53 | |
| 54 | int split_main(int argc, char **argv); |
| 55 | int split_main(int argc, char **argv) |
| 56 | { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 57 | 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-Fischer | 576de34 | 2007-03-26 18:19:29 +0000 | [diff] [blame] | 68 | opt = getopt32(argc, argv, "l:b:a:", &count_p, &count_p, &sfx); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 69 | |
Bernhard Reutner-Fischer | 576de34 | 2007-03-26 18:19:29 +0000 | [diff] [blame] | 70 | if (opt & SPLIT_OPT_l) |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 71 | cnt = xatoul(count_p); |
Bernhard Reutner-Fischer | 576de34 | 2007-03-26 18:19:29 +0000 | [diff] [blame] | 72 | if (opt & SPLIT_OPT_b) |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 73 | cnt = xatoul_sfx(count_p, split_suffices); |
Bernhard Reutner-Fischer | 576de34 | 2007-03-26 18:19:29 +0000 | [diff] [blame] | 74 | if (opt & SPLIT_OPT_a) |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 75 | suffix_len = xatou(sfx); |
Bernhard Reutner-Fischer | 576de34 | 2007-03-26 18:19:29 +0000 | [diff] [blame] | 76 | argv += optind; |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 77 | 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-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 85 | |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 86 | if (NAME_MAX < strlen(sfx) + suffix_len) |
| 87 | bb_error_msg_and_die("suffix too long"); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 88 | |
| 89 | { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 90 | char *char_p = xzalloc(suffix_len + 1); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 91 | memset(char_p, 'a', suffix_len); |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 92 | pfx = xasprintf("%s%s", sfx, char_p); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 93 | if (ENABLE_FEATURE_CLEAN_UP) |
| 94 | free(char_p); |
| 95 | } |
Bernhard Reutner-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 96 | |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 97 | 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-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 104 | do { |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 105 | 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-Fischer | b813eaf | 2007-03-26 14:28:12 +0000 | [diff] [blame] | 111 | } |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 112 | |
| 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-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 | |
| 130 | xwrite(1, src, to_write); |
| 131 | bytes_read -= to_write; |
| 132 | src += to_write; |
| 133 | } while (bytes_read); |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 134 | } |
Denis Vlasenko | 91dd275 | 2007-03-26 20:15:40 +0000 | [diff] [blame] | 135 | return 0; |
Bernhard Reutner-Fischer | 10f8f5f | 2007-03-25 21:50:18 +0000 | [diff] [blame] | 136 | } |