svn commit: r272198 - head/usr.bin/mkimg

Marcel Moolenaar marcel at FreeBSD.org
Sat Sep 27 04:53:52 UTC 2014


Author: marcel
Date: Sat Sep 27 04:53:51 2014
New Revision: 272198
URL: http://svnweb.freebsd.org/changeset/base/272198

Log:
  Add 3 long options for getting information about mkimg itself:
  --version	print the version of mkimg and also whether it's
  		64- or 32-bit.
  --formats	list the supported output formats separated by space.
  --schemes	list the supported partitioning schemes separated by
  		space.
  
  Inspired by a patch from: gjb@
  
  MFC after:	1 week
  Relnotes:	yes

Modified:
  head/usr.bin/mkimg/Makefile
  head/usr.bin/mkimg/mkimg.c

Modified: head/usr.bin/mkimg/Makefile
==============================================================================
--- head/usr.bin/mkimg/Makefile	Sat Sep 27 03:43:49 2014	(r272197)
+++ head/usr.bin/mkimg/Makefile	Sat Sep 27 04:53:51 2014	(r272198)
@@ -6,6 +6,8 @@ PROG=	mkimg
 SRCS=	format.c image.c mkimg.c scheme.c
 MAN=	mkimg.1
 
+MKIMG_VERSION=20140926
+CFLAGS+=-DMKIMG_VERSION=${MKIMG_VERSION}
 CFLAGS+=-DSPARSE_WRITE
 
 # List of formats to support

Modified: head/usr.bin/mkimg/mkimg.c
==============================================================================
--- head/usr.bin/mkimg/mkimg.c	Sat Sep 27 03:43:49 2014	(r272197)
+++ head/usr.bin/mkimg/mkimg.c	Sat Sep 27 04:53:51 2014	(r272198)
@@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$");
 #include <errno.h>
 #include <err.h>
 #include <fcntl.h>
+#include <getopt.h>
 #include <libutil.h>
 #include <limits.h>
 #include <stdio.h>
@@ -48,6 +49,17 @@ __FBSDID("$FreeBSD$");
 #include "mkimg.h"
 #include "scheme.h"
 
+#define	LONGOPT_FORMATS	0x01000001
+#define	LONGOPT_SCHEMES	0x01000002
+#define	LONGOPT_VERSION	0x01000003
+
+static struct option longopts[] = {
+	{ "formats", no_argument, NULL, LONGOPT_FORMATS },
+	{ "schemes", no_argument, NULL, LONGOPT_SCHEMES },
+	{ "version", no_argument, NULL, LONGOPT_VERSION },
+	{ NULL, 0, NULL, 0 }
+};
+
 struct partlisthead partlist = STAILQ_HEAD_INITIALIZER(partlist);
 u_int nparts = 0;
 
@@ -61,10 +73,69 @@ u_int secsz = 512;
 u_int blksz = 0;
 
 static void
-usage(const char *why)
+print_formats(int usage)
 {
 	struct mkimg_format *f, **f_iter;
+	const char *sep;
+
+	if (usage) {
+		fprintf(stderr, "\n    formats:\n");
+		SET_FOREACH(f_iter, formats) {
+			f = *f_iter;
+			fprintf(stderr, "\t%s\t-  %s\n", f->name,
+			    f->description);
+		}
+	} else {
+		sep = "";
+		SET_FOREACH(f_iter, formats) {
+			f = *f_iter;
+			printf("%s%s", sep, f->name);
+			sep = " ";
+		}
+		putchar('\n');
+	}
+}
+
+static void
+print_schemes(int usage)
+{
 	struct mkimg_scheme *s, **s_iter;
+	const char *sep;
+
+	if (usage) {
+		fprintf(stderr, "\n    schemes:\n");
+		SET_FOREACH(s_iter, schemes) {
+			s = *s_iter;
+			fprintf(stderr, "\t%s\t-  %s\n", s->name,
+			    s->description);
+		}
+	} else {
+		sep = "";
+		SET_FOREACH(s_iter, schemes) {
+			s = *s_iter;
+			printf("%s%s", sep, s->name);
+			sep = " ";
+		}
+		putchar('\n');
+	}
+}
+
+static void
+print_version(void)
+{
+	u_int width;
+
+#ifdef __LP64__
+	width = 64;
+#else
+	width = 32;
+#endif
+	printf("mkimg %u (%u-bit)\n", MKIMG_VERSION, width);
+}
+
+static void
+usage(const char *why)
+{
 
 	warnx("error: %s", why);
 	fprintf(stderr, "\nusage: %s <options>\n", getprogname());
@@ -82,17 +153,8 @@ usage(const char *why)
 	fprintf(stderr, "\t-S <num>\t-  logical sector size\n");
 	fprintf(stderr, "\t-T <num>\t-  number of tracks to simulate\n");
 
-	fprintf(stderr, "\n    formats:\n");
-	SET_FOREACH(f_iter, formats) {
-		f = *f_iter;
-		fprintf(stderr, "\t%s\t-  %s\n", f->name, f->description);
-	}
-
-	fprintf(stderr, "\n    schemes:\n");
-	SET_FOREACH(s_iter, schemes) {
-		s = *s_iter;
-		fprintf(stderr, "\t%s\t-  %s\n", s->name, s->description);
-	}
+	print_formats(1);
+	print_schemes(1);
 
 	fprintf(stderr, "\n    partition specification:\n");
 	fprintf(stderr, "\t<t>[/<l>]::<size>\t-  empty partition of given "
@@ -366,7 +428,8 @@ main(int argc, char *argv[])
 
 	bcfd = -1;
 	outfd = 1;	/* Write to stdout by default */
-	while ((c = getopt(argc, argv, "b:f:o:p:s:vyH:P:S:T:")) != -1) {
+	while ((c = getopt_long(argc, argv, "b:f:o:p:s:vyH:P:S:T:",
+	    longopts, NULL)) != -1) {
 		switch (c) {
 		case 'b':	/* BOOT CODE */
 			if (bcfd != -1)
@@ -432,6 +495,18 @@ main(int argc, char *argv[])
 			if (error)
 				errc(EX_DATAERR, error, "track size");
 			break;
+		case LONGOPT_FORMATS:
+			print_formats(0);
+			exit(EX_OK);
+			/*NOTREACHED*/
+		case LONGOPT_SCHEMES:
+			print_schemes(0);
+			exit(EX_OK);
+			/*NOTREACHED*/
+		case LONGOPT_VERSION:
+			print_version();
+			exit(EX_OK);
+			/*NOTREACHED*/
 		default:
 			usage("unknown option");
 		}


More information about the svn-src-head mailing list