X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/623e5e8eddc42ee4e3cff7c418dc2d3d4e96193f..35f9239da17d9e247372b31eff7d50efb738dded:/fortune/strfile/strfile.c diff --git a/fortune/strfile/strfile.c b/fortune/strfile/strfile.c index f8ff4a2f..a0598505 100644 --- a/fortune/strfile/strfile.c +++ b/fortune/strfile/strfile.c @@ -1,4 +1,4 @@ -/* $NetBSD: strfile.c,v 1.14 1999/09/18 19:38:50 jsm Exp $ */ +/* $NetBSD: strfile.c,v 1.39 2020/04/29 20:45:05 nia Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -15,11 +15,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -36,37 +32,45 @@ * SUCH DAMAGE. */ +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#ifdef __NetBSD__ #include #ifndef lint -__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\ - The Regents of the University of California. All rights reserved.\n"); +__COPYRIGHT("@(#) Copyright (c) 1989, 1993\ + The Regents of the University of California. All rights reserved."); #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: strfile.c,v 1.14 1999/09/18 19:38:50 jsm Exp $"); +__RCSID("$NetBSD: strfile.c,v 1.39 2020/04/29 20:45:05 nia Exp $"); #endif #endif /* not lint */ +#endif /* __NetBSD__ */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include "strfile.h" - -# ifndef MAXPATHLEN -# define MAXPATHLEN 1024 -# endif /* MAXPATHLEN */ +#include "strfile.h" + +#ifndef MAXPATHLEN +#define MAXPATHLEN 1024 +#endif /* MAXPATHLEN */ /* - * This program takes a file composed of strings seperated by + * This program takes a file composed of strings separated by * lines starting with two consecutive delimiting character (default * character is '%') and creates another file which consists of a table * describing the file (structure from "strfile.h"), a table of seek @@ -88,62 +92,51 @@ __RCSID("$NetBSD: strfile.c,v 1.14 1999/09/18 19:38:50 jsm Exp $"); * Added ordering options. */ -# define TRUE 1 -# define FALSE 0 - # define STORING_PTRS (Oflag || Rflag) # define CHUNKSIZE 512 -#ifdef lint -# define ALWAYS atoi("1") -#else -# define ALWAYS 1 -#endif -# define ALLOC(ptr,sz) if (ALWAYS) { \ +# define ALLOC(ptr,sz) do { \ if (ptr == NULL) \ - ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \ + ptr = malloc(CHUNKSIZE * sizeof *ptr); \ else if (((sz) + 1) % CHUNKSIZE == 0) \ - ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \ + ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof *ptr); \ if (ptr == NULL) \ - errx(1, "out of space"); \ - } else - -#ifdef NO_VOID -# define void char -#endif + err(1, "out of space"); \ + } while (0) typedef struct { char first; off_t pos; } STR; -char *Infile = NULL, /* input file name */ - Outfile[MAXPATHLEN] = "", /* output file name */ - Delimch = '%'; /* delimiting character */ +static char *Infile = NULL; /* input file name */ +static char Outfile[MAXPATHLEN] = ""; /* output file name */ +static char Delimch = '%'; /* delimiting character */ + +static int Sflag = 0; /* silent run flag */ +static int Oflag = 0; /* ordering flag */ +static int Iflag = 0; /* ignore case flag */ +static int Rflag = 0; /* randomize order flag */ +static int Xflag = 0; /* set rotated bit */ +static long Num_pts = 0; /* number of pointers/strings */ -int Sflag = FALSE; /* silent run flag */ -int Oflag = FALSE; /* ordering flag */ -int Iflag = FALSE; /* ignore case flag */ -int Rflag = FALSE; /* randomize order flag */ -int Xflag = FALSE; /* set rotated bit */ -long Num_pts = 0; /* number of pointers/strings */ +static off_t *Seekpts; -off_t *Seekpts; +static FILE *Sort_1, *Sort_2; /* pointers for sorting */ -FILE *Sort_1, *Sort_2; /* pointers for sorting */ +static STRFILE Tbl; /* statistics table */ -STRFILE Tbl; /* statistics table */ +static STR *Firstch; /* first chars of each string */ -STR *Firstch; /* first chars of each string */ -void add_offset __P((FILE *, off_t)); -int cmp_str __P((const void *, const void *)); -void do_order __P((void)); -void getargs __P((int, char *[])); -int main __P((int, char *[])); -void randomize __P((void)); -char *unctrl __P((char)); -void usage __P((void)) __attribute__((__noreturn__)); +static uint32_t h2nl(uint32_t h); +static void getargs(int argc, char **argv); +static void usage(void) __dead; +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); /* @@ -156,17 +149,20 @@ void usage __P((void)) __attribute__((__noreturn__)); * and then seek back to the beginning to write in the table. */ int -main(ac, av) - int ac; - char *av[]; +main(int ac, char **av) { char *sp, dc; FILE *inf, *outf; - off_t last_off, length, pos, *p; - int first, cnt; + off_t last_off, length, pos; + int first; char *nsp; STR *fp; static char string[257]; + long i; + + /* sanity test */ + if (sizeof(uint32_t) != 4) + errx(1, "sizeof(uint32_t) != 4"); getargs(ac, av); /* evalute arguments */ dc = Delimch; @@ -183,7 +179,7 @@ main(ac, av) */ Tbl.str_longlen = 0; - Tbl.str_shortlen = (unsigned int) 0xffffffff; + Tbl.str_shortlen = (unsigned int) 0x7fffffff; Tbl.str_delim = dc; Tbl.str_version = VERSION; first = Oflag; @@ -205,16 +201,16 @@ main(ac, av) first = Oflag; } else if (first) { - for (nsp = sp; !isalnum(*nsp); nsp++) + for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++) continue; ALLOC(Firstch, Num_pts); fp = &Firstch[Num_pts - 1]; - if (Iflag && isupper(*nsp)) - fp->first = tolower(*nsp); + if (Iflag && isupper((unsigned char)*nsp)) + fp->first = tolower((unsigned char)*nsp); else fp->first = *nsp; fp->pos = Seekpts[Num_pts - 1]; - first = FALSE; + first = 0; } } while (sp != NULL); @@ -238,23 +234,22 @@ main(ac, av) puts("There was 1 string"); else printf("There were %d strings\n", (int)(Num_pts - 1)); - printf("Longest string: %lu byte%s\n", Tbl.str_longlen, + printf("Longest string: %lu byte%s\n", (unsigned long)Tbl.str_longlen, Tbl.str_longlen == 1 ? "" : "s"); - printf("Shortest string: %lu byte%s\n", Tbl.str_shortlen, + 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); - HTOBE32(Tbl.str_version); - Tbl.str_numstr = htobe32(Num_pts - 1); - HTOBE32(Tbl.str_longlen); - HTOBE32(Tbl.str_shortlen); - HTOBE32(Tbl.str_flags); + 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 (p = Seekpts, cnt = Num_pts; cnt--; ++p) - HTOBE64(*p); - (void) fwrite((char *) Seekpts, sizeof *Seekpts, (int) Num_pts, outf); + for (i = 0; i < Num_pts; i++) + fwrite_be_offt(Seekpts[i], outf); } fflush(outf); if (ferror(outf)) @@ -266,12 +261,13 @@ main(ac, av) /* * This routine evaluates arguments from the command line */ -void -getargs(argc, argv) - int argc; - char **argv; +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) { @@ -305,24 +301,36 @@ getargs(argc, argv) if (*argv) { Infile = *argv; - if (*++argv) - (void) strcpy(Outfile, *argv); + if (*++argv) { + len = strlen(*argv); + if (len >= sizeof(Outfile)) { + puts("Bad output filename"); + usage(); + } + (void) memcpy(Outfile, *argv, len + 1); + } } if (!Infile) { puts("No input file name"); usage(); } if (*Outfile == '\0') { + len = strlen(Infile) + sizeof(".dat"); + if (len > sizeof(Outfile)) { + puts("Bad input filename"); + usage(); + } (void) strcpy(Outfile, Infile); (void) strcat(Outfile, ".dat"); } } -void -usage() +static void +usage(void) { (void) fprintf(stderr, - "strfile [-iorsx] [-c char] sourcefile [datafile]\n"); + "Usage: %s [-iorsx] [-c char] sourcefile [datafile]\n", + getprogname()); exit(1); } @@ -330,16 +338,12 @@ usage() * add_offset: * Add an offset to the list, or write it out, as appropriate. */ -void -add_offset(fp, off) - FILE *fp; - off_t off; +static void +add_offset(FILE *fp, off_t off) { - off_t net; if (!STORING_PTRS) { - net = htobe64(off); - fwrite(&net, 1, sizeof net, fp); + fwrite_be_offt(off, fp); } else { ALLOC(Seekpts, Num_pts + 1); Seekpts[Num_pts] = off; @@ -351,8 +355,8 @@ add_offset(fp, off) * do_order: * Order the strings alphabetically (possibly ignoring case). */ -void -do_order() +static void +do_order(void) { int i; off_t *lp; @@ -371,34 +375,8 @@ do_order() Tbl.str_flags |= STR_ORDERED; } -/* - * cmp_str: - * Compare two strings in the file - */ -char * -unctrl(c) - char c; -{ - static char buf[3]; - - if (isprint(c)) { - buf[0] = c; - buf[1] = '\0'; - } - else if (c == 0177) { - buf[0] = '^'; - buf[1] = '?'; - } - else { - buf[0] = '^'; - buf[1] = c + 'A' - 1; - } - return buf; -} - -int -cmp_str(vp1, vp2) - const void *vp1, *vp2; +static int +cmp_str(const void *vp1, const void *vp2) { const STR *p1, *p2; int c1, c2; @@ -418,8 +396,8 @@ cmp_str(vp1, vp2) (void) fseek(Sort_1, p1->pos, SEEK_SET); (void) fseek(Sort_2, p2->pos, SEEK_SET); - n1 = FALSE; - n2 = FALSE; + n1 = 0; + n2 = 0; while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0') SET_N(n1, c1); while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0') @@ -452,14 +430,14 @@ cmp_str(vp1, vp2) * not to randomize across delimiter boundaries. All * randomization is done within each block. */ -void -randomize() +static void +randomize(void) { int cnt, i; off_t tmp; off_t *sp; - srandom((int)(time((time_t *) NULL) + getpid())); + srandom((int)(time(NULL) + getpid())); Tbl.str_flags |= STR_RANDOM; cnt = Tbl.str_numstr; @@ -475,3 +453,36 @@ randomize() sp[i] = tmp; } } + +/* + * fwrite_be_offt: + * Write out the off paramater as a 64 bit big endian number + */ + +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); +}