Initial Import from SVN

This commit is contained in:
Matt Jenkins
2014-04-09 14:27:18 +01:00
parent 8976e834c4
commit 895f96d2f7
3153 changed files with 748589 additions and 0 deletions

33
src/cmd/diff/Makefile Normal file
View File

@@ -0,0 +1,33 @@
TOPSRC = $(shell cd ../../..; pwd)
include $(TOPSRC)/target.mk
CFLAGS += -Werror
SRCS = diff.c diffdir.c diffreg.c
OBJS = diff.o diffdir.o diffreg.o
DIFF = /bin/diff
DIFFH = /libexec/diffh
PR = /bin/pr
CFLAGS += -DDIFF='"${DIFF}"' -DDIFFH='"${DIFFH}"' -DPR='"${PR}"'
all: diff diffh
diff: ${OBJS}
${CC} ${LDFLAGS} -o diff.elf ${OBJS} ${LIBS}
${OBJDUMP} -S diff.elf > diff.dis
${SIZE} diff.elf
${ELF2AOUT} diff.elf $@ && rm diff.elf
diffh: diffh.o
${CC} ${LDFLAGS} -o diffh.elf $< ${LIBS}
${OBJDUMP} -S diffh.elf > diffh.dis
${SIZE} diffh.elf
${ELF2AOUT} diffh.elf $@ && rm diffh.elf
clean:
rm -f *.o *.elf ${MAN} diff diffh *.elf *.dis tags *~
install: all
install diff ${DESTDIR}/bin/diff
install diffh ${DESTDIR}/libexec/diffh

208
src/cmd/diff/diff.c Normal file
View File

@@ -0,0 +1,208 @@
/*
* diff - driver and subroutines
*/
#include "diff.h"
char diff[] = DIFF;
char diffh[] = DIFFH;
char pr[] = PR;
main(argc, argv)
int argc;
char **argv;
{
register char *argp;
ifdef1 = "FILE1"; ifdef2 = "FILE2";
status = 2;
diffargv = argv;
argc--, argv++;
while (argc > 2 && argv[0][0] == '-') {
argp = &argv[0][1];
argv++, argc--;
while (*argp) switch(*argp++) {
#ifdef notdef
case 'I':
opt = D_IFDEF;
wantelses = 0;
continue;
case 'E':
opt = D_IFDEF;
wantelses = 1;
continue;
case '1':
opt = D_IFDEF;
ifdef1 = argp;
*--argp = 0;
continue;
#endif
case 'D':
/* -Dfoo = -E -1 -2foo */
wantelses = 1;
ifdef1 = "";
/* fall through */
#ifdef notdef
case '2':
#endif
opt = D_IFDEF;
ifdef2 = argp;
*--argp = 0;
continue;
case 'e':
opt = D_EDIT;
continue;
case 'f':
opt = D_REVERSE;
continue;
case 'n':
opt = D_NREVERSE;
continue;
case 'b':
bflag = 1;
continue;
case 'w':
wflag = 1;
continue;
case 'i':
iflag = 1;
continue;
case 't':
tflag = 1;
continue;
case 'c':
opt = D_CONTEXT;
if (isdigit(*argp)) {
context = atoi(argp);
while (isdigit(*argp))
argp++;
if (*argp) {
fprintf(stderr,
"diff: -c: bad count\n");
done(0);
}
argp = "";
} else
context = 3;
continue;
case 'h':
hflag++;
continue;
case 'S':
if (*argp == 0) {
fprintf(stderr, "diff: use -Sstart\n");
done(0);
}
start = argp;
*--argp = 0; /* don't pass it on */
continue;
case 'r':
rflag++;
continue;
case 's':
sflag++;
continue;
case 'l':
lflag++;
continue;
default:
fprintf(stderr, "diff: -%s: unknown option\n",
--argp);
done(0);
}
}
if (argc != 2) {
fprintf(stderr, "diff: two filename arguments required\n");
done(0);
}
file1 = argv[0];
file2 = argv[1];
if (hflag && opt) {
fprintf(stderr,
"diff: -h doesn't support -e, -f, -n, -c, or -I\n");
done(0);
}
if (!strcmp(file1, "-"))
stb1.st_mode = S_IFREG;
else if (stat(file1, &stb1) < 0) {
fprintf(stderr, "diff: ");
perror(file1);
done(0);
}
if (!strcmp(file2, "-"))
stb2.st_mode = S_IFREG;
else if (stat(file2, &stb2) < 0) {
fprintf(stderr, "diff: ");
perror(file2);
done(0);
}
if ((stb1.st_mode & S_IFMT) == S_IFDIR &&
(stb2.st_mode & S_IFMT) == S_IFDIR) {
diffdir(argv);
} else
diffreg();
done(0);
}
char *
savestr(cp)
register char *cp;
{
register char *dp = malloc(strlen(cp)+1);
if (dp == 0) {
fprintf(stderr, "diff: ran out of memory\n");
done(0);
}
strcpy(dp, cp);
return (dp);
}
min(a,b)
int a,b;
{
return (a < b ? a : b);
}
max(a,b)
int a,b;
{
return (a > b ? a : b);
}
void done(sig)
int sig;
{
if (tempfile)
unlink(tempfile);
exit(status);
}
char *
talloc(n)
{
register char *p;
if ((p = malloc((unsigned)n)) != NULL)
return(p);
noroom();
}
char *
ralloc(p,n)
char *p;
{
register char *q;
if ((q = realloc(p, (unsigned)n)) == NULL)
noroom();
return(q);
}
noroom()
{
fprintf(stderr, "diff: files too big, try -h\n");
done(0);
}

84
src/cmd/diff/diff.h Normal file
View File

@@ -0,0 +1,84 @@
/*
* diff - common declarations
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <signal.h>
/*
* Output format options
*/
int opt;
#define D_NORMAL 0 /* Normal output */
#define D_EDIT -1 /* Editor script out */
#define D_REVERSE 1 /* Reverse editor script */
#define D_CONTEXT 2 /* Diff with context */
#define D_IFDEF 3 /* Diff with merged #ifdef's */
#define D_NREVERSE 4 /* Reverse ed script with numbered
lines and no trailing . */
int tflag; /* expand tabs on output */
/*
* Algorithm related options
*/
int hflag; /* -h, use halfhearted DIFFH */
int bflag; /* ignore blanks in comparisons */
int wflag; /* totally ignore blanks in comparisons */
int iflag; /* ignore case in comparisons */
/*
* Options on hierarchical diffs.
*/
int lflag; /* long output format with header */
int rflag; /* recursively trace directories */
int sflag; /* announce files which are same */
char *start; /* do file only if name >= this */
/*
* Variables for -I D_IFDEF option.
*/
int wantelses; /* -E */
char *ifdef1; /* String for -1 */
char *ifdef2; /* String for -2 */
char *endifname; /* What we will print on next #endif */
int inifdef;
/*
* Variables for -c context option.
*/
int context; /* lines of context to be printed */
/*
* State for exit status.
*/
int status;
int anychange;
char *tempfile; /* used when comparing against std input */
/*
* Variables for diffdir.
*/
char **diffargv; /* option list to pass to recursive diffs */
/*
* Input file names.
* With diffdir, file1 and file2 are allocated BUFSIZ space,
* and padded with a '/', and then efile0 and efile1 point after
* the '/'.
*/
char *file1, *file2, *efile1, *efile2;
struct stat stb1, stb2;
char *talloc(), *ralloc();
char *savestr(), *splice(), *splicen();
char *copytemp();
void done(int);
extern char diffh[], diff[], pr[];

420
src/cmd/diff/diffdir.c Normal file
View File

@@ -0,0 +1,420 @@
/*
* diff - directory comparison
*/
#include "diff.h"
#define d_flags d_ino
#define ONLY 1 /* Only in this directory */
#define SAME 2 /* Both places and same */
#define DIFFER 4 /* Both places and different */
#define DIRECT 8 /* Directory */
struct dir {
ino_t d_ino;
short d_reclen;
short d_namlen;
char *d_entry;
};
struct dir *setupdir();
int header;
char title[2*BUFSIZ], *etitle;
diffdir(argv)
char **argv;
{
register struct dir *d1, *d2;
struct dir *dir1, *dir2;
register int i;
int cmp;
if (opt == D_IFDEF) {
fprintf(stderr, "diff: can't specify -I with directories\n");
done(0);
}
if (opt == D_EDIT && (sflag || lflag))
fprintf(stderr,
"diff: warning: shouldn't give -s or -l with -e\n");
title[0] = 0;
strcpy(title, "diff ");
for (i = 1; diffargv[i+2]; i++) {
if (!strcmp(diffargv[i], "-"))
continue; /* was -S, dont look silly */
strcat(title, diffargv[i]);
strcat(title, " ");
}
for (etitle = title; *etitle; etitle++)
;
setfile(&file1, &efile1, file1);
setfile(&file2, &efile2, file2);
argv[0] = file1;
argv[1] = file2;
dir1 = setupdir(file1);
dir2 = setupdir(file2);
d1 = dir1; d2 = dir2;
while (d1->d_entry != 0 || d2->d_entry != 0) {
if (d1->d_entry && useless(d1->d_entry)) {
d1++;
continue;
}
if (d2->d_entry && useless(d2->d_entry)) {
d2++;
continue;
}
if (d1->d_entry == 0)
cmp = 1;
else if (d2->d_entry == 0)
cmp = -1;
else
cmp = strcmp(d1->d_entry, d2->d_entry);
if (cmp < 0) {
if (lflag)
d1->d_flags |= ONLY;
else if (opt == 0 || opt == 2)
only(d1, 1);
d1++;
} else if (cmp == 0) {
compare(d1);
d1++;
d2++;
} else {
if (lflag)
d2->d_flags |= ONLY;
else if (opt == 0 || opt == 2)
only(d2, 2);
d2++;
}
}
if (lflag) {
scanpr(dir1, ONLY, "Only in %.*s", file1, efile1, 0, 0);
scanpr(dir2, ONLY, "Only in %.*s", file2, efile2, 0, 0);
scanpr(dir1, SAME, "Common identical files in %.*s and %.*s",
file1, efile1, file2, efile2);
scanpr(dir1, DIFFER, "Binary files which differ in %.*s and %.*s",
file1, efile1, file2, efile2);
scanpr(dir1, DIRECT, "Common subdirectories of %.*s and %.*s",
file1, efile1, file2, efile2);
}
if (rflag) {
if (header && lflag)
printf("\f");
for (d1 = dir1; d1->d_entry; d1++) {
if ((d1->d_flags & DIRECT) == 0)
continue;
strcpy(efile1, d1->d_entry);
strcpy(efile2, d1->d_entry);
calldiff(0);
}
}
}
setfile(fpp, epp, file)
char **fpp, **epp;
char *file;
{
register char *cp;
*fpp = malloc(BUFSIZ);
if (*fpp == 0) {
fprintf(stderr, "diff: ran out of memory\n");
exit(1);
}
strcpy(*fpp, file);
for (cp = *fpp; *cp; cp++)
continue;
*cp++ = '/';
*epp = cp;
}
scanpr(dp, test, title, file1, efile1, file2, efile2)
register struct dir *dp;
int test;
char *title, *file1, *efile1, *file2, *efile2;
{
int titled = 0;
for (; dp->d_entry; dp++) {
if ((dp->d_flags & test) == 0)
continue;
if (titled == 0) {
if (header == 0)
header = 1;
else
printf("\n");
printf(title,
efile1 - file1 - 1, file1,
efile2 - file2 - 1, file2);
printf(":\n");
titled = 1;
}
printf("\t%s\n", dp->d_entry);
}
}
only(dp, which)
struct dir *dp;
int which;
{
char *file = which == 1 ? file1 : file2;
char *efile = which == 1 ? efile1 : efile2;
printf("Only in %.*s: %s\n", efile - file - 1, file, dp->d_entry);
}
int entcmp();
struct dir *
setupdir(cp)
char *cp;
{
register struct dir *dp = 0, *ep;
register struct direct *rp;
register int nitems, n;
DIR *dirp;
dirp = opendir(cp);
if (dirp == NULL) {
fprintf(stderr, "diff: ");
perror(cp);
done(0);
}
nitems = 0;
#ifdef pdp11
while (readdir(dirp))
nitems++;
rewinddir(dirp);
dp = (struct dir *)calloc(nitems+1, sizeof (struct dir));
nitems = 0;
#else
dp = (struct dir *)malloc(sizeof (struct dir));
#endif
if (dp == 0) {
fprintf(stderr, "diff: ran out of memory\n");
done(0);
}
while (rp = readdir(dirp)) {
ep = &dp[nitems++];
ep->d_reclen = rp->d_reclen;
ep->d_namlen = rp->d_namlen;
ep->d_entry = 0;
ep->d_flags = 0;
if (ep->d_namlen > 0) {
ep->d_entry = malloc(ep->d_namlen + 1);
if (ep->d_entry == 0) {
fprintf(stderr, "diff: out of memory\n");
done(0);
}
strcpy(ep->d_entry, rp->d_name);
}
#ifndef pdp11
dp = (struct dir *)realloc((char *)dp,
(nitems + 1) * sizeof (struct dir));
if (dp == 0) {
fprintf(stderr, "diff: ran out of memory\n");
done(0);
}
#endif
}
dp[nitems].d_entry = 0; /* delimiter */
closedir(dirp);
qsort(dp, nitems, sizeof (struct dir), entcmp);
return (dp);
}
entcmp(d1, d2)
struct dir *d1, *d2;
{
return (strcmp(d1->d_entry, d2->d_entry));
}
compare(dp)
register struct dir *dp;
{
register int i, j;
int f1, f2, fmt1, fmt2;
struct stat stb1, stb2;
int flag = 0;
char buf1[BUFSIZ], buf2[BUFSIZ];
strcpy(efile1, dp->d_entry);
strcpy(efile2, dp->d_entry);
f1 = open(file1, 0);
if (f1 < 0) {
perror(file1);
return;
}
f2 = open(file2, 0);
if (f2 < 0) {
perror(file2);
close(f1);
return;
}
fstat(f1, &stb1); fstat(f2, &stb2);
fmt1 = stb1.st_mode & S_IFMT;
fmt2 = stb2.st_mode & S_IFMT;
if (fmt1 != S_IFREG || fmt2 != S_IFREG) {
if (fmt1 == fmt2) {
if (fmt1 != S_IFDIR && stb1.st_rdev == stb2.st_rdev)
goto same;
if (fmt1 == S_IFDIR) {
dp->d_flags = DIRECT;
if (lflag || opt == D_EDIT)
goto closem;
printf("Common subdirectories: %s and %s\n",
file1, file2);
goto closem;
}
}
goto notsame;
}
if (stb1.st_size != stb2.st_size)
goto notsame;
for (;;) {
i = read(f1, buf1, BUFSIZ);
j = read(f2, buf2, BUFSIZ);
if (i < 0 || j < 0 || i != j)
goto notsame;
if (i == 0 && j == 0)
goto same;
for (j = 0; j < i; j++)
if (buf1[j] != buf2[j])
goto notsame;
}
same:
if (sflag == 0)
goto closem;
if (lflag)
dp->d_flags = SAME;
else
printf("Files %s and %s are identical\n", file1, file2);
goto closem;
notsame:
if (!ascii(f1) || !ascii(f2)) {
if (lflag)
dp->d_flags |= DIFFER;
else if (opt == D_NORMAL || opt == D_CONTEXT)
printf("Binary files %s and %s differ\n",
file1, file2);
goto closem;
}
close(f1); close(f2);
anychange = 1;
if (lflag)
calldiff(title);
else {
if (opt == D_EDIT) {
printf("ed - %s << '-*-END-*-'\n", dp->d_entry);
calldiff(0);
} else {
printf("%s%s %s\n", title, file1, file2);
calldiff(0);
}
if (opt == D_EDIT)
printf("w\nq\n-*-END-*-\n");
}
return;
closem:
close(f1); close(f2);
}
char *prargs[] = { "pr", "-h", 0, "-f", 0, 0 };
calldiff(wantpr)
char *wantpr;
{
int pid, status, status2, pv[2];
prargs[2] = wantpr;
fflush(stdout);
if (wantpr) {
sprintf(etitle, "%s %s", file1, file2);
pipe(pv);
pid = fork();
if (pid == -1) {
fprintf(stderr, "No more processes");
done(0);
}
if (pid == 0) {
close(0);
dup(pv[0]);
close(pv[0]);
close(pv[1]);
execv(pr+4, prargs);
execv(pr, prargs);
perror(pr);
done(0);
}
}
pid = fork();
if (pid == -1) {
fprintf(stderr, "diff: No more processes\n");
done(0);
}
if (pid == 0) {
if (wantpr) {
close(1);
dup(pv[1]);
close(pv[0]);
close(pv[1]);
}
execv(diff+4, diffargv);
execv(diff, diffargv);
perror(diff);
done(0);
}
if (wantpr) {
close(pv[0]);
close(pv[1]);
}
while (wait(&status) != pid)
continue;
while (wait(&status2) != -1)
continue;
/*
if ((status >> 8) >= 2)
done(0);
*/
}
#include <a.out.h>
ascii(f)
int f;
{
char buf[BUFSIZ];
register int cnt;
register char *cp;
lseek(f, (long)0, 0);
cnt = read(f, buf, BUFSIZ);
if (cnt >= sizeof (struct exec)) {
struct exec hdr;
hdr = *(struct exec *)buf;
if (!N_BADMAG(hdr))
return (0);
}
cp = buf;
while (--cnt >= 0)
if (*cp++ & 0200)
return (0);
return (1);
}
/*
* THIS IS CRUDE.
*/
useless(cp)
register char *cp;
{
if (cp[0] == '.') {
if (cp[1] == '\0')
return (1); /* directory "." */
if (cp[1] == '.' && cp[2] == '\0')
return (1); /* directory ".." */
}
if (start && strcmp(start, cp) > 0)
return (1);
return (0);
}

266
src/cmd/diff/diffh.c Normal file
View File

@@ -0,0 +1,266 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#define C 3
#define RANGE 30
#define LEN 255
#define INF 16384
char *text[2][RANGE];
long lineno[2] = {1, 1}; /*no. of 1st stored line in each file*/
int ntext[2]; /*number of stored lines in each*/
long n0,n1; /*scan pointer in each*/
int bflag;
int debug = 0;
FILE *file[2];
/* return pointer to line n of file f*/
char *getl(f,n)
long n;
{
register char *t;
register delta, nt;
again:
delta = n - lineno[f];
nt = ntext[f];
if(delta<0)
progerr("1");
if(delta<nt)
return(text[f][delta]);
if(delta>nt)
progerr("2");
if(nt>=RANGE)
progerr("3");
if(feof(file[f]))
return(NULL);
t = text[f][nt];
if(t==0) {
t = text[f][nt] = malloc(LEN+1);
if(t==NULL)
if(hardsynch())
goto again;
else
progerr("5");
}
t = fgets(t,LEN,file[f]);
if(t!=NULL)
ntext[f]++;
return(t);
}
/*remove thru line n of file f from storage*/
clrl(f,n)
long n;
{
register i,j;
j = n-lineno[f]+1;
for(i=0;i+j<ntext[f];i++)
movstr(text[f][i+j],text[f][i]);
lineno[f] = n+1;
ntext[f] -= j;
}
movstr(s,t)
register char *s, *t;
{
while(*t++= *s++)
continue;
}
main(argc,argv)
char **argv;
{
char *s0,*s1;
FILE *dopen();
register int status = 0;
while(*argv[1]=='-') {
argc--;
argv++;
while(*++argv[0])
if(*argv[0]=='b')
bflag++;
}
if(argc!=3)
error("must have 2 file arguments","");
file[0] = dopen(argv[1],argv[2]);
file[1] = dopen(argv[2],argv[1]);
for(;;) {
s0 = getl(0,++n0);
s1 = getl(1,++n1);
if(s0==NULL||s1==NULL)
break;
if(cmp(s0,s1)!=0) {
if(!easysynch()&&!hardsynch())
progerr("5");
status = 1;
} else {
clrl(0,n0);
clrl(1,n1);
}
}
if(s0==NULL&&s1==NULL)
exit(status);
if(s0==NULL)
output(-1,INF);
if(s1==NULL)
output(INF,-1);
exit(1);
}
/* synch on C successive matches*/
easysynch()
{
int i,j;
register k,m;
char *s0,*s1;
for(i=j=1;i<RANGE&&j<RANGE;i++,j++) {
s0 = getl(0,n0+i);
if(s0==NULL)
return(output(INF,INF));
for(k=C-1;k<j;k++) {
for(m=0;m<C;m++)
if(cmp(getl(0,n0+i-m),
getl(1,n1+k-m))!=0)
goto cont1;
return(output(i-C,k-C));
cont1: ;
}
s1 = getl(1,n1+j);
if(s1==NULL)
return(output(INF,INF));
for(k=C-1;k<=i;k++) {
for(m=0;m<C;m++)
if(cmp(getl(0,n0+k-m),
getl(1,n1+j-m))!=0)
goto cont2;
return(output(k-C,j-C));
cont2: ;
}
}
return(0);
}
output(a,b)
{
register i;
char *s;
if(a<0)
change(n0-1,0,n1,b,"a");
else if(b<0)
change(n0,a,n1-1,0,"d");
else
change(n0,a,n1,b,"c");
for(i=0;i<=a;i++) {
s = getl(0,n0+i);
if(s==NULL)
break;
printf("< %s",s);
clrl(0,n0+i);
}
n0 += i-1;
if(a>=0&&b>=0)
printf("---\n");
for(i=0;i<=b;i++) {
s = getl(1,n1+i);
if(s==NULL)
break;
printf("> %s",s);
clrl(1,n1+i);
}
n1 += i-1;
return(1);
}
change(a,b,c,d,s)
long a,c;
char *s;
{
range(a,b);
printf("%s",s);
range(c,d);
printf("\n");
}
range(a,b)
long a;
{
if(b==INF)
printf("%ld,$",a);
else if(b==0)
printf("%ld",a);
else
printf("%ld,%ld",a,a+b);
}
cmp(s,t)
char *s,*t;
{
if(debug)
printf("%s:%s\n",s,t);
for(;;){
if(bflag&&isspace(*s)&&isspace(*t)) {
while(isspace(*++s)) ;
while(isspace(*++t)) ;
}
if(*s!=*t||*s==0)
break;
s++;
t++;
}
return(*s-*t);
}
FILE *dopen(f1,f2)
char *f1,*f2;
{
FILE *f;
char b[100],*bptr,*eptr;
struct stat statbuf;
if(cmp(f1,"-")==0)
if(cmp(f2,"-")==0)
error("can't do - -","");
else
return(stdin);
if(stat(f1,&statbuf)==-1)
error("can't access ",f1);
if((statbuf.st_mode&S_IFMT)==S_IFDIR) {
for(bptr=b;*bptr= *f1++;bptr++) ;
*bptr++ = '/';
for(eptr=f2;*eptr;eptr++)
if(*eptr=='/'&&eptr[1]!=0&&eptr[1]!='/')
f2 = eptr+1;
while(*bptr++= *f2++) ;
f1 = b;
}
f = fopen(f1,"r");
if(f==NULL)
error("can't open",f1);
return(f);
}
progerr(s)
char *s;
{
error("program error ",s);
}
error(s,t)
char *s,*t;
{
fprintf(stderr,"diffh: %s%s\n",s,t);
exit(2);
}
/*stub for resychronization beyond limits of text buf*/
hardsynch()
{
change(n0,INF,n1,INF,"c");
printf("---change record omitted\n");
error("can't resynchronize","");
return(0);
}

1047
src/cmd/diff/diffreg.c Normal file

File diff suppressed because it is too large Load Diff