- implement suffices as required by SUSv3

- fix glitch in next_file (suffices were exhaused too early).
This commit is contained in:
Bernhard Reutner-Fischer 2007-03-26 14:28:12 +00:00
parent 53091ecd20
commit b813eaff23

View File

@ -5,7 +5,7 @@
* *
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/ */
/* BB_AUDIT: not yet SUSV3 compliant; FIXME: add -bN{k,m} /* BB_AUDIT: SUSv3 compliant
* SUSv3 requirements: * SUSv3 requirements:
* http://www.opengroup.org/onlinepubs/009695399/utilities/split.html * http://www.opengroup.org/onlinepubs/009695399/utilities/split.html
*/ */
@ -38,13 +38,16 @@ static bool next_file(char **old)
*curr += 1; *curr += 1;
break; break;
} }
*curr = 'a';
i++; i++;
} while (i <= suffix_len); if (i > suffix_len) {
if ((*curr == 'z') && (i == suffix_len)) bb_error_msg("Suffices exhausted");
return 1; return 1;
}
*curr = 'a';
} while (i <= suffix_len);
return 0; return 0;
} }
#define SPLIT_OPT_l (1<<0) #define SPLIT_OPT_l (1<<0)
#define SPLIT_OPT_b (1<<1) #define SPLIT_OPT_b (1<<1)
#define SPLIT_OPT_a (1<<2) #define SPLIT_OPT_a (1<<2)
@ -57,22 +60,28 @@ int split_main(int argc, char **argv)
char *sfx_len; char *sfx_len;
unsigned cnt = 1000; unsigned cnt = 1000;
char *input_file; char *input_file;
bool ret = EXIT_SUCCESS;
FILE *fp;
//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); getopt32(argc, argv, "l:b:a:", &count_p, &count_p, &sfx_len);
argv += optind; argv += optind;
if (option_mask32 & (SPLIT_OPT_l|SPLIT_OPT_b)) if (option_mask32 & SPLIT_OPT_l)
cnt = xatoi(count_p); cnt = xatoi(count_p);
if (option_mask32 & SPLIT_OPT_b)
cnt = xatoul_sfx(count_p, split_suffices);
if (option_mask32 & SPLIT_OPT_a) if (option_mask32 & SPLIT_OPT_a)
suffix_len = xatoul(sfx_len); suffix_len = xatoi(sfx_len);
if (!*argv) if (!*argv)
*--argv = (char*) "-"; *--argv = (char*) "-";
input_file = *argv; input_file = *argv;
if (NAME_MAX < strlen(*argv) + suffix_len) if (NAME_MAX < strlen(*argv) + 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);
@ -85,8 +94,9 @@ int split_main(int argc, char **argv)
char *buf; char *buf;
ssize_t i; ssize_t i;
ssize_t bytes = 0; ssize_t bytes = 0;
int inp = xopen(input_file, O_RDONLY);
int flags = O_WRONLY | O_CREAT | O_TRUNC; int flags = O_WRONLY | O_CREAT | O_TRUNC;
int inp = fileno(fp);
do { do {
int out = xopen(pfx, flags); int out = xopen(pfx, flags);
buf = xzalloc(cnt); buf = xzalloc(cnt);
@ -95,11 +105,12 @@ int split_main(int argc, char **argv)
xwrite(out, buf, i); xwrite(out, buf, i);
close(out); close(out);
free(buf); free(buf);
if (next_file(&pfx)) if (next_file(&pfx)) {
flags = O_WRONLY | O_APPEND; ret++;
} while(i > 0); goto bail;
}
} while (i == cnt); /* if we read less than cnt, then nothing is left */
} else { /* -l */ } else { /* -l */
FILE *fp = fopen_or_warn_stdin(input_file);
char *buf; char *buf;
do { do {
unsigned i = cnt; unsigned i = cnt;
@ -115,15 +126,16 @@ int split_main(int argc, char **argv)
}; };
close(out); close(out);
if (next_file(&pfx)) if (next_file(&pfx)) {
flags = O_WRONLY | O_APPEND; ret++;
} while (buf); goto bail;
if (ENABLE_FEATURE_CLEAN_UP)
fclose_if_not_stdin(fp);
} }
} while (buf);
}
bail:
if (ENABLE_FEATURE_CLEAN_UP) { if (ENABLE_FEATURE_CLEAN_UP) {
free(pfx); free(pfx);
fclose_if_not_stdin(fp);
} }
return EXIT_SUCCESS; return ret;
} }