[BusyBox-cvs] busybox/coreutils cut.c,1.25,1.26 date.c,1.39,1.40 dd.c,1.53,1.54 df.c,1.52,1.53 du.c,1.57,1.58 env.c,1.5,1.6 mkdir.c,1.34,1.35 mv.c,1.18,1.19 rm.c,1.36,1.37 stty.c,1.8,1.9 test.c,1.22,1.23

Erik Andersen andersen at busybox.net
Fri Jun 20 09:02:32 UTC 2003


Update of /var/cvs/busybox/coreutils
In directory winder:/tmp/cvs-serv14650/coreutils

Modified Files:
	cut.c date.c dd.c df.c du.c env.c mkdir.c mv.c rm.c stty.c 
	test.c 
Log Message:
last_patch89 from vodz:

    Manuel,

    I rewrite bb_getopt_ulflags() function for more universal usage.
    My version support now:
    - options with arguments (optional arg as GNU extension also)
    - complementaly and/or incomplementaly and/or incongruously and/or list
    options
    - long_opt (all applets may have long option, add supporting is trivial)
    This realisation full compatibile from your version.
    Code size grow 480 bytes, but only coreutils/* over compensate this size
    after using new function. Last patch reduced over 800 bytes and not full
    applied to all. "mkdir" and "mv" applets have long_opt now for demonstrate
    trivial addition support long_opt with usage new bb_getopt_ulflags().
    Complementaly and/or incomplementaly and/or incongruously and/or list options
    logic is not trivial, but new "cut" and "grep" applets using this logic
    for examples with full demostrating. New "grep" applet reduced over 300
    bytes.

    Mark,
    Also. I removed bug from "grep" applet.
    $ echo a b | busybox grep -e a b
    a b
    a b
    But right is printing one only.

    --w
    vodz



Index: cut.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/cut.c,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- cut.c	19 Mar 2003 09:11:32 -0000	1.25
+++ cut.c	20 Jun 2003 09:01:52 -0000	1.26
@@ -22,20 +22,22 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h> /* getopt */
+#include <getopt.h>
+#include <unistd.h>
 #include <string.h>
 #include <limits.h>
 #include "busybox.h"
 
 
-/* globals from other files */
-extern int optind;
-extern char *optarg;
-
-
 /* option vars */
-static char part = 0; /* (b)yte, (c)har, (f)ields */
-static unsigned int supress_non_delimited_lines = 0;
+static const char optstring[] = "b:c:f:d:sn";
+#define OPT_BYTE_FLGS    1
+#define OPT_CHAR_FLGS    2
+#define OPT_FIELDS_FLGS  4
+#define OPT_DELIM_FLGS   8
+#define OPT_SUPRESS_FLGS 16
+static char part; /* (b)yte, (c)har, (f)ields */
+static unsigned int supress_non_delimited_lines;
 static char delim = '\t'; /* delimiter, default is tab */
 
 struct cut_list {
@@ -270,11 +272,11 @@
 	while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
 
 		/* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
-		if (part == 'c' || part == 'b')
+		if ((part & (OPT_CHAR_FLGS | OPT_BYTE_FLGS)))
 			cut_line_by_chars(line);
 
 		/* cut based on fields */
-		else if (part == 'f') {
+		else {
 			if (delim == '\n')
 				cut_file_by_lines(line, linenum);
 			else
@@ -289,46 +291,32 @@
 
 extern int cut_main(int argc, char **argv)
 {
-	int opt;
+	unsigned long opt;
+	char *sopt, *sdopt;
 
-	while ((opt = getopt(argc, argv, "b:c:d:f:ns")) > 0) {
-		switch (opt) {
-			case 'b':
-			case 'c':
-			case 'f':
-				/* make sure they didn't ask for two types of lists */
-				if (part != 0) {
+	bb_opt_complementaly = "b~bcf:c~bcf:f~bcf";
+	opt = bb_getopt_ulflags(argc, argv, optstring, &sopt, &sopt, &sopt, &sdopt);
+	part = opt & (OPT_BYTE_FLGS|OPT_CHAR_FLGS|OPT_FIELDS_FLGS);
+	if(part == 0)
+		bb_error_msg_and_die("you must specify a list of bytes, characters, or fields");
+	if(opt & 0x80000000UL)
 					bb_error_msg_and_die("only one type of list may be specified");
-				}
-				part = (char)opt;
-				parse_lists(optarg);
-				break;
-			case 'd':
-				if (strlen(optarg) > 1) {
+	parse_lists(sopt);
+	if((opt & (OPT_DELIM_FLGS))) {
+		if (strlen(sdopt) > 1) {
 					bb_error_msg_and_die("the delimiter must be a single character");
 				}
-				delim = optarg[0];
-				break;
-			case 'n':
-				/* no-op */
-				break;
-			case 's':
-				supress_non_delimited_lines++;
-				break;
-		}
-	}
-
-	if (part == 0) {
-		bb_error_msg_and_die("you must specify a list of bytes, characters, or fields");
+		delim = sdopt[0];
 	}
+	supress_non_delimited_lines = opt & OPT_SUPRESS_FLGS;
 
 	/*  non-field (char or byte) cutting has some special handling */
-	if (part != 'f') {
+	if (part != OPT_FIELDS_FLGS) {
 		if (supress_non_delimited_lines) {
 			bb_error_msg_and_die("suppressing non-delimited lines makes sense"
 					" only when operating on fields");
 		}
-		if (delim != '\t' && part != 'f') {
+		if (delim != '\t') {
 			bb_error_msg_and_die("a delimiter may be specified only when operating on fields");
 		}
 	}

Index: date.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/date.c,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -d -r1.39 -r1.40
--- date.c	19 Mar 2003 09:11:32 -0000	1.39
+++ date.c	20 Jun 2003 09:01:52 -0000	1.40
@@ -120,74 +120,65 @@
 	char *date_str = NULL;
 	char *date_fmt = NULL;
 	char *t_buff;
-	int c;
-	int set_time = 0;
-	int rfc822 = 0;
-	int utc = 0;
+	int set_time;
+	int rfc822;
+	int utc;
 	int use_arg = 0;
 	time_t tm;
+	unsigned long opt;
 	struct tm tm_time;
 
 #ifdef CONFIG_FEATURE_DATE_ISOFMT
 	int ifmt = 0;
+	char *isofmt_arg;
 
 # define GETOPT_ISOFMT  "I::"
 #else
 # define GETOPT_ISOFMT
 #endif
-
-	/* Interpret command line args */
-	while ((c = getopt(argc, argv, "Rs:ud:" GETOPT_ISOFMT)) != EOF) {
-		switch (c) {
-		case 'R':
-			rfc822 = 1;
-			break;
-		case 's':
-			set_time = 1;
-			if ((date_str != NULL) || ((date_str = optarg) == NULL)) {
-				bb_show_usage();
-			}
-			break;
-		case 'u':
-			utc = 1;
+	bb_opt_complementaly = "d~ds:s~ds";
+	opt = bb_getopt_ulflags(argc, argv, "Rs:ud:" GETOPT_ISOFMT,
+					&date_str, &date_str
+#ifdef CONFIG_FEATURE_DATE_ISOFMT
+					, &isofmt_arg
+#endif
+					);
+	rfc822 = opt & 1;
+	set_time = opt & 2;
+	utc = opt & 4;
+	if(utc) {
 			if (putenv("TZ=UTC0") != 0)
 				bb_error_msg_and_die(bb_msg_memory_exhausted);
-			break;
-		case 'd':
-			use_arg = 1;
-			if ((date_str != NULL) || ((date_str = optarg) == NULL))
+	}
+	use_arg = opt & 8;
+	if(opt & 0x80000000UL)
 				bb_show_usage();
-			break;
 #ifdef CONFIG_FEATURE_DATE_ISOFMT
-		case 'I':
-			if (!optarg)
+	if(opt & 16) {
+		if (!isofmt_arg)
 				ifmt = 1;
 			else {
-				int ifmt_len = bb_strlen(optarg);
+			int ifmt_len = bb_strlen(isofmt_arg);
 
 				if ((ifmt_len <= 4)
-					&& (strncmp(optarg, "date", ifmt_len) == 0)) {
+				&& (strncmp(isofmt_arg, "date", ifmt_len) == 0)) {
 					ifmt = 1;
 				} else if ((ifmt_len <= 5)
-						   && (strncmp(optarg, "hours", ifmt_len) == 0)) {
+					   && (strncmp(isofmt_arg, "hours", ifmt_len) == 0)) {
 					ifmt = 2;
 				} else if ((ifmt_len <= 7)
-						   && (strncmp(optarg, "minutes", ifmt_len) == 0)) {
+					   && (strncmp(isofmt_arg, "minutes", ifmt_len) == 0)) {
 					ifmt = 3;
 				} else if ((ifmt_len <= 7)
-						   && (strncmp(optarg, "seconds", ifmt_len) == 0)) {
+					   && (strncmp(isofmt_arg, "seconds", ifmt_len) == 0)) {
 					ifmt = 4;
 				}
 			}
-			if (ifmt) {
-				break;	/* else bb_show_usage(); */
-			}
-#endif
-		default:
+		if (!ifmt) {
 			bb_show_usage();
 		}
 	}
-
+#endif
 
 	if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
 		date_fmt = &argv[optind][1];	/* Skip over the '+' */

Index: dd.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/dd.c,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -d -r1.53 -r1.54
--- dd.c	19 Mar 2003 09:11:33 -0000	1.53
+++ dd.c	20 Jun 2003 09:01:52 -0000	1.54
@@ -106,9 +106,7 @@
 	buf = xmalloc(bs);
 
 	if (infile != NULL) {
-		if ((ifd = open(infile, O_RDONLY)) < 0) {
-			bb_perror_msg_and_die("%s", infile);
-		}
+		ifd = bb_xopen(infile, O_RDONLY);
 	} else {
 		ifd = STDIN_FILENO;
 		infile = bb_msg_standard_input;

Index: df.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/df.c,v
retrieving revision 1.52
retrieving revision 1.53
diff -u -d -r1.52 -r1.53
--- df.c	23 Mar 2003 20:27:33 -0000	1.52
+++ df.c	20 Jun 2003 09:01:52 -0000	1.53
@@ -55,41 +55,27 @@
 	unsigned long df_disp_hr = KILOBYTE; 
 #endif
 	int status = EXIT_SUCCESS;
-	int opt;
+	unsigned long opt;
 	FILE *mount_table;
 	struct mntent *mount_entry;
 	struct statfs s;
 	static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */
 	const char *disp_units_hdr = hdr_1k;
 
-	while ((opt = getopt(argc, argv, "k"
-#ifdef CONFIG_FEATURE_HUMAN_READABLE
-	"hm"
-#endif
-)) > 0)
-	{
-		switch (opt) {
 #ifdef CONFIG_FEATURE_HUMAN_READABLE
-			case 'h':
+	bb_opt_complementaly = "h-km:k-hm:m-hk";
+	opt = bb_getopt_ulflags(argc, argv, "hmk");
+	if(opt & 1) {
 				df_disp_hr = 0;
 				disp_units_hdr = "     Size";
-				break;
-			case 'm':
+	}
+	if(opt & 2) {
 				df_disp_hr = MEGABYTE;
 				disp_units_hdr = "1M-blocks";
-				break;
-#endif
-			case 'k':
-				/* default display is kilobytes */
-#ifdef CONFIG_FEATURE_HUMAN_READABLE
-				df_disp_hr = KILOBYTE;
-				disp_units_hdr =  hdr_1k;
-#endif
-				break;
-			default:
-					  bb_show_usage();
-		}
 	}
+#else
+	opt = bb_getopt_ulflags(argc, argv, "k");
+#endif
 
 	bb_printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n",
 			  "", disp_units_hdr);

Index: du.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/du.c,v
retrieving revision 1.57
retrieving revision 1.58
diff -u -d -r1.57 -r1.58
--- du.c	26 May 2003 14:05:58 -0000	1.57
+++ du.c	20 Jun 2003 09:01:52 -0000	1.58
@@ -166,8 +166,9 @@
 {
 	long total;
 	int slink_depth_save;
-	int print_final_total = 0;
-	int c;
+	int print_final_total;
+	char *smax_print_depth;
+	unsigned long opt;
 
 #ifdef CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K
 	if (getenv("POSIXLY_CORRECT")) {	/* TODO - a new libbb function? */
@@ -185,57 +186,57 @@
 	 * gnu du exits with an error code in this case.  We choose to simply
 	 * ignore -a.  This is consistent with -s being equivalent to -d 0.
 	 */
-
-	while ((c = getopt(argc, argv, "aHkLsx" "d:" "lc"
-#ifdef CONFIG_FEATURE_HUMAN_READABLE
-					   "hm"
-#endif
-					   )) > 0) {
-		switch (c) {
-		case 'a':
-			print_files = INT_MAX;
-			break;
-		case 'H':
-			slink_depth = 1;
-			break;
-		case 'k':
 #ifdef CONFIG_FEATURE_HUMAN_READABLE
+	bb_opt_complementaly = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s";
+	opt = bb_getopt_ulflags(argc, argv, "aHkLsx" "d:" "lc" "hm", &smax_print_depth);
+	if((opt & (1 << 9))) {
+		/* -h opt */
+		disp_hr = 0;
+	}
+	if((opt & (1 << 10))) {
+		/* -m opt */
+		disp_hr = MEGABYTE;
+	}
+	if((opt & (1 << 2))) {
+		/* -k opt */
 			disp_hr = KILOBYTE;
-#elif !defined CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K
+	}
+#else
+	bb_opt_complementaly = "H-L:L-H:s-d:d-s";
+	opt = bb_getopt_ulflags(argc, argv, "aHkLsx" "d:" "lc", &smax_print_depth);
+#if !defined CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K
+	if((opt & (1 << 2))) {
+		/* -k opt */
 			disp_k = 1;
+	}
 #endif
-			break;
-		case 'L':
+#endif
+	if((opt & (1 << 0))) {
+		/* -a opt */
+		print_files = INT_MAX;
+	}
+	if((opt & (1 << 1))) {
+		/* -H opt */
+		slink_depth = 1;
+	}
+	if((opt & (1 << 3))) {
+		/* -L opt */
 			slink_depth = INT_MAX;
-			break;
-		case 's':
+	}
+	if((opt & (1 << 4))) {
+		/* -s opt */
 			max_print_depth = 0;
-			break;
-		case 'x':
-			one_file_system = 1;
-			break;
-
-		case 'd':
-			max_print_depth = bb_xgetularg10_bnd(optarg, 0, INT_MAX);
-			break;
-		case 'l':
-			count_hardlinks = 1;
-			break;
-		case 'c':
-			print_final_total = 1;
-			break;
-#ifdef CONFIG_FEATURE_HUMAN_READABLE
-		case 'h':
-			disp_hr = 0;
-			break;
-		case 'm':
-			disp_hr = MEGABYTE;
-			break;
-#endif
-		default:
-			bb_show_usage();
 		}
+	one_file_system = opt & (1 << 5); /* -x opt */
+	if((opt & (1 << 6))) {
+		/* -d opt */
+		max_print_depth = bb_xgetularg10_bnd(smax_print_depth, 0, INT_MAX);
 	}
+	if((opt & (1 << 7))) {
+		/* -l opt */
+		count_hardlinks = 1;
+	}
+	print_final_total = opt & (1 << 8); /* -c opt */
 
 	/* go through remaining args (if any) */
 	argv += optind;
@@ -252,7 +253,9 @@
 		total += du(*argv);
 		slink_depth = slink_depth_save;
 	} while (*++argv);
+#ifdef CONFIG_FEATURE_CLEAN_UP
 	reset_ino_dev_hashtable();
+#endif
 
 	if (print_final_total) {
 		print(total, "total");

Index: env.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/env.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- env.c	19 Mar 2003 09:11:33 -0000	1.5
+++ env.c	20 Jun 2003 09:01:53 -0000	1.6
@@ -44,20 +44,13 @@
 {
 	char **ep, *p;
 	char *cleanenv[1] = { NULL };
-	int ch;
+	unsigned long opt;
 
-	while ((ch = getopt(argc, argv, "iu:")) > 0) {
-		switch(ch) {
-		case 'i':
+	opt = bb_getopt_ulflags(argc, argv, "iu:", &p);
+	if(opt & 1)
 			environ = cleanenv;
-			break;
-		case 'u':
-			unsetenv(optarg);
-			break;
-		default:
-			bb_show_usage();
-		}
-	}
+	if(opt & 2)
+		unsetenv(p);
 
 	argv += optind;
 

Index: mkdir.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/mkdir.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -d -r1.34 -r1.35
--- mkdir.c	19 Mar 2003 09:11:33 -0000	1.34
+++ mkdir.c	20 Jun 2003 09:01:53 -0000	1.35
@@ -31,27 +31,33 @@
 
 #include <stdlib.h>
 #include <unistd.h>
+#include <getopt.h>
 #include "busybox.h"
 
+static const struct option mkdir_long_options[] = {
+	{ "mode", 1, NULL, 'm' },
+	{ "parents", 0, NULL, 'p' },
+	{ 0, 0, 0, 0 }
+};
+
 extern int mkdir_main (int argc, char **argv)
 {
 	mode_t mode = (mode_t)(-1);
 	int status = EXIT_SUCCESS;
 	int flags = 0;
-	int opt;
+	unsigned long opt;
+	char *smode;
 
-	while ((opt = getopt (argc, argv, "m:p")) > 0) {
-		if (opt == 'm') {
+	bb_applet_long_options = mkdir_long_options;
+	opt = bb_getopt_ulflags(argc, argv, "m:p", &smode);
+	if(opt & 1) {
 			mode = 0777;
-			if (!bb_parse_mode (optarg, &mode)) {
-				bb_error_msg_and_die ("invalid mode `%s'", optarg);
-			}
-		} else if (opt == 'p') {
-			flags |= FILEUTILS_RECUR;
-		} else {
-			bb_show_usage();
+		if (!bb_parse_mode (smode, &mode)) {
+			bb_error_msg_and_die ("invalid mode `%s'", smode);
 		}
 	}
+	if(opt & 2)
+		flags |= FILEUTILS_RECUR;
 
 	if (optind == argc) {
 		bb_show_usage();

Index: mv.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/mv.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- mv.c	19 Mar 2003 09:11:33 -0000	1.18
+++ mv.c	20 Jun 2003 09:01:53 -0000	1.19
@@ -31,10 +31,21 @@
 #include <dirent.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <getopt.h>
 #include "busybox.h"
 #include "libcoreutils/coreutils.h"
 
-static const char *fmt = "cannot overwrite %sdirectory with %sdirectory";
+static const struct option mv_long_options[] = {
+	{ "interactive", 0, NULL, 'i' },
+	{ "force", 0, NULL, 'f' },
+	{ 0, 0, 0, 0 }
+};
+
+static const char mv_getopt_short_option[] = "fi";
+#define OPT_FILEUTILS_FORCE       1
+#define OPT_FILEUTILS_INTERACTIVE 2
+
+static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";
 
 extern int mv_main(int argc, char **argv)
 {
@@ -44,20 +55,12 @@
 	const char *dest;
 	int dest_exists;
 	int source_exists;
-	int opt;
-	int flags = 0;
+	unsigned long flags;
 	int status = 0;
 
-	while ((opt = getopt(argc, argv, "fi")) > 0) {
-		flags &= ~(FILEUTILS_INTERACTIVE | FILEUTILS_FORCE);
-		if (opt == 'i') {
-			flags |= FILEUTILS_INTERACTIVE;
-		} else if (opt == 'f') {
-			flags |= FILEUTILS_FORCE;
-		} else {
-			bb_show_usage();
-		}
-	}
+	bb_applet_long_options = mv_long_options;
+	bb_opt_complementaly = "f-i:i-f";
+	flags = bb_getopt_ulflags(argc, argv, mv_getopt_short_option);
 
 	if (optind + 2 > argc)
 		bb_show_usage();
@@ -77,8 +80,7 @@
 	}
 	
 	do {
-		dest = concat_path_file(last,
-								bb_get_last_path_component(*argv));
+		dest = concat_path_file(last, bb_get_last_path_component(*argv));
 
 		if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) {
 			goto RET_1;
@@ -86,9 +88,9 @@
 
 	DO_MOVE:
 		
-		if (dest_exists && !(flags & FILEUTILS_FORCE) &&
+		if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
 			((access(dest, W_OK) < 0 && isatty(0)) ||
-			 (flags & FILEUTILS_INTERACTIVE))) {
+			 (flags & OPT_FILEUTILS_INTERACTIVE))) {
 				 if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) {
 					 goto RET_1;	/* Ouch! fprintf failed! */
 				 }

Index: rm.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/rm.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -d -r1.36 -r1.37
--- rm.c	19 Mar 2003 09:11:33 -0000	1.36
+++ rm.c	20 Jun 2003 09:01:53 -0000	1.37
@@ -36,22 +36,16 @@
 {
 	int status = 0;
 	int flags = 0;
-	int opt;
+	unsigned long opt;
 
-	while ((opt = getopt(argc, argv, "fiRr")) > 0) {
-		if ((opt == 'r') || (opt == 'R')) {
-			flags |= FILEUTILS_RECUR;
-		} else {
-			flags &= ~(FILEUTILS_INTERACTIVE | FILEUTILS_FORCE);
-			if (opt == 'i') {
-				flags |= FILEUTILS_INTERACTIVE;
-			} else if (opt == 'f') {
+	bb_opt_complementaly = "f-i:i-f";
+	opt = bb_getopt_ulflags(argc, argv, "fiRr");
+	if(opt & 1)
 				flags |= FILEUTILS_FORCE;
-			} else {
-				bb_show_usage();
-			}
-		}
-	}
+	if(opt & 2)
+		flags |= FILEUTILS_INTERACTIVE;
+	if(opt & 12)
+		flags |= FILEUTILS_RECUR;
 
 	if (*(argv += optind) != NULL) {
 		do {

Index: stty.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/stty.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- stty.c	19 Mar 2003 09:11:34 -0000	1.8
+++ stty.c	20 Jun 2003 09:01:53 -0000	1.9
@@ -414,22 +414,25 @@
 					int reversed, struct termios *mode);
 static speed_t       string_to_baud(const char *arg);
 static tcflag_t*     mode_type_flag(enum mode_type type, struct termios *mode);
-static void          display_all(struct termios *mode, int fd,
-								 const char *device_name);
-static void          display_changed(struct termios *mode, int fd,
-									 const char *device_name);
-static void          display_recoverable(struct termios *mode, int fd,
-										 const char *device_name);
+static void          display_all(struct termios *mode, int fd);
+static void          display_changed(struct termios *mode, int fd);
+static void          display_recoverable(struct termios *mode, int fd);
 static void          display_speed(struct termios *mode, int fancy);
-static void          display_window_size(int fancy, int fd,
-					const char *device_name);
+static void          display_window_size(int fancy, int fd);
 static void          sane_mode(struct termios *mode);
 static void          set_control_char(const struct control_info *info,
 					const char *arg, struct termios *mode);
 static void          set_speed(enum speed_setting type,
 					const char *arg, struct termios *mode);
-static void          set_window_size(int rows, int cols, int fd,
-					const char *device_name);
+static void          set_window_size(int rows, int cols, int fd);
+
+static const char *device_name;
+
+static __attribute__ ((noreturn)) void perror_on_device(const char *fmt)
+{
+	bb_perror_msg_and_die(fmt, device_name);
+}
+
 
 /* The width of the screen, for output wrapping. */
 static int max_col;
@@ -477,7 +480,7 @@
 #endif
 {
 	struct termios mode;
-	void (*output_func)(struct termios *, int, const char *);
+	void (*output_func)(struct termios *, int);
 	int    optc;
 	int    require_set_attr;
 	int    speed_was_set;
@@ -487,7 +490,7 @@
 	int    noargs = 1;
 	char * file_name = NULL;
 	int    fd;
-	const char *device_name;
+
 
 	output_func = display_changed;
 	verbose_output = 0;
@@ -543,13 +546,10 @@
 		int fdflags;
 
 		device_name = file_name;
-		fd = open(device_name, O_RDONLY | O_NONBLOCK);
-		if (fd < 0)
-			bb_perror_msg_and_die("%s", device_name);
+		fd = bb_xopen(device_name, O_RDONLY | O_NONBLOCK);
 		if ((fdflags = fcntl(fd, F_GETFL)) == -1
 			|| fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
-			bb_perror_msg_and_die("%s: couldn't reset non-blocking mode",
-							   device_name);
+			perror_on_device("%s: couldn't reset non-blocking mode");
 	} else {
 		fd = 0;
 		device_name = bb_msg_standard_input;
@@ -559,12 +559,12 @@
 	   spurious difference in an uninitialized portion of the structure.  */
 	memset(&mode, 0, sizeof(mode));
 	if (tcgetattr(fd, &mode))
-		bb_perror_msg_and_die("%s", device_name);
+		perror_on_device("%s");
 
 	if (verbose_output | recoverable_output | noargs) {
 		max_col = screen_columns();
 		current_col = 0;
-		output_func(&mode, fd, device_name);
+		output_func(&mode, fd);
 		return EXIT_SUCCESS;
 	}
 
@@ -644,18 +644,18 @@
 				    bb_error_msg_and_die("missing argument to `%s'", argv[k]);
 				++k;
 				set_window_size((int) bb_xparse_number(argv[k], stty_suffixes),
-								-1, fd, device_name);
+								-1, fd);
 			} else if (STREQ(argv[k], "cols") || STREQ(argv[k], "columns")) {
 				if (k == argc - 1)
 				    bb_error_msg_and_die("missing argument to `%s'", argv[k]);
 				++k;
 				set_window_size(-1,
 						(int) bb_xparse_number(argv[k], stty_suffixes),
-						fd, device_name);
+						fd);
 			} else if (STREQ(argv[k], "size")) {
 				max_col = screen_columns();
 				current_col = 0;
-				display_window_size(0, fd, device_name);
+				display_window_size(0, fd);
 			}
 #endif
 #ifdef HAVE_C_LINE
@@ -685,7 +685,7 @@
 		struct termios new_mode;
 
 		if (tcsetattr(fd, TCSADRAIN, &mode))
-			bb_perror_msg_and_die("%s", device_name);
+			perror_on_device("%s");
 
 		/* POSIX (according to Zlotnick's book) tcsetattr returns zero if
 		   it performs *any* of the requested operations.  This means it
@@ -698,7 +698,7 @@
 		   spurious difference in an uninitialized portion of the structure.  */
 		memset(&new_mode, 0, sizeof(new_mode));
 		if (tcgetattr(fd, &new_mode))
-			bb_perror_msg_and_die("%s", device_name);
+			perror_on_device("%s");
 
 		/* Normally, one shouldn't use memcmp to compare structures that
 		   may have `holes' containing uninitialized data, but we have been
@@ -721,8 +721,7 @@
 			new_mode.c_cflag &= (~CIBAUD);
 			if (speed_was_set || memcmp(&mode, &new_mode, sizeof(mode)) != 0)
 #endif
-				bb_error_msg_and_die ("%s: unable to perform all requested operations",
-					 device_name);
+				perror_on_device ("%s: unable to perform all requested operations");
 		}
 	}
 
@@ -948,13 +947,13 @@
 }
 
 static void
-set_window_size(int rows, int cols, int fd, const char *device_name)
+set_window_size(int rows, int cols, int fd)
 {
 	struct winsize win;
 
 	if (get_win_size(fd, &win)) {
 		if (errno != EINVAL)
-			bb_perror_msg_and_die("%s", device_name);
+			perror_on_device("%s");
 		memset(&win, 0, sizeof(win));
 	}
 
@@ -980,23 +979,23 @@
 
 		if ((ioctl(fd, TIOCSWINSZ, (char *) &win) != 0)
 			|| (ioctl(fd, TIOCSSIZE, (char *) &ttysz) != 0)) {
-			bb_perror_msg_and_die("%s", device_name);
+			perror_on_device("%s");
 		return;
 	}
 # endif
 
 	if (ioctl(fd, TIOCSWINSZ, (char *) &win))
-		bb_perror_msg_and_die("%s", device_name);
+		perror_on_device("%s");
 }
 
-static void display_window_size(int fancy, int fd, const char *device_name)
+static void display_window_size(int fancy, int fd)
 {
 	const char *fmt_str = "%s" "\0" "%s: no size information for this device";
 	struct winsize win;
 
 	if (get_win_size(fd, &win)) {
 		if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
-			bb_perror_msg_and_die(fmt_str, device_name);
+			perror_on_device(fmt_str);
 		}
 	} else {
 		wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n",
@@ -1047,7 +1046,7 @@
 	return NULL;
 }
 
-static void display_changed(struct termios *mode, int fd, const char *device_name)
+static void display_changed(struct termios *mode, int fd)
 {
 	int i;
 	int empty_line;
@@ -1122,7 +1121,7 @@
 }
 
 static void
-display_all(struct termios *mode, int fd, const char *device_name)
+display_all(struct termios *mode, int fd)
 {
 	int i;
 	tcflag_t *bitsp;
@@ -1131,7 +1130,7 @@
 
 	display_speed(mode, 1);
 #ifdef TIOCGWINSZ
-	display_window_size(1, fd, device_name);
+	display_window_size(1, fd);
 #endif
 #ifdef HAVE_C_LINE
 	wrapf("line = %d;", mode->c_line);
@@ -1202,7 +1201,7 @@
 		current_col = 0;
 }
 
-static void display_recoverable(struct termios *mode, int fd, const char *device_name)
+static void display_recoverable(struct termios *mode, int fd)
 {
 	int i;
 

Index: test.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/test.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -d -r1.22 -r1.23
--- test.c	19 Mar 2003 09:11:34 -0000	1.22
+++ test.c	20 Jun 2003 09:01:53 -0000	1.23
@@ -452,10 +452,8 @@
 	if (errno != 0)
 		bb_error_msg_and_die("%s: out of range", s);
 
-	while (isspace(*p))
-		p++;
-
-	if (*p)
+	/*   p = bb_skip_whitespace(p); avoid const warning */
+	if (*(bb_skip_whitespace(p)))
 		bb_error_msg_and_die("%s: bad number", s);
 
 	return (int) r;



More information about the busybox-cvs mailing list