[BusyBox] getopt - du

Marc Nijdam marc_nijdam at hp.com
Fri Jul 14 07:22:26 UTC 2000


Another one bites the dust... Should I be doing this directly into cvs
or does this mechanism work good enough?

Marc
-------------- next part --------------
Index: du.c
===================================================================
RCS file: /var/cvs/busybox/du.c,v
retrieving revision 1.21
diff -u -r1.21 du.c
--- du.c	2000/07/14 01:51:25	1.21
+++ du.c	2000/07/14 07:11:28
@@ -137,42 +137,32 @@
 int du_main(int argc, char **argv)
 {
 	int i;
-	char opt;
+	char c;
 
 	/* default behaviour */
 	print = print_normal;
 
 	/* parse argv[] */
-	for (i = 1; i < argc; i++) {
-		if (argv[i][0] == '-') {
-			opt = argv[i][1];
-			switch (opt) {
+	while ((c = getopt(argc, argv, "sl")) != EOF) {
+			switch (c) {
 			case 's':
-				print = print_summary;
-				break;
+					print = print_summary;
+					break;
 			case 'l':
-				count_hardlinks = 1;
-				break;
-			case 'h':
-			case '-':
-				usage(du_usage);
-				break;
+					count_hardlinks = 1;
+					break;
 			default:
-				errorMsg("invalid option -- %c\n", opt);
-				usage(du_usage);
+					usage(du_usage);
 			}
-		} else {
-			break;
-		}
 	}
 
 	/* go through remaining args (if any) */
-	if (i >= argc) {
+	if (optind >= argc) {
 		du(".");
 	} else {
 		long sum;
 
-		for (; i < argc; i++) {
+		for (i=optind; i < argc; i++) {
 			sum = du(argv[i]);
 			if (sum && isDirectory(argv[i], FALSE, NULL)) {
 				print_normal(sum, argv[i]);


More information about the busybox mailing list