2013-10-07 12:03:26 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-11-13 13:54:28 -05:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdint.h>
|
2013-06-14 12:55:25 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2013-06-14 12:55:25 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-02-01 08:00:47 -05:00
|
|
|
static int base = 26, start = 'a';
|
|
|
|
|
2015-03-07 07:33:39 -05:00
|
|
|
static int
|
2015-02-01 08:00:47 -05:00
|
|
|
itostr(char *str, int x, int n)
|
|
|
|
{
|
|
|
|
str[n] = '\0';
|
|
|
|
while (n-- > 0) {
|
|
|
|
str[n] = start + (x % base);
|
|
|
|
x /= base;
|
|
|
|
}
|
2015-03-17 17:59:48 -04:00
|
|
|
|
|
|
|
return x ? -1 : 0;
|
2015-02-01 08:00:47 -05:00
|
|
|
}
|
|
|
|
|
2015-03-07 07:33:39 -05:00
|
|
|
static FILE *
|
2015-02-01 08:00:47 -05:00
|
|
|
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);
|
2015-03-17 17:59:48 -04:00
|
|
|
|
2015-02-01 08:00:47 -05:00
|
|
|
return f;
|
|
|
|
}
|
2014-04-22 08:13:51 -04:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-17 17:59:48 -04:00
|
|
|
eprintf("usage: %s [-a num] [-b num[k|m|g] | -l num] [-d] "
|
|
|
|
"[file [prefix]]\n", argv0);
|
2014-04-22 08:13:51 -04:00
|
|
|
}
|
2013-06-14 12:55:25 -04:00
|
|
|
|
2013-07-20 01:27:42 -04:00
|
|
|
int
|
2014-04-18 06:51:18 -04:00
|
|
|
main(int argc, char *argv[])
|
2013-06-14 12:55:25 -04:00
|
|
|
{
|
2015-02-01 08:00:47 -05:00
|
|
|
FILE *in = stdin, *out = NULL;
|
2015-01-31 19:24:03 -05:00
|
|
|
size_t size = 1000, scale = 1, n;
|
2015-02-01 08:00:47 -05:00
|
|
|
long l;
|
2015-03-17 17:59:48 -04:00
|
|
|
int ch, plen, slen = 2, always = 0;
|
|
|
|
char name[NAME_MAX + 1], *prefix = "x", *file = NULL, *tmp, *end;
|
2013-06-14 12:55:25 -04:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2015-02-01 08:00:47 -05:00
|
|
|
case 'a':
|
|
|
|
slen = estrtonum(EARGF(usage()), 0, INT_MAX);
|
|
|
|
break;
|
2013-06-14 12:55:25 -04:00
|
|
|
case 'b':
|
|
|
|
always = 1;
|
2015-02-01 08:00:47 -05:00
|
|
|
tmp = EARGF(usage());
|
|
|
|
l = strtol(tmp, &end, 10);
|
|
|
|
if (l <= 0)
|
|
|
|
eprintf("invalid number of bytes: %s\n", tmp);
|
|
|
|
size = (size_t)l;
|
2014-11-13 16:16:29 -05:00
|
|
|
if (!*end)
|
2013-06-14 12:55:25 -04:00
|
|
|
break;
|
2014-11-13 12:29:30 -05:00
|
|
|
switch (toupper((int)*end)) {
|
2014-04-01 09:30:17 -04:00
|
|
|
case 'K':
|
|
|
|
scale = 1024;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
scale = 1024L * 1024L;
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
scale = 1024L * 1024L * 1024L;
|
|
|
|
break;
|
|
|
|
default:
|
2013-06-14 12:55:25 -04:00
|
|
|
usage();
|
2014-04-01 09:30:17 -04:00
|
|
|
}
|
2015-02-01 08:00:47 -05:00
|
|
|
if (size > (SIZE_MAX / scale))
|
2014-01-30 08:51:47 -05:00
|
|
|
eprintf("'%s': out of range\n", tmp);
|
2013-06-14 12:55:25 -04:00
|
|
|
size *= scale;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
base = 10;
|
|
|
|
start = '0';
|
|
|
|
break;
|
2015-02-01 08:00:47 -05:00
|
|
|
case 'l':
|
|
|
|
always = 0;
|
|
|
|
tmp = EARGF(usage());
|
|
|
|
size = estrtonum(tmp, 1, MIN(LLONG_MAX, SIZE_MAX));
|
|
|
|
break;
|
2013-06-14 12:55:25 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
if (*argv)
|
2013-06-14 12:55:25 -04:00
|
|
|
file = *argv++;
|
2014-11-13 12:29:30 -05:00
|
|
|
if (*argv)
|
2013-06-14 12:55:25 -04:00
|
|
|
prefix = *argv++;
|
2014-11-13 12:29:30 -05:00
|
|
|
if (*argv)
|
2013-06-14 12:55:25 -04:00
|
|
|
usage();
|
|
|
|
|
|
|
|
plen = strlen(prefix);
|
2015-02-01 08:00:47 -05:00
|
|
|
if (plen + slen > NAME_MAX)
|
2014-01-30 08:51:47 -05:00
|
|
|
eprintf("names cannot exceed %d bytes\n", NAME_MAX);
|
2015-03-17 06:24:49 -04:00
|
|
|
estrlcpy(name, prefix, sizeof(name));
|
2013-06-14 12:55:25 -04:00
|
|
|
|
2015-03-17 17:59:48 -04:00
|
|
|
if (file && strcmp(file, "-")) {
|
2015-02-01 08:00:47 -05:00
|
|
|
if (!(in = fopen(file, "r")))
|
2015-03-17 17:59:48 -04:00
|
|
|
eprintf("fopen %s:", file);
|
2013-06-14 12:55:25 -04:00
|
|
|
}
|
|
|
|
|
2015-02-01 08:00:47 -05:00
|
|
|
n = 0;
|
|
|
|
while ((ch = getc(in)) != EOF) {
|
|
|
|
if (!out || n >= size) {
|
|
|
|
if (!(out = nextfile(out, name, plen, slen)))
|
|
|
|
eprintf("fopen: %s:", name);
|
|
|
|
n = 0;
|
2013-06-14 12:55:25 -04:00
|
|
|
}
|
2015-02-01 08:00:47 -05:00
|
|
|
n += (always || ch == '\n');
|
|
|
|
putc(ch, out);
|
2013-06-14 12:55:25 -04:00
|
|
|
}
|
2015-03-17 17:59:48 -04:00
|
|
|
|
|
|
|
fclose(in);
|
2015-02-01 08:00:47 -05:00
|
|
|
if (out)
|
|
|
|
fclose(out);
|
2015-03-17 17:59:48 -04:00
|
|
|
|
2013-06-14 12:55:25 -04:00
|
|
|
return 0;
|
|
|
|
}
|