silvert@dalcs.UUCP (Bill Silvert) (04/24/85)
Here are several utilities from Bourne's "The Unix System" and from Kernighan & Pike's "The Unix Programming Environment". They are slightly modified but should be pretty close to the originals. Run the following shell script: ---------------------cut here------------------------------------- cat >field.c << xxFIELDxx /* field utility, from Bourne pp.228-9 */ #include <stdio.h> static char SCCSID[] = "@(#)field.c Ver. 1.9, 85/04/23 18:58:12"; #define MAXF 256 #define MAXL 4096 #define IFS ':' /* Field separator is colon on input, */ #define OFS ':' /* and on output. */ int fv[MAXF]; int nf; int mf; char *fp[MAXF]; char L[MAXL]; main(argc,argv) int argc; char *argv[]; { register char *cp; register char **ap; register int c; static char FIELD[] = "@(#)field.c 1.9 85/04/23: : in, \\t out"; /* SCCS identifier */ int f; while (argc>1) { if(sscanf(argv[1], "%d", &fv[nf++]) != 1) { printf("usage: field [ n ] ...\n"); return(2); } argc--; argv++; } /* read and copy input */ nf--; cp = L; ap = fp; *ap++ = cp; while(1){ c = getc(stdin); if(c=='\n' || c== EOF) { int fc; if(cp==L && c==EOF) break; *cp++ = 0; mf = ap-fp; /* print this line */ for(fc = 0; fc <= nf; fc++){ putf(fv[fc]-1); if(fc != nf) putchar(OFS); } if(c == EOF) break; putchar('\n'); cp = L; ap = fp; *ap++ = cp; } else if(c == IFS) { *cp++ = 0; *ap++ = cp; } else *cp++ = c; } return(0); } /* output field n from current line */ putf(n) { register char *cp = fp[n]; register char c; if(n<0 || n>=mf) return; while (c = *cp++) putchar(c); } xxFIELDxx cat >zap.c <<xxZAPxx /* zap: interactive process killer from Kernighan & Pike p. 191 */ static char SCCSID[] = "@(#)zap.c Ver. 1.1, 85/03/04 14:10:58"; #include <stdio.h> #include <signal.h> char *progname ; /* name of program (zap) */ char *ps ; /* form of the ps command */ main(argc,argv) int argc; char *argv[]; { FILE *fin, *popen(); char buf[BUFSIZ]; int pid; if(geteuid() == 0) ps = "ps -ag" ; /* only root can kill others' programs */ else ps = "ps -g" ; /* for all ordinary users */ progname=argv[0]; if((fin=popen(ps,"r")) == NULL) { fprintf(stderr,"%s: can't run %s\n",progname,ps); exit(1); } fgets(buf,sizeof buf,fin); /* get header line */ fprintf(stderr,"%s",buf); while(fgets(buf,sizeof buf,fin) != NULL) if(argc == 1 || strindex(buf,argv[1]) >= 0) { buf[strlen(buf)-1] = '\0'; /* suppress \n */ fprintf(stderr,"%s\t? ",buf); if(ttyin() == 'y') { sscanf(buf,"%d",&pid); kill(pid,SIGKILL); } } exit(0); } xxZAPxx cat >pick.c <<xxPICKxx /* pick: Kernighan & Pike p. 187 */ static char SCCSID[] = "@(#)pick.c Ver. 1.1, 85/03/04 14:45:27"; #include <stdio.h> char *progname; main(argc,argv) int argc; char *argv[]; { int i; char buf[BUFSIZ]; progname = argv[0]; if(argc == 2 && strcmp(argv[1],"-") == 0) while(fgets(buf,sizeof buf,stdin) != NULL) { buf[strlen(buf)-1] = '\0'; pick(buf); } else for(i=1;i<argc;i++) pick(argv[i]); exit(0); } pick(s) char *s; { fprintf(stderr,"%s? ",s); if(ttyin() == 'y') /* p. 185 */ printf("%s\n",s); } xxPICKxx cat >ttyin.c <<xxTTYINxx #include <stdio.h> static char SCCSID[] = "@(#)ttyin.c Ver. 1.3, 85/03/04 16:04:32"; ttyin() /* process response from /dev/tty -- Kernighan & Pike p. 185 */ { char buf[BUFSIZ]; FILE *fopen(); /* K&P use their own efopen, p. 182 */ static FILE *tty=NULL; if(tty==NULL) tty=fopen("/dev/tty","r"); for(;;) { if(fgets(buf,BUFSIZ,tty)==NULL || buf[0]=='q') exit(0); else if(buf[0]=='!') { system(buf+1); /* p. 224, available as system(3) */ printf("!\n"); } else /* ordinary line */ return buf[0]; } } xxTTYINxx cat >strindex.c <<xxSTRINDEXxx static char SCCSID[] = "@(#)strindex.c Ver. 1.2, 85/03/04 15:54:30"; strindex(s,t) /* C equivalent of fortran INDEX index(3) does characters, not strings Kernighan & Pike p. 192 */ char *s, *t; { int i,n; n=strlen(t); for(i=0;s[i] != '\0'; i++) if(strncmp(s+i,t,n) == 0) return i; return -1; /* INDEX returns 0, but C strings start at 0 */ } xxSTRINDEXxx -- Bill Silvert Marine Ecology Lab. Dartmouth, NS dalcs!biomel!bill