-/* $NetBSD: strfile.c,v 1.29 2009/08/12 06:06:28 dholland Exp $ */
+/* $NetBSD: strfile.c,v 1.39 2020/04/29 20:45:05 nia Exp $ */
/*-
* Copyright (c) 1989, 1993
* SUCH DAMAGE.
*/
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
#ifdef __NetBSD__
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: strfile.c,v 1.29 2009/08/12 06:06:28 dholland Exp $");
+__RCSID("$NetBSD: strfile.c,v 1.39 2020/04/29 20:45:05 nia Exp $");
#endif
#endif /* not lint */
#endif /* __NetBSD__ */
-/* n.b.: this file is used at build-time - i.e. during build.sh. */
-
-# include <sys/types.h>
-# include <sys/param.h>
-# include <ctype.h>
-# include <stdio.h>
-# include <stdlib.h>
-# include <string.h>
-# include <time.h>
-# include <unistd.h>
-# include <inttypes.h>
-
-# include "strfile.h"
-
-# ifndef MAXPATHLEN
-# define MAXPATHLEN 1024
-# endif /* MAXPATHLEN */
-
-static uint32_t h2nl(uint32_t h);
-static void getargs(int argc, char **argv);
-static void usage(void);
-static void die(const char *str);
-static void dieperror(const char *fmt, char *file);
-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);
-
-static uint32_t
-h2nl(uint32_t h)
-{
- unsigned char c[4];
- uint32_t rv;
+#include <sys/types.h>
+#include <sys/param.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <err.h>
- 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);
+#include "strfile.h"
- return (rv);
-}
+#ifndef MAXPATHLEN
+#define MAXPATHLEN 1024
+#endif /* MAXPATHLEN */
/*
* This program takes a file composed of strings separated by
* Added ordering options.
*/
-# define TRUE 1
-# define FALSE 0
-
# define STORING_PTRS (Oflag || Rflag)
# define CHUNKSIZE 512
else if (((sz) + 1) % CHUNKSIZE == 0) \
ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof *ptr); \
if (ptr == NULL) \
- die("out of space"); \
+ err(1, "out of space"); \
} while (0)
typedef struct {
static char Outfile[MAXPATHLEN] = ""; /* output file name */
static char Delimch = '%'; /* delimiting character */
-static int Sflag = FALSE; /* silent run flag */
-static int Oflag = FALSE; /* ordering flag */
-static int Iflag = FALSE; /* ignore case flag */
-static int Rflag = FALSE; /* randomize order flag */
-static int Xflag = FALSE; /* set rotated bit */
+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 */
static off_t *Seekpts;
static STR *Firstch; /* first chars of each string */
-#ifdef __GNUC__
-#define NORETURN __dead
-#else
-#define NORETURN
-#endif
-
-#ifndef __dead /* not NetBSD, presumably */
-#define __dead ;
-#endif
-void add_offset(FILE *, off_t);
-int cmp_str(const void *, const void *);
-void die(const char *) NORETURN;
-void dieperror(const char *, char *) NORETURN;
-void do_order(void);
-void fwrite_be_offt(off_t, FILE *);
-void getargs(int, char *[]);
-int main(int, char *[]);
-void randomize(void);
-void usage(void) 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);
/*
{
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)
- die("sizeof(uint32_t) != 4");
+ errx(1, "sizeof(uint32_t) != 4");
getargs(ac, av); /* evalute arguments */
dc = Delimch;
if ((inf = fopen(Infile, "r")) == NULL)
- dieperror("open `%s'", Infile);
+ err(1, "open `%s'", Infile);
if ((outf = fopen(Outfile, "w")) == NULL)
- dieperror("open `%s'", Outfile);
+ err(1, "open `%s'", Outfile);
if (!STORING_PTRS)
(void) fseek(outf, sizeof Tbl, SEEK_SET);
else
fp->first = *nsp;
fp->pos = Seekpts[Num_pts - 1];
- first = FALSE;
+ first = 0;
}
} while (sp != NULL);
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)
- fwrite_be_offt(*p, outf);
+ for (i = 0; i < Num_pts; i++)
+ fwrite_be_offt(Seekpts[i], outf);
}
fflush(outf);
if (ferror(outf))
- dieperror("fwrite %s", Outfile);
+ err(1, "fwrite %s", Outfile);
(void) fclose(outf);
exit(0);
}
int ch;
extern int optind;
extern char *optarg;
+ size_t len;
while ((ch = getopt(argc, argv, "c:iorsx")) != -1)
switch(ch) {
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");
}
usage(void)
{
(void) fprintf(stderr,
- "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
- exit(1);
-}
-
-static void
-die(const char *str)
-{
- fprintf(stderr, "strfile: %s\n", str);
- exit(1);
-}
-
-static void
-dieperror(const char *fmt, char *file)
-{
- fprintf(stderr, "strfile: ");
- fprintf(stderr, fmt, file);
- fprintf(stderr, ": ");
- perror(NULL);
+ "Usage: %s [-iorsx] [-c char] sourcefile [datafile]\n",
+ getprogname());
exit(1);
}
(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')
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;
}
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);
+}