diff --git a/src/cmd/tail.c b/src/cmd/tail.c index cab8a22..96ac5d9 100644 --- a/src/cmd/tail.c +++ b/src/cmd/tail.c @@ -17,13 +17,15 @@ * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. */ +#include +#include #include #include -#include -#include -#include +#include +#include #include -#include +#include +#include #ifdef pdp11 #define LBIN 16385 @@ -31,189 +33,201 @@ #define LBIN 32769 #endif -#undef BUFSIZ -#define BUFSIZ 8192 +#undef BUFSIZ +#define BUFSIZ 8192 -struct stat statb; +struct stat statb; int follow; int piped; char bin[LBIN]; -int errno; -main(argc,argv) -char **argv; +static void fexit(void); + +int main(int argc, char **argv) { - long n,di; - register i,j,k; - char *arg; - int partial,bylines,bkwds,fromend,lastnl; + long n, di; + int i, j, k; + char *arg; + int partial, bylines, bkwds, fromend, lastnl; char *p; arg = argv[1]; - if(argc<=1 || *arg!='-'&&*arg!='+') { + if (argc <= 1 || *arg != '-' && *arg != '+') { arg = "-10l"; argc++; argv--; } - fromend = *arg=='-'; + fromend = *arg == '-'; arg++; if (isdigit(*arg)) { n = 0; - while(isdigit(*arg)) - n = n*10 + *arg++ - '0'; + while (isdigit(*arg)) + n = n * 10 + *arg++ - '0'; } else n = -1; - if(!fromend&&n>0) + if (!fromend && n > 0) n--; - if(argc>2) { + if (argc > 2) { (void)close(0); - if(open(argv[2],0)!=0) { + if (open(argv[2], 0) != 0) { perror(argv[2]); exit(1); } } - (void)lseek(0,(off_t)0,L_INCR); - piped = errno==ESPIPE; - bylines = -1; bkwds = 0; - while(*arg) - switch(*arg++) { - - case 'b': - if (n == -1) n = 1; - n <<= 9; - if(bylines!=-1) goto errcom; - bylines=0; - break; - case 'c': - if(bylines!=-1) goto errcom; - bylines=0; - break; - case 'f': - follow = 1; - break; - case 'r': - if(n==-1) n = LBIN; - bkwds = 1; fromend = 1; bylines = 1; - break; - case 'l': - if(bylines!=-1) goto errcom; + (void)lseek(0, (off_t)0, L_INCR); + piped = errno == ESPIPE; + bylines = -1; + bkwds = 0; + while (*arg) + switch (*arg++) { + case 'b': + if (n == -1) + n = 1; + n <<= 9; + if (bylines != -1) + goto errcom; + bylines = 0; + break; + case 'c': + if (bylines != -1) + goto errcom; + bylines = 0; + break; + case 'f': + follow = 1; + break; + case 'r': + if (n == -1) + n = LBIN; + bkwds = 1; + fromend = 1; + bylines = 1; + break; + case 'l': + if (bylines != -1) + goto errcom; + bylines = 1; + break; + default: + goto errcom; + } + if (n == -1) + n = 10; + if (bylines == -1) bylines = 1; - break; - default: - goto errcom; - } - if (n==-1) n = 10; - if(bylines==-1) bylines = 1; - if(bkwds) follow=0; - if(fromend) + if (bkwds) + follow = 0; + if (fromend) goto keep; - /*seek from beginning */ + /*seek from beginning */ - if(bylines) { + if (bylines) { j = 0; - while(n-->0) { + while (n-- > 0) { do { - if(j--<=0) { + if (j-- <= 0) { p = bin; - j = read(0,p,BUFSIZ); - if(j--<=0) + j = read(0, p, BUFSIZ); + if (j-- <= 0) fexit(); } - } while(*p++ != '\n'); + } while (*p++ != '\n'); } - (void)write(1,p,j); - } else if(n>0) { - if(!piped) - (void)fstat(0,&statb); - if(piped||(statb.st_mode&S_IFMT)==S_IFCHR) - while(n>0) { - i = n>BUFSIZ?BUFSIZ:n; - i = read(0,bin,i); - if(i<=0) + (void)write(1, p, j); + } else if (n > 0) { + if (!piped) + (void)fstat(0, &statb); + if (piped || (statb.st_mode & S_IFMT) == S_IFCHR) + while (n > 0) { + i = n > BUFSIZ ? BUFSIZ : n; + i = read(0, bin, i); + if (i <= 0) fexit(); n -= i; } else - (void)lseek(0,(off_t)n,L_SET); + (void)lseek(0, (off_t)n, L_SET); } copy: - while((i=read(0,bin,BUFSIZ))>0) - (void)write(1,bin,i); + while ((i = read(0, bin, BUFSIZ)) > 0) + (void)write(1, bin, i); fexit(); - /*seek from end*/ + /*seek from end*/ keep: - if(n <= 0) + if (n <= 0) fexit(); - if(!piped) { - (void)fstat(0,&statb); + if (!piped) { + (void)fstat(0, &statb); /* If by lines, back up 1 buffer: else back up as needed */ - di = bylines?LBIN-1:n; - if(statb.st_size > di) - (void)lseek(0,(off_t)-di,L_XTND); - if(!bylines) + di = bylines ? LBIN - 1 : n; + if (statb.st_size > di) + (void)lseek(0, (off_t)-di, L_XTND); + if (!bylines) goto copy; } partial = 1; - for(;;) { + for (;;) { i = 0; do { - j = read(0,&bin[i],LBIN-i); - if(j<=0) + j = read(0, &bin[i], LBIN - i); + if (j <= 0) goto brka; i += j; - } while(i=LBIN ? i+1: - i-n+LBIN; + if (!bylines) { + k = n <= i ? i - n : partial ? 0 : n >= LBIN ? i + 1 : i - n + LBIN; k--; } else { - if(bkwds && bin[i==0?LBIN-1:i-1]!='\n'){ /* force trailing newline */ - bin[i]='\n'; - if(++i>=LBIN) {i = 0; partial = 0;} + if (bkwds && bin[i == 0 ? LBIN - 1 : i - 1] != '\n') { /* force trailing newline */ + bin[i] = '\n'; + if (++i >= LBIN) { + i = 0; + partial = 0; + } } k = i; j = 0; do { lastnl = k; do { - if(--k<0) { - if(partial) { - if(bkwds) - (void)write(1,bin,lastnl+1); + if (--k < 0) { + if (partial) { + if (bkwds) + (void)write(1, bin, lastnl + 1); goto brkb; } - k = LBIN -1; + k = LBIN - 1; } - } while(bin[k]!='\n'&&k!=i); - if(bkwds && j>0){ - if(k 0) { + if (k < lastnl) + (void)write(1, &bin[k + 1], lastnl - k); else { - (void)write(1,&bin[k+1],LBIN-k-1); - (void)write(1,bin,lastnl+1); + (void)write(1, &bin[k + 1], LBIN - k - 1); + (void)write(1, bin, lastnl + 1); } } - } while(j++=LBIN) - k = 0; - } while(bin[k]!='\n'&&k!=i); + if (bkwds) + exit(0); + if (k == i) + do { + if (++k >= LBIN) + k = 0; + } while (bin[k] != '\n' && k != i); } - if(k 0) - (void)write (1, bin, n); +void fexit() +{ + int n; + + if (!follow || piped) + exit(0); + for (;;) { + sleep(1); + while ((n = read(0, bin, BUFSIZ)) > 0) + (void)write(1, bin, n); } } diff --git a/src/cmd/tar.c b/src/cmd/tar.c index 4f1d564..867a7f5 100644 --- a/src/cmd/tar.c +++ b/src/cmd/tar.c @@ -7,27 +7,28 @@ /* * Tape Archival Program */ +#include +#include +#include #include #include #include #include -#include -#include +#include #include #include #include +#include +#include #include -#include -#include -#include -#define TBLOCK 512 -#define NBLOCK 20 -#define NAMSIZ 100 +#define TBLOCK 512 +#define NBLOCK 20 +#define NAMSIZ 100 -#define writetape(b) writetbuf(b, 1) -#define min(a,b) ((a) < (b) ? (a) : (b)) -#define max(a,b) ((a) > (b) ? (a) : (b)) +#define writetape(b) writetbuf(b, 1) +#define min(a, b) ((a) < (b) ? (a) : (b)) +#define max(a, b) ((a) > (b) ? (a) : (b)) union hblock { char dummy[TBLOCK]; @@ -45,17 +46,17 @@ union hblock { }; struct linkbuf { - ino_t inum; - dev_t devnum; + ino_t inum; + dev_t devnum; int count; - char pathname[NAMSIZ]; - struct linkbuf *nextp; + char pathname[NAMSIZ]; + struct linkbuf *nextp; }; -union hblock dblock; -union hblock *tbuf; -struct linkbuf *ihead; -struct stat stbuf; +union hblock dblock; +union hblock *tbuf; +struct linkbuf *ihead; +struct stat stbuf; int rflag; int xflag; @@ -85,49 +86,74 @@ daddr_t low; daddr_t high; daddr_t bsrch(); -FILE *vfile = stdout; -FILE *tfile; -char tname[] = "/tmp/tarXXXXXX"; -char *usefile; -char magtape[] = "/dev/rmt8"; +FILE *vfile = stdout; +FILE *tfile; +char tname[] = "/tmp/tarXXXXXX"; +char *usefile; +char magtape[] = "/dev/rmt8"; -void -onintr (sig) - int sig; +static void usage(void); +static void done(int n); +static int openmt(char *tape, int writing); +static void dorep(char *argv[]); +static void doxtract(char *argv[]); +static void dotable(char *argv[]); +static void getdir(void); +static void passtape(void); +static int endtape(void); +static void backtape(void); +static void putfile(char *longname, char *shortname, char *parent); +static void putempty(void); +static void flushtape(void); +static int readtape(char *buffer); +static int checksum(void); +static int readtbuf(char **bufpp, int size); +static int checkupdate(char *arg); +static int checkw(int c, char *name); +static int checkf(char *name, int mode, int howmuch); +static void tomodes(struct stat *sp); +static int writetbuf(char *buffer, int n); +static int wantit(char *argv[]); +static int checkdir(char *name); +static void dodirtimes(union hblock *hp); +static void setimes(char *path, time_t mt); +static void longt(struct stat *st); +static void pmode(struct stat *st); +static void selectbits(int *pairp, struct stat *st); +static int response(void); +static int prefix(char *s1, char *s2); +static int cmp(char *b, char *s, int n); +static void getbuf(void); +static int bread(int fd, char *buf, int size); +static void mterr(char *operation, int i, int exitcode); + +void onintr(int sig) { - (void) signal(SIGINT, SIG_IGN); + (void)signal(SIGINT, SIG_IGN); term++; } -void -onquit (sig) - int sig; +void onquit(int sig) { - (void) signal(SIGQUIT, SIG_IGN); + (void)signal(SIGQUIT, SIG_IGN); term++; } -void -onhup (sig) - int sig; +void onhup(int sig) { - (void) signal(SIGHUP, SIG_IGN); + (void)signal(SIGHUP, SIG_IGN); term++; } #ifdef notdef -void -onterm (sig) - int sig; +void onterm(int sig) { - (void) signal(SIGTERM, SIG_IGN); + (void)signal(SIGTERM, SIG_IGN); term++; } #endif -main(argc, argv) -int argc; -char *argv[]; +int main(int argc, char *argv[]) { char *cp; @@ -135,16 +161,14 @@ char *argv[]; usage(); tfile = NULL; - usefile = magtape; + usefile = magtape; argv[argc] = 0; argv++; for (cp = *argv++; *cp; cp++) - switch(*cp) { - + switch (*cp) { case 'f': if (*argv == 0) { - fprintf(stderr, - "tar: tapefile must be specified with 'f' option\n"); + fprintf(stderr, "tar: tapefile must be specified with 'f' option\n"); usage(); } usefile = *argv++; @@ -167,9 +191,7 @@ char *argv[]; case 'u': mktemp(tname); if ((tfile = fopen(tname, "w")) == NULL) { - fprintf(stderr, - "tar: cannot create temporary file (%s)\n", - tname); + fprintf(stderr, "tar: cannot create temporary file (%s)\n", tname); done(1); } fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n"); @@ -214,14 +236,12 @@ char *argv[]; case 'b': if (*argv == 0) { - fprintf(stderr, - "tar: blocksize must be specified with 'b' option\n"); + fprintf(stderr, "tar: blocksize must be specified with 'b' option\n"); usage(); } nblock = atoi(*argv); if (nblock <= 0) { - fprintf(stderr, - "tar: invalid blocksize \"%s\"\n", *argv); + fprintf(stderr, "tar: invalid blocksize \"%s\"\n", *argv); done(1); } argv++; @@ -258,14 +278,14 @@ char *argv[]; if (cflag && tfile != NULL) usage(); if (signal(SIGINT, SIG_IGN) != SIG_IGN) - (void) signal(SIGINT, onintr); + (void)signal(SIGINT, onintr); if (signal(SIGHUP, SIG_IGN) != SIG_IGN) - (void) signal(SIGHUP, onhup); + (void)signal(SIGHUP, onhup); if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) - (void) signal(SIGQUIT, onquit); + (void)signal(SIGQUIT, onquit); #ifdef notdef if (signal(SIGTERM, SIG_IGN) != SIG_IGN) - (void) signal(SIGTERM, onterm); + (void)signal(SIGTERM, onterm); #endif mt = openmt(usefile, 1); dorep(argv); @@ -279,27 +299,22 @@ char *argv[]; done(0); } -usage() +void usage() { fprintf(stderr, -"tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n"); + "tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n"); done(1); } -int -openmt(tape, writing) - char *tape; - int writing; +int openmt(char *tape, int writing) { - if (strcmp(tape, "-") == 0) { /* * Read from standard input or write to standard output. */ if (writing) { if (cflag == 0) { - fprintf(stderr, - "tar: can only create standard output archives\n"); + fprintf(stderr, "tar: can only create standard output archives\n"); done(1); } vfile = stderr; @@ -315,7 +330,7 @@ openmt(tape, writing) */ if (writing) { if (cflag) - mt = open(tape, O_RDWR|O_CREAT|O_TRUNC, 0666); + mt = open(tape, O_RDWR | O_CREAT | O_TRUNC, 0666); else mt = open(tape, O_RDWR); } else @@ -326,12 +341,10 @@ openmt(tape, writing) done(1); } } - return(mt); + return (mt); } -char * -getcwd(buf) - char *buf; +char *getcwd(char *buf) { if (getwd(buf) == NULL) { fprintf(stderr, "tar: %s\n", buf); @@ -340,10 +353,9 @@ getcwd(buf) return (buf); } -dorep(argv) - char *argv[]; +void dorep(char *argv[]) { - register char *cp, *cp2; + char *cp, *cp2; char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent; if (!cflag) { @@ -358,8 +370,9 @@ dorep(argv) if (tfile != NULL) { char buf[200]; - sprintf(buf, -"sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s", + sprintf( + buf, + "sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s", tname, tname, tname, tname, tname, tname); fflush(tfile); system(buf); @@ -369,8 +382,8 @@ dorep(argv) } } - (void) getcwd(wdir); - while (*argv && ! term) { + (void)getcwd(wdir); + while (*argv && !term) { cp2 = *argv; if (!strcmp(cp2, "-C") && argv[1]) { argv++; @@ -378,12 +391,12 @@ dorep(argv) fprintf(stderr, "tar: can't change directories to "); perror(*argv); } else - (void) getcwd(wdir); + (void)getcwd(wdir); argv++; continue; } - if (*argv[0] == '/'){ + if (*argv[0] == '/') { parent = ""; } else { parent = wdir; @@ -421,14 +434,14 @@ dorep(argv) } } -endtape() +int endtape() { return (dblock.dbuf.name[0] == '\0'); } -getdir() +void getdir() { - register struct stat *sp; + struct stat *sp; int i; top: readtape((char *)&dblock); @@ -445,8 +458,7 @@ top: sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime); sscanf(dblock.dbuf.chksum, "%o", &chksum); if (chksum != (i = checksum())) { - fprintf(stderr, "tar: directory checksum error (%d != %d)\n", - chksum, i); + fprintf(stderr, "tar: directory checksum error (%d != %d)\n", chksum, i); if (iflag) goto top; done(2); @@ -455,7 +467,7 @@ top: fprintf(tfile, "%s %s\n", dblock.dbuf.name, dblock.dbuf.mtime); } -passtape() +void passtape() { long blocks; char *bufp; @@ -463,44 +475,39 @@ passtape() if (dblock.dbuf.linkflag == '1') return; blocks = stbuf.st_size; - blocks += TBLOCK-1; + blocks += TBLOCK - 1; blocks /= TBLOCK; while (blocks-- > 0) - (void) readtbuf(&bufp, TBLOCK); + (void)readtbuf(&bufp, TBLOCK); } -char * -getmem(size) +char *getmem(int size) { - char *p = malloc((unsigned) size); + char *p = malloc((unsigned)size); if (p == NULL && freemem) { - fprintf(stderr, - "tar: out of memory, link and directory modtime info lost\n"); + fprintf(stderr, "tar: out of memory, link and directory modtime info lost\n"); freemem = 0; } return (p); } -putfile(longname, shortname, parent) - char *longname; - char *shortname; - char *parent; +void putfile(char *longname, char *shortname, char *parent) { int infile = 0; long blocks; char buf[TBLOCK]; char *bigbuf; - register char *cp; + char *cp; struct direct *dp; DIR *dirp; - register int i; + int i; long l; - char newparent[NAMSIZ+64]; + char newparent[NAMSIZ + 64]; extern int errno; int maxread; - int hint; /* amount to write to get "in sync" */ + int hint; /* amount to write to get "in sync" */ if (!hflag) i = lstat(shortname, &stbuf); @@ -520,21 +527,20 @@ putfile(longname, shortname, parent) switch (stbuf.st_mode & S_IFMT) { case S_IFDIR: - for (i = 0, cp = buf; *cp++ = longname[i++];) + for (i = 0, cp = buf; (*cp++ = longname[i++]);) ; *--cp = '/'; - *++cp = 0 ; + *++cp = 0; if (!oflag) { if ((cp - buf) >= NAMSIZ) { - fprintf(stderr, "tar: %s: file name too long\n", - longname); + fprintf(stderr, "tar: %s: file name too long\n", longname); return; } stbuf.st_size = 0; tomodes(&stbuf); - strcpy(dblock.dbuf.name,buf); + strcpy(dblock.dbuf.name, buf); sprintf(dblock.dbuf.chksum, "%6o", checksum()); - (void) writetape((char *)&dblock); + (void)writetape((char *)&dblock); } sprintf(newparent, "%s/%s", parent, shortname); if (chdir(shortname) < 0) { @@ -542,8 +548,7 @@ putfile(longname, shortname, parent) return; } if ((dirp = opendir(".")) == NULL) { - fprintf(stderr, "tar: %s: directory read error\n", - longname); + fprintf(stderr, "tar: %s: directory read error\n", longname); if (chdir(parent) < 0) { fprintf(stderr, "tar: cannot change back?: "); perror(parent); @@ -553,8 +558,7 @@ putfile(longname, shortname, parent) while ((dp = readdir(dirp)) != NULL && !term) { if (dp->d_ino == 0) continue; - if (!strcmp(".", dp->d_name) || - !strcmp("..", dp->d_name)) + if (!strcmp(".", dp->d_name) || !strcmp("..", dp->d_name)) continue; strcpy(cp, dp->d_name); l = telldir(dirp); @@ -573,14 +577,12 @@ putfile(longname, shortname, parent) case S_IFLNK: tomodes(&stbuf); if (strlen(longname) >= NAMSIZ) { - fprintf(stderr, "tar: %s: file name too long\n", - longname); + fprintf(stderr, "tar: %s: file name too long\n", longname); return; } strcpy(dblock.dbuf.name, longname); if (stbuf.st_size + 1 >= NAMSIZ) { - fprintf(stderr, "tar: %s: symbolic link too long\n", - longname); + fprintf(stderr, "tar: %s: symbolic link too long\n", longname); return; } i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1); @@ -592,11 +594,10 @@ putfile(longname, shortname, parent) dblock.dbuf.linkname[i] = '\0'; dblock.dbuf.linkflag = '2'; if (vflag) - fprintf(vfile, "a %s symbolic link to %s\n", - longname, dblock.dbuf.linkname); + fprintf(vfile, "a %s symbolic link to %s\n", longname, dblock.dbuf.linkname); sprintf(dblock.dbuf.size, "%11lo", 0L); sprintf(dblock.dbuf.chksum, "%6o", checksum()); - (void) writetape((char *)&dblock); + (void)writetape((char *)&dblock); break; case S_IFREG: @@ -607,8 +608,7 @@ putfile(longname, shortname, parent) } tomodes(&stbuf); if (strlen(longname) >= NAMSIZ) { - fprintf(stderr, "tar: %s: file name too long\n", - longname); + fprintf(stderr, "tar: %s: file name too long\n", longname); close(infile); return; } @@ -618,8 +618,7 @@ putfile(longname, shortname, parent) int found = 0; for (lp = ihead; lp != NULL; lp = lp->nextp) - if (lp->inum == stbuf.st_ino && - lp->devnum == stbuf.st_dev) { + if (lp->inum == stbuf.st_ino && lp->devnum == stbuf.st_dev) { found++; break; } @@ -627,15 +626,14 @@ putfile(longname, shortname, parent) strcpy(dblock.dbuf.linkname, lp->pathname); dblock.dbuf.linkflag = '1'; sprintf(dblock.dbuf.chksum, "%6o", checksum()); - (void) writetape( (char *) &dblock); + (void)writetape((char *)&dblock); if (vflag) - fprintf(vfile, "a %s link to %s\n", - longname, lp->pathname); + fprintf(vfile, "a %s link to %s\n", longname, lp->pathname); lp->count--; close(infile); return; } - lp = (struct linkbuf *) getmem(sizeof(*lp)); + lp = (struct linkbuf *)getmem(sizeof(*lp)); if (lp != NULL) { lp->nextp = ihead; ihead = lp; @@ -645,7 +643,7 @@ putfile(longname, shortname, parent) strcpy(lp->pathname, longname); } } - blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK; + blocks = (stbuf.st_size + (TBLOCK - 1)) / TBLOCK; if (vflag) fprintf(vfile, "a %s %ld blocks\n", longname, blocks); sprintf(dblock.dbuf.chksum, "%6o", checksum()); @@ -656,11 +654,10 @@ putfile(longname, shortname, parent) bigbuf = buf; } - while ((i = read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0 - && blocks > 0) { - register int nblks; + while ((i = read(infile, bigbuf, min((hint * TBLOCK), maxread))) > 0 && blocks > 0) { + int nblks; - nblks = ((i-1)/TBLOCK)+1; + nblks = ((i - 1) / TBLOCK) + 1; if (nblks > blocks) nblks = blocks; hint = writetbuf(bigbuf, nblks); @@ -673,31 +670,27 @@ putfile(longname, shortname, parent) fprintf(stderr, "tar: Read error on "); perror(longname); } else if (blocks != 0 || i != 0) - fprintf(stderr, "tar: %s: file changed size\n", - longname); - while (--blocks >= 0) + fprintf(stderr, "tar: %s: file changed size\n", longname); + while (--blocks >= 0) putempty(); break; default: - fprintf(stderr, "tar: %s is not a file. Not dumped\n", - longname); + fprintf(stderr, "tar: %s is not a file. Not dumped\n", longname); break; } } -doxtract(argv) - char *argv[]; +void doxtract(char *argv[]) { long blocks, bytes; int ofile, i; - extern int errno; for (;;) { if ((i = wantit(argv)) == 0) continue; if (i == -1) - break; /* end of tape */ + break; /* end of tape */ if (checkw('x', dblock.dbuf.name) == 0) { passtape(); continue; @@ -714,12 +707,12 @@ doxtract(argv) continue; } } - if (checkdir(dblock.dbuf.name)) { /* have a directory */ + if (checkdir(dblock.dbuf.name)) { /* have a directory */ if (mflag == 0) dodirtimes(&dblock); continue; } - if (dblock.dbuf.linkflag == '2') { /* symlink */ + if (dblock.dbuf.linkflag == '2') { /* symlink */ /* * only unlink non directories or empty * directories @@ -728,15 +721,14 @@ doxtract(argv) if (errno == ENOTDIR) unlink(dblock.dbuf.name); } - if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) { - fprintf(stderr, "tar: %s: symbolic link failed: ", - dblock.dbuf.name); + if (symlink(dblock.dbuf.linkname, dblock.dbuf.name) < 0) { + fprintf(stderr, "tar: %s: symbolic link failed: ", dblock.dbuf.name); perror(""); continue; } if (vflag) - fprintf(vfile, "x %s symbolic link to %s\n", - dblock.dbuf.name, dblock.dbuf.linkname); + fprintf(vfile, "x %s symbolic link to %s\n", dblock.dbuf.name, + dblock.dbuf.linkname); #ifdef notdef /* ignore alien orders */ chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); @@ -747,7 +739,7 @@ doxtract(argv) #endif continue; } - if (dblock.dbuf.linkflag == '1') { /* regular link */ + if (dblock.dbuf.linkflag == '1') { /* regular link */ /* * only unlink non directories or empty * directories @@ -757,46 +749,41 @@ doxtract(argv) unlink(dblock.dbuf.name); } if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) { - fprintf(stderr, "tar: can't link %s to %s: ", - dblock.dbuf.name, dblock.dbuf.linkname); + fprintf(stderr, "tar: can't link %s to %s: ", dblock.dbuf.name, + dblock.dbuf.linkname); perror(""); continue; } if (vflag) - fprintf(vfile, "%s linked to %s\n", - dblock.dbuf.name, dblock.dbuf.linkname); + fprintf(vfile, "%s linked to %s\n", dblock.dbuf.name, dblock.dbuf.linkname); continue; } - if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) { - fprintf(stderr, "tar: can't create %s: ", - dblock.dbuf.name); + if ((ofile = creat(dblock.dbuf.name, stbuf.st_mode & 0xfff)) < 0) { + fprintf(stderr, "tar: can't create %s: ", dblock.dbuf.name); perror(""); passtape(); continue; } chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); - blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK; + blocks = ((bytes = stbuf.st_size) + TBLOCK - 1) / TBLOCK; if (vflag) - fprintf(vfile, "x %s, %ld bytes, %ld tape blocks\n", - dblock.dbuf.name, bytes, blocks); + fprintf(vfile, "x %s, %ld bytes, %ld tape blocks\n", dblock.dbuf.name, bytes, blocks); for (; blocks > 0;) { - register int nread; - char *bufp; - register int nwant; + int nread; + char *bufp; + int nwant; - nwant = NBLOCK*TBLOCK; - if (nwant > (blocks*TBLOCK)) - nwant = (blocks*TBLOCK); + nwant = NBLOCK * TBLOCK; + if (nwant > (blocks * TBLOCK)) + nwant = (blocks * TBLOCK); nread = readtbuf(&bufp, nwant); if (write(ofile, bufp, (int)min(nread, bytes)) < 0) { - fprintf(stderr, - "tar: %s: HELP - extract write error", - dblock.dbuf.name); + fprintf(stderr, "tar: %s: HELP - extract write error", dblock.dbuf.name); perror(""); done(2); } bytes -= nread; - blocks -= (((nread-1)/TBLOCK)+1); + blocks -= (((nread - 1) / TBLOCK) + 1); } close(ofile); if (mflag == 0) @@ -810,16 +797,15 @@ doxtract(argv) } } -dotable(argv) - char *argv[]; +void dotable(char *argv[]) { - register int i; + int i; for (;;) { if ((i = wantit(argv)) == 0) continue; if (i == -1) - break; /* end of tape */ + break; /* end of tape */ if (vflag) longt(&stbuf); printf("%s", dblock.dbuf.name); @@ -832,39 +818,38 @@ dotable(argv) } } -putempty() +void putempty() { char buf[TBLOCK]; - bzero(buf, sizeof (buf)); - (void) writetape(buf); + bzero(buf, sizeof(buf)); + (void)writetape(buf); } -longt(st) - register struct stat *st; +void longt(struct stat *st) { - register char *cp; - char *ctime(); + char *cp; pmode(st); printf("%3d/%1d", st->st_uid, st->st_gid); printf("%7ld", st->st_size); cp = ctime(&st->st_mtime); - printf(" %-12.12s %-4.4s ", cp+4, cp+20); + printf(" %-12.12s %-4.4s ", cp + 4, cp + 20); } -#define SUID 04000 -#define SGID 02000 -#define ROWN 0400 -#define WOWN 0200 -#define XOWN 0100 -#define RGRP 040 -#define WGRP 020 -#define XGRP 010 -#define ROTH 04 -#define WOTH 02 -#define XOTH 01 -#define STXT 01000 +#define SUID 04000 +#define SGID 02000 +#define ROWN 0400 +#define WOWN 0200 +#define XOWN 0100 +#define RGRP 040 +#define WGRP 020 +#define XGRP 010 +#define ROTH 04 +#define WOTH 02 +#define XOTH 01 +#define STXT 01000 + int m1[] = { 1, ROWN, 'r', '-' }; int m2[] = { 1, WOWN, 'w', '-' }; int m3[] = { 2, SUID, 's', XOWN, 'x', '-' }; @@ -875,26 +860,23 @@ int m7[] = { 1, ROTH, 'r', '-' }; int m8[] = { 1, WOTH, 'w', '-' }; int m9[] = { 2, STXT, 't', XOTH, 'x', '-' }; -int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9}; +int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9 }; -pmode(st) - register struct stat *st; +void pmode(struct stat *st) { - register int **mp; + int **mp; for (mp = &m[0]; mp < &m[9];) selectbits(*mp++, st); } -selectbits(pairp, st) - int *pairp; - struct stat *st; +void selectbits(int *pairp, struct stat *st) { - register int n, *ap; + int n, *ap; ap = pairp; n = *ap++; - while (--n>=0 && (st->st_mode&*ap++)==0) + while (--n >= 0 && (st->st_mode & *ap++) == 0) ap++; putchar(*ap); } @@ -904,10 +886,9 @@ selectbits(pairp, st) * a directory on the tar tape (indicated by a trailing '/'), * return 1; else 0. */ -checkdir(name) - register char *name; +int checkdir(char *name) { - register char *cp; + char *cp; /* * Quick check for existence of directory. @@ -940,13 +921,12 @@ checkdir(name) } *cp = '/'; } - return (cp[-1]=='/'); + return (cp[-1] == '/'); } -tomodes(sp) -register struct stat *sp; +void tomodes(struct stat *sp) { - register char *cp; + char *cp; for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) *cp = '\0'; @@ -957,13 +937,12 @@ register struct stat *sp; sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime); } -checksum() +int checksum() { - register i; - register char *cp; + int i; + char *cp; - for (cp = dblock.dbuf.chksum; - cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++) + for (cp = dblock.dbuf.chksum; cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++) *cp = ' '; i = 0; for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) @@ -971,8 +950,7 @@ checksum() return (i); } -checkw(c, name) - char *name; +int checkw(int c, char *name) { if (!wflag) return (1); @@ -983,7 +961,7 @@ checkw(c, name) return (response() == 'y'); } -response() +int response() { char c; @@ -996,23 +974,20 @@ response() return (c); } -checkf(name, mode, howmuch) - char *name; - int mode, howmuch; +int checkf(char *name, int mode, int howmuch) { int l; - if ((mode & S_IFMT) == S_IFDIR){ - if ((strcmp(name, "SCCS")==0) || (strcmp(name, "RCS")==0)) - return(0); - return(1); + if ((mode & S_IFMT) == S_IFDIR) { + if ((strcmp(name, "SCCS") == 0) || (strcmp(name, "RCS") == 0)) + return (0); + return (1); } if ((l = strlen(name)) < 3) return (1); - if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o') + if (howmuch > 1 && name[l - 2] == '.' && name[l - 1] == 'o') return (0); - if (strcmp(name, "core") == 0 || - strcmp(name, "errs") == 0 || + if (strcmp(name, "core") == 0 || strcmp(name, "errs") == 0 || (howmuch > 1 && strcmp(name, "a.out") == 0)) return (0); /* SHOULD CHECK IF IT IS EXECUTABLE */ @@ -1020,8 +995,7 @@ checkf(name, mode, howmuch) } /* Is the current file a new file, or the newest one of the same name? */ -checkupdate(arg) - char *arg; +int checkupdate(char *arg) { char name[100]; long mtime; @@ -1038,7 +1012,7 @@ checkupdate(arg) } } -done(n) +void done(int n) { unlink(tname); exit(n); @@ -1048,10 +1022,9 @@ done(n) * Do we want the next entry on the tape, i.e. is it selected? If * not, skip over the entire entry. Return -1 if reached end of tape. */ -wantit(argv) - char *argv[]; +int wantit(char *argv[]) { - register char **cp; + char **cp; getdir(); if (endtape()) @@ -1068,8 +1041,7 @@ wantit(argv) /* * Does s2 begin with the string s1, on a directory boundary? */ -prefix(s1, s2) - register char *s1, *s2; +int prefix(char *s1, char *s2) { while (*s1) if (*s1++ != *s2++) @@ -1079,29 +1051,24 @@ prefix(s1, s2) return (1); } -#define N 200 +#define N 200 int njab; -daddr_t -lookup(s) - char *s; +daddr_t lookup(char *s) { - register i; + int i; daddr_t a; - for(i=0; s[i]; i++) + for (i = 0; s[i]; i++) if (s[i] == ' ') break; a = bsrch(s, i, low, high); return (a); } -daddr_t -bsrch(s, n, l, h) - daddr_t l, h; - char *s; +daddr_t bsrch(char *s, int n, daddr_t l, daddr_t h) { - register i, j; + int i, j; char b[N]; daddr_t m, m1; @@ -1109,28 +1076,28 @@ bsrch(s, n, l, h) loop: if (l >= h) - return ((daddr_t) -1); - m = l + (h-l)/2 - N/2; + return ((daddr_t)-1); + m = l + (h - l) / 2 - N / 2; if (m < l) m = l; fseek(tfile, m, 0); fread(b, 1, N, tfile); njab++; - for(i=0; i= h) - return ((daddr_t) -1); + return ((daddr_t)-1); m1 = m; j = i; - for(i++; i s[i]) + for (i = 0; i < n; i++) { + if (b[i + 1] > s[i]) return (-1); - if (b[i+1] < s[i]) + if (b[i + 1] < s[i]) return (1); } - return (b[i+1] == ' '? 0 : -1); + return (b[i + 1] == ' ' ? 0 : -1); } -readtape(buffer) - char *buffer; +int readtape(char *buffer) { char *bufp; if (first == 0) getbuf(); - (void) readtbuf(&bufp, TBLOCK); + (void)readtbuf(&bufp, TBLOCK); bcopy(bufp, buffer, TBLOCK); - return(TBLOCK); + return (TBLOCK); } -readtbuf(bufpp, size) - char **bufpp; - int size; +int readtbuf(char **bufpp, int size) { - register int i; + int i; if (recno >= nblock || first == 0) { - if ((i = bread(mt, (char *)tbuf, TBLOCK*nblock)) < 0) + if ((i = bread(mt, (char *)tbuf, TBLOCK * nblock)) < 0) mterr("read", i, 3); if (first == 0) { if ((i % TBLOCK) != 0) { @@ -1193,16 +1156,14 @@ readtbuf(bufpp, size) } recno = 0; } - if (size > ((nblock-recno)*TBLOCK)) - size = (nblock-recno)*TBLOCK; + if (size > ((nblock - recno) * TBLOCK)) + size = (nblock - recno) * TBLOCK; *bufpp = (char *)&tbuf[recno]; - recno += (size/TBLOCK); + recno += (size / TBLOCK); return (size); } -writetbuf(buffer, n) - register char *buffer; - register int n; +int writetbuf(char *buffer, int n) { int i; @@ -1211,8 +1172,8 @@ writetbuf(buffer, n) first = 1; } if (recno >= nblock) { - i = write(mt, (char *)tbuf, TBLOCK*nblock); - if (i != TBLOCK*nblock) + i = write(mt, (char *)tbuf, TBLOCK * nblock); + if (i != TBLOCK * nblock) mterr("write", i, 2); recno = 0; } @@ -1224,8 +1185,8 @@ writetbuf(buffer, n) * residual to the tape buffer. */ while (recno == 0 && n >= nblock) { - i = write(mt, buffer, TBLOCK*nblock); - if (i != TBLOCK*nblock) + i = write(mt, buffer, TBLOCK * nblock); + if (i != TBLOCK * nblock) mterr("write", i, 2); n -= nblock; buffer += (nblock * TBLOCK); @@ -1235,8 +1196,8 @@ writetbuf(buffer, n) bcopy(buffer, (char *)&tbuf[recno++], TBLOCK); buffer += TBLOCK; if (recno >= nblock) { - i = write(mt, (char *)tbuf, TBLOCK*nblock); - if (i != TBLOCK*nblock) + i = write(mt, (char *)tbuf, TBLOCK * nblock); + if (i != TBLOCK * nblock) mterr("write", i, 2); recno = 0; } @@ -1246,10 +1207,10 @@ writetbuf(buffer, n) return (nblock - recno); } -backtape() +void backtape() { static int mtdev = 1; - static struct mtop mtop = {MTBSR, 1}; + static struct mtop mtop = { MTBSR, 1 }; struct mtget mtget; if (mtdev == 1) @@ -1261,22 +1222,20 @@ backtape() done(4); } } else - lseek(mt, (daddr_t) -TBLOCK*nblock, 1); + lseek(mt, (daddr_t)-TBLOCK * nblock, 1); recno--; } -flushtape() +void flushtape() { int i; - i = write(mt, (char *)tbuf, TBLOCK*nblock); - if (i != TBLOCK*nblock) + i = write(mt, (char *)tbuf, TBLOCK * nblock); + if (i != TBLOCK * nblock) mterr("write", i, 2); } -mterr(operation, i, exitcode) - char *operation; - int i; +void mterr(char *operation, int i, int exitcode) { fprintf(stderr, "tar: tape %s error: ", operation); if (i < 0) @@ -1286,10 +1245,7 @@ mterr(operation, i, exitcode) done(exitcode); } -bread(fd, buf, size) - int fd; - char *buf; - int size; +int bread(int fd, char *buf, int size) { int count; static int lastread = 0; @@ -1309,9 +1265,8 @@ bread(fd, buf, size) return (count); } -getbuf() +void getbuf() { - if (nblock == 0) { fstat(mt, &stbuf); if ((stbuf.st_mode & S_IFMT) == S_IFCHR) @@ -1322,10 +1277,9 @@ getbuf() nblock = NBLOCK; } } - tbuf = (union hblock *)malloc((unsigned)nblock*TBLOCK); + tbuf = (union hblock *)malloc((unsigned)nblock * TBLOCK); if (tbuf == NULL) { - fprintf(stderr, "tar: blocksize %d too big, can't get memory\n", - nblock); + fprintf(stderr, "tar: blocksize %d too big, can't get memory\n", nblock); done(1); } } @@ -1348,15 +1302,14 @@ getbuf() * the tape and setting all the times at the end. */ char dirstack[NAMSIZ]; -#define NTIM (NAMSIZ/2+1) /* a/b/c/d/... */ +#define NTIM (NAMSIZ / 2 + 1) /* a/b/c/d/... */ time_t mtime[NTIM]; -dodirtimes(hp) - union hblock *hp; +void dodirtimes(union hblock *hp) { - register char *p = dirstack; - register char *q = hp->dbuf.name; - register int ndir = 0; + char *p = dirstack; + char *q = hp->dbuf.name; + int ndir = 0; char *savp; int savndir; @@ -1376,7 +1329,7 @@ dodirtimes(hp) */ if (*p++ == '/') if (mtime[++ndir] >= 0) { - *--p = '\0'; /* zap the slash */ + *--p = '\0'; /* zap the slash */ setimes(dirstack, mtime[ndir]); *p++ = '/'; } @@ -1385,19 +1338,17 @@ dodirtimes(hp) ndir = savndir; /* Push this one on the "stack" */ - while (*p = *q++) /* append the rest of the new dir */ + while ((*p = *q++)) /* append the rest of the new dir */ if (*p++ == '/') mtime[++ndir] = -1; - mtime[ndir] = stbuf.st_mtime; /* overwrite the last one */ + mtime[ndir] = stbuf.st_mtime; /* overwrite the last one */ } -setimes(path, mt) - char *path; - time_t mt; +void setimes(char *path, time_t mt) { struct timeval tv[2]; - tv[0].tv_sec = time((time_t *) 0); + tv[0].tv_sec = time((time_t *)0); tv[1].tv_sec = mt; tv[0].tv_usec = tv[1].tv_usec = 0; if (utimes(path, tv) < 0) { diff --git a/src/cmd/tee.c b/src/cmd/tee.c index df5f8da..ee6a6d5 100644 --- a/src/cmd/tee.c +++ b/src/cmd/tee.c @@ -1,13 +1,15 @@ /* * tee-- pipe fitting */ -#include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include -#define BUFSIZ 8192 +#define BUFSIZ 8192 int openf[20] = { 1 }; int n = 1; @@ -15,23 +17,23 @@ int t = 0; int aflag; char in[BUFSIZ]; - char out[BUFSIZ]; -putstr(s) -char *s; +static void stash(int p); + +void putstr(char *s) { - while(*s) - write(2,s++,1); + while (*s) + write(2, s++, 1); } -main(argc,argv) -char **argv; +int main(int argc, char **argv) { - int register r,w,p; + int r, w, p; struct stat buf; - while(argc>1&&argv[1][0]=='-') { - switch(argv[1][1]) { + + while (argc > 1 && argv[1][0] == '-') { + switch (argv[1][1]) { case 'a': aflag++; break; @@ -42,20 +44,20 @@ char **argv; argv++; argc--; } - fstat(1,&buf); - t = (buf.st_mode&S_IFMT)==S_IFCHR; - if(lseek(1,0L,1)==-1&&errno==ESPIPE) + fstat(1, &buf); + t = (buf.st_mode & S_IFMT) == S_IFCHR; + if (lseek(1, 0L, 1) == -1 && errno == ESPIPE) t++; - while(argc-->1) { - if(aflag) { - openf[n] = open(argv[1],1); - if(openf[n] < 0) - openf[n] = creat(argv[1],0666); - lseek(openf[n++],0L,2); + while (argc-- > 1) { + if (aflag) { + openf[n] = open(argv[1], 1); + if (openf[n] < 0) + openf[n] = creat(argv[1], 0666); + lseek(openf[n++], 0L, 2); } else - openf[n++] = creat(argv[1],0666); - if(stat(argv[1],&buf)>=0) { - if((buf.st_mode&S_IFMT)==S_IFCHR) + openf[n++] = creat(argv[1], 0666); + if (stat(argv[1], &buf) >= 0) { + if ((buf.st_mode & S_IFMT) == S_IFCHR) t++; } else { putstr("tee: cannot open "); @@ -66,13 +68,14 @@ char **argv; argv++; } r = w = 0; - for(;;) { - for(p=0;p=w) { - if(t>0&&p>0) break; - w = read(0,in,BUFSIZ); + for (;;) { + for (p = 0; p < BUFSIZ;) { + if (r >= w) { + if (t > 0 && p > 0) + break; + w = read(0, in, BUFSIZ); r = 0; - if(w<=0) { + if (w <= 0) { stash(p); exit(0); } @@ -83,13 +86,13 @@ char **argv; } } -stash(p) +void stash(int p) { int k; int i; int d; d = t ? 16 : p; - for(i=0; i #include #include -#include -#include +#include #include -#include +#include +#include -main(argc, argv) - int argc; - char **argv; +void printt(char *s, struct timeval *tv) +{ + fprintf(stderr, "%9ld.%01ld %s ", tv->tv_sec, tv->tv_usec / 100000, s); +} + +int main(int argc, char **argv) { int status; - register int p; + int p; struct timeval before, after; struct rusage ru; - if (argc<=1) + if (argc <= 1) exit(0); gettimeofday(&before, 0); p = fork(); @@ -35,7 +39,7 @@ main(argc, argv) while (wait3(&status, 0, &ru) != p) ; gettimeofday(&after, 0); - if ((status&0377) != 0) + if ((status & 0377) != 0) fprintf(stderr, "Command terminated abnormally.\n"); after.tv_sec -= before.tv_sec; after.tv_usec -= before.tv_usec; @@ -45,13 +49,5 @@ main(argc, argv) printt("user", &ru.ru_utime); printt("sys ", &ru.ru_stime); fprintf(stderr, "\n"); - exit (status>>8); -} - -printt(s, tv) - char *s; - struct timeval *tv; -{ - - fprintf(stderr, "%9ld.%01ld %s ", tv->tv_sec, tv->tv_usec/100000, s); + exit(status >> 8); } diff --git a/src/cmd/touch.c b/src/cmd/touch.c index c0509c9..ac7d0d7 100644 --- a/src/cmd/touch.c +++ b/src/cmd/touch.c @@ -6,19 +6,22 @@ */ #include #include -#include +#include +#include #include +#include int dontcreate; /* set if -c option */ int force; /* set if -f option */ char *whoami = "touch"; -main(argc,argv) - int argc; - char **argv; +static void touch(char *filename); +static void readwrite(char *filename, off_t size); + +int main(int argc, char **argv) { - char *argp; + char *argp; dontcreate = 0; force = 0; @@ -32,8 +35,7 @@ main(argc,argv) force = 1; break; default: - fprintf(stderr, "%s: bad option -%c\n", - whoami, *argp); + fprintf(stderr, "%s: bad option -%c\n", whoami, *argp); exit(1); } } @@ -43,40 +45,35 @@ main(argc,argv) } } -touch(filename) - char *filename; +void touch(char *filename) { struct stat statbuffer; - if (stat(filename,&statbuffer) == -1) { + if (stat(filename, &statbuffer) == -1) { if (!dontcreate) { - readwrite(filename,0L); + readwrite(filename, 0L); } else { - fprintf(stderr, "%s: %s: does not exist\n", - whoami, filename); + fprintf(stderr, "%s: %s: does not exist\n", whoami, filename); } return; } if ((statbuffer.st_mode & S_IFMT) != S_IFREG) { - fprintf(stderr, "%s: %s: can only touch regular files\n", - whoami, filename); + fprintf(stderr, "%s: %s: can only touch regular files\n", whoami, filename); return; } - if (!access(filename,4|2)) { - readwrite(filename,statbuffer.st_size); + if (!access(filename, 4 | 2)) { + readwrite(filename, statbuffer.st_size); return; } if (force) { - if (chmod(filename,0666)) { - fprintf(stderr, "%s: %s: couldn't chmod: ", - whoami, filename); + if (chmod(filename, 0666)) { + fprintf(stderr, "%s: %s: couldn't chmod: ", whoami, filename); perror(""); return; } - readwrite(filename,statbuffer.st_size); - if (chmod(filename,statbuffer.st_mode)) { - fprintf(stderr, "%s: %s: couldn't chmod back: ", - whoami, filename); + readwrite(filename, statbuffer.st_size); + if (chmod(filename, statbuffer.st_mode)) { + fprintf(stderr, "%s: %s: couldn't chmod back: ", whoami, filename); perror(""); return; } @@ -85,17 +82,15 @@ touch(filename) } } -readwrite(filename,size) - char *filename; - off_t size; +void readwrite(char *filename, off_t size) { int filedescriptor; - char first; + char first; if (size) { - filedescriptor = open(filename,2); + filedescriptor = open(filename, 2); if (filedescriptor == -1) { -error: + error: fprintf(stderr, "%s: %s: ", whoami, filename); perror(""); return; @@ -103,14 +98,14 @@ error: if (read(filedescriptor, &first, 1) != 1) { goto error; } - if (lseek(filedescriptor,0l,0) == -1) { + if (lseek(filedescriptor, 0l, 0) == -1) { goto error; } if (write(filedescriptor, &first, 1) != 1) { goto error; } } else { - filedescriptor = creat(filename,0666); + filedescriptor = creat(filename, 0666); if (filedescriptor == -1) { goto error; } diff --git a/src/cmd/tr.c b/src/cmd/tr.c index 0490b6a..df20d6c 100644 --- a/src/cmd/tr.c +++ b/src/cmd/tr.c @@ -4,21 +4,27 @@ #include #include -int dflag = 0; -int sflag = 0; +int dflag = 0; +int sflag = 0; int cflag = 0; -int save = 0; -char code[256]; -char squeez[256]; -char vect[256]; -struct string { int last, max; char *p; } string1, string2; +int save = 0; +char code[256]; +char squeez[256]; +char vect[256]; -main(argc,argv) -char **argv; +struct string { + int last, max; + char *p; +} string1, string2; + +static int next(struct string *s); +static int nextc(struct string *s); + +int main(int argc, char **argv) { - register i; + int i; int j; - register c, d; + int c, d; char *compl; int lastd; @@ -26,11 +32,11 @@ char **argv; string1.max = string2.max = 0; string1.p = string2.p = ""; - if(--argc>0) { + if (--argc > 0) { argv++; - if(*argv[0]=='-'&&argv[0][1]!=0) { - while(*++argv[0]) - switch(*argv[0]) { + if (*argv[0] == '-' && argv[0][1] != 0) { + while (*++argv[0]) + switch (*argv[0]) { case 'c': cflag++; continue; @@ -45,95 +51,106 @@ char **argv; argv++; } } - if(argc>0) string1.p = argv[0]; - if(argc>1) string2.p = argv[1]; - for(i=0; i<256; i++) + if (argc > 0) + string1.p = argv[0]; + if (argc > 1) + string2.p = argv[1]; + for (i = 0; i < 256; i++) code[i] = vect[i] = 0; - if(cflag) { - while(c = next(&string1)) - vect[c&0377] = 1; + if (cflag) { + while ((c = next(&string1))) + vect[c & 0377] = 1; j = 0; - for(i=1; i<256; i++) - if(vect[i]==0) vect[j++] = i; + for (i = 1; i < 256; i++) + if (vect[i] == 0) + vect[j++] = i; vect[j] = 0; compl = vect; } - for(i=0; i<256; i++) + for (i = 0; i < 256; i++) squeez[i] = 0; lastd = 0; - for(;;){ - if(cflag) c = *compl++; - else c = next(&string1); - if(c==0) break; + for (;;) { + if (cflag) + c = *compl ++; + else + c = next(&string1); + if (c == 0) + break; d = next(&string2); - if(d==0) d = lastd; - else lastd = d; - squeez[d&0377] = 1; - code[c&0377] = dflag?1:d; + if (d == 0) + d = lastd; + else + lastd = d; + squeez[d & 0377] = 1; + code[c & 0377] = dflag ? 1 : d; } - while(d = next(&string2)) - squeez[d&0377] = 1; + while ((d = next(&string2))) + squeez[d & 0377] = 1; squeez[0] = 1; - for(i=0;i<256;i++) { - if(code[i]==0) code[i] = i; - else if(dflag) code[i] = 0; + for (i = 0; i < 256; i++) { + if (code[i] == 0) + code[i] = i; + else if (dflag) + code[i] = 0; } clearerr(stdout); - while((c=getc(stdin)) != EOF ) { - if(c == 0) continue; - if(c = code[c&0377]&0377) - if(!sflag || c!=save || !squeez[c&0377]) { + while ((c = getc(stdin)) != EOF) { + if (c == 0) + continue; + if ((c = code[c & 0377] & 0377)) + if (!sflag || c != save || !squeez[c & 0377]) { putchar(save = c); - if(ferror(stdout)) + if (ferror(stdout)) exit(1); } } exit(0); } -next(s) -struct string *s; +int next(struct string *s) { - again: - if(s->max) { - if(s->last++ < s->max) - return(s->last); + if (s->max) { + if (s->last++ < s->max) + return (s->last); s->max = s->last = 0; } - if(s->last && *s->p=='-') { + if (s->last && *s->p == '-') { nextc(s); s->max = nextc(s); - if(s->max==0) { + if (s->max == 0) { s->p--; - return('-'); + return ('-'); } - if(s->max < s->last) { - s->last = s->max-1; - return('-'); + if (s->max < s->last) { + s->last = s->max - 1; + return ('-'); } goto again; } - return(s->last = nextc(s)); + return (s->last = nextc(s)); } -nextc(s) -struct string *s; +int nextc(struct string *s) { - register c, i, n; + int c, i, n; c = *s->p++; - if(c=='\\') { + if (c == '\\') { i = n = 0; - while(i<3 && (c = *s->p)>='0' && c<='7') { - n = n*8 + c - '0'; + while (i < 3 && (c = *s->p) >= '0' && c <= '7') { + n = n * 8 + c - '0'; i++; s->p++; } - if(i>0) c = n; - else c = *s->p++; + if (i > 0) + c = n; + else + c = *s->p++; } - if(c==0) *--s->p = 0; - return(c&0377); + if (c == 0) + *--s->p = 0; + return (c & 0377); } diff --git a/src/cmd/tsort.c b/src/cmd/tsort.c index 395bdc5..e4bb4fe 100644 --- a/src/cmd/tsort.c +++ b/src/cmd/tsort.c @@ -13,9 +13,9 @@ * make it easy to grow in natural order * states of the "live" field: */ -#define DEAD 0 /* already printed*/ -#define LIVE 1 /* not yet printed*/ -#define VISITED 2 /*used only in findloop()*/ +#define DEAD 0 /* already printed*/ +#define LIVE 1 /* not yet printed*/ +#define VISITED 2 /*used only in findloop()*/ /* a predecessor list tells all the immediate * predecessors of a given node @@ -30,154 +30,158 @@ struct nodelist { struct predlist *inedges; char *name; int live; -} firstnode = {NULL, NULL, NULL, DEAD}; +} firstnode = { NULL, NULL, NULL, DEAD }; struct nodelist *nindex(); struct nodelist *findloop(); struct nodelist *mark(); char *empty = ""; +static void error(char *s, char *t); +static int present(struct nodelist *i, struct nodelist *j); +static int anypred(struct nodelist *i); +static int cmp(char *s, char *t); +static void note(char *s, char *t); + /* the first for loop reads in the graph, * the second prints out the ordering */ -main(argc,argv) -char **argv; +int main(int argc, char **argv) { - register struct predlist *t; + struct predlist *t; FILE *input = stdin; - register struct nodelist *i, *j; + struct nodelist *i, *j; int x; char precedes[50], follows[50]; - if(argc>1) { - input = fopen(argv[1],"r"); - if(input==NULL) + + if (argc > 1) { + input = fopen(argv[1], "r"); + if (input == NULL) error("cannot open ", argv[1]); } - for(;;) { - x = fscanf(input,"%s%s",precedes, follows); - if(x==EOF) + for (;;) { + x = fscanf(input, "%s%s", precedes, follows); + if (x == EOF) break; - if(x!=2) - error("odd data",empty); + if (x != 2) + error("odd data", empty); i = nindex(precedes); j = nindex(follows); - if(i==j||present(i,j)) + if (i == j || present(i, j)) continue; t = (struct predlist *)malloc(sizeof(struct predlist)); t->nextpred = j->inedges; t->pred = i; j->inedges = t; } - for(;;) { - x = 0; /*anything LIVE on this sweep?*/ - for(i= &firstnode; i->nextnode!=NULL; i=i->nextnode) { - if(i->live==LIVE) { + for (;;) { + x = 0; /*anything LIVE on this sweep?*/ + for (i = &firstnode; i->nextnode != NULL; i = i->nextnode) { + if (i->live == LIVE) { x = 1; - if(!anypred(i)) + if (!anypred(i)) break; } } - if(x==0) + if (x == 0) break; - if(i->nextnode==NULL) + if (i->nextnode == NULL) i = findloop(); - printf("%s\n",i->name); + printf("%s\n", i->name); i->live = DEAD; } } /* is i present on j's predecessor list? */ -present(i,j) -struct nodelist *i, *j; +int present(struct nodelist *i, struct nodelist *j) { - register struct predlist *t; - for(t=j->inedges; t!=NULL; t=t->nextpred) - if(t->pred==i) - return(1); - return(0); + struct predlist *t; + + for (t = j->inedges; t != NULL; t = t->nextpred) + if (t->pred == i) + return (1); + return (0); } /* is there any live predecessor for i? */ -anypred(i) -struct nodelist *i; +int anypred(struct nodelist *i) { - register struct predlist *t; - for(t=i->inedges; t!=NULL; t=t->nextpred) - if(t->pred->live==LIVE) - return(1); - return(0); + struct predlist *t; + + for (t = i->inedges; t != NULL; t = t->nextpred) + if (t->pred->live == LIVE) + return (1); + return (0); } /* turn a string into a node pointer */ -struct nodelist * -nindex(s) -register char *s; +struct nodelist *nindex(char *s) { - register struct nodelist *i; - register char *t; - for(i= &firstnode; i->nextnode!=NULL; i=i->nextnode) - if(cmp(s,i->name)) - return(i); - for(t=s; *t; t++) ; - t = malloc((unsigned)(t+1-s)); + struct nodelist *i; + char *t; + + for (i = &firstnode; i->nextnode != NULL; i = i->nextnode) + if (cmp(s, i->name)) + return (i); + for (t = s; *t; t++) + ; + t = malloc((unsigned)(t + 1 - s)); i->nextnode = (struct nodelist *)malloc(sizeof(struct nodelist)); - if(i->nextnode==NULL||t==NULL) - error("too many items",empty); + if (i->nextnode == NULL || t == NULL) + error("too many items", empty); i->name = t; i->live = LIVE; i->nextnode->nextnode = NULL; i->nextnode->inedges = NULL; i->nextnode->live = DEAD; - while(*t++ = *s++); - return(i); + while ((*t++ = *s++)) + ; + return (i); } -cmp(s,t) -register char *s, *t; +int cmp(char *s, char *t) { - while(*s==*t) { - if(*s==0) - return(1); + while (*s == *t) { + if (*s == 0) + return (1); s++; t++; } - return(0); + return (0); } -error(s,t) -char *s, *t; +void error(char *s, char *t) { - note(s,t); + note(s, t); exit(1); } -note(s,t) -char *s,*t; +void note(char *s, char *t) { - fprintf(stderr,"tsort: %s%s\n",s,t); + fprintf(stderr, "tsort: %s%s\n", s, t); } /* given that there is a cycle, find some * node in it */ -struct nodelist * -findloop() +struct nodelist *findloop() { - register struct nodelist *i, *j; - for(i= &firstnode; i->nextnode!=NULL; i=i->nextnode) - if(i->live==LIVE) + struct nodelist *i, *j; + + for (i = &firstnode; i->nextnode != NULL; i = i->nextnode) + if (i->live == LIVE) break; - note("cycle in data",empty); + note("cycle in data", empty); i = mark(i); - if(i==NULL) - error("program error",empty); - for(j= &firstnode; j->nextnode!=NULL; j=j->nextnode) - if(j->live==VISITED) + if (i == NULL) + error("program error", empty); + for (j = &firstnode; j->nextnode != NULL; j = j->nextnode) + if (j->live == VISITED) j->live = LIVE; - return(i); + return (i); } /* depth-first search of LIVE predecessors @@ -185,23 +189,22 @@ findloop() * VISITED is a temporary state recording the * visits of the search */ -struct nodelist * -mark(i) -register struct nodelist *i; +struct nodelist *mark(struct nodelist *i) { - register struct nodelist *j; - register struct predlist *t; - if(i->live==DEAD) - return(NULL); - if(i->live==VISITED) - return(i); + struct nodelist *j; + struct predlist *t; + + if (i->live == DEAD) + return (NULL); + if (i->live == VISITED) + return (i); i->live = VISITED; - for(t=i->inedges; t!=NULL; t=t->nextpred) { + for (t = i->inedges; t != NULL; t = t->nextpred) { j = mark(t->pred); - if(j!=NULL) { - note(i->name,empty); - return(j); + if (j != NULL) { + note(i->name, empty); + return (j); } } - return(NULL); + return (NULL); } diff --git a/src/cmd/tty.c b/src/cmd/tty.c index 05ab425..4c7586f 100644 --- a/src/cmd/tty.c +++ b/src/cmd/tty.c @@ -3,17 +3,17 @@ */ #include #include +#include #include -main(argc, argv) -char **argv; +int main(int argc, char **argv) { - register char *p; + char *p; p = ttyname(0); - if(argc==2 && !strcmp(argv[1], "-s")) + if (argc == 2 && !strcmp(argv[1], "-s")) ; else - printf("%s\n", (p? p: "not a tty")); - exit(p? 0: 1); + printf("%s\n", (p ? p : "not a tty")); + exit(p ? 0 : 1); } diff --git a/src/cmd/uniq.c b/src/cmd/uniq.c index 9e0fa76..ff67ffc 100644 --- a/src/cmd/uniq.c +++ b/src/cmd/uniq.c @@ -1,33 +1,37 @@ /* * Deal with duplicated lines in a file */ +#include #include #include -#include int fields; int letters; int linec; -char mode; +char mode; int uniq; -char *skip(); -main(argc, argv) -int argc; -char *argv[]; +static void printe(char *p, char *s); +static int gline(char buf[]); +static void pline(char buf[]); +static int equal(char b1[], char b2[]); +static char *skip(char *s); + +int main(int argc, char *argv[]) { static char b1[1000], b2[1000]; - while(argc > 1) { - if(*argv[1] == '-') { + while (argc > 1) { + if (*argv[1] == '-') { if (isdigit(argv[1][1])) fields = atoi(&argv[1][1]); - else mode = argv[1][1]; + else + mode = argv[1][1]; argc--; argv++; continue; } - if(*argv[1] == '+') { + if (*argv[1] == '+') { letters = atoi(&argv[1][1]); argc--; argv++; @@ -37,62 +41,59 @@ char *argv[]; printe("cannot open %s\n", argv[1]); break; } - if(argc > 2 && freopen(argv[2], "w", stdout) == NULL) + if (argc > 2 && freopen(argv[2], "w", stdout) == NULL) printe("cannot create %s\n", argv[2]); - if(gline(b1)) + if (gline(b1)) exit(0); - for(;;) { + for (;;) { linec++; - if(gline(b2)) { + if (gline(b2)) { pline(b1); exit(0); } - if(!equal(b1, b2)) { + if (!equal(b1, b2)) { pline(b1); linec = 0; do { linec++; - if(gline(b1)) { + if (gline(b1)) { pline(b2); exit(0); } - } while(equal(b1, b2)); + } while (equal(b1, b2)); pline(b2); linec = 0; } } } -gline(buf) -register char buf[]; +int gline(char buf[]) { - register c; + int c; - while((c = getchar()) != '\n') { - if(c == EOF) - return(1); + while ((c = getchar()) != '\n') { + if (c == EOF) + return (1); *buf++ = c; } *buf = 0; - return(0); + return (0); } -pline(buf) -register char buf[]; +void pline(char buf[]) { - - switch(mode) { - + switch (mode) { case 'u': - if(uniq) { + if (uniq) { uniq = 0; return; } break; case 'd': - if(uniq) break; + if (uniq) + break; return; case 'c': @@ -103,41 +104,38 @@ register char buf[]; putchar('\n'); } -equal(b1, b2) -register char b1[], b2[]; +int equal(char b1[], char b2[]) { - register char c; + char c; b1 = skip(b1); b2 = skip(b2); - while((c = *b1++) != 0) - if(c != *b2++) return(0); - if(*b2 != 0) - return(0); + while ((c = *b1++) != 0) + if (c != *b2++) + return (0); + if (*b2 != 0) + return (0); uniq++; - return(1); + return (1); } -char * -skip(s) -register char *s; +char *skip(char *s) { - register nf, nl; + int nf, nl; nf = nl = 0; - while(nf++ < fields) { - while(*s == ' ' || *s == '\t') + while (nf++ < fields) { + while (*s == ' ' || *s == '\t') s++; - while( !(*s == ' ' || *s == '\t' || *s == 0) ) + while (!(*s == ' ' || *s == '\t' || *s == 0)) s++; } - while(nl++ < letters && *s != 0) - s++; - return(s); + while (nl++ < letters && *s != 0) + s++; + return (s); } -printe(p,s) -char *p,*s; +void printe(char *p, char *s) { fprintf(stderr, p, s); exit(1); diff --git a/src/cmd/w.c b/src/cmd/w.c index 55cebf6..7472b4a 100644 --- a/src/cmd/w.c +++ b/src/cmd/w.c @@ -6,83 +6,92 @@ * This program is similar to the systat command on Tenex/Tops 10/20 * It needs read permission on /dev/mem and /dev/swap. */ -#include -#include +#include +#include +#include +#include #include #include -#include -#include -#include #include +#include #include +#include #include -#include -#include -#include +#include +#include +#include -#define NMAX sizeof(utmp.ut_name) -#define LMAX sizeof(utmp.ut_line) -#define ARGWIDTH 33 /* # chars left on 80 col crt for args */ -#define ARGLIST 1024 /* amount of stack to examine for argument list */ +#define NMAX sizeof(utmp.ut_name) +#define LMAX sizeof(utmp.ut_line) +#define ARGWIDTH 33 /* # chars left on 80 col crt for args */ +#define ARGLIST 1024 /* amount of stack to examine for argument list */ struct smproc { - long w_addr; /* address in file for args */ - short w_pid; /* proc.p_pid */ - int w_igintr; /* INTR+3*QUIT, 0=die, 1=ign, 2=catch */ - time_t w_time; /* CPU time used by this process */ - time_t w_ctime; /* CPU time used by children */ - dev_t w_tty; /* tty device of process */ - char w_comm[15]; /* user.u_comm, null terminated */ - char w_args[ARGWIDTH+1]; /* args if interesting process */ -} *pr; + long w_addr; /* address in file for args */ + short w_pid; /* proc.p_pid */ + int w_igintr; /* INTR+3*QUIT, 0=die, 1=ign, 2=catch */ + time_t w_time; /* CPU time used by this process */ + time_t w_ctime; /* CPU time used by children */ + dev_t w_tty; /* tty device of process */ + char w_comm[15]; /* user.u_comm, null terminated */ + char w_args[ARGWIDTH + 1]; /* args if interesting process */ +} * pr; -FILE *ut; +FILE *ut; int swmem; -int swap; /* /dev/mem, mem, and swap */ +int swap; /* /dev/mem, mem, and swap */ int file; -dev_t tty; -char doing[520]; /* process attached to terminal */ -time_t proctime; /* cpu time of process in doing */ +dev_t tty; +char doing[520]; /* process attached to terminal */ +time_t proctime; /* cpu time of process in doing */ unsigned avenrun[3]; -extern int errno, optind; -#define DIV60(t) ((t+30)/60) /* x/60 rounded */ -#define TTYEQ (tty == pr[i].w_tty) -#define IGINT (1+3*1) /* ignoring both SIGINT & SIGQUIT */ +#define DIV60(t) ((t + 30) / 60) /* x/60 rounded */ +#define TTYEQ (tty == pr[i].w_tty) +#define IGINT (1 + 3 * 1) /* ignoring both SIGINT & SIGQUIT */ -char *getargs(); -char *getptr(); - -char *program; -int header = 1; /* true if -h flag: don't print heading */ -int lflag = 1; /* true if -l flag: long style output */ -time_t idle; /* number of minutes user is idle */ -int nusers; /* number of users logged in now */ -char * sel_user; /* login of particular user selected */ -int wcmd = 1; /* running as the w command */ -time_t jobtime; /* total cpu time visible */ -time_t now; /* the current time of day */ -struct tm *nowt; /* current time as time struct */ -struct timeval boottime; /* time since last reboot */ -time_t uptime; /* elapsed time since */ -int np; /* number of processes currently active */ -struct utmp utmp; -struct user up; +char *program; +int header = 1; /* true if -h flag: don't print heading */ +int lflag = 1; /* true if -l flag: long style output */ +time_t idle; /* number of minutes user is idle */ +int nusers; /* number of users logged in now */ +char *sel_user; /* login of particular user selected */ +int wcmd = 1; /* running as the w command */ +time_t jobtime; /* total cpu time visible */ +time_t now; /* the current time of day */ +struct tm *nowt; /* current time as time struct */ +struct timeval boottime; /* time since last reboot */ +time_t uptime; /* elapsed time since */ +int np; /* number of processes currently active */ +struct utmp utmp; +struct user up; struct addrmap { - long b1, e1; long f1; - long b2, e2; long f2; + long b1, e1; + long f1; + long b2, e2; + long f2; }; struct addrmap datmap; -main(argc, argv) - char **argv; +static void readpr(void); +static void prtat(struct tm *p); +static void gettty(void); +static int findidle(void); +static void putline(void); +static void prttime(time_t tim, char *tail); +static char *getargs(struct smproc *p); +static char *getptr(char **adr); +static int getbyte(char *adr); +static int within(char *adr, long lbd, long ubd); + +int main(int argc, char **argv) { int days, hrs, mins; - register int i; + int i; char *cp; - register int curpid, empty; - size_t size; + int curpid, empty; + size_t size; int mib[2]; program = argv[0]; @@ -91,34 +100,31 @@ main(argc, argv) if (*cp == 'u') wcmd = 0; - while ((i = getopt(argc, argv, "hlswu")) != EOF) - { - switch (i) - { - case 'h': - header = 0; - break; - case 'l': - lflag++; - break; - case 's': - lflag = 0; - break; - case 'u': - wcmd = 0; - break; - case 'w': - wcmd = 1; - break; - default: - fprintf(stderr, "Usage: %s [-hlswu] [user]\n", - program); - exit(1); - } + while ((i = getopt(argc, argv, "hlswu")) != EOF) { + switch (i) { + case 'h': + header = 0; + break; + case 'l': + lflag++; + break; + case 's': + lflag = 0; + break; + case 'u': + wcmd = 0; + break; + case 'w': + wcmd = 1; + break; + default: + fprintf(stderr, "Usage: %s [-hlswu] [user]\n", program); + exit(1); } + } argc -= optind; argv += optind; - if (*argv) + if (*argv) sel_user = *argv; if (wcmd) @@ -133,26 +139,25 @@ main(argc, argv) mib[0] = CTL_KERN; mib[1] = KERN_BOOTTIME; - size = sizeof (boottime); - if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && - boottime.tv_sec != 0) { + size = sizeof(boottime); + if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) { uptime = now - boottime.tv_sec; - days = uptime / (60L*60L*24L); - uptime %= (60L*60L*24L); - hrs = uptime / (60L*60L); - uptime %= (60L*60L); + days = uptime / (60L * 60L * 24L); + uptime %= (60L * 60L * 24L); + hrs = uptime / (60L * 60L); + uptime %= (60L * 60L); mins = DIV60(uptime); printf(" up"); if (days > 0) - printf(" %d day%s,", days, days>1?"s":""); + printf(" %d day%s,", days, days > 1 ? "s" : ""); if (hrs > 0 && mins > 0) { printf(" %2d:%02d,", hrs, mins); } else { if (hrs > 0) - printf(" %d hr%s,", hrs, hrs>1?"s":""); + printf(" %d hr%s,", hrs, hrs > 1 ? "s" : ""); if (mins > 0) - printf(" %d min%s,", mins, mins>1?"s":""); + printf(" %d min%s,", mins, mins > 1 ? "s" : ""); } } @@ -162,17 +167,16 @@ main(argc, argv) nusers++; } rewind(ut); - printf(" %d user%c", nusers, nusers > 1 ? 's' : '\0'); + printf(" %d user%c", nusers, nusers > 1 ? 's' : '\0'); if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) printf(", no load average information available\n"); else { printf(", load averages:"); - for (i = 0; i < (sizeof(avenrun)/sizeof(avenrun[0])); i++) { + for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) { if (i > 0) printf(","); - printf(" %u.%02u", avenrun[i] / 100, - avenrun[i] % 100); + printf(" %u.%02u", avenrun[i] / 100, avenrun[i] % 100); } } printf("\n"); @@ -181,24 +185,22 @@ main(argc, argv) /* Headers for rest of output */ if (lflag) - printf("%-*.*s %-*.*s login@ idle JCPU PCPU what\n", - NMAX, NMAX, "User", LMAX, LMAX, "tty"); + printf("%-*.*s %-*.*s login@ idle JCPU PCPU what\n", NMAX, NMAX, "User", LMAX, + LMAX, "tty"); else - printf("%-*.*s tty idle what\n", - NMAX, NMAX, "User"); + printf("%-*.*s tty idle what\n", NMAX, NMAX, "User"); fflush(stdout); } - - for (;;) { /* for each entry in utmp */ + for (;;) { /* for each entry in utmp */ if (fread(&utmp, sizeof(utmp), 1, ut) == NULL) { fclose(ut); exit(0); } if (utmp.ut_name[0] == '\0') - continue; /* that tty is free */ + continue; /* that tty is free */ if (sel_user && strncmp(utmp.ut_name, sel_user, NMAX) != 0) - continue; /* we wanted only somebody else */ + continue; /* we wanted only somebody else */ gettty(); jobtime = 0; @@ -207,19 +209,19 @@ main(argc, argv) empty = 1; curpid = -1; idle = findidle(); - for (i=0; icurpid && (pr[i].w_igintr!=IGINT || empty)){ + if (pr[i].w_pid > curpid && (pr[i].w_igintr != IGINT || empty)) { curpid = pr[i].w_pid; strcpy(doing, lflag ? pr[i].w_args : pr[i].w_comm); - if (doing[0]==0 || doing[0]=='-' && doing[1]<=' ' || doing[0] == '?') { + if (doing[0] == 0 || doing[0] == '-' && doing[1] <= ' ' || doing[0] == '?') { strcat(doing, " ("); strcat(doing, pr[i].w_comm); strcat(doing, ")"); @@ -231,7 +233,7 @@ main(argc, argv) } /* figure out the major/minor device # pair for this tty */ -gettty() +void gettty() { char ttybuf[20]; struct stat statbuf; @@ -246,9 +248,8 @@ gettty() /* * putline: print out the accumulated line of info about one user. */ -putline() +void putline() { - /* print login name of the user */ printf("%-*.*s ", NMAX, NMAX, utmp.ut_name); @@ -258,7 +259,7 @@ putline() printf("%-*.*s", LMAX, LMAX, utmp.ut_line); else { /* short form: 2 chars, skipping 'tty' if there */ - if (utmp.ut_line[0]=='t' && utmp.ut_line[1]=='t' && utmp.ut_line[2]=='y') + if (utmp.ut_line[0] == 't' && utmp.ut_line[1] == 't' && utmp.ut_line[2] == 'y') printf("%-2.2s", &utmp.ut_line[3]); else printf("%-2.2s", utmp.ut_line); @@ -269,22 +270,22 @@ putline() prtat(localtime(&utmp.ut_time)); /* print idle time */ - prttime(idle," "); + prttime(idle, " "); if (lflag) { /* print CPU time for all processes & children */ - prttime(DIV60(jobtime)," "); + prttime(DIV60(jobtime), " "); /* print cpu time for interesting process */ - prttime(DIV60(proctime)," "); + prttime(DIV60(proctime), " "); } /* what user is doing, either command tail or args */ - printf(" %-.32s\n",doing); + printf(" %-.32s\n", doing); fflush(stdout); } /* find & return number of minutes current tty has been idle */ -findidle() +int findidle() { struct stat stbuf; long lastaction, diff; @@ -297,8 +298,9 @@ findidle() lastaction = stbuf.st_atime; diff = now - lastaction; diff = DIV60(diff); - if (diff < 0) diff = 0; - return(diff); + if (diff < 0) + diff = 0; + return (diff); } /* @@ -306,21 +308,19 @@ findidle() * The character string tail is printed at the end, obvious * strings to pass are "", " ", or "am". */ -prttime(tim, tail) - time_t tim; - char *tail; +void prttime(time_t tim, char *tail) { - register int didhrs = 0; + int didhrs = 0; if (tim >= 60) { - printf("%3ld:", tim/60); + printf("%3ld:", tim / 60); didhrs++; } else { printf(" "); } tim %= 60; if (tim > 0 || didhrs) { - printf(didhrs&&tim<10 ? "%02ld" : "%2ld", tim); + printf(didhrs && tim < 10 ? "%02ld" : "%2ld", tim); } else { printf(" "); } @@ -328,39 +328,38 @@ prttime(tim, tail) } /* prtat prints a 12 hour time given a pointer to a time of day */ -prtat(p) - register struct tm *p; +void prtat(struct tm *p) { - register int pm; + int pm; time_t t; - t = p -> tm_hour; + t = p->tm_hour; pm = (t > 11); if (t > 11) t -= 12; if (t == 0) t = 12; - prttime(t*60 + p->tm_min, pm ? "pm" : "am"); + prttime(t * 60 + p->tm_min, pm ? "pm" : "am"); } /* * readpr finds and reads in the array pr, containing the interesting * parts of the proc and user tables for each live process. */ -readpr() +void readpr() { - struct kinfo_proc *kp; -register struct proc *p; -register struct smproc *smp; - struct kinfo_proc *kpt; + struct kinfo_proc *kp; + struct proc *p; + struct smproc *smp; + struct kinfo_proc *kpt; int pn, nproc; long addr, daddr, saddr; long txtsiz, datsiz, stksiz; int septxt; int mib[4], st; - size_t size; + size_t size; - if((swmem = open("/dev/mem", 0)) < 0) { + if ((swmem = open("/dev/mem", 0)) < 0) { perror("/dev/mem"); exit(1); } @@ -377,27 +376,25 @@ register struct smproc *smp; fprintf(stderr, "sysctl: %s \n", strerror(errno)); exit(1); } - if (size % sizeof (struct kinfo_proc) != 0) { - fprintf(stderr, "proc size mismatch (%d total, %d chunks)\n", - size, sizeof(struct kinfo_proc)); + if (size % sizeof(struct kinfo_proc) != 0) { + fprintf(stderr, "proc size mismatch (%d total, %d chunks)\n", size, + sizeof(struct kinfo_proc)); exit(1); } kpt = (struct kinfo_proc *)malloc(size); if (kpt == (struct kinfo_proc *)NULL) { - fprintf(stderr, "Not %d bytes of memory for proc table\n", - size); + fprintf(stderr, "Not %d bytes of memory for proc table\n", size); exit(1); } if (sysctl(mib, 4, kpt, &size, NULL, 0) == -1) { - fprintf(stderr, "sysctl fetch of proc table failed: %s\n", - strerror(errno)); + fprintf(stderr, "sysctl fetch of proc table failed: %s\n", strerror(errno)); exit(1); } - nproc = size / sizeof (struct kinfo_proc); - pr = (struct smproc *) malloc(nproc * sizeof(struct smproc)); + nproc = size / sizeof(struct kinfo_proc); + pr = (struct smproc *)malloc(nproc * sizeof(struct smproc)); if (pr == (struct smproc *)NULL) { - fprintf(stderr,"Not enough memory for proc table\n"); + fprintf(stderr, "Not enough memory for proc table\n"); exit(1); } /* @@ -409,7 +406,7 @@ register struct smproc *smp; for (pn = 0; pn < nproc; kp++, pn++) { p = &kp->kp_proc; /* decide if it's an interesting process */ - if (p->p_stat==0 || p->p_stat==SZOMB || p->p_pgrp==0) + if (p->p_stat == 0 || p->p_stat == SZOMB || p->p_pgrp == 0) continue; /* find & read in the user structure */ if (p->p_flag & SLOAD) { @@ -434,7 +431,7 @@ register struct smproc *smp; datsiz = up.u_dsize; stksiz = up.u_ssize; datmap.b1 = txtsiz; - datmap.e1 = datmap.b1+datsiz; + datmap.e1 = datmap.b1 + datsiz; datmap.f1 = daddr; datmap.b2 = stackbas(stksiz); datmap.e2 = stacktop(stksiz); @@ -444,13 +441,13 @@ register struct smproc *smp; smp->w_addr = saddr + (long)p->p_ssize - ARGLIST; smp->w_pid = p->p_pid; smp->w_igintr = ((up.u_signal[SIGINT] == SIG_IGN) + - 2 * ((unsigned)up.u_signal[SIGINT] > (unsigned)SIG_IGN) + - 3 * (up.u_signal[SIGQUIT] == SIG_IGN)) + - 6 * ((unsigned)up.u_signal[SIGQUIT] > (unsigned)SIG_IGN); + 2 * ((unsigned)up.u_signal[SIGINT] > (unsigned)SIG_IGN) + + 3 * (up.u_signal[SIGQUIT] == SIG_IGN)) + + 6 * ((unsigned)up.u_signal[SIGQUIT] > (unsigned)SIG_IGN); smp->w_time = up.u_ru.ru_utime + up.u_ru.ru_stime; smp->w_ctime = up.u_cru.ru_utime + up.u_cru.ru_stime; smp->w_tty = up.u_ttyd; - up.u_comm[14] = 0; /* Bug: This bombs next field. */ + up.u_comm[14] = 0; /* Bug: This bombs next field. */ strcpy(smp->w_comm, up.u_comm); /* * Get args if there's a chance we'll print it. @@ -458,8 +455,9 @@ register struct smproc *smp; * Cant use strncpy: that crock blank pads. */ smp->w_args[0] = 0; - strncat(smp->w_args,getargs(smp),ARGWIDTH); - if (smp->w_args[0]==0 || smp->w_args[0]=='-' && smp->w_args[1]<=' ' || smp->w_args[0] == '?') { + strncat(smp->w_args, getargs(smp), ARGWIDTH); + if (smp->w_args[0] == 0 || smp->w_args[0] == '-' && smp->w_args[1] <= ' ' || + smp->w_args[0] == '?') { strcat(smp->w_args, " ("); strcat(smp->w_args, smp->w_comm); strcat(smp->w_args, ")"); @@ -474,31 +472,29 @@ register struct smproc *smp; * getargs: given a pointer to a proc structure, this looks at the swap area * and tries to reconstruct the arguments. This is straight out of ps. */ -char * -getargs(p) - struct smproc *p; +char *getargs(struct smproc *p) { int c, nbad; static char abuf[ARGLIST]; - register int *ip; - register char *cp, *cp1; + int *ip; + char *cp, *cp1; char **ap; long addr; addr = p->w_addr; /* look for sh special */ - lseek(file, addr+ARGLIST-sizeof(char **), 0); + lseek(file, addr + ARGLIST - sizeof(char **), 0); if (read(file, (char *)&ap, sizeof(char *)) != sizeof(char *)) - return(NULL); + return (NULL); if (ap) { - char *b = (char *) abuf; + char *b = (char *)abuf; char *bp = b; - while((cp=getptr(ap++)) && cp && (bp'~') { - if (nbad++>3) + while ((c = getbyte(cp++)) && (bp < b + ARGWIDTH)) { + if (c < ' ' || c > '~') { + if (nbad++ > 3) break; continue; } @@ -507,22 +503,22 @@ getargs(p) *bp++ = ' '; } *bp++ = 0; - return(b); + return (b); } lseek(file, addr, 0); if (read(file, abuf, sizeof(abuf)) != sizeof(abuf)) - return((char *)1); - for (ip = (int *) &abuf[ARGLIST]-2; ip > (int *) abuf;) { + return ((char *)1); + for (ip = (int *)&abuf[ARGLIST] - 2; ip > (int *)abuf;) { /* Look from top for -1 or 0 as terminator flag. */ if (*--ip == -1 || *ip == 0) { - cp = (char *)(ip+1); - if (*cp==0) + cp = (char *)(ip + 1); + if (*cp == 0) cp++; - nbad = 0; /* up to 5 funny chars as ?'s */ + nbad = 0; /* up to 5 funny chars as ?'s */ for (cp1 = cp; cp1 < (char *)&abuf[ARGLIST]; cp1++) { - c = *cp1&0177; - if (c==0) /* nulls between args => spaces */ + c = *cp1 & 0177; + if (c == 0) /* nulls between args => spaces */ *cp1 = ' '; else if (c < ' ' || c > 0176) { if (++nbad >= 5) { @@ -530,64 +526,57 @@ getargs(p) break; } *cp1 = '?'; - } else if (c=='=') { /* Oops - found an - * environment var, back - * over & erase it. */ + } else if (c == '=') { /* Oops - found an + * environment var, back + * over & erase it. */ *cp1 = 0; - while (cp1>cp && *--cp1!=' ') + while (cp1 > cp && *--cp1 != ' ') *cp1 = 0; break; } } - while (*--cp1==' ') /* strip trailing spaces */ + while (*--cp1 == ' ') /* strip trailing spaces */ *cp1 = 0; - return(cp); + return (cp); } } return (p->w_comm); } -char * -getptr(adr) -char **adr; +char *getptr(char **adr) { char *ptr; - register char *p, *pa; - register i; + char *p, *pa; + int i; ptr = 0; pa = (char *)adr; p = (char *)&ptr; - for (i=0; ib1, amap->e1)) { - if(within(adr, amap->b2, amap->e2)) { + if (!within(adr, amap->b1, amap->e1)) { + if (within(adr, amap->b2, amap->e2)) { saddr = (unsigned)adr + amap->f2 - amap->b2; } else - return(0); + return (0); } else saddr = (unsigned)adr + amap->f1 - amap->b1; - if(lseek(file, saddr, 0)==-1 - || read(file, &b, 1)<1) { - return(0); + if (lseek(file, saddr, 0) == -1 || read(file, &b, 1) < 1) { + return (0); } - return((unsigned)b); + return ((unsigned)b); } - -within(adr,lbd,ubd) -char *adr; -long lbd, ubd; +int within(char *adr, long lbd, long ubd) { - return((unsigned)adr>=lbd && (unsigned)adr= lbd && (unsigned)adr < ubd); } diff --git a/src/cmd/wc.c b/src/cmd/wc.c index 77ce31e..0f96531 100644 --- a/src/cmd/wc.c +++ b/src/cmd/wc.c @@ -8,22 +8,26 @@ #include #include -long linect, wordct, charct, pagect; -long tlinect, twordct, tcharct, tpagect; -char *wd = "lwc"; +long linect, wordct, charct, pagect; +long tlinect, twordct, tcharct, tpagect; +char *wd = "lwc"; -main(argc, argv) -char **argv; +static void wcp(char *wd, long charct, long wordct, long linect); +static void ipr(long num); + +int main(int argc, char **argv) { int i, token; - register FILE *fp; - register int c; + FILE *fp; + int c; char *p; while (argc > 1 && *argv[1] == '-') { switch (argv[1][1]) { - case 'l': case 'w': case 'c': - wd = argv[1]+1; + case 'l': + case 'w': + case 'c': + wd = argv[1] + 1; break; default: usage: @@ -37,7 +41,7 @@ char **argv; i = 1; fp = stdin; do { - if(argc>1 && (fp=fopen(argv[i], "r")) == NULL) { + if (argc > 1 && (fp = fopen(argv[i], "r")) == NULL) { perror(argv[i]); continue; } @@ -45,28 +49,27 @@ char **argv; wordct = 0; charct = 0; token = 0; - for(;;) { + for (;;) { c = getc(fp); if (c == EOF) break; charct++; - if(' '1) { + if (argc > 1) { printf(" %s\n", argv[i]); } else printf("\n"); @@ -74,36 +77,34 @@ char **argv; tlinect += linect; twordct += wordct; tcharct += charct; - } while(++i 2) { + } while (++i < argc); + if (argc > 2) { wcp(wd, tcharct, twordct, tlinect); printf(" total\n"); } exit(0); } -wcp(wd, charct, wordct, linect) -register char *wd; -long charct; long wordct; long linect; +void wcp(char *wd, long charct, long wordct, long linect) { - while (*wd) switch (*wd++) { - case 'l': - ipr(linect); - break; + while (*wd) { + switch (*wd++) { + case 'l': + ipr(linect); + break; - case 'w': - ipr(wordct); - break; - - case 'c': - ipr(charct); - break; + case 'w': + ipr(wordct); + break; + case 'c': + ipr(charct); + break; + } } } -ipr(num) -long num; +void ipr(long num) { printf(" %7ld", num); } diff --git a/src/cmd/whereis.c b/src/cmd/whereis.c index 7f48d5b..dd2ea23 100644 --- a/src/cmd/whereis.c +++ b/src/cmd/whereis.c @@ -3,14 +3,15 @@ * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. */ -#include -#include +#include #include #include #include -#include +#include +#include static char *bindirs[] = { + // clang-format off "/etc", "/bin", "/sbin", @@ -20,8 +21,10 @@ static char *bindirs[] = { "/local/bin", "/new", 0 + // clang-format on }; static char *mandirs[] = { + // clang-format off "/man/man1", "/man/man2", "/man/man3", @@ -34,8 +37,10 @@ static char *mandirs[] = { "/man/mann", "/man/mano", 0 + // clang-format on }; -static char *srcdirs[] = { +static char *srcdirs[] = { + // clang-format off "/src/bin", "/src/sbin", "/src/etc", @@ -51,88 +56,96 @@ static char *srcdirs[] = { "/src/lib/libc/net/inet", "/src/lib/libc/net/misc", 0 + // clang-format on }; -char sflag = 1; -char bflag = 1; -char mflag = 1; -char **Sflag; +char sflag = 1; +char bflag = 1; +char mflag = 1; +char **Sflag; int Scnt; -char **Bflag; +char **Bflag; int Bcnt; -char **Mflag; +char **Mflag; int Mcnt; -char uflag; +char uflag; + +static void getlist(int *argcp, char ***argvp, char ***flagp, int *cntp); +static void zerof(void); +static void lookup(char *cp); +static void looksrc(char *cp); +static void lookbin(char *cp); +static void lookman(char *cp); +static void find(char **dirs, char *cp); +static void findv(char **dirv, int dirc, char *cp); +static void findin(char *dir, char *cp); +static int itsit(char *cp, char *dp); + /* * whereis name * look for source, documentation and binaries */ -main(argc, argv) - int argc; - char *argv[]; +int main(int argc, char *argv[]) { - argc--, argv++; if (argc == 0) { usage: fprintf(stderr, "whereis [ -sbmu ] [ -SBM dir ... -f ] name...\n"); exit(1); } - do + do { if (argv[0][0] == '-') { - register char *cp = argv[0] + 1; - while (*cp) switch (*cp++) { + char *cp = argv[0] + 1; - case 'f': - break; + while (*cp) { + switch (*cp++) { + case 'f': + break; - case 'S': - getlist(&argc, &argv, &Sflag, &Scnt); - break; + case 'S': + getlist(&argc, &argv, &Sflag, &Scnt); + break; - case 'B': - getlist(&argc, &argv, &Bflag, &Bcnt); - break; + case 'B': + getlist(&argc, &argv, &Bflag, &Bcnt); + break; - case 'M': - getlist(&argc, &argv, &Mflag, &Mcnt); - break; + case 'M': + getlist(&argc, &argv, &Mflag, &Mcnt); + break; - case 's': - zerof(); - sflag++; - continue; + case 's': + zerof(); + sflag++; + continue; - case 'u': - uflag++; - continue; + case 'u': + uflag++; + continue; - case 'b': - zerof(); - bflag++; - continue; + case 'b': + zerof(); + bflag++; + continue; - case 'm': - zerof(); - mflag++; - continue; + case 'm': + zerof(); + mflag++; + continue; - default: - goto usage; + default: + goto usage; + } } argv++; - } else + } else { lookup(*argv++); - while (--argc > 0); + } + } while (--argc > 0); } -getlist(argcp, argvp, flagp, cntp) - char ***argvp; - int *argcp; - char ***flagp; - int *cntp; +void getlist(int *argcp, char ***argvp, char ***flagp, int *cntp) { - (*argvp)++; *flagp = *argvp; *cntp = 0; @@ -142,21 +155,18 @@ getlist(argcp, argvp, flagp, cntp) (*argvp)--; } - -zerof() +void zerof() { - if (sflag && bflag && mflag) sflag = bflag = mflag = 0; } + int count; int print; - -lookup(cp) - register char *cp; +void lookup(char *cp) { - register char *dp; + char *dp; for (dp = cp; *dp; dp++) continue; @@ -204,8 +214,7 @@ again: printf("\n"); } -looksrc(cp) - char *cp; +void looksrc(char *cp) { if (Sflag == 0) { find(srcdirs, cp); @@ -213,8 +222,7 @@ looksrc(cp) findv(Sflag, Scnt, cp); } -lookbin(cp) - char *cp; +void lookbin(char *cp) { if (Bflag == 0) find(bindirs, cp); @@ -222,8 +230,7 @@ lookbin(cp) findv(Bflag, Bcnt, cp); } -lookman(cp) - char *cp; +void lookman(char *cp) { if (Mflag == 0) { find(mandirs, cp); @@ -231,27 +238,19 @@ lookman(cp) findv(Mflag, Mcnt, cp); } -findv(dirv, dirc, cp) - char **dirv; - int dirc; - char *cp; +void findv(char **dirv, int dirc, char *cp) { - while (dirc > 0) findin(*dirv++, cp), dirc--; } -find(dirs, cp) - char **dirs; - char *cp; +void find(char **dirs, char *cp) { - while (*dirs) findin(*dirs++, cp); } -findin(dir, cp) - char *dir, *cp; +void findin(char *dir, char *cp) { DIR *dirp; struct direct *dp; @@ -269,12 +268,11 @@ findin(dir, cp) closedir(dirp); } -itsit(cp, dp) - register char *cp, *dp; +int itsit(char *cp, char *dp) { - register int i = strlen(dp); + int i = strlen(dp); - if (dp[0] == 's' && dp[1] == '.' && itsit(cp, dp+2)) + if (dp[0] == 's' && dp[1] == '.' && itsit(cp, dp + 2)) return (1); while (*cp && *dp && *cp == *dp) cp++, dp++, i--; diff --git a/src/cmd/who.c b/src/cmd/who.c index b752044..16a6bb3 100644 --- a/src/cmd/who.c +++ b/src/cmd/who.c @@ -3,41 +3,41 @@ * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. */ -#include -#include +#include #include #include -#include -#include /* for MAXHOSTNAMELEN */ +#include +#include #include #include +#include /* for MAXHOSTNAMELEN */ #include -#include #include +#include #define NMAX sizeof(utmp.ut_name) #define LMAX sizeof(utmp.ut_line) #define HMAX sizeof(utmp.ut_host) -struct utmp utmp; -struct passwd *pw; -char hostname[MAXHOSTNAMELEN]; +struct utmp utmp; +struct passwd *pw; +char hostname[MAXHOSTNAMELEN]; -main(argc, argv) - int argc; - char **argv; +static void putline(void); + +int main(int argc, char **argv) { - register char *tp, *s; - register FILE *fi; + char *tp, *s; + FILE *fi; s = _PATH_UTMP; - if(argc == 2) + if (argc == 2) s = argv[1]; if (argc == 3) { tp = ttyname(0); if (tp) tp = rindex(tp, '/') + 1; - else { /* no tty - use best guess from passwd file */ + else { /* no tty - use best guess from passwd file */ pw = getpwuid(getuid()); strncpy(utmp.ut_name, pw ? pw->pw_name : "?", NMAX); strcpy(utmp.ut_line, "tty??"); @@ -52,7 +52,7 @@ main(argc, argv) } while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) { if (argc == 3) { - gethostname(hostname, sizeof (hostname)); + gethostname(hostname, sizeof(hostname)); if (strcmp(utmp.ut_line, tp)) continue; printf("%s!", hostname); @@ -65,15 +65,13 @@ main(argc, argv) } } -putline() +void putline() { - register char *cbuf; + char *cbuf; - printf("%-*.*s %-*.*s", - NMAX, NMAX, utmp.ut_name, - LMAX, LMAX, utmp.ut_line); + printf("%-*.*s %-*.*s", NMAX, NMAX, utmp.ut_name, LMAX, LMAX, utmp.ut_line); cbuf = ctime(&utmp.ut_time); - printf("%.12s", cbuf+4); + printf("%.12s", cbuf + 4); if (utmp.ut_host[0]) printf("\t(%.*s)", HMAX, utmp.ut_host); putchar('\n');