diff options
author | aldot <aldot@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-03-25 21:50:18 +0000 |
---|---|---|
committer | aldot <aldot@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-03-25 21:50:18 +0000 |
commit | 3c705200344cd09aa84e28e3c13e3595b3e764fb (patch) | |
tree | 70eb52b176ebc5bf9067caec93181891f8c1d4b6 | |
parent | e5ce1e126132ffbbf5248401a080e4aca029094b (diff) | |
download | busybox-w32-3c705200344cd09aa84e28e3c13e3595b3e764fb.tar.gz busybox-w32-3c705200344cd09aa84e28e3c13e3595b3e764fb.tar.bz2 busybox-w32-3c705200344cd09aa84e28e3c13e3595b3e764fb.zip |
- rough prototype for split(1). TODO: Still needs some love since it's way too big..
text data bss dec hex filename
602 4 0 606 25e coreutils/split.o
git-svn-id: svn://busybox.net/trunk/busybox@18236 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r-- | coreutils/Config.in | 8 | ||||
-rw-r--r-- | coreutils/Kbuild | 1 | ||||
-rw-r--r-- | coreutils/split.c | 118 | ||||
-rw-r--r-- | include/applets.h | 1 | ||||
-rw-r--r-- | include/usage.h | 6 | ||||
-rw-r--r-- | scripts/defconfig | 1 |
6 files changed, 135 insertions, 0 deletions
diff --git a/coreutils/Config.in b/coreutils/Config.in index 000f3a8af..baac799db 100644 --- a/coreutils/Config.in +++ b/coreutils/Config.in | |||
@@ -536,6 +536,14 @@ config FEATURE_SORT_BIG | |||
536 | The SuSv3 sort standard is available at: | 536 | The SuSv3 sort standard is available at: |
537 | http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html | 537 | http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html |
538 | 538 | ||
539 | config SPLIT | ||
540 | bool "split" | ||
541 | default n | ||
542 | help | ||
543 | split a file into pieces. | ||
544 | |||
545 | http://www.opengroup.org/onlinepubs/007904975/utilities/split.html | ||
546 | |||
539 | config STAT | 547 | config STAT |
540 | bool "stat" | 548 | bool "stat" |
541 | default n | 549 | default n |
diff --git a/coreutils/Kbuild b/coreutils/Kbuild index dfdcbd43c..1c6e6ed23 100644 --- a/coreutils/Kbuild +++ b/coreutils/Kbuild | |||
@@ -60,6 +60,7 @@ lib-$(CONFIG_RMDIR) += rmdir.o | |||
60 | lib-$(CONFIG_SEQ) += seq.o | 60 | lib-$(CONFIG_SEQ) += seq.o |
61 | lib-$(CONFIG_SHA1SUM) += md5_sha1_sum.o | 61 | lib-$(CONFIG_SHA1SUM) += md5_sha1_sum.o |
62 | lib-$(CONFIG_SLEEP) += sleep.o | 62 | lib-$(CONFIG_SLEEP) += sleep.o |
63 | lib-$(CONFIG_SPLIT) += split.o | ||
63 | lib-$(CONFIG_SORT) += sort.o | 64 | lib-$(CONFIG_SORT) += sort.o |
64 | lib-$(CONFIG_STAT) += stat.o | 65 | lib-$(CONFIG_STAT) += stat.o |
65 | lib-$(CONFIG_STTY) += stty.o | 66 | lib-$(CONFIG_STTY) += stty.o |
diff --git a/coreutils/split.c b/coreutils/split.c new file mode 100644 index 000000000..312906f6e --- /dev/null +++ b/coreutils/split.c | |||
@@ -0,0 +1,118 @@ | |||
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 | } | ||
diff --git a/include/applets.h b/include/applets.h index 3f5bf9bde..77258be42 100644 --- a/include/applets.h +++ b/include/applets.h | |||
@@ -274,6 +274,7 @@ USE_SHA1SUM(APPLET_ODDNAME(sha1sum, md5_sha1_sum, _BB_DIR_USR_BIN, _BB_SUID_NEVE | |||
274 | USE_SLEEP(APPLET(sleep, _BB_DIR_BIN, _BB_SUID_NEVER)) | 274 | USE_SLEEP(APPLET(sleep, _BB_DIR_BIN, _BB_SUID_NEVER)) |
275 | USE_SOFTLIMIT(APPLET_ODDNAME(softlimit, chpst, _BB_DIR_USR_BIN, _BB_SUID_NEVER, softlimit)) | 275 | USE_SOFTLIMIT(APPLET_ODDNAME(softlimit, chpst, _BB_DIR_USR_BIN, _BB_SUID_NEVER, softlimit)) |
276 | USE_SORT(APPLET(sort, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) | 276 | USE_SORT(APPLET(sort, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) |
277 | USE_SPLIT(APPLET(split, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) | ||
277 | USE_START_STOP_DAEMON(APPLET_ODDNAME(start-stop-daemon, start_stop_daemon, _BB_DIR_SBIN, _BB_SUID_NEVER, start_stop_daemon)) | 278 | USE_START_STOP_DAEMON(APPLET_ODDNAME(start-stop-daemon, start_stop_daemon, _BB_DIR_SBIN, _BB_SUID_NEVER, start_stop_daemon)) |
278 | USE_STAT(APPLET(stat, _BB_DIR_BIN, _BB_SUID_NEVER)) | 279 | USE_STAT(APPLET(stat, _BB_DIR_BIN, _BB_SUID_NEVER)) |
279 | USE_STRINGS(APPLET(strings, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) | 280 | USE_STRINGS(APPLET(strings, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) |
diff --git a/include/usage.h b/include/usage.h index 7adc1fc78..4843f5f67 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -2952,6 +2952,12 @@ | |||
2952 | ) \ | 2952 | ) \ |
2953 | "" | 2953 | "" |
2954 | 2954 | ||
2955 | #define split_trivial_usage \ | ||
2956 | "[OPTION] [INPUT [PREFIX]]" | ||
2957 | #define split_full_usage \ | ||
2958 | "Options:" \ | ||
2959 | "\n -blah XXX: Fixme: usage.h" | ||
2960 | |||
2955 | #define start_stop_daemon_trivial_usage \ | 2961 | #define start_stop_daemon_trivial_usage \ |
2956 | "[OPTIONS] [--start|--stop] ... [-- arguments...]" | 2962 | "[OPTIONS] [--start|--stop] ... [-- arguments...]" |
2957 | #define start_stop_daemon_full_usage \ | 2963 | #define start_stop_daemon_full_usage \ |
diff --git a/scripts/defconfig b/scripts/defconfig index c6c200263..6ca10b63d 100644 --- a/scripts/defconfig +++ b/scripts/defconfig | |||
@@ -193,6 +193,7 @@ CONFIG_SEQ=y | |||
193 | CONFIG_SHA1SUM=y | 193 | CONFIG_SHA1SUM=y |
194 | CONFIG_SLEEP=y | 194 | CONFIG_SLEEP=y |
195 | CONFIG_FEATURE_FANCY_SLEEP=y | 195 | CONFIG_FEATURE_FANCY_SLEEP=y |
196 | CONFIG_SPLIT=y | ||
196 | CONFIG_SORT=y | 197 | CONFIG_SORT=y |
197 | CONFIG_FEATURE_SORT_BIG=y | 198 | CONFIG_FEATURE_SORT_BIG=y |
198 | CONFIG_STAT=y | 199 | CONFIG_STAT=y |