busybox/coreutils/split.c
<<
>>
Prefs
   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
  26static const struct suffix_mult split_suffices[] = {
  27#if ENABLE_FEATURE_SPLIT_FANCY
  28        { "b", 512 },
  29#endif
  30        { "k", 1024 },
  31        { "m", 1024*1024 },
  32#if ENABLE_FEATURE_SPLIT_FANCY
  33        { "g", 1024*1024*1024 },
  34#endif
  35        { "", 0 }
  36};
  37
  38/* Increment the suffix part of the filename.
  39 * Returns NULL if we are out of filenames.
  40 */
  41static char *next_file(char *old, unsigned suffix_len)
  42{
  43        size_t end = strlen(old);
  44        unsigned i = 1;
  45        char *curr;
  46
  47        while (1) {
  48                curr = old + end - i;
  49                if (*curr < 'z') {
  50                        *curr += 1;
  51                        break;
  52                }
  53                i++;
  54                if (i > suffix_len) {
  55                        return NULL;
  56                }
  57                *curr = 'a';
  58        }
  59
  60        return old;
  61}
  62
  63#define read_buffer bb_common_bufsiz1
  64enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
  65
  66#define SPLIT_OPT_l (1<<0)
  67#define SPLIT_OPT_b (1<<1)
  68#define SPLIT_OPT_a (1<<2)
  69
  70int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  71int split_main(int argc UNUSED_PARAM, char **argv)
  72{
  73        unsigned suffix_len = 2;
  74        char *pfx;
  75        char *count_p;
  76        const char *sfx;
  77        off_t cnt = 1000;
  78        off_t remaining = 0;
  79        unsigned opt;
  80        ssize_t bytes_read, to_write;
  81        char *src;
  82
  83        opt_complementary = "?2:a+"; /* max 2 args; -a N */
  84        opt = getopt32(argv, "l:b:a:", &count_p, &count_p, &suffix_len);
  85
  86        if (opt & SPLIT_OPT_l)
  87                cnt = XATOOFF(count_p);
  88        if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
  89                cnt = xatoull_sfx(count_p, split_suffices);
  90        sfx = "x";
  91
  92        argv += optind;
  93        if (argv[0]) {
  94                int fd;
  95                if (argv[1])
  96                        sfx = argv[1];
  97                fd = xopen_stdin(argv[0]);
  98                xmove_fd(fd, STDIN_FILENO);
  99        } else {
 100                argv[0] = (char *) bb_msg_standard_input;
 101        }
 102
 103        if (NAME_MAX < strlen(sfx) + suffix_len)
 104                bb_error_msg_and_die("suffix too long");
 105
 106        {
 107                char *char_p = xzalloc(suffix_len + 1);
 108                memset(char_p, 'a', suffix_len);
 109                pfx = xasprintf("%s%s", sfx, char_p);
 110                if (ENABLE_FEATURE_CLEAN_UP)
 111                        free(char_p);
 112        }
 113
 114        while (1) {
 115                bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
 116                if (!bytes_read)
 117                        break;
 118                if (bytes_read < 0)
 119                        bb_simple_perror_msg_and_die(argv[0]);
 120                src = read_buffer;
 121                do {
 122                        if (!remaining) {
 123                                if (!pfx)
 124                                        bb_error_msg_and_die("suffixes exhausted");
 125                                xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
 126                                pfx = next_file(pfx, suffix_len);
 127                                remaining = cnt;
 128                        }
 129
 130                        if (opt & SPLIT_OPT_b) {
 131                                /* split by bytes */
 132                                to_write = (bytes_read < remaining) ? bytes_read : remaining;
 133                                remaining -= to_write;
 134                        } else {
 135                                /* split by lines */
 136                                /* can be sped up by using _memrchr_
 137                                 * and writing many lines at once... */
 138                                char *end = memchr(src, '\n', bytes_read);
 139                                if (end) {
 140                                        --remaining;
 141                                        to_write = end - src + 1;
 142                                } else {
 143                                        to_write = bytes_read;
 144                                }
 145                        }
 146
 147                        xwrite(STDOUT_FILENO, src, to_write);
 148                        bytes_read -= to_write;
 149                        src += to_write;
 150                } while (bytes_read);
 151        }
 152        return EXIT_SUCCESS;
 153}
 154