- rough prototype for split(1). TODO: Still needs some love since it's way too big..
[oweals/busybox.git] / coreutils / split.c
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  */
8 /* BB_AUDIT: not yet SUSV3 compliant; FIXME: add -bN{k,m}
9  * SUSv3 requirements:
10  * http://www.opengroup.org/onlinepubs/009695399/utilities/split.html
11  */
12 #include "busybox.h"
13 static unsigned suffix_len = 2;
14
15 /* Increment the suffix part of the filename.
16  * Returns 0 on success and 1 on error (if we are out of files)
17  */
18 static bool next_file(char **old)
19 {
20         size_t end = strlen(*old);
21         unsigned i = 1;
22         char *curr;
23
24         do {
25 //              if (**(old + end - i) < 'z') {
26                 curr = *old + end - i;
27                 if (*curr < 'z') {
28                         *(*old + end - i) += 1;
29                         break;
30                 }
31                 *(*old +end - i) = 'a';
32                 i++;
33         } while (i <= suffix_len);
34         if ((*curr == 'z') && (i == suffix_len))
35                 return 1;
36         return 0;
37 }
38 #define SPLIT_OPT_l (1<<0)
39 #define SPLIT_OPT_b (1<<1)
40
41 int split_main(int argc, char **argv);
42 int split_main(int argc, char **argv)
43 {
44         char *pfx;
45         char *count_p = NULL;
46         char *sfx_len = NULL;
47         unsigned cnt = 1000;
48         char *input_file;
49
50 //XXX: FIXME    opt_complementary = "+2"; /* at most 2 non-option arguments */
51         getopt32(argc, argv, "l:b:a:", &count_p, &count_p, &sfx_len);
52         argv += optind;
53
54         if (count_p)
55                 cnt = xatoi(count_p);
56         if (sfx_len)
57                 suffix_len = xatoul(sfx_len);
58
59         if (!*argv)
60                 *--argv = (char*) "-";
61         input_file = *argv;
62         if (NAME_MAX < strlen(*argv) + suffix_len)
63                 bb_error_msg_and_die("Suffix too long");
64
65         {
66                 char *char_p = xzalloc(suffix_len);
67                 memset(char_p, 'a', suffix_len);
68                 pfx = xasprintf("%s%s", (argc > optind + 1) ? *++argv : "x", char_p);
69                 if (ENABLE_FEATURE_CLEAN_UP)
70                         free(char_p);
71         }
72 //XXX:FIXME: unify those two file-handling schemata below (FILE vs fd) !
73         if (option_mask32 & SPLIT_OPT_b) {
74                 char *buf;
75                 ssize_t i;
76                 ssize_t bytes = 0;
77                 int inp = xopen(input_file, O_RDONLY);
78                 int flags = O_WRONLY | O_CREAT | O_TRUNC;
79                 do {
80                         int out = xopen(pfx, flags);
81                         buf = xzalloc(cnt);
82                         lseek(inp, bytes, SEEK_SET);
83                         bytes += i = full_read(inp, buf, cnt);
84                         xwrite(out, buf, i);
85                         close(out);
86                         free(buf);
87                         if (next_file(&pfx))
88                                 flags = O_WRONLY | O_APPEND;
89                 } while(i > 0);
90         } else { /* -l */
91                 FILE *fp = fopen_or_warn_stdin(input_file);
92                 char *buf;
93                 do {
94                         unsigned i = cnt;
95                         int flags = O_WRONLY | O_CREAT | O_TRUNC;
96                         int out = xopen(pfx, flags);
97                         buf = NULL;
98                         while (i--) {
99                             buf = xmalloc_fgets(fp);
100                                 if (buf == NULL)
101                                         break;
102                                 xwrite(out, buf, buf ? strlen(buf) : 0);
103                                 free(buf);
104                         };
105                         close(out);
106
107                         if (next_file(&pfx))
108                                 flags = O_WRONLY | O_APPEND;
109                 } while (buf);
110                 if (ENABLE_FEATURE_CLEAN_UP)
111                         fclose_if_not_stdin(fp);
112         }
113
114         if (ENABLE_FEATURE_CLEAN_UP) {
115                 free(pfx);
116         }
117         return EXIT_SUCCESS;
118 }