diff: fix style

remove useless macros, bring formatting closer
to regular 9front formatting, and use the
ARGBEGIN/ARGEND macros. No functional change.
This commit is contained in:
Ori Bernstein 2022-05-22 16:34:33 +00:00
parent 408242edcf
commit 4649189126
5 changed files with 109 additions and 114 deletions

View file

@ -1,15 +1,21 @@
char mode; /* '\0', 'e', 'f', 'h' */ typedef struct Line Line;
char bflag; /* ignore multiple and trailing blanks */
char rflag; /* recurse down directory trees */ struct Line {
char mflag; /* pseudo flag: doing multiple files, one dir */ int serial;
int anychange; 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 Biobuf stdout;
extern int binary; 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 #define MAXPATHLEN 1024
int mkpathname(char *, char *, char *); int mkpathname(char *, char *, char *);

View file

@ -22,7 +22,7 @@ scandir(char *name)
if ((fd = open(name, OREAD)) < 0) { if ((fd = open(name, OREAD)) < 0) {
fprint(2, "%s: can't open %s: %r\n", argv0, name); fprint(2, "%s: can't open %s: %r\n", argv0, name);
/* fake an empty directory */ /* fake an empty directory */
cp = MALLOC(char*, 1); cp = emalloc(sizeof(char*));
cp[0] = 0; cp[0] = 0;
return cp; return cp;
} }
@ -30,14 +30,14 @@ scandir(char *name)
nitems = 0; nitems = 0;
if((n = dirreadall(fd, &db)) > 0){ if((n = dirreadall(fd, &db)) > 0){
while (n--) { while (n--) {
cp = REALLOC(cp, char *, (nitems+1)); cp = erealloc(cp, (nitems+1)*sizeof(char*));
cp[nitems] = MALLOC(char, strlen((db+n)->name)+1); cp[nitems] = emalloc(strlen((db+n)->name)+1);
strcpy(cp[nitems], (db+n)->name); strcpy(cp[nitems], (db+n)->name);
nitems++; nitems++;
} }
free(db); free(db);
} }
cp = REALLOC(cp, char*, (nitems+1)); cp = erealloc(cp, (nitems+1)*sizeof(char*));
cp[nitems] = 0; cp[nitems] = 0;
close(fd); close(fd);
qsort((char *)cp, nitems, sizeof(char*), itemcmp); qsort((char *)cp, nitems, sizeof(char*), itemcmp);
@ -105,9 +105,9 @@ diffdir(char *f, char *t, int level)
df++; dt++; df++; dt++;
} }
for (df = dirf; *df; df++) for (df = dirf; *df; df++)
FREE(*df); free(*df);
for (dt = dirt; *dt; dt++) for (dt = dirt; *dt; dt++)
FREE(*dt); free(*dt);
FREE(dirf); free(dirf);
FREE(dirt); free(dirt);
} }

View file

@ -4,15 +4,6 @@
#include <ctype.h> #include <ctype.h>
#include "diff.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 Biobuf *input[2];
static char *file1, *file2; static char *file1, *file2;
static int firstchange; static int firstchange;
@ -115,7 +106,7 @@ readhash(Biobuf *bp, char *buf)
Biobuf * Biobuf *
prepare(int i, char *arg) prepare(int i, char *arg)
{ {
struct line *p; Line *p;
int j, h; int j, h;
Biobuf *bp; Biobuf *bp;
char *cp, buf[MAXLINELEN]; char *cp, buf[MAXLINELEN];
@ -145,9 +136,9 @@ prepare(int i, char *arg)
} }
Bseek(bp, 0, 0); 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) 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; len[i] = j;
file[i] = p; file[i] = p;
input[i] = bp; /*fix*/ input[i] = bp; /*fix*/

View file

@ -66,25 +66,25 @@
* 3*(number of k-candidates installed), typically about * 3*(number of k-candidates installed), typically about
* 6n words for files of length n. * 6n words for files of length n.
*/ */
/* TIDY THIS UP */ typedef struct Cand Cand;
struct cand {
struct Cand {
int x; int x;
int y; int y;
int pred; int pred;
} cand; };
struct line {
int serial; Cand cand;
int value; Line *file[2], line;
} *file[2], line;
int len[2]; int len[2];
int binary; 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 slen[2];
int pref, suff; /*length of prefix and suffix*/ int pref, suff; /*length of prefix and suffix*/
int *class; /*will be overlaid on file[0]*/ int *class; /*will be overlaid on file[0]*/
int *member; /*will be overlaid on file[1]*/ int *member; /*will be overlaid on file[1]*/
int *klist; /*will be overlaid on file[0] after class*/ int *klist; /*will be overlaid on file[0] after class*/
struct cand *clist; /* merely a free storage pot for candidates */ Cand *clist; /* merely a free storage pot for candidates */
int clen; int clen;
int *J; /*will be overlaid on class*/ int *J; /*will be overlaid on class*/
long *ixold; /*will be overlaid on klist*/ long *ixold; /*will be overlaid on klist*/
@ -92,11 +92,11 @@ long *ixnew; /*will be overlaid on file[1]*/
/* END OF SOME TIDYING */ /* END OF SOME TIDYING */
static void static void
sort(struct line *a, int n) /*shellsort CACM #201*/ sort(Line *a, int n) /*shellsort CACM #201*/
{ {
int m; int m;
struct line *ai, *aim, *j, *k; Line *ai, *aim, *j, *k;
struct line w; Line w;
int i; int i;
m = 0; m = 0;
@ -124,17 +124,17 @@ sort(struct line *a, int n) /*shellsort CACM #201*/
} }
static void static void
unsort(struct line *f, int l, int *b) unsort(Line *f, int l, int *b)
{ {
int *a; int *a;
int i; int i;
a = MALLOC(int, (l+1)); a = malloc((l+1)*sizeof(int));
for(i=1;i<=l;i++) for(i=1;i<=l;i++)
a[f[i].serial] = f[i].value; a[f[i].serial] = f[i].value;
for(i=1;i<=l;i++) for(i=1;i<=l;i++)
b[i] = a[i]; b[i] = a[i];
FREE(a); free(a);
} }
static void static void
@ -157,7 +157,7 @@ prune(void)
} }
static void 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; int i, j;
@ -187,9 +187,9 @@ equiv(struct line *a, int n, struct line *b, int m, int *c)
static int static int
newcand(int x, int y, int pred) 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 = clist + clen;
q->x = x; q->x = x;
q->y = y; q->y = y;
@ -263,7 +263,7 @@ static void
unravel(int p) unravel(int p)
{ {
int i; int i;
struct cand *q; Cand *q;
for(i=0; i<=len[0]; i++) { for(i=0; i<=len[0]; i++) {
if (i <= pref) if (i <= pref)
@ -298,8 +298,7 @@ output(void)
J[i1] = j1; J[i1] = j1;
change(i0, i1, j0, j1); change(i0, i1, j0, j1);
} }
} } else {
else {
for (i0 = m; i0 >= 1; i0 = i1-1) { for (i0 = m; i0 >= 1; i0 = i1-1) {
while (i0 >= 1 && J[i0] == J[i0+1]-1 && J[i0]) while (i0 >= 1 && J[i0] == J[i0+1]-1 && J[i0])
i0--; i0--;
@ -393,28 +392,31 @@ diffreg(char *f, char *t)
member = (int *)file[1]; member = (int *)file[1];
equiv(sfile[0], slen[0], sfile[1], slen[1], member); 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]; class = (int *)file[0];
unsort(sfile[0], slen[0], class); 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); klist = emalloc((slen[0]+2)*sizeof(int));
clist = MALLOC(struct cand, 1); clist = emalloc(sizeof(Cand));
k = stone(class, slen[0], member, klist); k = stone(class, slen[0], member, klist);
FREE(member); free(member);
FREE(class); free(class);
J = MALLOC(int, len[0]+2); J = emalloc((len[0]+2)*sizeof(int));
unravel(klist[k]); unravel(klist[k]);
FREE(clist); free(clist);
FREE(klist); free(klist);
ixold = MALLOC(long, len[0]+2); ixold = emalloc((len[0]+2)*sizeof(long));
ixnew = MALLOC(long, len[1]+2); ixnew = emalloc((len[1]+2)*sizeof(long));
Bseek(b0, 0, 0); Bseek(b1, 0, 0); Bseek(b0, 0, 0); Bseek(b1, 0, 0);
check(b0, b1); check(b0, b1);
output(); output();
FREE(J); FREE(ixold); FREE(ixnew); free(J);
Bterm(b0); Bterm(b1); /* ++++ */ free(ixold);
free(ixnew);
Bterm(b0);
Bterm(b1);
} }

View file

@ -7,11 +7,14 @@
#define REGULAR_FILE(s) ((s)->type == 'M' && !DIRECTORY(s)) #define REGULAR_FILE(s) ((s)->type == 'M' && !DIRECTORY(s))
Biobuf stdout; 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 char *tmp[] = {"/tmp/diff1XXXXXXXXXXX", "/tmp/diff2XXXXXXXXXXX"};
static int whichtmp; static int whichtmp;
static char *progname;
static char usage[] = "diff [-abcefmnrw] file1 ... file2\n";
static void static void
rmtmpfiles(void) rmtmpfiles(void)
@ -45,7 +48,7 @@ panic(int status, char *fmt, ...)
Bflush(&stdout); Bflush(&stdout);
fprint(2, "%s: ", progname); fprint(2, "%s: ", argv0);
va_start(arg, fmt); va_start(arg, fmt);
vfprint(2, fmt, arg); vfprint(2, fmt, arg);
va_end(arg); va_end(arg);
@ -114,8 +117,7 @@ statfile(char *file, Dir **sb)
} }
free(dir); free(dir);
return mktmpfile(0, sb); return mktmpfile(0, sb);
} } else if (!REGULAR_FILE(dir) && !DIRECTORY(dir)) {
else if (!REGULAR_FILE(dir) && !DIRECTORY(dir)) {
free(dir); free(dir);
if ((input = open(file, OREAD)) == -1) { if ((input = open(file, OREAD)) == -1) {
panic(mflag ? 0: 2, "cannot open %s: %r\n", file); panic(mflag ? 0: 2, "cannot open %s: %r\n", file);
@ -123,8 +125,7 @@ statfile(char *file, Dir **sb)
} }
file = mktmpfile(input, sb); file = mktmpfile(input, sb);
close(input); close(input);
} } else
else
*sb = dir; *sb = dir;
return file; return file;
} }
@ -145,8 +146,7 @@ diff(char *f, char *t, int level)
if (rflag || level == 0) if (rflag || level == 0)
diffdir(fp, tp, level); diffdir(fp, tp, level);
else else
Bprint(&stdout, "Common subdirectories: %s and %s\n", Bprint(&stdout, "Common subdirectories: %s and %s\n", fp, tp);
fp, tp);
} }
else if (REGULAR_FILE(fsb) && REGULAR_FILE(tsb)) else if (REGULAR_FILE(fsb) && REGULAR_FILE(tsb))
diffreg(fp, tp); diffreg(fp, tp);
@ -158,8 +158,7 @@ diff(char *f, char *t, int level)
p++; p++;
if (mkpathname(tb, tp, p) == 0) if (mkpathname(tb, tp, p) == 0)
diffreg(fp, tb); diffreg(fp, tb);
} } else {
else {
if ((p = utfrrune(t, '/')) == 0) if ((p = utfrrune(t, '/')) == 0)
p = t; p = t;
else else
@ -174,61 +173,58 @@ Return:
rmtmpfiles(); rmtmpfiles();
} }
void
usage(void)
{
fprint(2, "usage: %s [-abcefmnrw] file1 ... file2\n", argv0);
exits("usage");
}
void void
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *p;
int i; int i;
Dir *fsb, *tsb; Dir *fsb, *tsb;
Binit(&stdout, 1, OWRITE); Binit(&stdout, 1, OWRITE);
progname = argv0 = *argv; ARGBEGIN{
while (--argc && (*++argv)[0] == '-' && (*argv)[1]) { case 'e':
for (p = *argv+1; *p; p++) { case 'f':
switch (*p) { case 'n':
case 'c':
case 'a':
case 'u':
mode = ARGC();
break;
case 'w':
bflag = 2;
break;
case 'e': case 'b':
case 'f': bflag = 1;
case 'n': break;
case 'c':
case 'a':
case 'u':
mode = *p;
break;
case 'w': case 'r':
bflag = 2; rflag = 1;
break; break;
case 'b': case 'm':
bflag = 1; mflag = 1;
break; break;
case 'r': case 'h':
rflag = 1; default:
break; usage();
}ARGEND;
case 'm':
mflag = 1;
break;
case 'h':
default:
progname = "Usage";
panic(2, usage);
}
}
}
if (argc < 2) if (argc < 2)
panic(2, usage, progname); usage();
if ((tsb = dirstat(argv[argc-1])) == nil) if ((tsb = dirstat(argv[argc-1])) == nil)
panic(2, "can't stat %s\n", argv[argc-1]); panic(2, "can't stat %s\n", argv[argc-1]);
if (argc > 2) { if (argc > 2) {
if (!DIRECTORY(tsb)) if (!DIRECTORY(tsb))
panic(2, usage, progname); panic(2, "not directory: %s", argv[argc-1]);
mflag = 1; mflag = 1;
} } else {
else {
if ((fsb = dirstat(argv[0])) == nil) if ((fsb = dirstat(argv[0])) == nil)
panic(2, "can't stat %s\n", argv[0]); panic(2, "can't stat %s\n", argv[0]);
if (DIRECTORY(fsb) && DIRECTORY(tsb)) if (DIRECTORY(fsb) && DIRECTORY(tsb))
@ -257,7 +253,7 @@ emalloc(unsigned n)
void * void *
erealloc(void *p, unsigned n) erealloc(void *p, unsigned n)
{ {
register void *rp; void *rp;
if ((rp = realloc(p, n)) == 0) if ((rp = realloc(p, n)) == 0)
panic(2, noroom); panic(2, noroom);