diff options
-rw-r--r-- | coreutils/split.c | 50 |
1 files changed, 21 insertions, 29 deletions
diff --git a/coreutils/split.c b/coreutils/split.c index 392bf0c69..ca546986e 100644 --- a/coreutils/split.c +++ b/coreutils/split.c | |||
@@ -44,7 +44,7 @@ static bool next_file(char **old) | |||
44 | return 1; | 44 | return 1; |
45 | } | 45 | } |
46 | *curr = 'a'; | 46 | *curr = 'a'; |
47 | } while (i <= suffix_len); | 47 | } while (1); |
48 | return 0; | 48 | return 0; |
49 | } | 49 | } |
50 | 50 | ||
@@ -55,77 +55,69 @@ static bool next_file(char **old) | |||
55 | int split_main(int argc, char **argv); | 55 | int split_main(int argc, char **argv); |
56 | int split_main(int argc, char **argv) | 56 | int split_main(int argc, char **argv) |
57 | { | 57 | { |
58 | char *pfx; | 58 | char *pfx, *buf, *input_file; |
59 | char *count_p; | 59 | unsigned cnt = 1000, opt; |
60 | char *sfx_len; | ||
61 | unsigned cnt = 1000; | ||
62 | char *input_file; | ||
63 | bool ret = EXIT_SUCCESS; | 60 | bool ret = EXIT_SUCCESS; |
64 | FILE *fp; | 61 | FILE *fp; |
65 | 62 | char *count_p, *sfx; | |
66 | //XXX: FIXME opt_complementary = "+2"; /* at most 2 non-option arguments */ | 63 | //XXX: FIXME opt_complementary = "+2"; /* at most 2 non-option arguments */ |
67 | getopt32(argc, argv, "l:b:a:", &count_p, &count_p, &sfx_len); | 64 | opt = getopt32(argc, argv, "l:b:a:", &count_p, &count_p, &sfx); |
68 | argv += optind; | ||
69 | 65 | ||
70 | if (option_mask32 & SPLIT_OPT_l) | 66 | if (opt & SPLIT_OPT_l) |
71 | cnt = xatoi(count_p); | 67 | cnt = xatoi(count_p); |
72 | if (option_mask32 & SPLIT_OPT_b) | 68 | if (opt & SPLIT_OPT_b) |
73 | cnt = xatoul_sfx(count_p, split_suffices); | 69 | cnt = xatoul_sfx(count_p, split_suffices); |
74 | if (option_mask32 & SPLIT_OPT_a) | 70 | if (opt & SPLIT_OPT_a) |
75 | suffix_len = xatoi(sfx_len); | 71 | suffix_len = xatoi(sfx); |
76 | 72 | argv += optind; | |
77 | if (!*argv) | 73 | if (!*argv) |
78 | *--argv = (char*) "-"; | 74 | *--argv = (char*) "-"; |
79 | input_file = *argv; | 75 | input_file = *argv; |
76 | sfx = *++argv; | ||
80 | 77 | ||
81 | if (NAME_MAX < strlen(*argv) + suffix_len) | 78 | if (sfx && (NAME_MAX < strlen(sfx) + suffix_len)) |
82 | bb_error_msg_and_die("Suffix too long"); | 79 | bb_error_msg_and_die("Suffix too long"); |
83 | 80 | ||
84 | fp = fopen_or_warn_stdin(input_file); | ||
85 | { | 81 | { |
86 | char *char_p = xzalloc(suffix_len); | 82 | char *char_p = xzalloc(suffix_len); |
87 | memset(char_p, 'a', suffix_len); | 83 | memset(char_p, 'a', suffix_len); |
88 | pfx = xasprintf("%s%s", (argc > optind + 1) ? *++argv : "x", char_p); | 84 | pfx = xasprintf("%s%s", sfx ? sfx : "x", char_p); |
89 | if (ENABLE_FEATURE_CLEAN_UP) | 85 | if (ENABLE_FEATURE_CLEAN_UP) |
90 | free(char_p); | 86 | free(char_p); |
91 | } | 87 | } |
88 | fp = fopen_or_warn_stdin(input_file); | ||
92 | //XXX:FIXME: unify those two file-handling schemata below (FILE vs fd) ! | 89 | //XXX:FIXME: unify those two file-handling schemata below (FILE vs fd) ! |
93 | if (option_mask32 & SPLIT_OPT_b) { | 90 | if (opt & SPLIT_OPT_b) { |
94 | char *buf; | ||
95 | ssize_t i; | 91 | ssize_t i; |
96 | ssize_t bytes = 0; | 92 | ssize_t bytes = 0; |
97 | int flags = O_WRONLY | O_CREAT | O_TRUNC; | ||
98 | int inp = fileno(fp); | 93 | int inp = fileno(fp); |
99 | 94 | ||
100 | do { | 95 | do { |
101 | int out = xopen(pfx, flags); | 96 | int out = xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC); |
102 | buf = xzalloc(cnt); | ||
103 | lseek(inp, bytes, SEEK_SET); | 97 | lseek(inp, bytes, SEEK_SET); |
98 | buf = xzalloc(cnt); | ||
104 | bytes += i = full_read(inp, buf, cnt); | 99 | bytes += i = full_read(inp, buf, cnt); |
105 | xwrite(out, buf, i); | 100 | xwrite(out, buf, i); |
106 | close(out); | ||
107 | free(buf); | 101 | free(buf); |
102 | close(out); | ||
108 | if (next_file(&pfx)) { | 103 | if (next_file(&pfx)) { |
109 | ret++; | 104 | ret++; |
110 | goto bail; | 105 | goto bail; |
111 | } | 106 | } |
112 | } while (i == cnt); /* if we read less than cnt, then nothing is left */ | 107 | } while (i == cnt); /* if we read less than cnt, then nothing is left */ |
113 | } else { /* -l */ | 108 | } else { /* -l */ |
114 | char *buf; | ||
115 | do { | 109 | do { |
116 | unsigned i = cnt; | 110 | unsigned i = cnt; |
117 | int flags = O_WRONLY | O_CREAT | O_TRUNC; | 111 | int out = xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC); |
118 | int out = xopen(pfx, flags); | ||
119 | buf = NULL; | 112 | buf = NULL; |
120 | while (i--) { | 113 | while (i--) { |
121 | buf = xmalloc_fgets(fp); | 114 | buf = xmalloc_fgets(fp); |
122 | if (buf == NULL) | 115 | if (buf == NULL) |
123 | break; | 116 | break; |
124 | xwrite(out, buf, buf ? strlen(buf) : 0); | 117 | xwrite(out, buf, strlen(buf)); |
125 | free(buf); | 118 | free(buf); |
126 | }; | 119 | }; |
127 | close(out); | 120 | close(out); |
128 | |||
129 | if (next_file(&pfx)) { | 121 | if (next_file(&pfx)) { |
130 | ret++; | 122 | ret++; |
131 | goto bail; | 123 | goto bail; |