svn commit: trunk/busybox/coreutils

vda at busybox.net vda at busybox.net
Sat Mar 31 10:19:12 UTC 2007


Author: vda
Date: 2007-03-31 03:19:11 -0700 (Sat, 31 Mar 2007)
New Revision: 18286

Log:
uniq: getopt32-ization. -38 bytes.


Modified:
   trunk/busybox/coreutils/uniq.c


Changeset:
Modified: trunk/busybox/coreutils/uniq.c
===================================================================
--- trunk/busybox/coreutils/uniq.c	2007-03-31 10:17:24 UTC (rev 18285)
+++ trunk/busybox/coreutils/uniq.c	2007-03-31 10:19:11 UTC (rev 18286)
@@ -12,13 +12,14 @@
 
 #include "busybox.h"
 
-static const char uniq_opts[] = "f:s:" "cdu\0\1\2\4";
+static const char uniq_opts[] = "cdu" "f:s:" "cdu\0\1\2\4";
 
 static FILE *xgetoptfile_uniq_s(char **argv, int read0write2)
 {
 	const char *n;
 
-	if ((n = *argv) != NULL) {
+	n = *argv;
+	if (n != NULL) {
 		if ((*n != '-') || n[1]) {
 			return xfopen(n, "r\0w" + read0write2);
 		}
@@ -30,29 +31,29 @@
 int uniq_main(int argc, char **argv)
 {
 	FILE *in, *out;
-	unsigned long dups, skip_fields, skip_chars, i, uniq_flags;
+	unsigned long dups, skip_fields, skip_chars, i;
 	const char *s0, *e0, *s1, *e1, *input_filename;
-	int opt;
+	unsigned opt;
 
-	uniq_flags = skip_fields = skip_chars = 0;
+	enum {
+		OPT_c = 0x1,
+		OPT_d = 0x2,
+		OPT_u = 0x4,
+		OPT_f = 0x8,
+		OPT_s = 0x10,
+	};
 
-	while ((opt = getopt(argc, argv, uniq_opts)) > 0) {
-		if ((opt == 'f') || (opt == 's')) {
-			unsigned long t = xatoul(optarg);
-			if (opt == 'f') {
-				skip_fields = t;
-			} else {
-				skip_chars = t;
-			}
-		} else if ((s0 = strchr(uniq_opts, opt)) != NULL) {
-			uniq_flags |= s0[4];
-		} else {
-			bb_show_usage();
-		}
-	}
+	skip_fields = skip_chars = 0;
 
-	input_filename = *(argv += optind);
+	opt = getopt32(argc, argv, "cduf:s:", &s0, &s1);
+	if (opt & OPT_f)
+		skip_fields = xatoul(s0);
+	if (opt & OPT_s)
+		skip_chars = xatoul(s1);
+	argv += optind;
 
+	input_filename = *argv;
+
 	in = xgetoptfile_uniq_s(argv, 0);
 	if (*argv) {
 		++argv;
@@ -90,8 +91,8 @@
 		}
 
 		if (s0) {
-			if (!(uniq_flags & (2 << !!dups))) {
-				fprintf(out, "\0%d " + (uniq_flags & 1), dups + 1);
+			if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_e) */
+				fprintf(out, "\0%d " + (opt & 1), dups + 1);
 				fprintf(out, "%s\n", s0);
 			}
 			free((void *)s0);




More information about the busybox-cvs mailing list