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
|
=* setsid non-posix none
|
||||||
=* sleep yes none
|
=* sleep yes none
|
||||||
sort no -m, -o, -d, -f, -i
|
sort no -m, -o, -d, -f, -i
|
||||||
split yes none
|
=* split yes none
|
||||||
= sponge non-posix none
|
= sponge non-posix none
|
||||||
strings no -a, -n, -t
|
strings no -a, -n, -t
|
||||||
= sync non-posix none
|
= sync non-posix none
|
||||||
|
120
split.c
120
split.c
@ -8,38 +8,66 @@
|
|||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static int itostr(char *, int, int);
|
static int base = 26, start = 'a';
|
||||||
static FILE *nextfile(FILE *, char *, int, int);
|
|
||||||
|
int
|
||||||
|
itostr(char *str, int x, int n)
|
||||||
|
{
|
||||||
|
str[n] = '\0';
|
||||||
|
while (n-- > 0) {
|
||||||
|
str[n] = start + (x % base);
|
||||||
|
x /= base;
|
||||||
|
}
|
||||||
|
if (x)
|
||||||
|
return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *
|
||||||
|
nextfile(FILE *f, char *buf, int plen, int slen)
|
||||||
|
{
|
||||||
|
static int filecount = 0;
|
||||||
|
|
||||||
|
if (f)
|
||||||
|
fclose(f);
|
||||||
|
if (itostr(buf + plen, filecount++, slen) < 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (!(f = fopen(buf, "w")))
|
||||||
|
eprintf("'%s':", buf);
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
eprintf("usage: split [-d] [-a len] [-b [bytes[k|m|g]]] [-l [lines]] [input [prefix]]\n");
|
eprintf("usage: %s [-a len] [-b bytes[k|m|g]] [-d] [-l lines] "
|
||||||
|
"[input [prefix]]\n", argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int base = 26, start = 'a';
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int plen, slen = 2;
|
FILE *in = stdin, *out = NULL;
|
||||||
int ch;
|
|
||||||
char name[NAME_MAX + 1];
|
char name[NAME_MAX + 1];
|
||||||
char *prefix = "x";
|
char *prefix = "x";
|
||||||
char *file = NULL;
|
char *file = NULL;
|
||||||
char *tmp, *end;
|
char *tmp, *end;
|
||||||
size_t size = 1000, scale = 1, n;
|
size_t size = 1000, scale = 1, n;
|
||||||
int always = 0;
|
int ch, plen, slen = 2, always = 0;
|
||||||
FILE *in = stdin, *out = NULL;
|
long l;
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
|
case 'a':
|
||||||
|
slen = estrtonum(EARGF(usage()), 0, INT_MAX);
|
||||||
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
always = 1;
|
always = 1;
|
||||||
tmp = ARGF();
|
tmp = EARGF(usage());
|
||||||
if (!tmp)
|
l = strtol(tmp, &end, 10);
|
||||||
break;
|
if (l <= 0)
|
||||||
|
eprintf("invalid number of bytes: %s\n", tmp);
|
||||||
size = strtoull(tmp, &end, 10);
|
size = (size_t)l;
|
||||||
if (!*end)
|
if (!*end)
|
||||||
break;
|
break;
|
||||||
switch (toupper((int)*end)) {
|
switch (toupper((int)*end)) {
|
||||||
@ -59,19 +87,15 @@ main(int argc, char *argv[])
|
|||||||
eprintf("'%s': out of range\n", tmp);
|
eprintf("'%s': out of range\n", tmp);
|
||||||
size *= scale;
|
size *= scale;
|
||||||
break;
|
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':
|
case 'd':
|
||||||
base = 10;
|
base = 10;
|
||||||
start = '0';
|
start = '0';
|
||||||
break;
|
break;
|
||||||
|
case 'l':
|
||||||
|
always = 0;
|
||||||
|
tmp = EARGF(usage());
|
||||||
|
size = estrtonum(tmp, 1, MIN(LLONG_MAX, SIZE_MAX));
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
} ARGEND;
|
} ARGEND;
|
||||||
@ -89,53 +113,23 @@ main(int argc, char *argv[])
|
|||||||
strlcpy(name, prefix, sizeof(name));
|
strlcpy(name, prefix, sizeof(name));
|
||||||
|
|
||||||
if (file && strcmp(file, "-") != 0) {
|
if (file && strcmp(file, "-") != 0) {
|
||||||
in = fopen(file, "r");
|
if (!(in = fopen(file, "r")))
|
||||||
if (!in)
|
|
||||||
eprintf("'%s':", file);
|
eprintf("'%s':", file);
|
||||||
}
|
}
|
||||||
|
|
||||||
Nextfile:
|
|
||||||
while ((out = nextfile(out, name, plen, slen))) {
|
|
||||||
n = 0;
|
n = 0;
|
||||||
while ((ch = getc(in)) != EOF) {
|
while ((ch = getc(in)) != EOF) {
|
||||||
putc(ch, out);
|
if (!out || n >= size) {
|
||||||
|
if (!(out = nextfile(out, name, plen, slen)))
|
||||||
|
eprintf("fopen: %s:", name);
|
||||||
|
n = 0;
|
||||||
|
}
|
||||||
n += (always || ch == '\n');
|
n += (always || ch == '\n');
|
||||||
if (n >= size)
|
putc(ch, out);
|
||||||
goto Nextfile;
|
|
||||||
}
|
}
|
||||||
|
if (in != stdin)
|
||||||
|
fclose(in);
|
||||||
|
if (out)
|
||||||
fclose(out);
|
fclose(out);
|
||||||
break;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
itostr(char *str, int x, int n)
|
|
||||||
{
|
|
||||||
str[n] = '\0';
|
|
||||||
while (n-- > 0) {
|
|
||||||
str[n] = start + (x % base);
|
|
||||||
x /= base;
|
|
||||||
}
|
|
||||||
if (x)
|
|
||||||
return -1;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
FILE *
|
|
||||||
nextfile(FILE *f, char *buf, int plen, int slen)
|
|
||||||
{
|
|
||||||
static int n = 0;
|
|
||||||
int s;
|
|
||||||
|
|
||||||
if (f)
|
|
||||||
fclose(f);
|
|
||||||
s = itostr(buf+plen, n++, slen);
|
|
||||||
if (s < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
f = fopen(buf, "w");
|
|
||||||
if (!f)
|
|
||||||
eprintf("'%s':", buf);
|
|
||||||
return f;
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user