From 9ee179a6089b185e0a162155b0073f9cfb45b4bf Mon Sep 17 00:00:00 2001 From: christos Date: Thu, 30 Apr 2020 12:32:26 +0000 Subject: Simplify, errors to stderr. --- fortune/strfile/strfile.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/fortune/strfile/strfile.c b/fortune/strfile/strfile.c index a0598505..891c77dd 100644 --- a/fortune/strfile/strfile.c +++ b/fortune/strfile/strfile.c @@ -1,4 +1,4 @@ -/* $NetBSD: strfile.c,v 1.39 2020/04/29 20:45:05 nia Exp $ */ +/* $NetBSD: strfile.c,v 1.40 2020/04/30 12:32:26 christos Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -47,7 +47,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993\ #if 0 static char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: strfile.c,v 1.39 2020/04/29 20:45:05 nia Exp $"); +__RCSID("$NetBSD: strfile.c,v 1.40 2020/04/30 12:32:26 christos Exp $"); #endif #endif /* not lint */ #endif /* __NetBSD__ */ @@ -55,6 +55,7 @@ __RCSID("$NetBSD: strfile.c,v 1.39 2020/04/29 20:45:05 nia Exp $"); #include #include #include +#include #include #include #include @@ -131,7 +132,7 @@ static STR *Firstch; /* first chars of each string */ static uint32_t h2nl(uint32_t h); static void getargs(int argc, char **argv); -static void usage(void) __dead; +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); @@ -295,39 +296,39 @@ getargs(int argc, char **argv) break; case '?': default: - usage(); + usage(NULL); } argv += optind; if (*argv) { Infile = *argv; if (*++argv) { - len = strlen(*argv); + len = strlcpy(Outfile, *argv, sizeof(Outfile)); if (len >= sizeof(Outfile)) { - puts("Bad output filename"); - usage(); + usage("Too long output filename"); } - (void) memcpy(Outfile, *argv, len + 1); } } if (!Infile) { - puts("No input file name"); - usage(); + usage("No input file name"); } if (*Outfile == '\0') { - len = strlen(Infile) + sizeof(".dat"); - if (len > sizeof(Outfile)) { - puts("Bad input filename"); - usage(); + len = snprintf(Outfile, sizeof(Outfile), "%s.dat", Infile); + if (len >= sizeof(Outfile)) { + usage("Too long input filename"); } - (void) strcpy(Outfile, Infile); - (void) strcat(Outfile, ".dat"); } } 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()); -- cgit v1.2.3-56-ge451