-/* $NetBSD: strfile.c,v 1.34 2011/08/31 16:24:55 plunky Exp $ */
+/* $NetBSD: strfile.c,v 1.42 2020/07/26 15:14:09 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.34 2011/08/31 16:24:55 plunky Exp $");
+__RCSID("$NetBSD: strfile.c,v 1.42 2020/07/26 15:14:09 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 <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXPATHLEN 1024
#endif /* MAXPATHLEN */
-#if defined(__NetBSD__) || defined(__dead)
-#define NORETURN __dead
-#elif defined __GNUC__
-#define NORETURN __attribute__((__noreturn__))
-#else
-#define NORETURN
-#endif
-
/*
* This program takes a file composed of strings separated by
* lines starting with two consecutive delimiting character (default
* Added ordering options.
*/
-# define TRUE 1
-# define FALSE 0
-
# define STORING_PTRS (Oflag || Rflag)
# define CHUNKSIZE 512
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 uint32_t h2nl(uint32_t h);
static void getargs(int argc, char **argv);
-static void usage(void) NORETURN;
+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);
{
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)
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))
int ch;
extern int optind;
extern char *optarg;
+ size_t len;
while ((ch = getopt(argc, argv, "c:iorsx")) != -1)
switch(ch) {
break;
case '?':
default:
- usage();
+ usage(NULL);
}
argv += optind;
if (*argv) {
Infile = *argv;
- if (*++argv)
- (void) strcpy(Outfile, *argv);
+ if (*++argv) {
+ len = strlcpy(Outfile, *argv, sizeof(Outfile));
+ if (len >= sizeof(Outfile)) {
+ usage("Too long output filename");
+ }
+ }
}
if (!Infile) {
- puts("No input file name");
- usage();
+ usage("No input file name");
}
if (*Outfile == '\0') {
- (void) strcpy(Outfile, Infile);
- (void) strcat(Outfile, ".dat");
+ len = snprintf(Outfile, sizeof(Outfile), "%s.dat", Infile);
+ if (len >= sizeof(Outfile)) {
+ usage("Too long input filename");
+ }
}
}
static void
-usage(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());
(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')