#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#ifdef __NetBSD__
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1989, 1993\
The Regents of the University of California. All rights reserved.");
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: strfile.c,v 1.44 2022/08/07 11:06:18 andvar Exp $");
#endif
#endif
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <inttypes.h>
#include <err.h>
#include "strfile.h"
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
# define STORING_PTRS (Oflag || Rflag)
# define CHUNKSIZE 512
# define ALLOC(ptr,sz) do { \
if (ptr == NULL) \
ptr = malloc(CHUNKSIZE * sizeof *ptr); \
else if (((sz) + 1) % CHUNKSIZE == 0) \
ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof *ptr); \
if (ptr == NULL) \
err(1, "out of space"); \
} while (0)
typedef struct {
char first;
off_t pos;
} STR;
static char *Infile = NULL;
static char Outfile[MAXPATHLEN] = "";
static char Delimch = '%';
static int Sflag = 0;
static int Oflag = 0;
static int Iflag = 0;
static int Rflag = 0;
static int Xflag = 0;
static long Num_pts = 0;
static off_t *Seekpts;
static FILE *Sort_1, *Sort_2;
static STRFILE Tbl;
static STR *Firstch;
static uint32_t h2nl(uint32_t h);
static void getargs(int argc, char **argv);
static void usage(const char *, ...) __dead __printflike(1, 2);
static void add_offset(FILE *fp, off_t off);
static void do_order(void);
static int cmp_str(const void *vp1, const void *vp2);
static void randomize(void);
static void fwrite_be_offt(off_t off, FILE *f);
int
main(int ac, char **av)
{
char *sp, dc;
FILE *inf, *outf;
off_t last_off, length, pos;
int first;
char *nsp;
STR *fp;
static char string[257];
long i;
if (sizeof(uint32_t) != 4)
errx(1, "sizeof(uint32_t) != 4");
getargs(ac, av);
dc = Delimch;
if ((inf = fopen(Infile, "r")) == NULL)
err(1, "open `%s'", Infile);
if ((outf = fopen(Outfile, "w")) == NULL)
err(1, "open `%s'", Outfile);
if (!STORING_PTRS)
(void) fseek(outf, sizeof Tbl, SEEK_SET);
Tbl.str_longlen = 0;
Tbl.str_shortlen = (unsigned int) 0x7fffffff;
Tbl.str_delim = dc;
Tbl.str_version = VERSION;
first = Oflag;
add_offset(outf, ftell(inf));
last_off = 0;
do {
sp = fgets(string, 256, inf);
if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
pos = ftell(inf);
length = pos - last_off - (sp ? strlen(sp) : 0);
last_off = pos;
if (!length)
continue;
add_offset(outf, pos);
if ((off_t)Tbl.str_longlen < length)
Tbl.str_longlen = length;
if ((off_t)Tbl.str_shortlen > length)
Tbl.str_shortlen = length;
first = Oflag;
}
else if (first) {
for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++)
continue;
ALLOC(Firstch, Num_pts);
fp = &Firstch[Num_pts - 1];
if (Iflag && isupper((unsigned char)*nsp))
fp->first = tolower((unsigned char)*nsp);
else
fp->first = *nsp;
fp->pos = Seekpts[Num_pts - 1];
first = 0;
}
} while (sp != NULL);
(void) fclose(inf);
if (Oflag)
do_order();
else if (Rflag)
randomize();
if (Xflag)
Tbl.str_flags |= STR_ROTATED;
if (!Sflag) {
printf("\"%s\" created\n", Outfile);
if (Num_pts == 2)
puts("There was 1 string");
else
printf("There were %d strings\n", (int)(Num_pts - 1));
printf("Longest string: %lu byte%s\n", (unsigned long)Tbl.str_longlen,
Tbl.str_longlen == 1 ? "" : "s");
printf("Shortest string: %lu byte%s\n", (unsigned long)Tbl.str_shortlen,
Tbl.str_shortlen == 1 ? "" : "s");
}
(void) fseek(outf, (off_t) 0, SEEK_SET);
Tbl.str_version = h2nl(Tbl.str_version);
Tbl.str_numstr = h2nl(Num_pts - 1);
Tbl.str_longlen = h2nl(Tbl.str_longlen);
Tbl.str_shortlen = h2nl(Tbl.str_shortlen);
Tbl.str_flags = h2nl(Tbl.str_flags);
(void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
if (STORING_PTRS) {
for (i = 0; i < Num_pts; i++)
fwrite_be_offt(Seekpts[i], outf);
}
fflush(outf);
if (ferror(outf))
err(1, "fwrite %s", Outfile);
(void) fclose(outf);
exit(0);
}
static void
getargs(int argc, char **argv)
{
int ch;
extern int optind;
extern char *optarg;
size_t len;
while ((ch = getopt(argc, argv, "c:iorsx")) != -1)
switch(ch) {
case 'c':
Delimch = *optarg;
if (!isascii(Delimch)) {
printf("bad delimiting character: '\\%o\n'",
Delimch);
}
break;
case 'i':
Iflag++;
break;
case 'o':
Oflag++;
break;
case 'r':
Rflag++;
break;
case 's':
Sflag++;
break;
case 'x':
Xflag++;
break;
case '?':
default:
usage(NULL);
}
argv += optind;
if (*argv) {
Infile = *argv;
if (*++argv) {
len = strlcpy(Outfile, *argv, sizeof(Outfile));
if (len >= sizeof(Outfile)) {
usage("Too long output filename");
}
}
}
if (!Infile) {
usage("No input file name");
}
if (*Outfile == '\0') {
len = snprintf(Outfile, sizeof(Outfile), "%s.dat", Infile);
if (len >= sizeof(Outfile)) {
usage("Too long input filename");
}
}
}
static void
usage(const char *fmt, ...)
{
if (fmt) {
va_list ap;
va_start(ap, fmt);
vwarnx(fmt, ap);
va_end(ap);
}
(void) fprintf(stderr,
"Usage: %s [-iorsx] [-c char] sourcefile [datafile]\n",
getprogname());
exit(1);
}
static void
add_offset(FILE *fp, off_t off)
{
if (!STORING_PTRS) {
fwrite_be_offt(off, fp);
} else {
ALLOC(Seekpts, Num_pts + 1);
Seekpts[Num_pts] = off;
}
Num_pts++;
}
static void
do_order(void)
{
int i;
off_t *lp;
STR *fp;
Sort_1 = fopen(Infile, "r");
Sort_2 = fopen(Infile, "r");
qsort((char *) Firstch, (int) Tbl.str_numstr, sizeof *Firstch, cmp_str);
i = Tbl.str_numstr;
lp = Seekpts;
fp = Firstch;
while (i--)
*lp++ = fp++->pos;
(void) fclose(Sort_1);
(void) fclose(Sort_2);
Tbl.str_flags |= STR_ORDERED;
}
static int
cmp_str(const void *vp1, const void *vp2)
{
const STR *p1, *p2;
int c1, c2;
int n1, n2;
p1 = (const STR *)vp1;
p2 = (const STR *)vp2;
# define SET_N(nf,ch) (nf = (ch == '\n'))
# define IS_END(ch,nf) (ch == Delimch && nf)
c1 = p1->first;
c2 = p2->first;
if (c1 != c2)
return c1 - c2;
(void) fseek(Sort_1, p1->pos, SEEK_SET);
(void) fseek(Sort_2, p2->pos, SEEK_SET);
n1 = 0;
n2 = 0;
while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0')
SET_N(n1, c1);
while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0')
SET_N(n2, c2);
while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
if (Iflag) {
if (isupper(c1))
c1 = tolower(c1);
if (isupper(c2))
c2 = tolower(c2);
}
if (c1 != c2)
return c1 - c2;
SET_N(n1, c1);
SET_N(n2, c2);
c1 = getc(Sort_1);
c2 = getc(Sort_2);
}
if (IS_END(c1, n1))
c1 = 0;
if (IS_END(c2, n2))
c2 = 0;
return c1 - c2;
}
static void
randomize(void)
{
int cnt, i;
off_t tmp;
off_t *sp;
srandom((int)(time(NULL) + getpid()));
Tbl.str_flags |= STR_RANDOM;
cnt = Tbl.str_numstr;
for (sp = Seekpts; cnt > 0; cnt--, sp++) {
i = random() % cnt;
tmp = sp[0];
sp[0] = sp[i];
sp[i] = tmp;
}
}
static void
fwrite_be_offt(off_t off, FILE *f)
{
int i;
unsigned char c[8];
for (i = 7; i >= 0; i--) {
c[i] = off & 0xff;
off >>= 8;
}
fwrite(c, sizeof(c), 1, f);
}
static uint32_t
h2nl(uint32_t h)
{
unsigned char c[4];
uint32_t rv;
c[0] = (h >> 24) & 0xff;
c[1] = (h >> 16) & 0xff;
c[2] = (h >> 8) & 0xff;
c[3] = (h >> 0) & 0xff;
memcpy(&rv, c, sizeof rv);
return (rv);
}