- check the suffix proper plus misc touch up

This commit is contained in:
Bernhard Reutner-Fischer 2007-03-26 18:19:29 +00:00
parent afa7023b46
commit 576de34d1c

View File

@ -44,7 +44,7 @@ static bool next_file(char **old)
return 1; return 1;
} }
*curr = 'a'; *curr = 'a';
} while (i <= suffix_len); } while (1);
return 0; return 0;
} }
@ -55,77 +55,69 @@ static bool next_file(char **old)
int split_main(int argc, char **argv); int split_main(int argc, char **argv);
int split_main(int argc, char **argv) int split_main(int argc, char **argv)
{ {
char *pfx; char *pfx, *buf, *input_file;
char *count_p; unsigned cnt = 1000, opt;
char *sfx_len;
unsigned cnt = 1000;
char *input_file;
bool ret = EXIT_SUCCESS; bool ret = EXIT_SUCCESS;
FILE *fp; FILE *fp;
char *count_p, *sfx;
//XXX: FIXME opt_complementary = "+2"; /* at most 2 non-option arguments */ //XXX: FIXME opt_complementary = "+2"; /* at most 2 non-option arguments */
getopt32(argc, argv, "l:b:a:", &count_p, &count_p, &sfx_len); opt = getopt32(argc, argv, "l:b:a:", &count_p, &count_p, &sfx);
argv += optind;
if (option_mask32 & SPLIT_OPT_l) if (opt & SPLIT_OPT_l)
cnt = xatoi(count_p); cnt = xatoi(count_p);
if (option_mask32 & SPLIT_OPT_b) if (opt & SPLIT_OPT_b)
cnt = xatoul_sfx(count_p, split_suffices); cnt = xatoul_sfx(count_p, split_suffices);
if (option_mask32 & SPLIT_OPT_a) if (opt & SPLIT_OPT_a)
suffix_len = xatoi(sfx_len); suffix_len = xatoi(sfx);
argv += optind;
if (!*argv) if (!*argv)
*--argv = (char*) "-"; *--argv = (char*) "-";
input_file = *argv; input_file = *argv;
sfx = *++argv;
if (NAME_MAX < strlen(*argv) + suffix_len) if (sfx && (NAME_MAX < strlen(sfx) + suffix_len))
bb_error_msg_and_die("Suffix too long"); bb_error_msg_and_die("Suffix too long");
fp = fopen_or_warn_stdin(input_file);
{ {
char *char_p = xzalloc(suffix_len); char *char_p = xzalloc(suffix_len);
memset(char_p, 'a', suffix_len); memset(char_p, 'a', suffix_len);
pfx = xasprintf("%s%s", (argc > optind + 1) ? *++argv : "x", char_p); pfx = xasprintf("%s%s", sfx ? sfx : "x", char_p);
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
free(char_p); free(char_p);
} }
fp = fopen_or_warn_stdin(input_file);
//XXX:FIXME: unify those two file-handling schemata below (FILE vs fd) ! //XXX:FIXME: unify those two file-handling schemata below (FILE vs fd) !
if (option_mask32 & SPLIT_OPT_b) { if (opt & SPLIT_OPT_b) {
char *buf;
ssize_t i; ssize_t i;
ssize_t bytes = 0; ssize_t bytes = 0;
int flags = O_WRONLY | O_CREAT | O_TRUNC;
int inp = fileno(fp); int inp = fileno(fp);
do { do {
int out = xopen(pfx, flags); int out = xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC);
buf = xzalloc(cnt);
lseek(inp, bytes, SEEK_SET); lseek(inp, bytes, SEEK_SET);
buf = xzalloc(cnt);
bytes += i = full_read(inp, buf, cnt); bytes += i = full_read(inp, buf, cnt);
xwrite(out, buf, i); xwrite(out, buf, i);
close(out);
free(buf); free(buf);
close(out);
if (next_file(&pfx)) { if (next_file(&pfx)) {
ret++; ret++;
goto bail; goto bail;
} }
} while (i == cnt); /* if we read less than cnt, then nothing is left */ } while (i == cnt); /* if we read less than cnt, then nothing is left */
} else { /* -l */ } else { /* -l */
char *buf;
do { do {
unsigned i = cnt; unsigned i = cnt;
int flags = O_WRONLY | O_CREAT | O_TRUNC; int out = xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC);
int out = xopen(pfx, flags);
buf = NULL; buf = NULL;
while (i--) { while (i--) {
buf = xmalloc_fgets(fp); buf = xmalloc_fgets(fp);
if (buf == NULL) if (buf == NULL)
break; break;
xwrite(out, buf, buf ? strlen(buf) : 0); xwrite(out, buf, strlen(buf));
free(buf); free(buf);
}; };
close(out); close(out);
if (next_file(&pfx)) { if (next_file(&pfx)) {
ret++; ret++;
goto bail; goto bail;