-/* $NetBSD: strfile.c,v 1.22 2003/08/07 09:37:14 agc Exp $ */
+/* $NetBSD: strfile.c,v 1.30 2011/08/16 11:06:34 christos Exp $ */
/*-
* Copyright (c) 1989, 1993
#ifdef __NetBSD__
#include <sys/cdefs.h>
#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.22 2003/08/07 09:37:14 agc Exp $");
+__RCSID("$NetBSD: strfile.c,v 1.30 2011/08/16 11:06:34 christos Exp $");
#endif
#endif /* not lint */
#endif /* __NetBSD__ */
-# 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>
-
-# ifndef u_int32_t
-# define u_int32_t unsigned int
-# endif
-# include "strfile.h"
-
-# ifndef MAXPATHLEN
-# define MAXPATHLEN 1024
-# endif /* MAXPATHLEN */
-
-u_int32_t
-h2nl(u_int32_t h)
+/* 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 <err.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) __attribute__((__noreturn__));
+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];
- u_int32_t rv;
+ uint32_t rv;
c[0] = (h >> 24) & 0xff;
c[1] = (h >> 16) & 0xff;
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 {
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 */
-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 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 long Num_pts = 0; /* number of pointers/strings */
-off_t *Seekpts;
+static off_t *Seekpts;
-FILE *Sort_1, *Sort_2; /* pointers for sorting */
+static FILE *Sort_1, *Sort_2; /* pointers for sorting */
-STRFILE Tbl; /* statistics table */
+static STRFILE Tbl; /* statistics table */
-STR *Firstch; /* first chars of each string */
+static STR *Firstch; /* first chars of each string */
#ifdef __GNUC__
-#define NORETURN __attribute__((__noreturn__))
+#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 *[]);
* 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;
static char string[257];
/* sanity test */
- if (sizeof(u_int32_t) != 4)
- die("sizeof(unsigned int) != 4");
+ if (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);
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];
}
fflush(outf);
if (ferror(outf))
- dieperror("fwrite %s", Outfile);
+ err(1, "fwrite %s", Outfile);
(void) fclose(outf);
exit(0);
}
/*
* 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;
}
}
-void
-usage()
+static void
+usage(void)
{
(void) fprintf(stderr,
- "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
- exit(1);
-}
-
-void
-die(str)
- const char *str;
-{
- fprintf(stderr, "strfile: %s\n", str);
- exit(1);
-}
-
-void
-dieperror(fmt, file)
- 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);
}
* 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)
{
if (!STORING_PTRS) {
* do_order:
* Order the strings alphabetically (possibly ignoring case).
*/
-void
-do_order()
+static void
+do_order(void)
{
int i;
off_t *lp;
Tbl.str_flags |= STR_ORDERED;
}
-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;
* 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;
* Write out the off paramater as a 64 bit big endian number
*/
-void
-fwrite_be_offt(off, f)
- off_t off;
- FILE *f;
+static void
+fwrite_be_offt(off_t off, FILE *f)
{
int i;
unsigned char c[8];