~sircmpwn/ctools

2068266c66b123159ff7210ca09c3ccc407b431c — Drew DeVault 2 years ago 216800a
all: swap usage strings for usage functions
7 files changed, 71 insertions(+), 31 deletions(-)

M src/basename.c
M src/cat.c
M src/chgrp.c
M src/chmod.c
M src/cmp.c
M src/uname.c
M src/unlink.c
M src/basename.c => src/basename.c +7 -3
@@ 5,19 5,23 @@
#include <string.h>
#include <time.h>

const char usage[] = "usage: basename string [suffix]\n";
static void
usage(void)
{
	fprintf(stderr, "usage: basename string [suffix]\n");
}

int
main(int argc, char *argv[])
{
	if (getopt(argc, argv, "") != -1) {
		fprintf(stderr, usage);
		usage();
		return 1;
	}
	argc -= optind;
	argv += optind;
	if (argc <= 0 || argc > 2) {
		fprintf(stderr, usage);
		usage();
		return 1;
	}


M src/cat.c => src/cat.c +7 -1
@@ 5,6 5,12 @@
#include <getopt.h>
#include <unistd.h>

static void
usage(void)
{
	fprintf(stderr, "usage: cat [-u] [file...]\n");
}

static int
cat(const char *path)
{


@@ 54,7 60,7 @@ main(int argc, char *argv[])
			 * going to do anyway. */
			break;
		default:
			fprintf(stderr, "usage: cat [-u] [file...]\n");
			usage();
			return 1;
		}
	}

M src/chgrp.c => src/chgrp.c +21 -11
@@ 16,11 16,6 @@
#include <time.h>
#include <unistd.h>

static const char
	chgrp_usage[] = "usage: chgrp <[-h] | -R [-H|-L|-P]> group file...\n",
	chown_usage[] = "usage: chown <[-h] | -R [-H|-L|-P]> owner[:group] file...\n",
	mutex[] = "%s: -h and -R are mutually exclusive\n";

enum program_mode {
	MODE_CHOWN,
	MODE_CHGRP,


@@ 41,6 36,21 @@ struct context {
	gid_t gid;
};

static void
usage(enum program_mode mode)
{
	switch (mode) {
	case MODE_CHOWN:
		fprintf(stderr, "usage: chgrp <[-h] | -R [-H|-L|-P]> "
				"group file...\n");
		break;
	case MODE_CHGRP:
		fprintf(stderr, "usage: chown <[-h] | -R [-H|-L|-P]> "
				"owner[:group] file...\n");
		break;
	}
}

static int
process(const char *path, struct context *ctx, int depth)
{


@@ 142,14 152,16 @@ main(int argc, char *argv[])
		switch (c) {
		case 'h':
			if (ctx.recursive) {
				fprintf(stderr, mutex, argv[0]);
				fprintf(stderr, "%s: -h and -R are mutually "
						"exclusive\n", argv[0]);
				return 1;
			}
			ctx.explicit_links = true;
			break;
		case 'R':
			if (ctx.explicit_links) {
				fprintf(stderr, mutex, argv[0]);
				fprintf(stderr, "%s: -h and -R are mutually "
						"exclusive\n", argv[0]);
				return 1;
			}
			ctx.recursive = true;


@@ 164,8 176,7 @@ main(int argc, char *argv[])
			ctx.symlinks = UPDATE_LINKS;
			break;
		default:
			fprintf(stderr, ctx.mode == MODE_CHGRP ?
					chgrp_usage : chown_usage);
			usage(ctx.mode);
			return 1;
		}
	}


@@ 181,8 192,7 @@ main(int argc, char *argv[])
	argv = &argv[optind];

	if (argc < 2) {
		fprintf(stderr, ctx.mode == MODE_CHGRP ?
				chgrp_usage : chown_usage);
		usage(ctx.mode);
		return 1;
	}


M src/chmod.c => src/chmod.c +7 -3
@@ 11,7 11,11 @@
#include <sys/types.h>
#include <time.h>

static const char usage[] = "usage: chmod -R mode file...\n";
static void
usage(void)
{
	fprintf(stderr, "usage: chmod -R mode file...\n");
}

enum mode_operation {
	MODE_SET, /* chmod 777 */


@@ 217,13 221,13 @@ main(int argc, char *argv[])
			recursive = true;
			break;
		default:
			fprintf(stderr, usage);
			usage();
			return 1;
		}
	}

	if (optind + 1 >= argc) {
		fprintf(stderr, usage);
		usage();
		return 1;
	}


M src/cmp.c => src/cmp.c +15 -11
@@ 7,11 7,11 @@
#include <time.h>
#include <unistd.h>

const char usage[] = "usage: cmp [-l|-s] file1 file2\n",
      mutex[] = "cmp: -l and -s are mutually exclusive\n",
      both_stdin[] = "cmp: behavior is unspecified when both inputs are stdin\n",
      special[] = "cmp: behavior is unspecified when both inputs are the "
	      "same special file\n";
static void
usage(void)
{
	fprintf(stderr, "usage: cmp [-l|-s] file1 file2\n");
}

enum program_mode {
	DEFAULT,


@@ 38,26 38,28 @@ main(int argc, char *argv[])
		switch (c) {
		case 'l':
			if (mode != DEFAULT) {
				fprintf(stderr, mutex);
				fprintf(stderr, "cmp: -l and -s are mutually "
						"exclusive\n");
				return 2;
			}
			mode = LOUD;
			break;
		case 's':
			if (mode != DEFAULT) {
				fprintf(stderr, mutex);
				fprintf(stderr, "cmp: -l and -s are mutually "
						"exclusive\n");
				return 2;
			}
			mode = SILENT;
			break;
		default:
			fprintf(stderr, usage);
			usage();
			return 2;
		}
	}

	if (argc - optind != 2) {
		fprintf(stderr, usage);
		usage();
		return 2;
	}



@@ 72,7 74,8 @@ main(int argc, char *argv[])
	}

	if (fd1 == STDIN_FILENO && fd2 == STDIN_FILENO) {
		fprintf(stderr, both_stdin);
		fprintf(stderr, "cmp: behavior is unspecified when both inputs "
				"are stdin\n");
		return 2;
	}



@@ 83,7 86,8 @@ main(int argc, char *argv[])
		}
		if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)
				|| S_ISFIFO(st.st_mode)) {
			fprintf(stderr, special);
			fprintf(stderr, "cmp: behavior is unspecified when "
					"both inputs are the same special file\n");
			return 2;
		}
	}

M src/uname.c => src/uname.c +7 -1
@@ 3,6 3,12 @@
#include <sys/utsname.h>
#include <unistd.h>

static void
usage(void)
{
	fprintf(stderr, "usage: uname [-amnrsv]\n");
}

enum uname_fields {
	FIELD_SYSNAME = 1 << 0,
	FIELD_NODENAME = 1 << 1,


@@ 61,7 67,7 @@ main(int argc, char *argv[])
			fields |= FIELD_VERSION;
			break;
		default:
			fprintf(stderr, "usage: uname [-amnrsv]\n");
			usage();
			return 1;
		}
	}

M src/unlink.c => src/unlink.c +7 -1
@@ 1,11 1,17 @@
#include <stdio.h>
#include <unistd.h>

static void
usage(void)
{
	fprintf(stderr, "usage: unlink <file>\n");
}

int
main(int argc, char *argv[])
{
	if (getopt(argc, argv, "") != -1 || optind != argc - 1) {
		fprintf(stderr, "usage: unlink <file>\n");
		usage();
		return 1;
	}