Compare commits

...

3 Commits

6 changed files with 36 additions and 26 deletions

View File

@ -146,11 +146,13 @@ parse(char *s) {
} }
static int static int
usage(char *s) { usage(char *argv0) {
fprintf( (void)fprintf(
stderr, "Usage: %s [-Hn] [-a byte] [-c count]\n" stderr,
"Usage: %s [-Hn] [-a byte] [-c count]\n"
"\t[-i file] [-b block_size] [-s offset]\n" "\t[-i file] [-b block_size] [-s offset]\n"
"\t[-o file] [-B block_size] [-S offset]\n", s "\t[-o file] [-B block_size] [-S offset]\n",
argv0
); );
return EX_USAGE; return EX_USAGE;

View File

@ -32,8 +32,9 @@
char *program_name = "intcmp"; char *program_name = "intcmp";
int usage(char *s) { static int
fprintf(stderr, "Usage: %s [-egl] integer integer...\n", s); usage(char *argv0) {
(void)fprintf(stderr, "Usage: %s [-egl] integer integer...\n", argv0);
return EX_USAGE; return EX_USAGE;
} }

View File

@ -26,8 +26,6 @@
#include <unistd.h> /* getopt(3) */ #include <unistd.h> /* getopt(3) */
#include <sysexits.h> /* EX_IOERR, EX_OK, EX_OSERR, EX_USAGE */ #include <sysexits.h> /* EX_IOERR, EX_OK, EX_OSERR, EX_USAGE */
extern int errno;
/* This structure is how open files are tracked. */ /* This structure is how open files are tracked. */
struct Files{ struct Files{
size_t a; /* allocation */ size_t a; /* allocation */
@ -109,11 +107,13 @@ oserr(char *s, char *r) {
} \ } \
return retval return retval
/* Prints a usage text, in which s is the program being run (i.e. argv[0]), and static int
* returns an exit status appropriate for a usage error. */ usage(char *argv0) {
int usage(char *s) { (void)fprintf(
stderr,
fprintf(stderr, "Usage: %s [-aenu] [-i input]... [-o output]...\n", s); "Usage: %s [-aenu] [-i input]... [-o output]...\n",
argv0
);
return EX_USAGE; return EX_USAGE;
} }
@ -149,9 +149,9 @@ int main(int argc, char *argv[]) {
k = 0; k = 0;
if (argc > 0) { program_name = argv[0]; } if (argc > 0) {
program_name = argv[0];
if (argc > 1) {
while ((c = getopt(argc, argv, "aehi:no:u")) != -1) { while ((c = getopt(argc, argv, "aehi:no:u")) != -1) {
switch (c){ switch (c){
case 'a': /* "rb+" -> "ab" */ case 'a': /* "rb+" -> "ab" */

View File

@ -25,8 +25,10 @@
char *program_name = "npc"; char *program_name = "npc";
int usage(char *s) { static int
fprintf(stderr, "Usage: %s [-et]\n", s); usage(char *argv0) {
fprintf(stderr, "Usage: %s [-et]\n", argv0);
return EX_USAGE; return EX_USAGE;
} }

View File

@ -27,15 +27,18 @@
* S_ISUID, S_ISVTX */ * S_ISUID, S_ISVTX */
char *program_name = "scrut"; char *program_name = "scrut";
static char args[] = "bcdefgkprsuwxLS"; #define OPTS "bcdefgkprsuwxLS"
static char *opts = OPTS;
static int
usage(char *argv0) {
(void)fprintf(stderr, "Usage: %s [-" OPTS "] file...\n", argv0);
int usage(char *s) {
fprintf(stderr, "Usage: %s [-%s] file...\n", s, args);
return EX_USAGE; return EX_USAGE;
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char sel[(sizeof args) / (sizeof *args)]; char sel[(sizeof opts) / (sizeof *opts)];
program_name = (argv[0] == NULL ? program_name : argv[0]); program_name = (argv[0] == NULL ? program_name : argv[0]);
if (argc < 2) { return usage(program_name); } if (argc < 2) { return usage(program_name); }
@ -44,9 +47,9 @@ int main(int argc, char *argv[]) {
char *p; char *p;
memset(sel, '\0', sizeof sel); memset(sel, '\0', sizeof sel);
for (int c; (c = getopt(argc, argv, args)) != -1;) { for (int c; (c = getopt(argc, argv, opts)) != -1;) {
if ((p = strchr(args, c)) == NULL) { return usage(program_name); } if ((p = strchr(opts, c)) == NULL) { return usage(program_name); }
else { sel[p - args] = c; } else { sel[p - opts] = c; }
} }
/* straighten out selections; permute out nulls */ /* straighten out selections; permute out nulls */

View File

@ -45,8 +45,10 @@ static struct {
{ NULL, NULL } /* marks end */ { NULL, NULL } /* marks end */
}; };
int usage(char *s){ static int
fprintf(stderr, "Usage: %s type string...\n", s); usage(char *argv0){
(void)fprintf(stderr, "Usage: %s type string...\n", argv0);
return EX_USAGE; return EX_USAGE;
} }