shithub: riscv

Download patch

ref: 8e07a4c9a52a0c9203f52e9d835e6da114f8a659
parent: 9b5b68a3022a6503aab296986fec1b463f9e6b1e
author: cinap_lenrek <[email protected]>
date: Wed Aug 1 17:57:13 EDT 2012

cb: import updates from sources

--- a/sys/man/1/cb
+++ b/sys/man/1/cb
@@ -32,7 +32,7 @@
 Print code in the so-called K&R style used in
 .IR "The C Programming Language" .
 .TP 
-.B -l length
+.B -l
 Split lines that are longer than
 .IR length .
 .PD
--- a/sys/src/cmd/cb/cb.c
+++ b/sys/src/cmd/cb/cb.c
@@ -4,35 +4,34 @@
 #include "cb.h"
 #include "cbtype.h"
 
+static void
+usage(void)
+{
+	fprint(2, "usage: cb [-sj] [-l width]\n");
+	exits("usage");
+}
+
 void
 main(int argc, char *argv[])
 {
 	Biobuf stdin, stdout;
 
-	while (--argc > 0 && (*++argv)[0] == '-'){
-		switch ((*argv)[1]){
-		case 's':
-			strict = 1;
-			continue;
-		case 'j':
-			join = 1;
-			continue;
-		case 'l':
-			if((*argv)[2] != '\0'){
-				maxleng = atoi( &((*argv)[2]) );
-			}
-			else{
-				maxleng = atoi(*++argv);
-				argc--;
-			}
-			maxtabs = maxleng/TABLENG - 2;
-			maxleng -= (maxleng + 5)/10;
-			continue;
-		default:
-			fprint(2, "cb: illegal option %c\n", *argv[1]);
-			exits("boom");
-		}
-	}
+	ARGBEGIN{
+	case 'j':
+		join = 1;
+		break;
+	case 'l':
+		maxleng = atoi(EARGF(usage()));
+		maxtabs = maxleng/TABLENG - 2;
+		maxleng -= (maxleng + 5)/10;
+		break;
+	case 's':
+		strict = 1;
+		break;
+	default:
+		usage();
+	}ARGEND
+
 	Binit(&stdout, 1, OWRITE);
 	output = &stdout;
 	if (argc <= 0){
@@ -39,13 +38,13 @@
 		Binit(&stdin, 0, OREAD);
 		input = &stdin;
 		work();
+		Bterm(input);
 	} else {
 		while (argc-- > 0){
-			if ((input = Bopen( *argv, OREAD)) == 0){
-				fprint(2, "cb: cannot open input file %s\n", *argv);
-				exits("boom");
-			}
+			if ((input = Bopen(*argv, OREAD)) == 0)
+				sysfatal("can't open input file %s: %r", *argv);
 			work();
+			Bterm(input);
 			argv++;
 		}
 	}