Update du, echo, ed, fgrep, file, grep, head, hostid, join.
This commit is contained in:
42
src/cmd/du.c
42
src/cmd/du.c
@@ -2,29 +2,29 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/dir.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/dir.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
char path[BUFSIZ], name[BUFSIZ];
|
||||
char path[BUFSIZ], name[BUFSIZ];
|
||||
int aflg;
|
||||
int sflg;
|
||||
char *dot = ".";
|
||||
char *dot = ".";
|
||||
|
||||
#define ML 1000
|
||||
#define ML 1000
|
||||
struct {
|
||||
int dev;
|
||||
ino_t ino;
|
||||
ino_t ino;
|
||||
} ml[ML];
|
||||
int mlx;
|
||||
|
||||
long descend();
|
||||
long descend();
|
||||
|
||||
#define kb(n) (howmany(n * DEV_BSIZE, 1024))
|
||||
#define kb(n) (howmany(n * DEV_BSIZE, 1024))
|
||||
|
||||
main(argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
long blocks = 0;
|
||||
register char *np;
|
||||
@@ -57,9 +57,9 @@ again:
|
||||
wait((int *)0);
|
||||
}
|
||||
if (argc == 1 || pid == 0) {
|
||||
(void) strcpy(path, *argv);
|
||||
(void) strcpy(name, *argv);
|
||||
if (np = rindex(name, '/')) {
|
||||
(void)strcpy(path, *argv);
|
||||
(void)strcpy(name, *argv);
|
||||
if ((np = rindex(name, '/'))) {
|
||||
*np++ = '\0';
|
||||
if (chdir(*name ? name : "/") < 0) {
|
||||
perror(*name ? name : "/");
|
||||
@@ -80,9 +80,7 @@ again:
|
||||
|
||||
DIR *dirp = NULL;
|
||||
|
||||
long
|
||||
descend(base, name)
|
||||
char *base, *name;
|
||||
long descend(char *base, char *name)
|
||||
{
|
||||
char *ebase0, *ebase;
|
||||
struct stat stb;
|
||||
@@ -99,7 +97,7 @@ descend(base, name)
|
||||
*ebase0 = 0;
|
||||
return (0);
|
||||
}
|
||||
if (stb.st_nlink > 1 && (stb.st_mode&S_IFMT) != S_IFDIR) {
|
||||
if (stb.st_nlink > 1 && (stb.st_mode & S_IFMT) != S_IFDIR) {
|
||||
for (i = 0; i <= mlx; i++)
|
||||
if (ml[i].ino == stb.st_ino && ml[i].dev == stb.st_dev)
|
||||
return (0);
|
||||
@@ -110,7 +108,7 @@ descend(base, name)
|
||||
}
|
||||
}
|
||||
blocks = stb.st_blocks;
|
||||
if ((stb.st_mode&S_IFMT) != S_IFDIR) {
|
||||
if ((stb.st_mode & S_IFMT) != S_IFDIR) {
|
||||
if (aflg)
|
||||
printf("%ld\t%s\n", kb(blocks), base);
|
||||
return (blocks);
|
||||
@@ -130,12 +128,12 @@ descend(base, name)
|
||||
dirp = NULL;
|
||||
return (0);
|
||||
}
|
||||
while (dp = readdir(dirp)) {
|
||||
while ((dp = readdir(dirp))) {
|
||||
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
|
||||
continue;
|
||||
(void) sprintf(ebase, "/%s", dp->d_name);
|
||||
(void)sprintf(ebase, "/%s", dp->d_name);
|
||||
curoff = telldir(dirp);
|
||||
blocks += descend(base, ebase+1);
|
||||
blocks += descend(base, ebase + 1);
|
||||
*ebase = 0;
|
||||
if (dirp == NULL) {
|
||||
dirp = opendir(".");
|
||||
@@ -151,7 +149,7 @@ descend(base, name)
|
||||
if (sflg == 0)
|
||||
printf("%ld\t%s\n", kb(blocks), base);
|
||||
if (chdir("..") < 0) {
|
||||
(void) sprintf(index(base, 0), "/..");
|
||||
(void)sprintf(index(base, 0), "/..");
|
||||
perror(base);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -1,24 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
main(argc, argv)
|
||||
int argc;
|
||||
char *argv[];
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
register int i, nflg;
|
||||
int i, nflg;
|
||||
|
||||
nflg = 0;
|
||||
if(argc > 1 && argv[1][0] == '-' && argv[1][1] == 'n' && !argv[1][2]) {
|
||||
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'n' && !argv[1][2]) {
|
||||
nflg++;
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
for(i=1; i<argc; i++) {
|
||||
for (i = 1; i < argc; i++) {
|
||||
fputs(argv[i], stdout);
|
||||
if (i < argc-1)
|
||||
if (i < argc - 1)
|
||||
putchar(' ');
|
||||
}
|
||||
if(nflg == 0)
|
||||
if (nflg == 0)
|
||||
putchar('\n');
|
||||
exit(0);
|
||||
}
|
||||
|
||||
1285
src/cmd/ed.c
1285
src/cmd/ed.c
File diff suppressed because it is too large
Load Diff
298
src/cmd/fgrep.c
298
src/cmd/fgrep.c
@@ -6,9 +6,11 @@
|
||||
* 1 - ok, but no matches
|
||||
* 2 - some error
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
@@ -18,31 +20,34 @@
|
||||
#define QSIZE 400
|
||||
|
||||
struct words {
|
||||
char inp;
|
||||
char out;
|
||||
struct words *nst;
|
||||
struct words *link;
|
||||
struct words *fail;
|
||||
char inp;
|
||||
char out;
|
||||
struct words *nst;
|
||||
struct words *link;
|
||||
struct words *fail;
|
||||
} w[MAXSIZ], *smax, *q;
|
||||
|
||||
long lnum;
|
||||
long lnum;
|
||||
int bflag, cflag, fflag, lflag, nflag, vflag, xflag, yflag;
|
||||
int hflag = 1;
|
||||
int hflag = 1;
|
||||
int sflag;
|
||||
int retcode = 0;
|
||||
int nfile;
|
||||
long blkno;
|
||||
long blkno;
|
||||
int nsucc;
|
||||
long tln;
|
||||
FILE *wordf;
|
||||
char *argptr;
|
||||
long tln;
|
||||
FILE *wordf;
|
||||
char *argptr;
|
||||
|
||||
main(argc, argv)
|
||||
char **argv;
|
||||
static void cgotofn(void);
|
||||
static void cfail(void);
|
||||
static void execute(char *file);
|
||||
static void overflo(void);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
while (--argc > 0 && (++argv)[0][0]=='-')
|
||||
while (--argc > 0 && (++argv)[0][0] == '-')
|
||||
switch (argv[0][1]) {
|
||||
|
||||
case 's':
|
||||
sflag++;
|
||||
continue;
|
||||
@@ -84,8 +89,8 @@ char **argv;
|
||||
xflag++;
|
||||
continue;
|
||||
|
||||
case 'i': /* Berkeley */
|
||||
case 'y': /* Btl */
|
||||
case 'i': /* Berkeley */
|
||||
case 'y': /* Btl */
|
||||
yflag++;
|
||||
continue;
|
||||
default:
|
||||
@@ -93,43 +98,43 @@ char **argv;
|
||||
continue;
|
||||
}
|
||||
out:
|
||||
if (argc<=0)
|
||||
if (argc <= 0)
|
||||
exit(2);
|
||||
if (fflag) {
|
||||
wordf = fopen(*argv, "r");
|
||||
if (wordf==NULL) {
|
||||
if (wordf == NULL) {
|
||||
fprintf(stderr, "fgrep: can't open %s\n", *argv);
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
else argptr = *argv;
|
||||
} else
|
||||
argptr = *argv;
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
cgotofn();
|
||||
cfail();
|
||||
nfile = argc;
|
||||
if (argc<=0) {
|
||||
if (lflag) exit(1);
|
||||
if (argc <= 0) {
|
||||
if (lflag)
|
||||
exit(1);
|
||||
execute((char *)NULL);
|
||||
}
|
||||
else while (--argc >= 0) {
|
||||
execute(*argv);
|
||||
argv++;
|
||||
}
|
||||
} else
|
||||
while (--argc >= 0) {
|
||||
execute(*argv);
|
||||
argv++;
|
||||
}
|
||||
exit(retcode != 0 ? retcode : nsucc == 0);
|
||||
}
|
||||
|
||||
#define ccomp(a,b) (yflag ? lca(a)==lca(b) : a==b)
|
||||
#define ccomp(a, b) (yflag ? lca(a) == lca(b) : a == b)
|
||||
#define lca(x) (isupper(x) ? tolower(x) : x)
|
||||
|
||||
execute(file)
|
||||
char *file;
|
||||
void execute(char *file)
|
||||
{
|
||||
register struct words *c;
|
||||
register ccount;
|
||||
register char ch;
|
||||
register char *p;
|
||||
struct words *c;
|
||||
int ccount;
|
||||
char ch;
|
||||
char *p;
|
||||
static char *buf;
|
||||
static int blksize;
|
||||
struct stat stb;
|
||||
@@ -142,14 +147,14 @@ char *file;
|
||||
retcode = 2;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else f = 0;
|
||||
} else
|
||||
f = 0;
|
||||
if (buf == NULL) {
|
||||
if (fstat(f, &stb) >= 0 && stb.st_blksize > 0)
|
||||
blksize = stb.st_blksize;
|
||||
else
|
||||
blksize = BLKSIZE;
|
||||
buf = (char *)malloc(2*blksize);
|
||||
buf = (char *)malloc(2 * blksize);
|
||||
if (buf == NULL) {
|
||||
fprintf(stderr, "egrep: no memory for %s\n", file);
|
||||
retcode = 2;
|
||||
@@ -166,83 +171,93 @@ char *file;
|
||||
c = w;
|
||||
for (;;) {
|
||||
if (--ccount <= 0) {
|
||||
if (p == &buf[2*blksize]) p = buf;
|
||||
if (p == &buf[2 * blksize])
|
||||
p = buf;
|
||||
if (p > &buf[blksize]) {
|
||||
if ((ccount = read(f, p, &buf[2*blksize] - p)) <= 0) break;
|
||||
}
|
||||
else if ((ccount = read(f, p, blksize)) <= 0) break;
|
||||
if ((ccount = read(f, p, &buf[2 * blksize] - p)) <= 0)
|
||||
break;
|
||||
} else if ((ccount = read(f, p, blksize)) <= 0)
|
||||
break;
|
||||
blkno += ccount;
|
||||
}
|
||||
nstate:
|
||||
if (ccomp(c->inp, *p)) {
|
||||
c = c->nst;
|
||||
}
|
||||
else if (c->link != 0) {
|
||||
c = c->link;
|
||||
goto nstate;
|
||||
}
|
||||
else {
|
||||
c = c->fail;
|
||||
failed = 1;
|
||||
if (c==0) {
|
||||
c = w;
|
||||
istate:
|
||||
if (ccomp(c->inp , *p)) {
|
||||
c = c->nst;
|
||||
}
|
||||
else if (c->link != 0) {
|
||||
c = c->link;
|
||||
goto istate;
|
||||
}
|
||||
nstate:
|
||||
if (ccomp(c->inp, *p)) {
|
||||
c = c->nst;
|
||||
} else if (c->link != 0) {
|
||||
c = c->link;
|
||||
goto nstate;
|
||||
} else {
|
||||
c = c->fail;
|
||||
failed = 1;
|
||||
if (c == 0) {
|
||||
c = w;
|
||||
istate:
|
||||
if (ccomp(c->inp, *p)) {
|
||||
c = c->nst;
|
||||
} else if (c->link != 0) {
|
||||
c = c->link;
|
||||
goto istate;
|
||||
}
|
||||
else goto nstate;
|
||||
}
|
||||
} else
|
||||
goto nstate;
|
||||
}
|
||||
if (c->out) {
|
||||
while (*p++ != '\n') {
|
||||
if (--ccount <= 0) {
|
||||
if (p == &buf[2*blksize]) p = buf;
|
||||
if (p == &buf[2 * blksize])
|
||||
p = buf;
|
||||
if (p > &buf[blksize]) {
|
||||
if ((ccount = read(f, p, &buf[2*blksize] - p)) <= 0) break;
|
||||
}
|
||||
else if ((ccount = read(f, p, blksize)) <= 0) break;
|
||||
if ((ccount = read(f, p, &buf[2 * blksize] - p)) <= 0)
|
||||
break;
|
||||
} else if ((ccount = read(f, p, blksize)) <= 0)
|
||||
break;
|
||||
blkno += ccount;
|
||||
}
|
||||
}
|
||||
if ( (vflag && (failed == 0 || xflag == 0)) || (vflag == 0 && xflag && failed) )
|
||||
if ((vflag && (failed == 0 || xflag == 0)) || (vflag == 0 && xflag && failed))
|
||||
goto nomatch;
|
||||
succeed: nsucc = 1;
|
||||
if (cflag) tln++;
|
||||
succeed:
|
||||
nsucc = 1;
|
||||
if (cflag)
|
||||
tln++;
|
||||
else if (sflag)
|
||||
; /* ugh */
|
||||
; /* ugh */
|
||||
else if (lflag) {
|
||||
printf("%s\n", file);
|
||||
close(f);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
if (nfile > 1 && hflag) printf("%s:", file);
|
||||
if (bflag) printf("%ld:", (blkno-ccount-1)/DEV_BSIZE);
|
||||
if (nflag) printf("%ld:", lnum);
|
||||
} else {
|
||||
if (nfile > 1 && hflag)
|
||||
printf("%s:", file);
|
||||
if (bflag)
|
||||
printf("%ld:", (blkno - ccount - 1) / DEV_BSIZE);
|
||||
if (nflag)
|
||||
printf("%ld:", lnum);
|
||||
if (p <= nlp) {
|
||||
while (nlp < &buf[2*blksize]) putchar(*nlp++);
|
||||
while (nlp < &buf[2 * blksize])
|
||||
putchar(*nlp++);
|
||||
nlp = buf;
|
||||
}
|
||||
while (nlp < p) putchar(*nlp++);
|
||||
while (nlp < p)
|
||||
putchar(*nlp++);
|
||||
}
|
||||
nomatch: lnum++;
|
||||
nomatch:
|
||||
lnum++;
|
||||
nlp = p;
|
||||
c = w;
|
||||
failed = 0;
|
||||
continue;
|
||||
}
|
||||
if (*p++ == '\n')
|
||||
if (vflag) goto succeed;
|
||||
else {
|
||||
if (*p++ == '\n') {
|
||||
if (vflag) {
|
||||
goto succeed;
|
||||
} else {
|
||||
lnum++;
|
||||
nlp = p;
|
||||
c = w;
|
||||
failed = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
close(f);
|
||||
if (cflag) {
|
||||
@@ -252,35 +267,40 @@ char *file;
|
||||
}
|
||||
}
|
||||
|
||||
getargc()
|
||||
int getargc()
|
||||
{
|
||||
register c;
|
||||
int c;
|
||||
|
||||
if (wordf)
|
||||
return(getc(wordf));
|
||||
return (getc(wordf));
|
||||
if ((c = *argptr++) == '\0')
|
||||
return(EOF);
|
||||
return(c);
|
||||
return (EOF);
|
||||
return (c);
|
||||
}
|
||||
|
||||
cgotofn() {
|
||||
register c;
|
||||
register struct words *s;
|
||||
void cgotofn()
|
||||
{
|
||||
int c;
|
||||
struct words *s;
|
||||
|
||||
s = smax = w;
|
||||
nword: for(;;) {
|
||||
nword:
|
||||
for (;;) {
|
||||
c = getargc();
|
||||
if (c==EOF)
|
||||
if (c == EOF)
|
||||
return;
|
||||
if (c == '\n') {
|
||||
if (xflag) {
|
||||
for(;;) {
|
||||
for (;;) {
|
||||
if (s->inp == c) {
|
||||
s = s->nst;
|
||||
break;
|
||||
}
|
||||
if (s->inp == 0) goto nenter;
|
||||
if (s->inp == 0)
|
||||
goto nenter;
|
||||
if (s->link == 0) {
|
||||
if (smax >= &w[MAXSIZ -1]) overflo();
|
||||
if (smax >= &w[MAXSIZ - 1])
|
||||
overflo();
|
||||
s->link = ++smax;
|
||||
s = smax;
|
||||
goto nenter;
|
||||
@@ -291,13 +311,16 @@ nword: for(;;) {
|
||||
s->out = 1;
|
||||
s = w;
|
||||
} else {
|
||||
loop: if (s->inp == c) {
|
||||
loop:
|
||||
if (s->inp == c) {
|
||||
s = s->nst;
|
||||
continue;
|
||||
}
|
||||
if (s->inp == 0) goto enter;
|
||||
if (s->inp == 0)
|
||||
goto enter;
|
||||
if (s->link == 0) {
|
||||
if (smax >= &w[MAXSIZ - 1]) overflo();
|
||||
if (smax >= &w[MAXSIZ - 1])
|
||||
overflo();
|
||||
s->link = ++smax;
|
||||
s = smax;
|
||||
goto enter;
|
||||
@@ -307,16 +330,19 @@ nword: for(;;) {
|
||||
}
|
||||
}
|
||||
|
||||
enter:
|
||||
enter:
|
||||
do {
|
||||
s->inp = c;
|
||||
if (smax >= &w[MAXSIZ - 1]) overflo();
|
||||
if (smax >= &w[MAXSIZ - 1])
|
||||
overflo();
|
||||
s->nst = ++smax;
|
||||
s = smax;
|
||||
} while ((c = getargc()) != '\n' && c!=EOF);
|
||||
} while ((c = getargc()) != '\n' && c != EOF);
|
||||
if (xflag) {
|
||||
nenter: s->inp = '\n';
|
||||
if (smax >= &w[MAXSIZ -1]) overflo();
|
||||
nenter:
|
||||
s->inp = '\n';
|
||||
if (smax >= &w[MAXSIZ - 1])
|
||||
overflo();
|
||||
s->nst = ++smax;
|
||||
}
|
||||
smax->out = 1;
|
||||
@@ -325,55 +351,69 @@ nword: for(;;) {
|
||||
goto nword;
|
||||
}
|
||||
|
||||
overflo() {
|
||||
void overflo()
|
||||
{
|
||||
fprintf(stderr, "wordlist too large\n");
|
||||
exit(2);
|
||||
}
|
||||
cfail() {
|
||||
|
||||
void cfail()
|
||||
{
|
||||
struct words *queue[QSIZE];
|
||||
struct words **front, **rear;
|
||||
struct words *state;
|
||||
int bstart;
|
||||
register char c;
|
||||
register struct words *s;
|
||||
char c;
|
||||
struct words *s;
|
||||
|
||||
s = w;
|
||||
front = rear = queue;
|
||||
init: if ((s->inp) != 0) {
|
||||
init:
|
||||
if ((s->inp) != 0) {
|
||||
*rear++ = s->nst;
|
||||
if (rear >= &queue[QSIZE - 1]) overflo();
|
||||
if (rear >= &queue[QSIZE - 1])
|
||||
overflo();
|
||||
}
|
||||
if ((s = s->link) != 0) {
|
||||
goto init;
|
||||
}
|
||||
|
||||
while (rear!=front) {
|
||||
while (rear != front) {
|
||||
s = *front;
|
||||
if (front == &queue[QSIZE-1])
|
||||
if (front == &queue[QSIZE - 1])
|
||||
front = queue;
|
||||
else front++;
|
||||
cloop: if ((c = s->inp) != 0) {
|
||||
else
|
||||
front++;
|
||||
cloop:
|
||||
if ((c = s->inp) != 0) {
|
||||
bstart = 0;
|
||||
*rear = (q = s->nst);
|
||||
if (front < rear)
|
||||
if (rear >= &queue[QSIZE-1])
|
||||
if (front == queue) overflo();
|
||||
else rear = queue;
|
||||
else rear++;
|
||||
else
|
||||
if (++rear == front) overflo();
|
||||
if (rear >= &queue[QSIZE - 1])
|
||||
if (front == queue)
|
||||
overflo();
|
||||
else
|
||||
rear = queue;
|
||||
else
|
||||
rear++;
|
||||
else if (++rear == front)
|
||||
overflo();
|
||||
state = s->fail;
|
||||
floop: if (state == 0) {
|
||||
floop:
|
||||
if (state == 0) {
|
||||
state = w;
|
||||
bstart = 1;
|
||||
}
|
||||
if (state->inp == c) {
|
||||
qloop: q->fail = state->nst;
|
||||
if ((state->nst)->out == 1) q->out = 1;
|
||||
if ((q = q->link) != 0) goto qloop;
|
||||
}
|
||||
else if ((state = state->link) != 0)
|
||||
qloop:
|
||||
q->fail = state->nst;
|
||||
if ((state->nst)->out == 1)
|
||||
q->out = 1;
|
||||
if ((q = q->link) != 0)
|
||||
goto qloop;
|
||||
} else if ((state = state->link) != 0)
|
||||
goto floop;
|
||||
else if(bstart == 0){
|
||||
else if (bstart == 0) {
|
||||
state = 0;
|
||||
goto floop;
|
||||
}
|
||||
|
||||
382
src/cmd/file.c
382
src/cmd/file.c
@@ -1,42 +1,46 @@
|
||||
/*
|
||||
* file - determine type of file
|
||||
*/
|
||||
#include <sys/param.h>
|
||||
#include <sys/stat.h>
|
||||
#include <a.out.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <a.out.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
int in;
|
||||
int i = 0;
|
||||
int i = 0;
|
||||
char buf[BUFSIZ];
|
||||
char *troff[] = { /* new troff intermediate lang */
|
||||
"x","T","res","init","font","202","V0","p1",0};
|
||||
char *fort[] = {
|
||||
"function","subroutine","common","dimension","block","integer",
|
||||
"real","data","double",0};
|
||||
char *asc[] = {
|
||||
"sys","mov","tst","clr","jmp",0};
|
||||
char *c[] = {
|
||||
"int","char","float","double","struct","extern",0};
|
||||
char *as[] = {
|
||||
"globl","byte","align","text","data","comm",0};
|
||||
char *sh[] = {
|
||||
"fi", "elif", "esac", "done", "export",
|
||||
"readonly", "trap", "PATH", "HOME", 0 };
|
||||
char *csh[] = {
|
||||
"alias", "breaksw", "endsw", "foreach", "limit", "onintr",
|
||||
"repeat", "setenv", "source", "path", "home", 0 };
|
||||
char *troff[] = { /* new troff intermediate lang */
|
||||
"x", "T", "res", "init", "font", "202", "V0", "p1", 0
|
||||
};
|
||||
char *fort[] = { "function", "subroutine", "common", "dimension", "block",
|
||||
"integer", "real", "data", "double", 0 };
|
||||
char *asc[] = { "sys", "mov", "tst", "clr", "jmp", 0 };
|
||||
char *c[] = { "int", "char", "float", "double", "struct", "extern", 0 };
|
||||
char *as[] = { "globl", "byte", "align", "text", "data", "comm", 0 };
|
||||
char *sh[] = { "fi", "elif", "esac", "done", "export", "readonly", "trap", "PATH", "HOME", 0 };
|
||||
char *csh[] = { "alias", "breaksw", "endsw", "foreach", "limit", "onintr",
|
||||
"repeat", "setenv", "source", "path", "home", 0 };
|
||||
int ifile;
|
||||
|
||||
main(argc, argv)
|
||||
char **argv;
|
||||
static void type(char *file);
|
||||
static int shellscript(char buf[], struct stat *sb);
|
||||
static int ccom(void);
|
||||
static int lookup(char *tab[]);
|
||||
static int ascom(void);
|
||||
static int shell(char *bp, int n, char *tab[]);
|
||||
static int troffint(char *bp, int n);
|
||||
static int english(char *bp, int n);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
register FILE *fl;
|
||||
register char *p;
|
||||
FILE *fl;
|
||||
char *p;
|
||||
char ap[MAXPATHLEN + 1];
|
||||
|
||||
if (argc < 2) {
|
||||
@@ -44,23 +48,23 @@ char **argv;
|
||||
exit(3);
|
||||
}
|
||||
|
||||
if (argc>1 && argv[1][0]=='-' && argv[1][1]=='f') {
|
||||
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'f') {
|
||||
if ((fl = fopen(argv[2], "r")) == NULL) {
|
||||
perror(argv[2]);
|
||||
exit(2);
|
||||
}
|
||||
while ((p = fgets(ap, sizeof ap, fl)) != NULL) {
|
||||
int l = strlen(p);
|
||||
if (l>0)
|
||||
p[l-1] = '\0';
|
||||
if (l > 0)
|
||||
p[l - 1] = '\0';
|
||||
printf("%s: ", p);
|
||||
type(p);
|
||||
if (ifile>=0)
|
||||
if (ifile >= 0)
|
||||
close(ifile);
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
while(argc > 1) {
|
||||
while (argc > 1) {
|
||||
printf("%s: ", argv[1]);
|
||||
type(argv[1]);
|
||||
fflush(stdout);
|
||||
@@ -72,10 +76,9 @@ char **argv;
|
||||
exit(0);
|
||||
}
|
||||
|
||||
type(file)
|
||||
char *file;
|
||||
void type(char *file)
|
||||
{
|
||||
register int j;
|
||||
int j;
|
||||
int nl;
|
||||
char ch;
|
||||
struct stat mbuf;
|
||||
@@ -87,7 +90,6 @@ char *file;
|
||||
return;
|
||||
}
|
||||
switch (mbuf.st_mode & S_IFMT) {
|
||||
|
||||
case S_IFLNK:
|
||||
printf("symbolic link");
|
||||
j = readlink(file, slink, sizeof slink - 1);
|
||||
@@ -106,9 +108,8 @@ char *file;
|
||||
|
||||
case S_IFCHR:
|
||||
case S_IFBLK:
|
||||
printf("%s special (%d/%d)\n",
|
||||
(mbuf.st_mode&S_IFMT) == S_IFCHR ? "character" : "block",
|
||||
major(mbuf.st_rdev), minor(mbuf.st_rdev));
|
||||
printf("%s special (%d/%d)\n", (mbuf.st_mode & S_IFMT) == S_IFCHR ? "character" : "block",
|
||||
major(mbuf.st_rdev), minor(mbuf.st_rdev));
|
||||
return;
|
||||
|
||||
case S_IFSOCK:
|
||||
@@ -117,17 +118,16 @@ char *file;
|
||||
}
|
||||
|
||||
ifile = open(file, 0);
|
||||
if(ifile < 0) {
|
||||
if (ifile < 0) {
|
||||
printf("%s\n", strerror(errno));
|
||||
return;
|
||||
}
|
||||
in = read(ifile, buf, BUFSIZ);
|
||||
if(in == 0){
|
||||
if (in == 0) {
|
||||
printf("empty\n");
|
||||
return;
|
||||
}
|
||||
switch(*(int *)buf) {
|
||||
|
||||
switch (*(int *)buf) {
|
||||
case 0413:
|
||||
printf("demand paged ");
|
||||
|
||||
@@ -140,7 +140,7 @@ char *file;
|
||||
goto exec;
|
||||
|
||||
case 0407:
|
||||
exec:
|
||||
exec:
|
||||
if (mbuf.st_mode & S_ISUID)
|
||||
printf("set-uid ");
|
||||
if (mbuf.st_mode & S_ISGID)
|
||||
@@ -148,10 +148,9 @@ exec:
|
||||
if (mbuf.st_mode & S_ISVTX)
|
||||
printf("sticky ");
|
||||
printf("executable");
|
||||
if(((int *)buf)[4] != 0)
|
||||
if (((int *)buf)[4] != 0)
|
||||
printf(" not stripped");
|
||||
if (((int *)buf)[1] == 0 && ((int *)buf)[2] != 0 &&
|
||||
((int *)buf)[3] == 0)
|
||||
if (((int *)buf)[1] == 0 && ((int *)buf)[2] != 0 && ((int *)buf)[3] == 0)
|
||||
printf(" (likely vax)");
|
||||
printf("\n");
|
||||
return;
|
||||
@@ -169,109 +168,125 @@ exec:
|
||||
return;
|
||||
}
|
||||
|
||||
if (buf[0] == '#' && buf[1] == '!' && shellscript(buf+2, &mbuf))
|
||||
if (buf[0] == '#' && buf[1] == '!' && shellscript(buf + 2, &mbuf))
|
||||
return;
|
||||
if (buf[0] == '\037' && buf[1] == '\235') {
|
||||
if (buf[2]&0x80)
|
||||
if (buf[2] & 0x80)
|
||||
printf("block ");
|
||||
printf("compressed %d bit code data\n", buf[2]&0x1f);
|
||||
printf("compressed %d bit code data\n", buf[2] & 0x1f);
|
||||
return;
|
||||
}
|
||||
if(strncmp(buf, "!<arch>\n__.SYMDEF", 17) == 0 ) {
|
||||
if (strncmp(buf, "!<arch>\n__.SYMDEF", 17) == 0) {
|
||||
printf("archive random library\n");
|
||||
return;
|
||||
}
|
||||
if (strncmp(buf, "!<arch>\n", 8)==0) {
|
||||
if (strncmp(buf, "!<arch>\n", 8) == 0) {
|
||||
printf("archive\n");
|
||||
return;
|
||||
}
|
||||
if (mbuf.st_size % 512 == 0) { /* it may be a PRESS file */
|
||||
lseek(ifile, -512L, 2); /* last block */
|
||||
if (mbuf.st_size % 512 == 0) { /* it may be a PRESS file */
|
||||
lseek(ifile, -512L, 2); /* last block */
|
||||
if (read(ifile, buf, BUFSIZ) > 0 && *(short *)buf == 12138) {
|
||||
printf("PRESS file\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
i = 0;
|
||||
if(ccom() == 0)goto notc;
|
||||
while(buf[i] == '#'){
|
||||
if (ccom() == 0)
|
||||
goto notc;
|
||||
while (buf[i] == '#') {
|
||||
j = i;
|
||||
while(buf[i++] != '\n'){
|
||||
if(i - j > 255){
|
||||
while (buf[i++] != '\n') {
|
||||
if (i - j > 255) {
|
||||
printf("data\n");
|
||||
return;
|
||||
}
|
||||
if(i >= in)goto notc;
|
||||
if (i >= in)
|
||||
goto notc;
|
||||
}
|
||||
if(ccom() == 0)goto notc;
|
||||
if (ccom() == 0)
|
||||
goto notc;
|
||||
}
|
||||
check:
|
||||
if(lookup(c) == 1){
|
||||
while((ch = buf[i++]) != ';' && ch != '{')if(i >= in)goto notc;
|
||||
if (lookup(c) == 1) {
|
||||
while ((ch = buf[i++]) != ';' && ch != '{')
|
||||
if (i >= in)
|
||||
goto notc;
|
||||
printf("c program text");
|
||||
goto outa;
|
||||
}
|
||||
nl = 0;
|
||||
while(buf[i] != '('){
|
||||
if(buf[i] <= 0)
|
||||
while (buf[i] != '(') {
|
||||
if (buf[i] <= 0)
|
||||
goto notas;
|
||||
if(buf[i] == ';'){
|
||||
if (buf[i] == ';') {
|
||||
i++;
|
||||
goto check;
|
||||
}
|
||||
if(buf[i++] == '\n')
|
||||
if(nl++ > 6)goto notc;
|
||||
if(i >= in)goto notc;
|
||||
if (buf[i++] == '\n')
|
||||
if (nl++ > 6)
|
||||
goto notc;
|
||||
if (i >= in)
|
||||
goto notc;
|
||||
}
|
||||
while(buf[i] != ')'){
|
||||
if(buf[i++] == '\n')
|
||||
if(nl++ > 6)goto notc;
|
||||
if(i >= in)goto notc;
|
||||
while (buf[i] != ')') {
|
||||
if (buf[i++] == '\n')
|
||||
if (nl++ > 6)
|
||||
goto notc;
|
||||
if (i >= in)
|
||||
goto notc;
|
||||
}
|
||||
while(buf[i] != '{'){
|
||||
if(buf[i++] == '\n')
|
||||
if(nl++ > 6)goto notc;
|
||||
if(i >= in)goto notc;
|
||||
while (buf[i] != '{') {
|
||||
if (buf[i++] == '\n')
|
||||
if (nl++ > 6)
|
||||
goto notc;
|
||||
if (i >= in)
|
||||
goto notc;
|
||||
}
|
||||
printf("c program text");
|
||||
goto outa;
|
||||
notc:
|
||||
i = 0;
|
||||
while(buf[i] == 'c' || buf[i] == '#'){
|
||||
while(buf[i++] != '\n')if(i >= in)goto notfort;
|
||||
while (buf[i] == 'c' || buf[i] == '#') {
|
||||
while (buf[i++] != '\n')
|
||||
if (i >= in)
|
||||
goto notfort;
|
||||
}
|
||||
if(lookup(fort) == 1){
|
||||
if (lookup(fort) == 1) {
|
||||
printf("fortran program text");
|
||||
goto outa;
|
||||
}
|
||||
notfort:
|
||||
i=0;
|
||||
if(ascom() == 0)goto notas;
|
||||
j = i-1;
|
||||
if(buf[i] == '.'){
|
||||
i = 0;
|
||||
if (ascom() == 0)
|
||||
goto notas;
|
||||
j = i - 1;
|
||||
if (buf[i] == '.') {
|
||||
i++;
|
||||
if(lookup(as) == 1){
|
||||
if (lookup(as) == 1) {
|
||||
printf("assembler program text");
|
||||
goto outa;
|
||||
}
|
||||
else if(buf[j] == '\n' && isalpha(buf[j+2])){
|
||||
} else if (buf[j] == '\n' && isalpha(buf[j + 2])) {
|
||||
printf("roff, nroff, or eqn input text");
|
||||
goto outa;
|
||||
}
|
||||
}
|
||||
while(lookup(asc) == 0){
|
||||
if(ascom() == 0)goto notas;
|
||||
while(buf[i] != '\n' && buf[i++] != ':')
|
||||
if(i >= in)goto notas;
|
||||
while(buf[i] == '\n' || buf[i] == ' ' || buf[i] == '\t')if(i++ >= in)goto notas;
|
||||
j = i-1;
|
||||
if(buf[i] == '.'){
|
||||
while (lookup(asc) == 0) {
|
||||
if (ascom() == 0)
|
||||
goto notas;
|
||||
while (buf[i] != '\n' && buf[i++] != ':')
|
||||
if (i >= in)
|
||||
goto notas;
|
||||
while (buf[i] == '\n' || buf[i] == ' ' || buf[i] == '\t')
|
||||
if (i++ >= in)
|
||||
goto notas;
|
||||
j = i - 1;
|
||||
if (buf[i] == '.') {
|
||||
i++;
|
||||
if(lookup(as) == 1){
|
||||
if (lookup(as) == 1) {
|
||||
printf("assembler program text");
|
||||
goto outa;
|
||||
}
|
||||
else if(buf[j] == '\n' && isalpha(buf[j+2])){
|
||||
} else if (buf[j] == '\n' && isalpha(buf[j + 2])) {
|
||||
printf("roff, nroff, or eqn input text");
|
||||
goto outa;
|
||||
}
|
||||
@@ -280,14 +295,15 @@ notfort:
|
||||
printf("assembler program text");
|
||||
goto outa;
|
||||
notas:
|
||||
for(i=0; i < in; i++)if(buf[i]&0200){
|
||||
if (buf[0]=='\100' && buf[1]=='\357')
|
||||
printf("troff (CAT) output\n");
|
||||
else
|
||||
printf("data\n");
|
||||
return;
|
||||
}
|
||||
if (mbuf.st_mode&((S_IEXEC)|(S_IEXEC>>3)|(S_IEXEC>>6))) {
|
||||
for (i = 0; i < in; i++)
|
||||
if (buf[i] & 0200) {
|
||||
if (buf[0] == '\100' && buf[1] == '\357')
|
||||
printf("troff (CAT) output\n");
|
||||
else
|
||||
printf("data\n");
|
||||
return;
|
||||
}
|
||||
if (mbuf.st_mode & ((S_IEXEC) | (S_IEXEC >> 3) | (S_IEXEC >> 6))) {
|
||||
if (mbuf.st_mode & S_ISUID)
|
||||
printf("set-uid ");
|
||||
if (mbuf.st_mode & S_ISGID)
|
||||
@@ -311,103 +327,117 @@ notas:
|
||||
else
|
||||
printf("ascii text");
|
||||
outa:
|
||||
while(i < in)
|
||||
if((buf[i++]&0377) > 127){
|
||||
while (i < in) {
|
||||
if ((buf[i++] & 0377) > 127) {
|
||||
printf(" with garbage\n");
|
||||
return;
|
||||
}
|
||||
/* if next few lines in then read whole file looking for nulls ...
|
||||
while((in = read(ifile,buf,BUFSIZ)) > 0)
|
||||
for(i = 0; i < in; i++)
|
||||
if((buf[i]&0377) > 127){
|
||||
printf(" with garbage\n");
|
||||
return;
|
||||
}
|
||||
/*.... */
|
||||
}
|
||||
#if 0
|
||||
/* if next few lines in then read whole file looking for nulls... */
|
||||
while ((in = read(ifile,buf,BUFSIZ)) > 0) {
|
||||
for (i = 0; i < in; i++) {
|
||||
if ((buf[i]&0377) > 127) {
|
||||
printf(" with garbage\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
troffint(bp, n)
|
||||
char *bp;
|
||||
int n;
|
||||
int troffint(char *bp, int n)
|
||||
{
|
||||
int k;
|
||||
|
||||
i = 0;
|
||||
for (k = 0; k < 6; k++) {
|
||||
if (lookup(troff) == 0)
|
||||
return(0);
|
||||
return (0);
|
||||
if (lookup(troff) == 0)
|
||||
return(0);
|
||||
return (0);
|
||||
while (i < n && buf[i] != '\n')
|
||||
i++;
|
||||
if (i++ >= n)
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
return(1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
lookup(tab)
|
||||
char *tab[];
|
||||
int lookup(char *tab[])
|
||||
{
|
||||
char r;
|
||||
int k,j,l;
|
||||
while(buf[i] == ' ' || buf[i] == '\t' || buf[i] == '\n')i++;
|
||||
for(j=0; tab[j] != 0; j++){
|
||||
l=0;
|
||||
for(k=i; ((r=tab[j][l++]) == buf[k] && r != '\0');k++);
|
||||
if(r == '\0')
|
||||
if(buf[k] == ' ' || buf[k] == '\n' || buf[k] == '\t'
|
||||
|| buf[k] == '{' || buf[k] == '/'){
|
||||
i=k;
|
||||
return(1);
|
||||
int k, j, l;
|
||||
while (buf[i] == ' ' || buf[i] == '\t' || buf[i] == '\n')
|
||||
i++;
|
||||
for (j = 0; tab[j] != 0; j++) {
|
||||
l = 0;
|
||||
for (k = i; ((r = tab[j][l++]) == buf[k] && r != '\0'); k++)
|
||||
;
|
||||
if (r == '\0')
|
||||
if (buf[k] == ' ' || buf[k] == '\n' || buf[k] == '\t' || buf[k] == '{' ||
|
||||
buf[k] == '/') {
|
||||
i = k;
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
ccom()
|
||||
int ccom()
|
||||
{
|
||||
char cc;
|
||||
while((cc = buf[i]) == ' ' || cc == '\t' || cc == '\n')if(i++ >= in)return(0);
|
||||
if(buf[i] == '/' && buf[i+1] == '*'){
|
||||
while ((cc = buf[i]) == ' ' || cc == '\t' || cc == '\n')
|
||||
if (i++ >= in)
|
||||
return (0);
|
||||
if (buf[i] == '/' && buf[i + 1] == '*') {
|
||||
i += 2;
|
||||
while(buf[i] != '*' || buf[i+1] != '/'){
|
||||
if(buf[i] == '\\')i += 2;
|
||||
else i++;
|
||||
if(i >= in)return(0);
|
||||
while (buf[i] != '*' || buf[i + 1] != '/') {
|
||||
if (buf[i] == '\\')
|
||||
i += 2;
|
||||
else
|
||||
i++;
|
||||
if (i >= in)
|
||||
return (0);
|
||||
}
|
||||
if((i += 2) >= in)return(0);
|
||||
if ((i += 2) >= in)
|
||||
return (0);
|
||||
}
|
||||
if(buf[i] == '\n')if(ccom() == 0)return(0);
|
||||
return(1);
|
||||
if (buf[i] == '\n')
|
||||
if (ccom() == 0)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
ascom()
|
||||
int ascom()
|
||||
{
|
||||
while(buf[i] == '/'){
|
||||
while (buf[i] == '/') {
|
||||
i++;
|
||||
while(buf[i++] != '\n')if(i >= in)return(0);
|
||||
while(buf[i] == '\n')if(i++ >= in)return(0);
|
||||
while (buf[i++] != '\n')
|
||||
if (i >= in)
|
||||
return (0);
|
||||
while (buf[i] == '\n')
|
||||
if (i++ >= in)
|
||||
return (0);
|
||||
}
|
||||
return(1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
english (bp, n)
|
||||
char *bp;
|
||||
int english(char *bp, int n)
|
||||
{
|
||||
#define NASC 128
|
||||
int ct[NASC], j, vow, freq, rare;
|
||||
int badpun = 0, punct = 0;
|
||||
if (n<50) return(0); /* no point in statistics on squibs */
|
||||
for(j=0; j<NASC; j++)
|
||||
ct[j]=0;
|
||||
for(j=0; j<n; j++)
|
||||
{
|
||||
if (bp[j]<NASC)
|
||||
ct[bp[j]|040]++;
|
||||
switch (bp[j])
|
||||
{
|
||||
if (n < 50)
|
||||
return (0); /* no point in statistics on squibs */
|
||||
for (j = 0; j < NASC; j++)
|
||||
ct[j] = 0;
|
||||
for (j = 0; j < n; j++) {
|
||||
unsigned c = bp[j];
|
||||
if (c < NASC)
|
||||
ct[c | 040]++;
|
||||
switch (c) {
|
||||
case '.':
|
||||
case ',':
|
||||
case ')':
|
||||
@@ -416,27 +446,25 @@ char *bp;
|
||||
case ':':
|
||||
case '?':
|
||||
punct++;
|
||||
if ( j < n-1 &&
|
||||
bp[j+1] != ' ' &&
|
||||
bp[j+1] != '\n')
|
||||
if (j < n - 1 && bp[j + 1] != ' ' && bp[j + 1] != '\n')
|
||||
badpun++;
|
||||
}
|
||||
}
|
||||
if (badpun*5 > punct)
|
||||
return(0);
|
||||
if (badpun * 5 > punct)
|
||||
return (0);
|
||||
vow = ct['a'] + ct['e'] + ct['i'] + ct['o'] + ct['u'];
|
||||
freq = ct['e'] + ct['t'] + ct['a'] + ct['i'] + ct['o'] + ct['n'];
|
||||
rare = ct['v'] + ct['j'] + ct['k'] + ct['q'] + ct['x'] + ct['z'];
|
||||
if (2*ct[';'] > ct['e']) return(0);
|
||||
if ( (ct['>']+ct['<']+ct['/'])>ct['e']) return(0); /* shell file test */
|
||||
return (vow*5 >= n-ct[' '] && freq >= 10*rare);
|
||||
if (2 * ct[';'] > ct['e'])
|
||||
return (0);
|
||||
if ((ct['>'] + ct['<'] + ct['/']) > ct['e'])
|
||||
return (0); /* shell file test */
|
||||
return (vow * 5 >= n - ct[' '] && freq >= 10 * rare);
|
||||
}
|
||||
|
||||
shellscript(buf, sb)
|
||||
char buf[];
|
||||
struct stat *sb;
|
||||
int shellscript(char buf[], struct stat *sb)
|
||||
{
|
||||
register char *tp;
|
||||
char *tp;
|
||||
char *cp, *xp, *index();
|
||||
|
||||
cp = index(buf, '\n');
|
||||
@@ -454,9 +482,9 @@ shellscript(buf, sb)
|
||||
printf("set-uid ");
|
||||
if (sb->st_mode & S_ISGID)
|
||||
printf("set-gid ");
|
||||
if (strncmp(xp, "/bin/sh", tp-xp) == 0)
|
||||
if (strncmp(xp, "/bin/sh", tp - xp) == 0)
|
||||
xp = "shell";
|
||||
else if (strncmp(xp, "/bin/csh", tp-xp) == 0)
|
||||
else if (strncmp(xp, "/bin/csh", tp - xp) == 0)
|
||||
xp = "c-shell";
|
||||
else
|
||||
*tp = '\0';
|
||||
@@ -464,12 +492,8 @@ shellscript(buf, sb)
|
||||
return (1);
|
||||
}
|
||||
|
||||
shell(bp, n, tab)
|
||||
char *bp;
|
||||
int n;
|
||||
char *tab[];
|
||||
int shell(char *bp, int n, char *tab[])
|
||||
{
|
||||
|
||||
i = 0;
|
||||
do {
|
||||
if (buf[i] == '#' || buf[i] == ':')
|
||||
|
||||
416
src/cmd/grep.c
416
src/cmd/grep.c
@@ -6,66 +6,62 @@
|
||||
* 1 - ok, but no matches
|
||||
* 2 - some error
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define CBRA 1
|
||||
#define CCHR 2
|
||||
#define CDOT 4
|
||||
#define CBRA 1
|
||||
#define CCHR 2
|
||||
#define CDOT 4
|
||||
#define CCL 6
|
||||
#define NCCL 8
|
||||
#define CDOL 10
|
||||
#define CEOF 11
|
||||
#define CKET 12
|
||||
#define CBRC 14
|
||||
#define CLET 15
|
||||
#define CBACK 18
|
||||
#define NCCL 8
|
||||
#define CDOL 10
|
||||
#define CEOF 11
|
||||
#define CKET 12
|
||||
#define CBRC 14
|
||||
#define CLET 15
|
||||
#define CBACK 18
|
||||
|
||||
#define STAR 01
|
||||
#define STAR 01
|
||||
|
||||
#define LBSIZE BUFSIZ
|
||||
#define ESIZE 256
|
||||
#define NBRA 9
|
||||
#define LBSIZE BUFSIZ
|
||||
#define ESIZE 256
|
||||
#define NBRA 9
|
||||
|
||||
char expbuf[ESIZE];
|
||||
long lnum;
|
||||
char linebuf[LBSIZE+1];
|
||||
char ybuf[ESIZE];
|
||||
char expbuf[ESIZE];
|
||||
long lnum;
|
||||
char linebuf[LBSIZE + 1];
|
||||
char ybuf[ESIZE];
|
||||
int bflag;
|
||||
int lflag;
|
||||
int nflag;
|
||||
int cflag;
|
||||
int vflag;
|
||||
int nfile;
|
||||
int hflag = 1;
|
||||
int hflag = 1;
|
||||
int sflag;
|
||||
int yflag;
|
||||
int wflag;
|
||||
int retcode = 0;
|
||||
int circf;
|
||||
int blkno;
|
||||
long tln;
|
||||
long tln;
|
||||
int nsucc;
|
||||
char *braslist[NBRA];
|
||||
char *braelist[NBRA];
|
||||
char bittab[] = {
|
||||
1,
|
||||
2,
|
||||
4,
|
||||
8,
|
||||
16,
|
||||
32,
|
||||
64,
|
||||
128
|
||||
};
|
||||
char *braslist[NBRA];
|
||||
char *braelist[NBRA];
|
||||
char bittab[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
|
||||
|
||||
main(argc, argv)
|
||||
char **argv;
|
||||
static void errexit(char *s, char *f);
|
||||
static void compile(char *astr);
|
||||
static void execute(char *file);
|
||||
static int advance(char *lp, char *ep);
|
||||
static void succeed(char *f);
|
||||
static int ecmp(char *a, char *b, int count);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
while (--argc > 0 && (++argv)[0][0]=='-')
|
||||
while (--argc > 0 && (++argv)[0][0] == '-')
|
||||
switch (argv[0][1]) {
|
||||
|
||||
case 'i':
|
||||
case 'y':
|
||||
yflag++;
|
||||
@@ -113,11 +109,11 @@ char **argv;
|
||||
continue;
|
||||
}
|
||||
out:
|
||||
if (argc<=0)
|
||||
if (argc <= 0)
|
||||
exit(2);
|
||||
if (yflag) {
|
||||
register char *p, *s;
|
||||
for (s = ybuf, p = *argv; *p; ) {
|
||||
char *p, *s;
|
||||
for (s = ybuf, p = *argv; *p;) {
|
||||
if (*p == '\\') {
|
||||
*s++ = *p++;
|
||||
if (*p)
|
||||
@@ -132,7 +128,7 @@ out:
|
||||
*s++ = ']';
|
||||
} else
|
||||
*s++ = *p++;
|
||||
if (s >= ybuf+ESIZE-5)
|
||||
if (s >= ybuf + ESIZE - 5)
|
||||
errexit("grep: argument too long\n", (char *)NULL);
|
||||
}
|
||||
*s = '\0';
|
||||
@@ -140,22 +136,22 @@ out:
|
||||
}
|
||||
compile(*argv);
|
||||
nfile = --argc;
|
||||
if (argc<=0) {
|
||||
if (argc <= 0) {
|
||||
if (lflag)
|
||||
exit(1);
|
||||
execute((char *)NULL);
|
||||
} else while (--argc >= 0) {
|
||||
argv++;
|
||||
execute(*argv);
|
||||
}
|
||||
} else
|
||||
while (--argc >= 0) {
|
||||
argv++;
|
||||
execute(*argv);
|
||||
}
|
||||
exit(retcode != 0 ? retcode : nsucc == 0);
|
||||
}
|
||||
|
||||
compile(astr)
|
||||
char *astr;
|
||||
void compile(char *astr)
|
||||
{
|
||||
register c;
|
||||
register char *ep, *sp;
|
||||
int c;
|
||||
char *ep, *sp;
|
||||
char *cstart;
|
||||
char *lastep;
|
||||
int cclcnt;
|
||||
@@ -181,7 +177,6 @@ char *astr;
|
||||
if ((c = *sp++) != '*')
|
||||
lastep = ep;
|
||||
switch (c) {
|
||||
|
||||
case '\0':
|
||||
if (wflag)
|
||||
*ep++ = CLET;
|
||||
@@ -193,8 +188,8 @@ char *astr;
|
||||
continue;
|
||||
|
||||
case '*':
|
||||
if (lastep==0 || *lastep==CBRA || *lastep==CKET ||
|
||||
*lastep == CBRC || *lastep == CLET)
|
||||
if (lastep == 0 || *lastep == CBRA || *lastep == CKET || *lastep == CBRC ||
|
||||
*lastep == CLET)
|
||||
goto defchar;
|
||||
*lastep |= STAR;
|
||||
continue;
|
||||
@@ -206,27 +201,27 @@ char *astr;
|
||||
continue;
|
||||
|
||||
case '[':
|
||||
if(&ep[17] >= &expbuf[ESIZE])
|
||||
if (&ep[17] >= &expbuf[ESIZE])
|
||||
goto cerror;
|
||||
*ep++ = CCL;
|
||||
neg = 0;
|
||||
if((c = *sp++) == '^') {
|
||||
if ((c = *sp++) == '^') {
|
||||
neg = 1;
|
||||
c = *sp++;
|
||||
}
|
||||
cstart = sp;
|
||||
do {
|
||||
if (c=='\0')
|
||||
if (c == '\0')
|
||||
goto cerror;
|
||||
if (c=='-' && sp>cstart && *sp!=']') {
|
||||
for (c = sp[-2]; c<*sp; c++)
|
||||
ep[c>>3] |= bittab[c&07];
|
||||
if (c == '-' && sp > cstart && *sp != ']') {
|
||||
for (c = sp[-2]; c < *sp; c++)
|
||||
ep[c >> 3] |= bittab[c & 07];
|
||||
sp++;
|
||||
}
|
||||
ep[c>>3] |= bittab[c&07];
|
||||
} while((c = *sp++) != ']');
|
||||
if(neg) {
|
||||
for(cclcnt = 0; cclcnt < 16; cclcnt++)
|
||||
ep[c >> 3] |= bittab[c & 07];
|
||||
} while ((c = *sp++) != ']');
|
||||
if (neg) {
|
||||
for (cclcnt = 0; cclcnt < 16; cclcnt++)
|
||||
ep[cclcnt] ^= -1;
|
||||
ep[0] &= 0376;
|
||||
}
|
||||
@@ -236,18 +231,18 @@ char *astr;
|
||||
continue;
|
||||
|
||||
case '\\':
|
||||
if((c = *sp++) == 0)
|
||||
if ((c = *sp++) == 0)
|
||||
goto cerror;
|
||||
if(c == '<') {
|
||||
if (c == '<') {
|
||||
*ep++ = CBRC;
|
||||
continue;
|
||||
}
|
||||
if(c == '>') {
|
||||
if (c == '>') {
|
||||
*ep++ = CLET;
|
||||
continue;
|
||||
}
|
||||
if(c == '(') {
|
||||
if(numbra >= NBRA) {
|
||||
if (c == '(') {
|
||||
if (numbra >= NBRA) {
|
||||
goto cerror;
|
||||
}
|
||||
*bracketp++ = numbra;
|
||||
@@ -255,8 +250,8 @@ char *astr;
|
||||
*ep++ = numbra++;
|
||||
continue;
|
||||
}
|
||||
if(c == ')') {
|
||||
if(bracketp <= bracket) {
|
||||
if (c == ')') {
|
||||
if (bracketp <= bracket) {
|
||||
goto cerror;
|
||||
}
|
||||
*ep++ = CKET;
|
||||
@@ -265,8 +260,8 @@ char *astr;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(c >= '1' && c <= '9') {
|
||||
if((c -= '1') >= closed)
|
||||
if (c >= '1' && c <= '9') {
|
||||
if ((c -= '1') >= closed)
|
||||
goto cerror;
|
||||
*ep++ = CBACK;
|
||||
*ep++ = c;
|
||||
@@ -279,15 +274,14 @@ char *astr;
|
||||
*ep++ = c;
|
||||
}
|
||||
}
|
||||
cerror:
|
||||
cerror:
|
||||
errexit("grep: RE error\n", (char *)NULL);
|
||||
}
|
||||
|
||||
execute(file)
|
||||
char *file;
|
||||
void execute(char *file)
|
||||
{
|
||||
register char *p1, *p2;
|
||||
register c;
|
||||
char *p1, *p2;
|
||||
int c;
|
||||
|
||||
if (file) {
|
||||
if (freopen(file, "r", stdin) == NULL) {
|
||||
@@ -303,15 +297,15 @@ char *file;
|
||||
while ((c = getchar()) != '\n') {
|
||||
if (c == EOF) {
|
||||
if (cflag) {
|
||||
if (nfile>1)
|
||||
if (nfile > 1)
|
||||
printf("%s:", file);
|
||||
printf("%D\n", tln);
|
||||
printf("%ld\n", tln);
|
||||
fflush(stdout);
|
||||
}
|
||||
return;
|
||||
}
|
||||
*p1++ = c;
|
||||
if (p1 >= &linebuf[LBSIZE-1])
|
||||
if (p1 >= &linebuf[LBSIZE - 1])
|
||||
break;
|
||||
}
|
||||
*p1++ = '\0';
|
||||
@@ -323,10 +317,10 @@ char *file;
|
||||
goto nfound;
|
||||
}
|
||||
/* fast check for first character */
|
||||
if (*p2==CCHR) {
|
||||
if (*p2 == CCHR) {
|
||||
c = p2[1];
|
||||
do {
|
||||
if (*p1!=c)
|
||||
if (*p1 != c)
|
||||
continue;
|
||||
if (advance(p1, p2))
|
||||
goto found;
|
||||
@@ -343,142 +337,142 @@ char *file;
|
||||
succeed(file);
|
||||
continue;
|
||||
found:
|
||||
if (vflag==0)
|
||||
if (vflag == 0)
|
||||
succeed(file);
|
||||
}
|
||||
}
|
||||
|
||||
advance(lp, ep)
|
||||
register char *lp, *ep;
|
||||
int advance(char *lp, char *ep)
|
||||
{
|
||||
register char *curlp;
|
||||
char *curlp;
|
||||
char c;
|
||||
char *bbeg;
|
||||
int ct;
|
||||
|
||||
for (;;) switch (*ep++) {
|
||||
|
||||
case CCHR:
|
||||
if (*ep++ == *lp++)
|
||||
continue;
|
||||
return(0);
|
||||
|
||||
case CDOT:
|
||||
if (*lp++)
|
||||
continue;
|
||||
return(0);
|
||||
|
||||
case CDOL:
|
||||
if (*lp==0)
|
||||
continue;
|
||||
return(0);
|
||||
|
||||
case CEOF:
|
||||
return(1);
|
||||
|
||||
case CCL:
|
||||
c = *lp++ & 0177;
|
||||
if(ep[c>>3] & bittab[c & 07]) {
|
||||
ep += 16;
|
||||
continue;
|
||||
}
|
||||
return(0);
|
||||
case CBRA:
|
||||
braslist[*ep++] = lp;
|
||||
continue;
|
||||
|
||||
case CKET:
|
||||
braelist[*ep++] = lp;
|
||||
continue;
|
||||
|
||||
case CBACK:
|
||||
bbeg = braslist[*ep];
|
||||
if (braelist[*ep]==0)
|
||||
return(0);
|
||||
ct = braelist[*ep++] - bbeg;
|
||||
if(ecmp(bbeg, lp, ct)) {
|
||||
lp += ct;
|
||||
continue;
|
||||
}
|
||||
return(0);
|
||||
|
||||
case CBACK|STAR:
|
||||
bbeg = braslist[*ep];
|
||||
if (braelist[*ep]==0)
|
||||
return(0);
|
||||
ct = braelist[*ep++] - bbeg;
|
||||
curlp = lp;
|
||||
while(ecmp(bbeg, lp, ct))
|
||||
lp += ct;
|
||||
while(lp >= curlp) {
|
||||
if(advance(lp, ep)) return(1);
|
||||
lp -= ct;
|
||||
}
|
||||
return(0);
|
||||
|
||||
|
||||
case CDOT|STAR:
|
||||
curlp = lp;
|
||||
while (*lp++);
|
||||
goto star;
|
||||
|
||||
case CCHR|STAR:
|
||||
curlp = lp;
|
||||
while (*lp++ == *ep);
|
||||
ep++;
|
||||
goto star;
|
||||
|
||||
case CCL|STAR:
|
||||
curlp = lp;
|
||||
do {
|
||||
c = *lp++ & 0177;
|
||||
} while(ep[c>>3] & bittab[c & 07]);
|
||||
ep += 16;
|
||||
goto star;
|
||||
|
||||
star:
|
||||
if(--lp == curlp) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(*ep == CCHR) {
|
||||
c = ep[1];
|
||||
do {
|
||||
if(*lp != c)
|
||||
continue;
|
||||
if(advance(lp, ep))
|
||||
return(1);
|
||||
} while(lp-- > curlp);
|
||||
return(0);
|
||||
}
|
||||
|
||||
do {
|
||||
if (advance(lp, ep))
|
||||
return(1);
|
||||
} while (lp-- > curlp);
|
||||
return(0);
|
||||
|
||||
case CBRC:
|
||||
if (lp == expbuf)
|
||||
continue;
|
||||
#define uletter(c) (isalpha(c) || (c) == '_')
|
||||
if (uletter(*lp) || isdigit(*lp))
|
||||
if (!uletter(lp[-1]) && !isdigit(lp[-1]))
|
||||
for (;;)
|
||||
switch (*ep++) {
|
||||
case CCHR:
|
||||
if (*ep++ == *lp++)
|
||||
continue;
|
||||
return (0);
|
||||
return (0);
|
||||
|
||||
case CLET:
|
||||
if (!uletter(*lp) && !isdigit(*lp))
|
||||
case CDOT:
|
||||
if (*lp++)
|
||||
continue;
|
||||
return (0);
|
||||
|
||||
case CDOL:
|
||||
if (*lp == 0)
|
||||
continue;
|
||||
return (0);
|
||||
|
||||
case CEOF:
|
||||
return (1);
|
||||
|
||||
case CCL:
|
||||
c = *lp++ & 0177;
|
||||
if (ep[c >> 3] & bittab[c & 07]) {
|
||||
ep += 16;
|
||||
continue;
|
||||
}
|
||||
return (0);
|
||||
case CBRA:
|
||||
braslist[*ep++] = lp;
|
||||
continue;
|
||||
return (0);
|
||||
|
||||
default:
|
||||
errexit("grep RE botch\n", (char *)NULL);
|
||||
}
|
||||
case CKET:
|
||||
braelist[*ep++] = lp;
|
||||
continue;
|
||||
|
||||
case CBACK:
|
||||
bbeg = braslist[*ep];
|
||||
if (braelist[*ep] == 0)
|
||||
return (0);
|
||||
ct = braelist[*ep++] - bbeg;
|
||||
if (ecmp(bbeg, lp, ct)) {
|
||||
lp += ct;
|
||||
continue;
|
||||
}
|
||||
return (0);
|
||||
|
||||
case CBACK | STAR:
|
||||
bbeg = braslist[*ep];
|
||||
if (braelist[*ep] == 0)
|
||||
return (0);
|
||||
ct = braelist[*ep++] - bbeg;
|
||||
curlp = lp;
|
||||
while (ecmp(bbeg, lp, ct))
|
||||
lp += ct;
|
||||
while (lp >= curlp) {
|
||||
if (advance(lp, ep))
|
||||
return (1);
|
||||
lp -= ct;
|
||||
}
|
||||
return (0);
|
||||
|
||||
case CDOT | STAR:
|
||||
curlp = lp;
|
||||
while (*lp++)
|
||||
;
|
||||
goto star;
|
||||
|
||||
case CCHR | STAR:
|
||||
curlp = lp;
|
||||
while (*lp++ == *ep)
|
||||
;
|
||||
ep++;
|
||||
goto star;
|
||||
|
||||
case CCL | STAR:
|
||||
curlp = lp;
|
||||
do {
|
||||
c = *lp++ & 0177;
|
||||
} while (ep[c >> 3] & bittab[c & 07]);
|
||||
ep += 16;
|
||||
goto star;
|
||||
|
||||
star:
|
||||
if (--lp == curlp) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (*ep == CCHR) {
|
||||
c = ep[1];
|
||||
do {
|
||||
if (*lp != c)
|
||||
continue;
|
||||
if (advance(lp, ep))
|
||||
return (1);
|
||||
} while (lp-- > curlp);
|
||||
return (0);
|
||||
}
|
||||
|
||||
do {
|
||||
if (advance(lp, ep))
|
||||
return (1);
|
||||
} while (lp-- > curlp);
|
||||
return (0);
|
||||
|
||||
case CBRC:
|
||||
if (lp == expbuf)
|
||||
continue;
|
||||
#define uletter(c) (isalpha(c) || (c) == '_')
|
||||
if (uletter(*lp) || isdigit(*lp))
|
||||
if (!uletter(lp[-1]) && !isdigit(lp[-1]))
|
||||
continue;
|
||||
return (0);
|
||||
|
||||
case CLET:
|
||||
if (!uletter(*lp) && !isdigit(*lp))
|
||||
continue;
|
||||
return (0);
|
||||
|
||||
default:
|
||||
errexit("grep RE botch\n", (char *)NULL);
|
||||
}
|
||||
}
|
||||
|
||||
succeed(f)
|
||||
char *f;
|
||||
void succeed(char *f)
|
||||
{
|
||||
nsucc = 1;
|
||||
if (sflag)
|
||||
@@ -503,17 +497,17 @@ char *f;
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
ecmp(a, b, count)
|
||||
char *a, *b;
|
||||
int ecmp(char *a, char *b, int count)
|
||||
{
|
||||
register cc = count;
|
||||
while(cc--)
|
||||
if(*a++ != *b++) return(0);
|
||||
return(1);
|
||||
int cc = count;
|
||||
|
||||
while (cc--)
|
||||
if (*a++ != *b++)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
errexit(s, f)
|
||||
char *s, *f;
|
||||
void errexit(char *s, char *f)
|
||||
{
|
||||
fprintf(stderr, s, f);
|
||||
exit(2);
|
||||
|
||||
@@ -9,18 +9,18 @@
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int linecnt = 10;
|
||||
int argc;
|
||||
static int getnum(char *cp);
|
||||
static void copyout(int cnt);
|
||||
|
||||
main(Argc, argv)
|
||||
int Argc;
|
||||
char *argv[];
|
||||
int main(int Argc, char *argv[])
|
||||
{
|
||||
register int argc;
|
||||
int argc;
|
||||
char *name;
|
||||
register char *argp;
|
||||
static int around;
|
||||
char *argp;
|
||||
int linecnt = 10;
|
||||
int around = 0;
|
||||
|
||||
Argc--, argv++;
|
||||
argc = Argc;
|
||||
@@ -51,10 +51,9 @@ main(Argc, argv)
|
||||
} while (argc > 0);
|
||||
}
|
||||
|
||||
copyout(cnt)
|
||||
register int cnt;
|
||||
void copyout(int cnt)
|
||||
{
|
||||
register int c;
|
||||
int c;
|
||||
char lbuf[BUFSIZ];
|
||||
|
||||
while (cnt > 0 && fgets(lbuf, sizeof lbuf, stdin) != 0) {
|
||||
@@ -64,10 +63,9 @@ copyout(cnt)
|
||||
}
|
||||
}
|
||||
|
||||
getnum(cp)
|
||||
register char *cp;
|
||||
int getnum(char *cp)
|
||||
{
|
||||
register int i;
|
||||
int i;
|
||||
|
||||
for (i = 0; *cp >= '0' && *cp <= '9'; cp++)
|
||||
i *= 10, i += *cp - '0';
|
||||
|
||||
@@ -3,23 +3,24 @@
|
||||
* All rights reserved. The Berkeley software License Agreement
|
||||
* specifies the terms and conditions for redistribution.
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#if HAVE_NET
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
main(argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
void usage()
|
||||
{
|
||||
register char *id;
|
||||
errx(1, "usage: [hexnum or internet name/address]");
|
||||
/* NOTREACHED */
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char *id;
|
||||
u_long addr;
|
||||
long hostid;
|
||||
struct hostent *hp;
|
||||
@@ -36,7 +37,7 @@ main(argc, argv)
|
||||
hostid = addr;
|
||||
} else
|
||||
#endif
|
||||
if (index(id, '.')) {
|
||||
if (index(id, '.')) {
|
||||
if ((hostid = inet_addr(id)) == -1L)
|
||||
usage();
|
||||
} else {
|
||||
@@ -50,9 +51,3 @@ main(argc, argv)
|
||||
err(1, "sethostid");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
usage()
|
||||
{
|
||||
errx (1,"usage: [hexnum or internet name/address]");
|
||||
/* NOTREACHED */
|
||||
}
|
||||
|
||||
117
src/cmd/join.c
117
src/cmd/join.c
@@ -3,30 +3,36 @@
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define F1 0
|
||||
#define F2 1
|
||||
#define NFLD 20 /* max field per line */
|
||||
#define comp() cmp(ppi[F1][fj1],ppi[F2][fj2])
|
||||
#define F1 0
|
||||
#define F2 1
|
||||
#define NFLD 20 /* max field per line */
|
||||
#define comp() cmp(ppi[F1][fj1], ppi[F2][fj2])
|
||||
|
||||
FILE *f[2];
|
||||
char buf[2][BUFSIZ]; /*input lines */
|
||||
char *ppi[2][NFLD]; /* pointers to fields in lines */
|
||||
char *s1,*s2;
|
||||
int fj1 = 1; /* join of this field of file 1 */
|
||||
int fj2 = 1; /* join of this field of file 2 */
|
||||
int olist[2*NFLD]; /* output these fields */
|
||||
int olistf[2*NFLD]; /* from these files */
|
||||
int no; /* number of entries in olist */
|
||||
int sep1 = ' '; /* default field separator */
|
||||
int sep2 = '\t';
|
||||
char* null = "";
|
||||
char buf[2][BUFSIZ]; /*input lines */
|
||||
char *ppi[2][NFLD]; /* pointers to fields in lines */
|
||||
char *s1, *s2;
|
||||
int fj1 = 1; /* join of this field of file 1 */
|
||||
int fj2 = 1; /* join of this field of file 2 */
|
||||
int olist[2 * NFLD]; /* output these fields */
|
||||
int olistf[2 * NFLD]; /* from these files */
|
||||
int no; /* number of entries in olist */
|
||||
int sep1 = ' '; /* default field separator */
|
||||
int sep2 = '\t';
|
||||
char *null = "";
|
||||
int unpub1;
|
||||
int unpub2;
|
||||
int aflg;
|
||||
|
||||
main(argc, argv)
|
||||
char *argv[];
|
||||
static void error(char *fmt, ...);
|
||||
static int input(int n);
|
||||
static int cmp(char *s1, char *s2);
|
||||
static void output(int on1, int on2);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
int n1, n2;
|
||||
@@ -38,7 +44,7 @@ char *argv[];
|
||||
break;
|
||||
switch (argv[1][1]) {
|
||||
case 'a':
|
||||
switch(argv[1][2]) {
|
||||
switch (argv[1][2]) {
|
||||
case '1':
|
||||
aflg |= 1;
|
||||
break;
|
||||
@@ -58,7 +64,7 @@ char *argv[];
|
||||
sep1 = sep2 = argv[1][2];
|
||||
break;
|
||||
case 'o':
|
||||
for (no = 0; no < 2*NFLD; no++) {
|
||||
for (no = 0; no < 2 * NFLD; no++) {
|
||||
if (argv[2][0] == '1' && argv[2][1] == '.') {
|
||||
olistf[no] = F1;
|
||||
olist[no] = atoi(&argv[2][2]);
|
||||
@@ -90,7 +96,7 @@ char *argv[];
|
||||
if (argc != 3)
|
||||
error("usage: join [-an] [-estring] [-j1 x -j2 y] [-o list] [-tc] file1 file2");
|
||||
fj1--;
|
||||
fj2--; /* everyone else believes in 0 origin */
|
||||
fj2--; /* everyone else believes in 0 origin */
|
||||
s1 = ppi[F1][fj1];
|
||||
s2 = ppi[F2][fj2];
|
||||
if (argv[1][0] == '-')
|
||||
@@ -100,21 +106,23 @@ char *argv[];
|
||||
if ((f[F2] = fopen(argv[2], "r")) == NULL)
|
||||
error("can't open %s", argv[2]);
|
||||
|
||||
#define get1() n1=input(F1)
|
||||
#define get2() n2=input(F2)
|
||||
#define get1() n1 = input(F1)
|
||||
#define get2() n2 = input(F2)
|
||||
get1();
|
||||
bot2 = ftell(f[F2]);
|
||||
get2();
|
||||
while(n1>0 && n2>0 || aflg!=0 && n1+n2>0) {
|
||||
if(n1>0 && n2>0 && comp()>0 || n1==0) {
|
||||
if(aflg&2) output(0, n2);
|
||||
while (n1 > 0 && n2 > 0 || aflg != 0 && n1 + n2 > 0) {
|
||||
if (n1 > 0 && n2 > 0 && comp() > 0 || n1 == 0) {
|
||||
if (aflg & 2)
|
||||
output(0, n2);
|
||||
bot2 = ftell(f[F2]);
|
||||
get2();
|
||||
} else if(n1>0 && n2>0 && comp()<0 || n2==0) {
|
||||
if(aflg&1) output(n1, 0);
|
||||
} else if (n1 > 0 && n2 > 0 && comp() < 0 || n2 == 0) {
|
||||
if (aflg & 1)
|
||||
output(n1, 0);
|
||||
get1();
|
||||
} else /*(n1>0 && n2>0 && comp()==0)*/ {
|
||||
while(n2>0 && comp()==0) {
|
||||
while (n2 > 0 && comp() == 0) {
|
||||
output(n1, n2);
|
||||
top2 = ftell(f[F2]);
|
||||
get2();
|
||||
@@ -122,15 +130,15 @@ char *argv[];
|
||||
fseek(f[F2], bot2, 0);
|
||||
get2();
|
||||
get1();
|
||||
for(;;) {
|
||||
if(n1>0 && n2>0 && comp()==0) {
|
||||
for (;;) {
|
||||
if (n1 > 0 && n2 > 0 && comp() == 0) {
|
||||
output(n1, n2);
|
||||
get2();
|
||||
} else if(n1>0 && n2>0 && comp()<0 || n2==0) {
|
||||
} else if (n1 > 0 && n2 > 0 && comp() < 0 || n2 == 0) {
|
||||
fseek(f[F2], bot2, 0);
|
||||
get2();
|
||||
get1();
|
||||
} else /*(n1>0 && n2>0 && comp()>0 || n1==0)*/{
|
||||
} else /*(n1>0 && n2>0 && comp()>0 || n1==0)*/ {
|
||||
fseek(f[F2], top2, 0);
|
||||
bot2 = top2;
|
||||
get2();
|
||||
@@ -139,10 +147,11 @@ char *argv[];
|
||||
}
|
||||
}
|
||||
}
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
input(n) /* get input line and split into fields */
|
||||
/* get input line and split into fields */
|
||||
int input(int n)
|
||||
{
|
||||
register int i, c;
|
||||
char *bp;
|
||||
@@ -151,11 +160,11 @@ input(n) /* get input line and split into fields */
|
||||
bp = buf[n];
|
||||
pp = ppi[n];
|
||||
if (fgets(bp, BUFSIZ, f[n]) == NULL)
|
||||
return(0);
|
||||
for (i = 0; ; i++) {
|
||||
if (sep1 == ' ') /* strip multiples */
|
||||
return (0);
|
||||
for (i = 0;; i++) {
|
||||
if (sep1 == ' ') /* strip multiples */
|
||||
while ((c = *bp) == sep1 || c == sep2)
|
||||
bp++; /* skip blanks */
|
||||
bp++; /* skip blanks */
|
||||
else
|
||||
c = *bp;
|
||||
if (c == '\n' || c == '\0')
|
||||
@@ -163,21 +172,21 @@ input(n) /* get input line and split into fields */
|
||||
*pp++ = bp; /* record beginning */
|
||||
while ((c = *bp) != sep1 && c != '\n' && c != sep2 && c != '\0')
|
||||
bp++;
|
||||
*bp++ = '\0'; /* mark end by overwriting blank */
|
||||
/* fails badly if string doesn't have \n at end */
|
||||
*bp++ = '\0'; /* mark end by overwriting blank */
|
||||
/* fails badly if string doesn't have \n at end */
|
||||
}
|
||||
*pp = 0;
|
||||
return(i);
|
||||
return (i);
|
||||
}
|
||||
|
||||
output(on1, on2) /* print items from olist */
|
||||
int on1, on2;
|
||||
/* print items from olist */
|
||||
void output(int on1, int on2)
|
||||
{
|
||||
int i;
|
||||
char *temp;
|
||||
|
||||
if (no <= 0) { /* default case */
|
||||
printf("%s", on1? ppi[F1][fj1]: ppi[F2][fj2]);
|
||||
if (no <= 0) { /* default case */
|
||||
printf("%s", on1 ? ppi[F1][fj1] : ppi[F2][fj2]);
|
||||
for (i = 0; i < on1; i++)
|
||||
if (i != fj1)
|
||||
printf("%c%s", sep1, ppi[F1][i]);
|
||||
@@ -188,9 +197,8 @@ int on1, on2;
|
||||
} else {
|
||||
for (i = 0; i < no; i++) {
|
||||
temp = ppi[olistf[i]][olist[i]];
|
||||
if(olistf[i]==F1 && on1<=olist[i] ||
|
||||
olistf[i]==F2 && on2<=olist[i] ||
|
||||
*temp==0)
|
||||
if (olistf[i] == F1 && on1 <= olist[i] || olistf[i] == F2 && on2 <= olist[i] ||
|
||||
*temp == 0)
|
||||
temp = null;
|
||||
printf("%s", temp);
|
||||
if (i == no - 1)
|
||||
@@ -201,17 +209,18 @@ int on1, on2;
|
||||
}
|
||||
}
|
||||
|
||||
error(s1, s2, s3, s4, s5)
|
||||
char *s1;
|
||||
void error(char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
fprintf(stderr, "join: ");
|
||||
fprintf(stderr, s1, s2, s3, s4, s5);
|
||||
vfprintf(stderr, fmt, args);
|
||||
fprintf(stderr, "\n");
|
||||
va_end(args);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
cmp(s1, s2)
|
||||
char *s1, *s2;
|
||||
int cmp(char *s1, char *s2)
|
||||
{
|
||||
return(strcmp(s1, s2));
|
||||
return (strcmp(s1, s2));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user