#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <regexpr.h>
#include <signal.h>
#include <locale.h>
#include <libintl.h>
#define LAST 0LL
#define ERR -1
#define FALSE 0
#define TRUE 1
#define EXPMODE 2
#define LINMODE 3
#define LINSIZ LINE_MAX
char linbuf[LINSIZ];
char *expbuf;
char tmpbuf[BUFSIZ];
char file[8192] = "xx";
char *targ;
char *sptr;
FILE *infile, *outfile;
int silent, keep, create;
int errflg;
int fiwidth = 2;
extern int optind;
extern char *optarg;
offset_t offset;
offset_t curline;
#define PERROR(x) fatal("%s: Illegal Regular Expression\n", targ);
static int asc_to_ll(char *, long long *);
static void closefile(void);
static void fatal(char *, char *);
static offset_t findline(char *, offset_t);
static void flush(void);
static FILE *getfile(void);
static char *getaline(int);
static void line_arg(char *);
static void num_arg(char *, int);
static void re_arg(char *);
static void sig(int);
static void to_line(offset_t);
static void usage(void);
int
main(int argc, char **argv)
{
int ch, mode;
char *ptr;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
while ((ch = getopt(argc, argv, "skf:n:")) != EOF) {
switch (ch) {
case 'f':
(void) strcpy(file, optarg);
if ((ptr = strrchr(optarg, '/')) == NULL)
ptr = optarg;
else
ptr++;
break;
case 'n':
for (ptr = optarg; *ptr != '\0'; ptr++)
if (!isdigit((int)*ptr))
fatal("-n num\n", NULL);
fiwidth = atoi(optarg);
break;
case 'k':
keep++;
break;
case 's':
silent++;
break;
case '?':
errflg++;
}
}
argv = &argv[optind];
argc -= optind;
if (argc <= 1 || errflg)
usage();
if (strcmp(*argv, "-") == 0) {
infile = tmpfile();
while (fread(tmpbuf, 1, BUFSIZ, stdin) != 0) {
if (fwrite(tmpbuf, 1, BUFSIZ, infile) == 0)
if (errno == ENOSPC) {
(void) fprintf(stderr, "csplit: ");
(void) fprintf(stderr, gettext(
"No space left on device\n"));
exit(1);
} else {
(void) fprintf(stderr, "csplit: ");
(void) fprintf(stderr, gettext(
"Bad write to temporary "
"file\n"));
exit(1);
}
(void) memset(tmpbuf, '\0', sizeof (tmpbuf));
}
rewind(infile);
} else if ((infile = fopen(*argv, "r")) == NULL)
fatal("Cannot open %s\n", *argv);
++argv;
curline = (offset_t)1;
(void) signal(SIGINT, sig);
for (; *argv; ++argv) {
targ = *argv;
switch (**argv) {
case '/':
mode = EXPMODE;
create = TRUE;
re_arg(*argv);
break;
case '%':
mode = EXPMODE;
create = FALSE;
re_arg(*argv);
break;
case '{':
num_arg(*argv, mode);
mode = FALSE;
break;
default:
mode = LINMODE;
create = TRUE;
line_arg(*argv);
break;
}
}
create = TRUE;
to_line(LAST);
return (0);
}
static int
asc_to_ll(char *str, long long *plc)
{
int f;
*plc = 0;
f = 0;
for (; ; str++) {
switch (*str) {
case ' ':
case '\t':
continue;
case '-':
f++;
case '+':
str++;
}
break;
}
for (; *str != '\0'; str++)
if (*str >= '0' && *str <= '9')
*plc = *plc * 10 + *str - '0';
else
return (ERR);
if (f)
*plc = -(*plc);
return (TRUE);
}
static void
closefile()
{
if (!silent && create) {
(void) fseeko(outfile, (offset_t)0, SEEK_END);
(void) fprintf(stdout, "%lld\n", (offset_t)ftello(outfile));
}
if (create)
(void) fclose(outfile);
}
static void
fatal(char *string, char *arg)
{
char *fls;
int num;
(void) fprintf(stderr, "csplit: ");
(void) fprintf(stderr, gettext(string), arg);
if (!keep) {
if (outfile) {
(void) fclose(outfile);
for (fls = file; *fls != '\0'; fls++)
continue;
fls -= fiwidth;
for (num = atoi(fls); num >= 0; num--) {
(void) sprintf(fls, "%.*d", fiwidth, num);
(void) unlink(file);
}
}
} else
if (outfile)
closefile();
exit(1);
}
static offset_t
findline(char *expr, offset_t oset)
{
static int benhere = 0;
offset_t lncnt = 0, saveloc;
saveloc = ftello(infile);
if (curline != (offset_t)1 || benhere)
(void) getaline(FALSE);
else
lncnt--;
benhere = 1;
while (getaline(FALSE) != NULL) {
lncnt++;
if ((sptr = strrchr(linbuf, '\n')) != NULL)
*sptr = '\0';
if (step(linbuf, expr)) {
(void) fseeko(infile, (offset_t)saveloc, SEEK_SET);
return (curline+lncnt+oset);
}
}
(void) fseeko(infile, (offset_t)saveloc, SEEK_SET);
return (curline+lncnt+oset+2);
}
static void
flush()
{
if (create)
(void) fputs(linbuf, outfile);
}
FILE *
getfile()
{
static char *fptr;
static int ctr;
FILE *opfil;
char tfile[15];
char *delim;
char savedelim;
if (create) {
if (fptr == 0)
for (fptr = file; *fptr != '\0'; fptr++)
continue;
(void) sprintf(fptr, "%.*d", fiwidth, ctr++);
if (strlen(fptr) > fiwidth) {
fatal("Suffix longer than %ld chars; increase -n\n",
(char *)fiwidth);
}
delim = strrchr(file, '/');
if (delim == (char *)NULL) {
if (strlen(file) > pathconf(".", _PC_NAME_MAX)) {
fatal("Name too long: %s\n", file);
}
} else {
savedelim = *delim;
*delim = '\0';
if (strlen(delim + 1) > pathconf(file, _PC_NAME_MAX)) {
fatal("Name too long: %s\n", delim + 1);
}
*delim = savedelim;
}
if ((opfil = fopen(file, "w")) == NULL) {
(void) strlcpy(tfile, file, sizeof (tfile));
(void) sprintf(fptr, "%.*d", fiwidth, (ctr-2));
fatal("Cannot create %s\n", tfile);
}
return (opfil);
}
return (NULL);
}
static char *
getaline(int bumpcur)
{
char *ret;
if (bumpcur)
curline++;
ret = fgets(linbuf, LINSIZ, infile);
return (ret);
}
static void
line_arg(char *line)
{
long long to;
if (asc_to_ll(line, &to) == ERR)
fatal("%s: bad line number\n", line);
to_line(to);
}
static void
num_arg(char *arg, int md)
{
offset_t repeat, toline;
char rep[21];
char *ptr;
int len;
ptr = rep;
for (++arg; *arg != '}'; arg += len) {
if (*arg == '\0')
fatal("%s: missing '}'\n", targ);
if ((len = mblen(arg, MB_LEN_MAX)) <= 0)
len = 1;
if ((ptr + len) >= &rep[20])
fatal("%s: Repeat count too large\n", targ);
(void) memcpy(ptr, arg, len);
ptr += len;
}
*ptr = '\0';
if ((asc_to_ll(rep, &repeat) == ERR) || repeat < 0L)
fatal("Illegal repeat count: %s\n", targ);
if (md == LINMODE) {
toline = offset = curline;
for (; repeat > 0LL; repeat--) {
toline += offset;
to_line(toline);
}
} else if (md == EXPMODE)
for (; repeat > 0LL; repeat--)
to_line(findline(expbuf, offset));
else
fatal("No operation for %s\n", targ);
}
static void
re_arg(char *string)
{
char *ptr;
char ch;
int len;
ch = *string;
ptr = string;
ptr++;
while (*ptr != ch) {
if (*ptr == '\\')
++ptr;
if (*ptr == '\0')
fatal("%s: missing delimiter\n", targ);
if ((len = mblen(ptr, MB_LEN_MAX)) <= 0)
len = 1;
ptr += len;
}
*ptr = '\0';
if (asc_to_ll(++ptr, &offset) == ERR)
fatal("%s: illegal offset\n", string);
string++;
expbuf = compile(string, (char *)0, (char *)0);
if (regerrno)
PERROR(regerrno);
to_line(findline(expbuf, offset));
}
static void
sig(int s)
{
(void) signal(SIGINT, sig);
fatal("Interrupt - program aborted at arg '%s'\n", targ);
}
static void
to_line(offset_t ln)
{
outfile = getfile();
if (ln != LAST) {
if (curline > ln)
fatal("%s - out of range\n", targ);
while (curline < ln) {
if (getaline(TRUE) == NULL)
fatal("%s - out of range\n", targ);
flush();
}
} else
if (getaline(TRUE) != NULL) {
flush();
for (;;) {
if (getaline(TRUE) == NULL)
break;
flush();
}
} else
fatal("%s - out of range\n", targ);
closefile();
}
static void
usage()
{
(void) fprintf(stderr, gettext(
"usage: csplit [-ks] [-f prefix] [-n number] "
"file arg1 ...argn\n"));
exit(1);
}