shithub: riscv

Download patch

ref: 4649189126e1ad98f9a07afd078096227668bfd1
parent: 408242edcf4ccf64baa1d599cec448451184c419
author: Ori Bernstein <ori@eigenstate.org>
date: Sun May 22 12:34:33 EDT 2022

diff: fix style

remove useless macros, bring formatting closer
to regular 9front formatting, and use the
ARGBEGIN/ARGEND macros. No functional change.

--- a/sys/src/cmd/diff/diff.h
+++ b/sys/src/cmd/diff/diff.h
@@ -1,14 +1,20 @@
-char mode;			/* '\0', 'e', 'f', 'h' */
-char bflag;			/* ignore multiple and trailing blanks */
-char rflag;			/* recurse down directory trees */
-char mflag;			/* pseudo flag: doing multiple files, one dir */
-int anychange;
+typedef struct Line Line;
+
+struct Line {
+	int	serial;
+	int	value;
+};
+extern Line *file[2];
+extern int len[2];
+extern long *ixold, *ixnew;
+extern int *J;
+extern char mode;
+extern char bflag;
+extern char rflag;
+extern char mflag;
+extern int anychange;
 extern Biobuf	stdout;
 extern int	binary;
-
-#define MALLOC(t, n)		((t *)emalloc((n)*sizeof(t)))
-#define REALLOC(p, t, n)	((t *)erealloc((void *)(p), (n)*sizeof(t)))
-#define FREE(p)			free((void *)(p))
 
 #define MAXPATHLEN	1024
 
--- a/sys/src/cmd/diff/diffdir.c
+++ b/sys/src/cmd/diff/diffdir.c
@@ -22,7 +22,7 @@
 	if ((fd = open(name, OREAD)) < 0) {
 		fprint(2, "%s: can't open %s: %r\n", argv0, name);
 		/* fake an empty directory */
-		cp = MALLOC(char*, 1);
+		cp = emalloc(sizeof(char*));
 		cp[0] = 0;
 		return cp;
 	}
@@ -30,14 +30,14 @@
 	nitems = 0;
 	if((n = dirreadall(fd, &db)) > 0){
 		while (n--) {
-			cp = REALLOC(cp, char *, (nitems+1));
-			cp[nitems] = MALLOC(char, strlen((db+n)->name)+1);
+			cp = erealloc(cp, (nitems+1)*sizeof(char*));
+			cp[nitems] = emalloc(strlen((db+n)->name)+1);
 			strcpy(cp[nitems], (db+n)->name);
 			nitems++;
 		}
 		free(db);
 	}
-	cp = REALLOC(cp, char*, (nitems+1));
+	cp = erealloc(cp, (nitems+1)*sizeof(char*));
 	cp[nitems] = 0;
 	close(fd);
 	qsort((char *)cp, nitems, sizeof(char*), itemcmp);
@@ -105,9 +105,9 @@
 		df++; dt++;
 	}
 	for (df = dirf; *df; df++)
-		FREE(*df);
+		free(*df);
 	for (dt = dirt; *dt; dt++)
-		FREE(*dt);
-	FREE(dirf);
-	FREE(dirt);
+		free(*dt);
+	free(dirf);
+	free(dirt);
 }
--- a/sys/src/cmd/diff/diffio.c
+++ b/sys/src/cmd/diff/diffio.c
@@ -4,15 +4,6 @@
 #include <ctype.h>
 #include "diff.h"
 
-struct line {
-	int	serial;
-	int	value;
-};
-extern struct line *file[2];
-extern int len[2];
-extern long *ixold, *ixnew;
-extern int *J;
-
 static Biobuf *input[2];
 static char *file1, *file2;
 static int firstchange;
@@ -115,7 +106,7 @@
 Biobuf *
 prepare(int i, char *arg)
 {
-	struct line *p;
+	Line *p;
 	int j, h;
 	Biobuf *bp;
 	char *cp, buf[MAXLINELEN];
@@ -145,9 +136,9 @@
 		}
 		Bseek(bp, 0, 0);
 	}
-	p = MALLOC(struct line, 3);
+	p = emalloc(3*sizeof(Line));
 	for (j = 0; h = readhash(bp, buf); p[j].value = h)
-		p = REALLOC(p, struct line, (++j+3));
+		p = erealloc(p, (++j+3)*sizeof(Line));
 	len[i] = j;
 	file[i] = p;
 	input[i] = bp;			/*fix*/
--- a/sys/src/cmd/diff/diffreg.c
+++ b/sys/src/cmd/diff/diffreg.c
@@ -66,25 +66,25 @@
 *	3*(number of k-candidates installed),  typically about
 *	6n words for files of length n. 
 */
-/* TIDY THIS UP */
-struct cand {
+typedef struct Cand Cand;
+
+struct Cand {
 	int x;
 	int y;
 	int pred;
-} cand;
-struct line {
-	int serial;
-	int value;
-} *file[2], line;
+};
+
+Cand cand;
+Line *file[2], line;
 int len[2];
 int binary;
-struct line *sfile[2];	/*shortened by pruning common prefix and suffix*/
+Line *sfile[2];	/*shortened by pruning common prefix and suffix*/
 int slen[2];
 int pref, suff;	/*length of prefix and suffix*/
 int *class;	/*will be overlaid on file[0]*/
 int *member;	/*will be overlaid on file[1]*/
-int *klist;		/*will be overlaid on file[0] after class*/
-struct cand *clist;	/* merely a free storage pot for candidates */
+int *klist;	/*will be overlaid on file[0] after class*/
+Cand *clist;	/* merely a free storage pot for candidates */
 int clen;
 int *J;		/*will be overlaid on class*/
 long *ixold;	/*will be overlaid on klist*/
@@ -92,11 +92,11 @@
 /* END OF SOME TIDYING */
 
 static void	
-sort(struct line *a, int n)	/*shellsort CACM #201*/
+sort(Line *a, int n)	/*shellsort CACM #201*/
 {
 	int m;
-	struct line *ai, *aim, *j, *k;
-	struct line w;
+	Line *ai, *aim, *j, *k;
+	Line w;
 	int i;
 
 	m = 0;
@@ -124,17 +124,17 @@
 }
 
 static void
-unsort(struct line *f, int l, int *b)
+unsort(Line *f, int l, int *b)
 {
 	int *a;
 	int i;
 
-	a = MALLOC(int, (l+1));
+	a = malloc((l+1)*sizeof(int));
 	for(i=1;i<=l;i++)
 		a[f[i].serial] = f[i].value;
 	for(i=1;i<=l;i++)
 		b[i] = a[i];
-	FREE(a);
+	free(a);
 }
 
 static void
@@ -157,7 +157,7 @@
 }
 
 static void
-equiv(struct line *a, int n, struct line *b, int m, int *c)
+equiv(Line *a, int n, Line *b, int m, int *c)
 {
 	int i, j;
 
@@ -187,9 +187,9 @@
 static int
 newcand(int x, int  y, int pred)
 {
-	struct cand *q;
+	Cand *q;
 
-	clist = REALLOC(clist, struct cand, (clen+1));
+	clist = erealloc(clist, (clen+1)*sizeof(Cand));
 	q = clist + clen;
 	q->x = x;
 	q->y = y;
@@ -263,7 +263,7 @@
 unravel(int p)
 {
 	int i;
-	struct cand *q;
+	Cand *q;
 
 	for(i=0; i<=len[0]; i++) {
 		if (i <= pref)
@@ -298,8 +298,7 @@
 			J[i1] = j1;
 			change(i0, i1, j0, j1);
 		}
-	}
-	else {
+	} else {
 		for (i0 = m; i0 >= 1; i0 = i1-1) {
 			while (i0 >= 1 && J[i0] == J[i0+1]-1 && J[i0])
 				i0--;
@@ -393,28 +392,31 @@
 
 	member = (int *)file[1];
 	equiv(sfile[0], slen[0], sfile[1], slen[1], member);
-	member = REALLOC(member, int, slen[1]+2);
+	member = erealloc(member, (slen[1]+2)*sizeof(int));
 
 	class = (int *)file[0];
 	unsort(sfile[0], slen[0], class);
-	class = REALLOC(class, int, slen[0]+2);
+	class = erealloc(class, (slen[0]+2)*sizeof(int));
 
-	klist = MALLOC(int, slen[0]+2);
-	clist = MALLOC(struct cand, 1);
+	klist = emalloc((slen[0]+2)*sizeof(int));
+	clist = emalloc(sizeof(Cand));
 	k = stone(class, slen[0], member, klist);
-	FREE(member);
-	FREE(class);
+	free(member);
+	free(class);
 
-	J = MALLOC(int, len[0]+2);
+	J = emalloc((len[0]+2)*sizeof(int));
 	unravel(klist[k]);
-	FREE(clist);
-	FREE(klist);
+	free(clist);
+	free(klist);
 
-	ixold = MALLOC(long, len[0]+2);
-	ixnew = MALLOC(long, len[1]+2);
+	ixold = emalloc((len[0]+2)*sizeof(long));
+	ixnew = emalloc((len[1]+2)*sizeof(long));
 	Bseek(b0, 0, 0); Bseek(b1, 0, 0);
 	check(b0, b1);
 	output();
-	FREE(J); FREE(ixold); FREE(ixnew);
-	Bterm(b0); Bterm(b1);			/* ++++ */
+	free(J);
+	free(ixold);
+	free(ixnew);
+	Bterm(b0);
+	Bterm(b1);
 }
--- a/sys/src/cmd/diff/main.c
+++ b/sys/src/cmd/diff/main.c
@@ -7,11 +7,14 @@
 #define	REGULAR_FILE(s)		((s)->type == 'M' && !DIRECTORY(s))
 
 Biobuf	stdout;
+char	mode;			/* '\0', 'e', 'f', 'h' */
+char	bflag;			/* ignore multiple and trailing blanks */
+char	rflag;			/* recurse down directory trees */
+char	mflag;			/* pseudo flag: doing multiple files, one dir */
+int	anychange;
 
 static char *tmp[] = {"/tmp/diff1XXXXXXXXXXX", "/tmp/diff2XXXXXXXXXXX"};
 static int whichtmp;
-static char *progname;
-static char usage[] = "diff [-abcefmnrw] file1 ... file2\n";
 
 static void
 rmtmpfiles(void)
@@ -45,7 +48,7 @@
 
 	Bflush(&stdout);
 
-	fprint(2, "%s: ", progname);
+	fprint(2, "%s: ", argv0);
 	va_start(arg, fmt);
 	vfprint(2, fmt, arg);
 	va_end(arg);
@@ -114,8 +117,7 @@
 		}
 		free(dir);
 		return mktmpfile(0, sb);
-	}
-	else if (!REGULAR_FILE(dir) && !DIRECTORY(dir)) {
+	} else if (!REGULAR_FILE(dir) && !DIRECTORY(dir)) {
 		free(dir);
 		if ((input = open(file, OREAD)) == -1) {
 			panic(mflag ? 0: 2, "cannot open %s: %r\n", file);
@@ -123,8 +125,7 @@
 		}
 		file = mktmpfile(input, sb);
 		close(input);
-	}
-	else
+	} else
 		*sb = dir;
 	return file;
 }
@@ -145,8 +146,7 @@
 		if (rflag || level == 0)
 			diffdir(fp, tp, level);
 		else
-			Bprint(&stdout, "Common subdirectories: %s and %s\n",
-				fp, tp);
+			Bprint(&stdout, "Common subdirectories: %s and %s\n", fp, tp);
 	}
 	else if (REGULAR_FILE(fsb) && REGULAR_FILE(tsb))
 		diffreg(fp, tp);
@@ -158,8 +158,7 @@
 				p++;
 			if (mkpathname(tb, tp, p) == 0)
 				diffreg(fp, tb);
-		}
-		else {
+		} else {
 			if ((p = utfrrune(t, '/')) == 0)
 				p = t;
 			else
@@ -175,60 +174,57 @@
 }
 
 void
+usage(void)
+{
+	fprint(2, "usage: %s [-abcefmnrw] file1 ... file2\n", argv0);
+	exits("usage");
+}
+
+void
 main(int argc, char *argv[])
 {
-	char *p;
 	int i;
 	Dir *fsb, *tsb;
 
 	Binit(&stdout, 1, OWRITE);
-	progname = argv0 = *argv;
-	while (--argc && (*++argv)[0] == '-' && (*argv)[1]) {
-		for (p = *argv+1; *p; p++) {
-			switch (*p) {
+	ARGBEGIN{
+	case 'e':
+	case 'f':
+	case 'n':
+	case 'c':
+	case 'a':
+	case 'u':
+		mode = ARGC();
+		break;
+	case 'w':
+		bflag = 2;
+		break;
 
-			case 'e':
-			case 'f':
-			case 'n':
-			case 'c':
-			case 'a':
-			case 'u':
-				mode = *p;
-				break;
+	case 'b':
+		bflag = 1;
+		break;
 
-			case 'w':
-				bflag = 2;
-				break;
+	case 'r':
+		rflag = 1;
+		break;
 
-			case 'b':
-				bflag = 1;
-				break;
+	case 'm':
+		mflag = 1;	
+		break;
 
-			case 'r':
-				rflag = 1;
-				break;
-
-			case 'm':
-				mflag = 1;	
-				break;
-
-			case 'h':
-			default:
-				progname = "Usage";
-				panic(2, usage);
-			}
-		}
-	}
+	case 'h':
+	default:
+		usage();
+	}ARGEND;
 	if (argc < 2)
-		panic(2, usage, progname);
+		usage();
 	if ((tsb = dirstat(argv[argc-1])) == nil)
 		panic(2, "can't stat %s\n", argv[argc-1]);
 	if (argc > 2) {
 		if (!DIRECTORY(tsb))
-			panic(2, usage, progname);
+			panic(2, "not directory: %s", argv[argc-1]);
 		mflag = 1;
-	}
-	else {
+	} else {
 		if ((fsb = dirstat(argv[0])) == nil)
 			panic(2, "can't stat %s\n", argv[0]);
 		if (DIRECTORY(fsb) && DIRECTORY(tsb))
@@ -257,7 +253,7 @@
 void *
 erealloc(void *p, unsigned n)
 {
-	register void *rp;
+	void *rp;
 
 	if ((rp = realloc(p, n)) == 0)
 		panic(2, noroom);