revert to per-cmd usage()

This commit is contained in:
Connor Lane Smith 2012-05-15 13:32:56 +01:00
parent f3188246d8
commit fcb8821246
7 changed files with 38 additions and 33 deletions

View File

@ -5,7 +5,7 @@
#include <string.h> #include <string.h>
#include "util.h" #include "util.h"
#define USAGE() usage("name [suffix]") static void usage(void);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -15,11 +15,11 @@ main(int argc, char *argv[])
ARGBEGIN { ARGBEGIN {
default: default:
USAGE(); usage();
} ARGEND; } ARGEND;
if(argc < 1) if(argc < 1)
USAGE(); usage();
s = basename(argv[0]); s = basename(argv[0]);
if(argc == 2 && argv[1]) { if(argc == 2 && argv[1]) {
@ -31,3 +31,9 @@ main(int argc, char *argv[])
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void
usage(void)
{
eprintf("usage: %s name [suffix]\n", argv0);
}

11
cksum.c
View File

@ -5,9 +5,8 @@
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
#define USAGE() usage("[files...]")
static void cksum(int, const char *); static void cksum(int, const char *);
static void usage(void);
static const unsigned long crctab[] = { 0x00000000, static const unsigned long crctab[] = { 0x00000000,
0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
@ -70,7 +69,7 @@ main(int argc, char *argv[])
ARGBEGIN { ARGBEGIN {
default: default:
USAGE(); usage();
} ARGEND; } ARGEND;
if(argc == 0) if(argc == 0)
@ -106,3 +105,9 @@ cksum(int fd, const char *s)
printf(" %s", s); printf(" %s", s);
putchar('\n'); putchar('\n');
} }
void
usage(void)
{
eprintf("usage: %s [files...]\n", argv0);
}

12
kill.c
View File

@ -8,8 +8,6 @@
#include <sys/wait.h> #include <sys/wait.h>
#include "util.h" #include "util.h"
#define USAGE() killusage()
struct { struct {
const char *name; const char *name;
int sig; int sig;
@ -21,7 +19,7 @@ struct {
#undef SIG #undef SIG
}; };
static void killusage(void); static void usage(void);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -50,10 +48,10 @@ main(int argc, char *argv[])
eprintf("%s: unknown signal\n", optarg); eprintf("%s: unknown signal\n", optarg);
break; break;
default: default:
USAGE(); usage();
} }
if(optind < argc-1) if(optind < argc-1)
USAGE(); usage();
if(lflag) { if(lflag) {
sig = (optind == argc) ? 0 : estrtol(argv[optind], 0); sig = (optind == argc) ? 0 : estrtol(argv[optind], 0);
@ -73,8 +71,8 @@ main(int argc, char *argv[])
} }
void void
killusage(void) usage(void)
{ {
fprintf(stderr, "usage: %s [-s signal] [pid...]\n" eprintf("usage: %s [-s signal] [pid...]\n"
" %s -l [signum]\n", argv0, argv0); " %s -l [signum]\n", argv0, argv0);
} }

14
test.c
View File

@ -7,11 +7,9 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "util.h" #include "util.h"
#define USAGE() testusage()
static bool unary(const char *, const char *); static bool unary(const char *, const char *);
static bool binary(const char *, const char *, const char *); static bool binary(const char *, const char *, const char *);
static void testusage(void); static void usage(void);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -23,7 +21,7 @@ main(int argc, char *argv[])
/* [ ... ] alias */ /* [ ... ] alias */
if(!strcmp(argv[0], "[")) { if(!strcmp(argv[0], "[")) {
if(strcmp(argv[argc-1], "]") != 0) if(strcmp(argv[argc-1], "]") != 0)
USAGE(); usage();
argc--; argc--;
} }
if(argc > 1 && !strcmp(argv[1], "!")) { if(argc > 1 && !strcmp(argv[1], "!")) {
@ -42,7 +40,7 @@ main(int argc, char *argv[])
ret = binary(argv[1], argv[2], argv[3]); ret = binary(argv[1], argv[2], argv[3]);
break; break;
default: default:
USAGE(); usage();
} }
if(not) if(not)
ret = !ret; ret = !ret;
@ -56,7 +54,7 @@ unary(const char *op, const char *arg)
int r; int r;
if(op[0] != '-' || op[1] == '\0' || op[2] != '\0') if(op[0] != '-' || op[1] == '\0' || op[2] != '\0')
USAGE(); usage();
switch(op[1]) { switch(op[1]) {
case 'b': case 'c': case 'd': case 'f': case 'g': case 'b': case 'c': case 'd': case 'f': case 'g':
case 'p': case 'S': case 's': case 'u': case 'p': case 'S': case 's': case 'u':
@ -99,7 +97,7 @@ unary(const char *op, const char *arg)
case 'z': case 'z':
return arg[0] == '\0'; return arg[0] == '\0';
default: default:
USAGE(); usage();
} }
return false; /* should not reach */ return false; /* should not reach */
} }
@ -112,7 +110,7 @@ binary(const char *arg1, const char *op, const char *arg2)
} }
void void
testusage(void) usage(void)
{ {
const char *ket = (*argv0 == '[') ? " ]" : ""; const char *ket = (*argv0 == '[') ? " ]" : "";

1
util.h
View File

@ -40,4 +40,3 @@ long estrtol(const char *, int);
void fnck(const char *, const char *, int (*)(const char *, const char *)); void fnck(const char *, const char *, int (*)(const char *, const char *));
void putword(const char *); void putword(const char *);
void recurse(const char *, void (*)(const char *)); void recurse(const char *, void (*)(const char *));
void usage(const char *);

View File

@ -29,13 +29,6 @@ enprintf(int status, const char *fmt, ...)
va_end(ap); va_end(ap);
} }
void
usage(const char *s)
{
fprintf(stderr, "usage: %s %s\n", argv0, s);
exit(EXIT_FAILURE);
}
void void
venprintf(int status, const char *fmt, va_list ap) venprintf(int status, const char *fmt, va_list ap)
{ {

12
yes.c
View File

@ -3,7 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "util.h" #include "util.h"
#define USAGE() usage("[string]") static void usage(void);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -12,7 +12,7 @@ main(int argc, char *argv[])
ARGBEGIN { ARGBEGIN {
default: default:
USAGE(); usage();
} ARGEND; } ARGEND;
switch(argc) { switch(argc) {
@ -24,7 +24,13 @@ main(int argc, char *argv[])
puts(s); puts(s);
break; break;
default: default:
USAGE(); usage();
} }
return EXIT_FAILURE; /* should not reach */ return EXIT_FAILURE; /* should not reach */
} }
void
usage(void)
{
eprintf("usage: %s [string]\n", argv0);
}