split: fixes
- -b argument required, don't allow -b <= 0. - -l argument required, don't allow -l <= 0. - usage() and argument parsing: sort flags. - don't write 0 size file, get rid of goto reproducable with: printf 'a' | split -b 1. - close FILE *in if != stdin. - mark split as finished in README. - codestyle fixes.
This commit is contained in:
parent
5552db75ba
commit
949f930280
2
README
2
README
@ -60,7 +60,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
|
||||
=* setsid non-posix none
|
||||
=* sleep yes none
|
||||
sort no -m, -o, -d, -f, -i
|
||||
split yes none
|
||||
=* split yes none
|
||||
= sponge non-posix none
|
||||
strings no -a, -n, -t
|
||||
= sync non-posix none
|
||||
|
204
split.c
204
split.c
@ -8,107 +8,8 @@
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static int itostr(char *, int, int);
|
||||
static FILE *nextfile(FILE *, char *, int, int);
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
eprintf("usage: split [-d] [-a len] [-b [bytes[k|m|g]]] [-l [lines]] [input [prefix]]\n");
|
||||
}
|
||||
|
||||
static int base = 26, start = 'a';
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int plen, slen = 2;
|
||||
int ch;
|
||||
char name[NAME_MAX+1];
|
||||
char *prefix = "x";
|
||||
char *file = NULL;
|
||||
char *tmp, *end;
|
||||
size_t size = 1000, scale = 1, n;
|
||||
int always = 0;
|
||||
FILE *in = stdin, *out = NULL;
|
||||
|
||||
ARGBEGIN {
|
||||
case 'b':
|
||||
always = 1;
|
||||
tmp = ARGF();
|
||||
if (!tmp)
|
||||
break;
|
||||
|
||||
size = strtoull(tmp, &end, 10);
|
||||
if (!*end)
|
||||
break;
|
||||
switch (toupper((int)*end)) {
|
||||
case 'K':
|
||||
scale = 1024;
|
||||
break;
|
||||
case 'M':
|
||||
scale = 1024L * 1024L;
|
||||
break;
|
||||
case 'G':
|
||||
scale = 1024L * 1024L * 1024L;
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
if (size > (SIZE_MAX/scale))
|
||||
eprintf("'%s': out of range\n", tmp);
|
||||
size *= scale;
|
||||
break;
|
||||
case 'l':
|
||||
always = 0;
|
||||
tmp = ARGF();
|
||||
if (tmp)
|
||||
size = estrtonum(tmp, 0, MIN(LLONG_MAX, SIZE_MAX));
|
||||
break;
|
||||
case 'a':
|
||||
slen = estrtonum(EARGF(usage()), 0, INT_MAX);
|
||||
break;
|
||||
case 'd':
|
||||
base = 10;
|
||||
start = '0';
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if (*argv)
|
||||
file = *argv++;
|
||||
if (*argv)
|
||||
prefix = *argv++;
|
||||
if (*argv)
|
||||
usage();
|
||||
|
||||
plen = strlen(prefix);
|
||||
if (plen+slen > NAME_MAX)
|
||||
eprintf("names cannot exceed %d bytes\n", NAME_MAX);
|
||||
strlcpy(name, prefix, sizeof(name));
|
||||
|
||||
if (file && strcmp(file, "-") != 0) {
|
||||
in = fopen(file, "r");
|
||||
if (!in)
|
||||
eprintf("'%s':", file);
|
||||
}
|
||||
|
||||
Nextfile:
|
||||
while ((out = nextfile(out, name, plen, slen))) {
|
||||
n = 0;
|
||||
while ((ch = getc(in)) != EOF) {
|
||||
putc(ch, out);
|
||||
n += (always || ch == '\n');
|
||||
if (n >= size)
|
||||
goto Nextfile;
|
||||
}
|
||||
fclose(out);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
itostr(char *str, int x, int n)
|
||||
{
|
||||
@ -125,17 +26,110 @@ itostr(char *str, int x, int n)
|
||||
FILE *
|
||||
nextfile(FILE *f, char *buf, int plen, int slen)
|
||||
{
|
||||
static int n = 0;
|
||||
int s;
|
||||
static int filecount = 0;
|
||||
|
||||
if (f)
|
||||
fclose(f);
|
||||
s = itostr(buf+plen, n++, slen);
|
||||
if (s < 0)
|
||||
if (itostr(buf + plen, filecount++, slen) < 0)
|
||||
return NULL;
|
||||
|
||||
f = fopen(buf, "w");
|
||||
if (!f)
|
||||
if (!(f = fopen(buf, "w")))
|
||||
eprintf("'%s':", buf);
|
||||
return f;
|
||||
}
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
eprintf("usage: %s [-a len] [-b bytes[k|m|g]] [-d] [-l lines] "
|
||||
"[input [prefix]]\n", argv0);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
FILE *in = stdin, *out = NULL;
|
||||
char name[NAME_MAX + 1];
|
||||
char *prefix = "x";
|
||||
char *file = NULL;
|
||||
char *tmp, *end;
|
||||
size_t size = 1000, scale = 1, n;
|
||||
int ch, plen, slen = 2, always = 0;
|
||||
long l;
|
||||
|
||||
ARGBEGIN {
|
||||
case 'a':
|
||||
slen = estrtonum(EARGF(usage()), 0, INT_MAX);
|
||||
break;
|
||||
case 'b':
|
||||
always = 1;
|
||||
tmp = EARGF(usage());
|
||||
l = strtol(tmp, &end, 10);
|
||||
if (l <= 0)
|
||||
eprintf("invalid number of bytes: %s\n", tmp);
|
||||
size = (size_t)l;
|
||||
if (!*end)
|
||||
break;
|
||||
switch (toupper((int)*end)) {
|
||||
case 'K':
|
||||
scale = 1024;
|
||||
break;
|
||||
case 'M':
|
||||
scale = 1024L * 1024L;
|
||||
break;
|
||||
case 'G':
|
||||
scale = 1024L * 1024L * 1024L;
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
if (size > (SIZE_MAX / scale))
|
||||
eprintf("'%s': out of range\n", tmp);
|
||||
size *= scale;
|
||||
break;
|
||||
case 'd':
|
||||
base = 10;
|
||||
start = '0';
|
||||
break;
|
||||
case 'l':
|
||||
always = 0;
|
||||
tmp = EARGF(usage());
|
||||
size = estrtonum(tmp, 1, MIN(LLONG_MAX, SIZE_MAX));
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if (*argv)
|
||||
file = *argv++;
|
||||
if (*argv)
|
||||
prefix = *argv++;
|
||||
if (*argv)
|
||||
usage();
|
||||
|
||||
plen = strlen(prefix);
|
||||
if (plen + slen > NAME_MAX)
|
||||
eprintf("names cannot exceed %d bytes\n", NAME_MAX);
|
||||
strlcpy(name, prefix, sizeof(name));
|
||||
|
||||
if (file && strcmp(file, "-") != 0) {
|
||||
if (!(in = fopen(file, "r")))
|
||||
eprintf("'%s':", file);
|
||||
}
|
||||
|
||||
n = 0;
|
||||
while ((ch = getc(in)) != EOF) {
|
||||
if (!out || n >= size) {
|
||||
if (!(out = nextfile(out, name, plen, slen)))
|
||||
eprintf("fopen: %s:", name);
|
||||
n = 0;
|
||||
}
|
||||
n += (always || ch == '\n');
|
||||
putc(ch, out);
|
||||
}
|
||||
if (in != stdin)
|
||||
fclose(in);
|
||||
if (out)
|
||||
fclose(out);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user