From 35371dec1e2b253df8bc26206721757ff9166b22 Mon Sep 17 00:00:00 2001 From: Edward Wang Date: Thu, 13 Dec 1984 22:43:56 -0800 Subject: [PATCH] performance SCCS-vsn: bin/csh/time.c 4.6 SCCS-vsn: bin/csh/exec.c 4.10 SCCS-vsn: bin/csh/hist.c 4.9 SCCS-vsn: bin/csh/proc.c 4.15 SCCS-vsn: bin/csh/dir.h 4.2 SCCS-vsn: bin/csh/err.c 4.2 SCCS-vsn: bin/csh/exp.c 4.2 SCCS-vsn: bin/csh/init.c 4.4 SCCS-vsn: bin/csh/proc.h 4.5 SCCS-vsn: bin/csh/misc.c 4.4 SCCS-vsn: bin/csh/parse.c 4.2 SCCS-vsn: bin/csh/sem.c 4.5 SCCS-vsn: bin/csh/set.c 4.4 SCCS-vsn: bin/csh/glob.c 4.7 SCCS-vsn: bin/csh/dol.c 4.5 SCCS-vsn: bin/csh/csh.c 4.20 SCCS-vsn: bin/csh/dir.c 4.3 SCCS-vsn: bin/csh/lex.c 4.7 SCCS-vsn: bin/csh/csh.h 4.12 SCCS-vsn: bin/csh/func.c 4.13 SCCS-vsn: bin/csh/alloc.c 4.4 SCCS-vsn: bin/csh/Makefile 4.7 SCCS-vsn: bin/csh/file.c 1.6 --- usr/src/bin/csh/Makefile | 167 ++++++++++++++--- usr/src/bin/csh/alloc.c | 56 +++--- usr/src/bin/csh/csh.c | 159 ++++++++-------- usr/src/bin/csh/csh.h | 70 ++++--- usr/src/bin/csh/dir.c | 26 +-- usr/src/bin/csh/dir.h | 2 +- usr/src/bin/csh/dol.c | 67 ++++--- usr/src/bin/csh/err.c | 9 +- usr/src/bin/csh/exec.c | 107 ++++++----- usr/src/bin/csh/exp.c | 55 +++--- usr/src/bin/csh/file.c | 80 ++++---- usr/src/bin/csh/func.c | 145 +++++++++------ usr/src/bin/csh/glob.c | 119 ++++++------ usr/src/bin/csh/hist.c | 37 ++-- usr/src/bin/csh/init.c | 18 +- usr/src/bin/csh/lex.c | 341 +++++++++++++++++----------------- usr/src/bin/csh/misc.c | 161 +++++++++------- usr/src/bin/csh/parse.c | 25 +-- usr/src/bin/csh/proc.c | 96 +++++----- usr/src/bin/csh/proc.h | 6 +- usr/src/bin/csh/sem.c | 103 ++++++----- usr/src/bin/csh/set.c | 386 ++++++++++++++++++++++++++------------- usr/src/bin/csh/time.c | 18 +- 23 files changed, 1292 insertions(+), 961 deletions(-) diff --git a/usr/src/bin/csh/Makefile b/usr/src/bin/csh/Makefile index b3b5d25890..153a36d324 100644 --- a/usr/src/bin/csh/Makefile +++ b/usr/src/bin/csh/Makefile @@ -1,13 +1,15 @@ # -# Makefile 4.6 %G% +# @(#)Makefile 4.7 (Berkeley) %G% # # C Shell with process control; VM/UNIX VAX Makefile # Bill Joy UC Berkeley; Jim Kulp IIASA, Austria # -CFLAGS= -O -DTELL -DVMUNIX -DVFORK +# To profile, put -DPROF in DEFS and -pg in CFLAGS, and recompile. + +DEFS= -DTELL -DVFORK -DFILEC +CFLAGS= $(DEFS) -O XSTR= /usr/ucb/xstr -ED= -ed -AS= -as +AS= as RM= -rm CXREF= /usr/ucb/cxref VGRIND= csh /usr/ucb/vgrind @@ -15,11 +17,10 @@ CTAGS= /usr/ucb/ctags LIBES= SCCS= /usr/local/sccs -# strings.o must be last in OBJS since it can change when previous files compile -OBJS= alloc.o doprnt.o printf.o sh.dir.o sh.dol.o sh.err.o sh.exec.o \ - sh.exp.o sh.func.o sh.glob.o sh.hist.o sh.init.o sh.lex.o sh.misc.o \ - sh.parse.o sh.print.o sh.proc.o sh.sem.o sh.set.o sh.time.o \ - strings.o +OBJS= alloc.o doprnt.o printf.o sh.o sh.char.o sh.dir.o sh.dol.o sh.err.o \ + sh.exec.o sh.exp.o sh.file.o sh.func.o sh.glob.o sh.hist.o sh.init.o \ + sh.lex.o sh.misc.o sh.parse.o sh.print.o sh.proc.o sh.sem.o sh.set.o \ + sh.time.o # Special massaging of C files for sharing of strings .c.o: @@ -28,29 +29,17 @@ OBJS= alloc.o doprnt.o printf.o sh.dir.o sh.dol.o sh.err.o sh.exec.o \ mv -f x.o $*.o rm -f x.c -csh: sh.o ${OBJS} sh.local.h +# strings.o must be last since it can change when previous files compile +csh: ${OBJS} strings.o rm -f csh - ${CC} sh.o ${OBJS} -o csh ${LIBES} - -# To make csh.prof: put -pg in CFLAGS, make clean, make csh.prof -csh.prof: sh.prof.o ${OBJS} sh.local.h - rm -f csh.prof - ${CC} -pg sh.prof.o ${OBJS} ${LIBES} -o csh.prof - -sh.prof.o: sh.c - ${CC} -E ${CFLAGS} -DPROF sh.c | ${XSTR} -c - - ${CC} -c ${CFLAGS} -DPROF x.c - mv -f x.o sh.prof.o - rm -f x.c + ${CC} ${OBJS} strings.o -o csh ${LIBES} .DEFAULT: ${SCCS} get $< # need an old doprnt, whose output we can trap doprnt.o: doprnt.c - ${CC} -E doprnt.c > doprnt.s - ${AS} -o doprnt.o doprnt.s - rm -f doprnt.s + ${CC} -E doprnt.c | ${AS} -o doprnt.o # strings.o and sh.init.o are specially processed to be shared strings.o: strings @@ -66,7 +55,7 @@ sh.init.o: rm -f x.c lint: - lint ${CFLAGS} sh*.c + lint -z ${DEFS} sh*.c alloc.c print: @pr READ_ME @@ -91,12 +80,130 @@ vgrind: @for i in *.c; do vgrind -t -h "C Shell" $$i >/crp/bill/csh/$$i.t; done @vgrind -t -x -h Index index >/crp/bill/csh/index.t -install: csh sh.local.h +install: csh install -s csh ${DESTDIR}/bin/csh clean: ${RM} -f a.out strings x.c xs.c csh errs - ${RM} -f *.o sh.prof.c + ${RM} -f *.o + +tags: + ${CTAGS} *.h sh*.c -tags: /tmp - ${CTAGS} sh*.c +### +alloc.o: /usr/include/sys/types.h +sh.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h \ + /usr/include/sys/ioctl.h /usr/include/sys/ttychars.h \ + /usr/include/sys/ttydev.h /usr/include/sys/jioctl.h \ + /usr/include/fcntl.h /usr/include/pwd.h +sh.char.o: sh.char.h +sh.dir.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h sh.dir.h +sh.dol.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h sh.char.h +sh.err.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h \ + /usr/include/sys/ioctl.h /usr/include/sys/ttychars.h \ + /usr/include/sys/ttydev.h /usr/include/sys/jioctl.h +sh.exec.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h \ + /usr/include/sys/dir.h +sh.exp.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h +sh.file.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h \ + /usr/include/sgtty.h /usr/include/sys/ioctl.h \ + /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h \ + /usr/include/sys/jioctl.h /usr/include/sys/dir.h /usr/include/pwd.h +sh.func.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h \ + /usr/include/sys/ioctl.h /usr/include/sys/ttychars.h \ + /usr/include/sys/ttydev.h /usr/include/sys/jioctl.h +sh.glob.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h sh.char.h \ + /usr/include/sys/dir.h +sh.hist.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h +sh.init.o: sh.local.h +sh.lex.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h sh.char.h \ + /usr/include/sgtty.h /usr/include/sys/ioctl.h \ + /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h \ + /usr/include/sys/jioctl.h +sh.misc.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h +sh.parse.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h +sh.print.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h \ + /usr/include/sys/ioctl.h /usr/include/sys/ttychars.h \ + /usr/include/sys/ttydev.h /usr/include/sys/jioctl.h +sh.proc.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h sh.dir.h \ + sh.proc.h /usr/include/sys/wait.h /usr/include/sys/ioctl.h \ + /usr/include/sys/ttychars.h /usr/include/sys/ttydev.h \ + /usr/include/sys/jioctl.h +sh.sem.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h sh.proc.h \ + /usr/include/sys/ioctl.h /usr/include/sys/ttychars.h \ + /usr/include/sys/ttydev.h /usr/include/sys/jioctl.h \ + /usr/include/fcntl.h +sh.set.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h +sh.time.o: sh.h /usr/include/sys/time.h /usr/include/sys/resource.h \ + /usr/include/sys/param.h /usr/include/machine/machparam.h \ + /usr/include/signal.h /usr/include/signal.h /usr/include/sys/types.h \ + /usr/include/sys/stat.h /usr/include/sys/signal.h \ + /usr/include/errno.h /usr/include/setjmp.h sh.local.h diff --git a/usr/src/bin/csh/alloc.c b/usr/src/bin/csh/alloc.c index 854faa242d..66452616bb 100644 --- a/usr/src/bin/csh/alloc.c +++ b/usr/src/bin/csh/alloc.c @@ -1,5 +1,5 @@ #ifndef lint -static char *sccsid = "@(#)alloc.c 4.3 (Berkeley from Caltech) %G%"; +static char *sccsid = "@(#)alloc.c 4.4 (Berkeley from Caltech) %G%"; #endif /* @@ -60,13 +60,11 @@ union overhead { static union overhead *nextf[NBUCKETS]; extern char *sbrk(); -#ifdef debug /* * nmalloc[i] is the difference between the number of mallocs and frees * for a given block size. */ static u_int nmalloc[NBUCKETS]; -#endif #ifdef debug #define ASSERT(p) if (!(p)) botch("p"); else @@ -113,9 +111,7 @@ malloc(nbytes) nextf[bucket] = nextf[bucket]->ov_next; p->ov_magic = MAGIC; p->ov_index= bucket; -#ifdef debug nmalloc[bucket]++; -#endif #ifdef RCHECK /* * Record allocated size of block and @@ -150,7 +146,7 @@ morecore(bucket) */ op = (union overhead *)sbrk(0); if ((int)op & 0x3ff) - sbrk(1024 - ((int)op & 0x3ff)); + (void) sbrk(1024 - ((int)op & 0x3ff)); /* take 2k unless the block is bigger than that */ rnu = (bucket <= 8) ? 11 : bucket + 3; nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */ @@ -210,9 +206,7 @@ free(cp) size = op->ov_index; op->ov_next = nextf[size]; nextf[size] = op; -#ifdef debug nmalloc[size]--; -#endif } /* @@ -284,7 +278,6 @@ int srchlen; return (-1); } -#ifdef debug /* * mstats - print out statistics about malloc * @@ -293,32 +286,31 @@ int srchlen; * frees for each size category. */ showall(s) - char **s; +char **s; { - register int i, j; - register union overhead *p; - int totfree = 0, - totused = 0; + register int i, j; + register union overhead *p; + int totfree = 0, + totused = 0; if (s[1]) - printf("Memory allocation statistics %s\nfree:\t", s[1]); - for (i = 0; i < NBUCKETS; i++) { - for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) - ; - - if (s[1]) - printf(" %d", j); - totfree += j * (1 << (i + 3)); - } + printf("Memory allocation statistics %s\nfree:", s[1]); + for (i = 0; i < NBUCKETS; i++) { + for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) + ; + + if (s[1]) + printf(" %d", j); + totfree += j * (1 << (i + 3)); + } if (s[1]) - printf("\nused:\t"); - for (i = 0; i < NBUCKETS; i++) { - if (s[1]) - printf(" %d", nmalloc[i]); - totused += nmalloc[i] * (1 << (i + 3)); - } + printf("\nused:"); + for (i = 0; i < NBUCKETS; i++) { + if (s[1]) + printf(" %d", nmalloc[i]); + totused += nmalloc[i] * (1 << (i + 3)); + } if (s[1]) - printf("\n\t"); - printf("Total in use: %d, total free: %d\n", totused, totfree); + printf("\n"); + printf("Total in use: %d, total free: %d\n", totused, totfree); } -#endif diff --git a/usr/src/bin/csh/csh.c b/usr/src/bin/csh/csh.c index b56d1884c7..33d290a9fd 100644 --- a/usr/src/bin/csh/csh.c +++ b/usr/src/bin/csh/csh.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)csh.c 4.19 %G%"; +#ifndef lint +static char *sccsid = "@(#)csh.c 4.20 (Berkeley) %G%"; +#endif #include "sh.h" #include @@ -42,7 +44,7 @@ main(c, av) uid = getuid(); loginsh = **v == '-' && c == 1; if (loginsh) - time(&chktim); + (void) time(&chktim); /* * Move the descriptors to safe places. @@ -78,29 +80,11 @@ main(c, av) */ if ((cp = getenv("PATH")) == NOSTR) set1("path", saveblk(pathlist), &shvhed); - else { - register unsigned i = 0; - register char *dp; - register char **pv; - - for (dp = cp; *dp; dp++) - if (*dp == ':') - i++; - pv = (char **)calloc(i+2, sizeof (char **)); - for (dp = cp, i = 0; ;) - if (*dp == ':') { - *dp = 0; - pv[i++] = savestr(*cp ? cp : "."); - *dp++ = ':'; - cp = dp; - } else if (*dp++ == 0) { - pv[i++] = savestr(*cp ? cp : "."); - break; - } - pv[i] = 0; - set1("path", pv, &shvhed); - } - set("shell", SHELLPATH); + else + importpath(cp); + if ((cp = getenv("SHELL")) == NOSTR) + cp = SHELLPATH; + set("shell", savestr(cp)); doldol = putn(getpid()); /* For $$ */ shtemp = strspl("/tmp/sh", doldol); /* For << */ @@ -112,14 +96,16 @@ main(c, av) * Our children inherit termination from our parent. * We catch it only if we are the login shell. */ - sigvec(SIGINT, 0, &osv); /* parents interruptibility */ + /* parents interruptibility */ + (void) sigvec(SIGINT, (struct sigvec *)0, &osv); parintr = osv.sv_handler; - sigvec(SIGTERM, 0, &osv); /* parents terminability */ + /* parents terminability */ + (void) sigvec(SIGTERM, (struct sigvec *)0, &osv); parterm = osv.sv_handler; if (loginsh) { - signal(SIGHUP, phup); /* exit processing on HUP */ - signal(SIGXCPU, phup); /* ...and on XCPU */ - signal(SIGXFSZ, phup); /* ...and on XFSZ */ + (void) signal(SIGHUP, phup); /* exit processing on HUP */ + (void) signal(SIGXCPU, phup); /* ...and on XCPU */ + (void) signal(SIGXFSZ, phup); /* ...and on XFSZ */ } /* @@ -195,10 +181,9 @@ main(c, av) } while (*cp); v++, c--; } -argsdone: if (quitit) /* With all due haste, for debugging */ - signal(SIGQUIT, SIG_DFL); + (void) signal(SIGQUIT, SIG_DFL); /* * Unless prevented by -c, -i, -s, or -t, if there @@ -213,10 +198,11 @@ argsdone: } file = v[0]; SHIN = dmove(nofile, FSHIN); /* Replace FSHIN */ + (void) ioctl(SHIN, FIOCLEX, (char *)0); prompt = 0; c--, v++; } - if (uid != geteuid() && !batch) { + if (!batch && uid != geteuid()) { errno = EACCES; child++; /* So this ... */ Perror("csh"); /* ... doesn't return */ @@ -259,14 +245,14 @@ argsdone: if (setintr) { **av = '-'; if (!quitit) /* Wary! */ - signal(SIGQUIT, SIG_IGN); - signal(SIGINT, pintr); - sigblock(sigmask(SIGINT)); - signal(SIGTERM, SIG_IGN); + (void) signal(SIGQUIT, SIG_IGN); + (void) signal(SIGINT, pintr); + (void) sigblock(sigmask(SIGINT)); + (void) signal(SIGTERM, SIG_IGN); if (quitit == 0 && arginp == 0) { - signal(SIGTSTP, SIG_IGN); - signal(SIGTTIN, SIG_IGN); - signal(SIGTTOU, SIG_IGN); + (void) signal(SIGTSTP, SIG_IGN); + (void) signal(SIGTTIN, SIG_IGN); + (void) signal(SIGTTOU, SIG_IGN); /* * Wait till in foreground, in case someone * stupidly runs @@ -282,31 +268,33 @@ argsdone: else f = -1; retry: - if (ioctl(f, TIOCGPGRP, &tpgrp) == 0 && tpgrp != -1) { + if (ioctl(f, TIOCGPGRP, (char *)&tpgrp) == 0 && + tpgrp != -1) { int ldisc; if (tpgrp != shpgrp) { int (*old)() = signal(SIGTTIN, SIG_DFL); - kill(0, SIGTTIN); - signal(SIGTTIN, old); + (void) kill(0, SIGTTIN); + (void) signal(SIGTTIN, old); goto retry; } - if (ioctl(f, TIOCGETD, &oldisc) != 0) + if (ioctl(f, TIOCGETD, (char *)&oldisc) != 0) goto notty; if (oldisc != NTTYDISC) { #ifdef DEBUG printf("Switching to new tty driver...\n"); #endif DEBUG ldisc = NTTYDISC; - ioctl(f, TIOCSETD, &ldisc); + (void) ioctl(f, TIOCSETD, + (char *)&ldisc); } else oldisc = -1; opgrp = shpgrp; shpgrp = getpid(); tpgrp = shpgrp; - ioctl(f, TIOCSPGRP, &shpgrp); - setpgrp(0, shpgrp); - dcopy(f, FSHTTY); - ioctl(FSHTTY, FIOCLEX, 0); + (void) ioctl(f, TIOCSPGRP, (char *)&shpgrp); + (void) setpgrp(0, shpgrp); + (void) ioctl(dcopy(f, FSHTTY), FIOCLEX, + (char *)0); } else { notty: printf("Warning: no access to tty; thus no job control in this shell...\n"); @@ -316,7 +304,7 @@ notty: } if (setintr == 0 && parintr == SIG_DFL) setintr++; - signal(SIGCHLD, pchild); /* while signals not ready */ + (void) signal(SIGCHLD, pchild); /* while signals not ready */ /* * Set an exit here in case of an interrupt or error reading @@ -328,7 +316,7 @@ notty: reenter++; /* Will have value("home") here because set fast if don't */ srccat(value("home"), "/.cshrc"); - if (!fast && !arginp && !onelflg) + if (!fast && !arginp && !onelflg && !havhash) dohash(); dosource(loadhist); if (loginsh) { @@ -357,7 +345,7 @@ notty: */ if (loginsh) { printf("logout\n"); - close(SHIN); + (void) close(SHIN); child++; goodbye(); } @@ -369,13 +357,13 @@ untty() { if (tpgrp > 0) { - setpgrp(0, opgrp); - ioctl(FSHTTY, TIOCSPGRP, &opgrp); + (void) setpgrp(0, opgrp); + (void) ioctl(FSHTTY, TIOCSPGRP, (char *)&opgrp); if (oldisc != -1 && oldisc != NTTYDISC) { #ifdef DEBUG printf("\nReverting to old tty driver...\n"); #endif DEBUG - ioctl(FSHTTY, TIOCSETD, &oldisc); + (void) ioctl(FSHTTY, TIOCSETD, (char *)&oldisc); } } } @@ -397,7 +385,7 @@ importpath(cp) * There are i+1 directories in the path plus we need * room for a zero terminator. */ - pv = (char **) calloc(i+2, sizeof (char **)); + pv = (char **) calloc((unsigned) (i + 2), sizeof (char **)); dp = cp; i = 0; if (*dp) @@ -426,7 +414,7 @@ srccat(cp, dp) register char *ep = strspl(cp, dp); register int unit = dmove(open(ep, 0), -1); - /* ioctl(unit, FIOCLEX, NULL); */ + (void) ioctl(unit, FIOCLEX, (char *)0); xfree(ep); #ifdef INGRES srcunit(unit, 0, 0); @@ -471,7 +459,7 @@ srcunit(unit, onlyown, hflg) if (fstat(unit, &stb) < 0 || (stb.st_uid != uid && stb.st_gid != getgid())) { - close(unit); + (void) close(unit); return; } } @@ -509,14 +497,14 @@ srcunit(unit, onlyown, hflg) * we let ourselves be interrupted. */ if (setintr) - sigsetmask(omask); + (void) sigsetmask(omask); #ifdef TELL settell(); #endif process(0); /* 0 -> blow away on errors */ } if (setintr) - sigsetmask(omask); + (void) sigsetmask(omask); if (oSHIN >= 0) { register int i; @@ -529,7 +517,7 @@ srcunit(unit, onlyown, hflg) /* Reset input arena */ copy((char *)&B, (char *)&saveB, sizeof B); - close(SHIN), SHIN = oSHIN; + (void) close(SHIN), SHIN = oSHIN; arginp = oarginp, onelflg = oonelflg; evalp = oevalp, evalvec = oevalvec; intty = oldintty, whyles = oldwhyl, gointr = ogointr; @@ -558,8 +546,8 @@ rechist() if (!fast) { if (value("savehist")[0] == '\0') return; - strcpy(buf, value("home")); - strcat(buf, "/.history"); + (void) strcpy(buf, value("home")); + (void) strcat(buf, "/.history"); fp = creat(buf, 0666); if (fp == -1) return; @@ -567,10 +555,10 @@ rechist() didfds = 0; ftmp = SHOUT; SHOUT = fp; - strcpy(buf, value("savehist")); + (void) strcpy(buf, value("savehist")); dumphist[2] = buf; dohist(dumphist); - close(fp); + (void) close(fp); SHOUT = ftmp; didfds = oldidfds; } @@ -579,9 +567,9 @@ rechist() goodbye() { if (loginsh) { - signal(SIGQUIT, SIG_IGN); - signal(SIGINT, SIG_IGN); - signal(SIGTERM, SIG_IGN); + (void) signal(SIGQUIT, SIG_IGN); + (void) signal(SIGINT, SIG_IGN); + (void) signal(SIGTERM, SIG_IGN); setintr = 0; /* No interrupts after "logout" */ if (adrof("home")) srccat(value("home"), "/.logout"); @@ -593,6 +581,9 @@ goodbye() exitstat() { +#ifdef PROF + monitor(0); +#endif /* * Note that if STATUS is corrupted (i.e. getn bombs) * then error will exit directly because we poke child here. @@ -632,7 +623,7 @@ pintr1(wantnl) omask = sigblock(0); if (setintr) { - sigsetmask(omask & ~sigmask(SIGINT)); + (void) sigsetmask(omask & ~sigmask(SIGINT)); if (pjobs) { pjobs = 0; printf("\n"); @@ -640,7 +631,7 @@ pintr1(wantnl) bferr("Interrupted"); } } - sigsetmask(omask & ~sigmask(SIGCHLD)); + (void) sigsetmask(omask & ~sigmask(SIGCHLD)); draino(); /* @@ -678,9 +669,8 @@ pintr1(wantnl) process(catch) bool catch; { - register char *cp; jmp_buf osetexit; - struct command *t; + register struct command *t; getexit(osetexit); for (;;) { @@ -695,7 +685,7 @@ process(catch) * Interruptible during interactive reads */ if (setintr) - sigsetmask(sigblock(0) & ~sigmask(SIGINT)); + (void) sigsetmask(sigblock(0) & ~sigmask(SIGINT)); /* * For the sake of reset() @@ -740,7 +730,6 @@ process(catch) */ if (fseekp == feobp) printprompt(); - flush(); } err = 0; @@ -759,7 +748,7 @@ process(catch) * The parser may lose space if interrupted. */ if (setintr) - sigblock(sigmask(SIGINT)); + (void) sigblock(sigmask(SIGINT)); /* * Save input text on the history list if @@ -819,12 +808,13 @@ dosource(t) t++; hflg++; } - strcpy(buf, *t); + (void) strcpy(buf, *t); f = globone(buf); u = dmove(open(f, 0), -1); xfree(f); if (u < 0 && !hflg) Perror(f); + (void) ioctl(u, FIOCLEX, (char *)0); srcunit(u, 0, hflg); } @@ -849,7 +839,7 @@ mailchk() v = adrof("mail"); if (v == 0) return; - time(&t); + (void) time(&t); vp = v->vec; cnt = blklen(vp); intvl = (cnt && number(*vp)) ? (--cnt, getn(*vp++)) : MAILINTVL; @@ -887,7 +877,7 @@ gethdir(home) if (pp == 0) return (1); - strcpy(home, pp->pw_dir); + (void) strcpy(home, pp->pw_dir); return (0); } @@ -898,12 +888,11 @@ gethdir(home) initdesc() { - didcch = 0; /* Havent closed for child */ didfds = 0; /* 0, 1, 2 aren't set up */ - SHIN = dcopy(0, FSHIN); - SHOUT = dcopy(1, FSHOUT); - SHDIAG = dcopy(2, FSHDIAG); - OLDSTD = dcopy(SHIN, FOLDSTD); + (void) ioctl(SHIN = dcopy(0, FSHIN), FIOCLEX, (char *)0); + (void) ioctl(SHOUT = dcopy(1, FSHOUT), FIOCLEX, (char *)0); + (void) ioctl(SHDIAG = dcopy(2, FSHDIAG), FIOCLEX, (char *)0); + (void) ioctl(OLDSTD = dcopy(SHIN, FOLDSTD), FIOCLEX, (char *)0); closem(); } @@ -916,11 +905,7 @@ exit(i) { untty(); -#ifdef PROF - monitor(0); -#else _exit(i); -#endif } printprompt() diff --git a/usr/src/bin/csh/csh.h b/usr/src/bin/csh/csh.h index b2df042fe8..56d4208d47 100644 --- a/usr/src/bin/csh/csh.h +++ b/usr/src/bin/csh/csh.h @@ -1,8 +1,13 @@ -/* csh.h 4.11 84/08/31 */ +/* @(#)csh.h 4.12 (Berkeley) %G% */ -#include "sh.local.h" #include #include +#include +#include +#include +#include +#include +#include "sh.local.h" /* * C shell @@ -13,16 +18,9 @@ * Jim Kulp, IIASA, Laxenburg Austria * April, 1980 */ -#include -#include #define isdir(d) ((d.st_mode & S_IFMT) == S_IFDIR) -#include -#include -#include -#include - typedef char bool; #define eq(a, b) (strcmp(a, b) == 0) @@ -31,7 +29,6 @@ typedef char bool; * Global flags */ bool chkstop; /* Warned of stopped jobs... allow exit */ -bool didcch; /* Have closed unused fd's for child */ bool didfds; /* Have setup i/o fd's for child */ bool doneinp; /* EOF indicator after reset from readc */ bool exiterr; /* Exit if error or non-zero exit status */ @@ -46,6 +43,10 @@ bool noexec; /* Don't execute, just syntax check */ bool pjobs; /* want to print jobs if interrupted */ bool setintr; /* Set interrupts on/off -> Wait intr... */ bool timflg; /* Time the next waited for command */ +bool havhash; /* path hashing is available */ +#ifdef FILEC +bool filec; /* doing filename expansion */ +#endif /* * Global i/o info @@ -71,7 +72,6 @@ int tpgrp; /* Terminal process group */ /* If tpgrp is -1, leave tty alone! */ int opgrp; /* Initial pgrp and tty pgrp */ int oldisc; /* Initial line discipline or -1 */ -struct tms shtimes; /* shell and child times for process timing */ /* * These are declared here because they want to be @@ -83,13 +83,13 @@ struct biltins { int (*bfunct)(); short minargs, maxargs; } bfunc[]; - -#define INF 1000 +extern int nbfunc; struct srch { char *s_name; short s_value; } srchn[]; +extern int nsrchn; /* * To be able to redirect i/o for builtins easily, the shell moves the i/o @@ -114,8 +114,8 @@ short OLDSTD; /* Old standard input (def for cmds) */ jmp_buf reslab; -#define setexit() setjmp(reslab) -#define reset() longjmp(reslab) +#define setexit() ((void) setjmp(reslab)) +#define reset() longjmp(reslab, 0) /* Should use structure assignment here */ #define getexit(a) copy((char *)(a), (char *)reslab, sizeof reslab) #define resexit(a) copy((char *)reslab, ((char *)(a)), sizeof reslab) @@ -165,7 +165,6 @@ off_t btell(); off_t lineloc; #ifdef TELL -off_t tell(); bool cantell; /* Is current source tellable ? */ #endif @@ -293,13 +292,21 @@ struct whyle { /* * Variable structure * - * Lists of aliases and variables are sorted alphabetically by name + * Aliases and variables are stored in AVL balanced binary trees. */ struct varent { char **vec; /* Array of words which is the value */ - char *name; /* Name of variable/alias */ - struct varent *link; + char *v_name; /* Name of variable/alias */ + struct varent *v_link[3]; /* The links, see below */ + int v_bal; /* Balance factor */ } shvhed, aliases; +#define v_left v_link[0] +#define v_right v_link[1] +#define v_parent v_link[2] + +struct varent *adrof1(); +#define adrof(v) adrof1(v, &shvhed) +#define value(v) value1(v, &shvhed) /* * The following are for interfacing redo substitution in @@ -361,13 +368,25 @@ int lastev; /* Last event reference (default) */ char HIST; /* history invocation character */ char HISTSUB; /* auto-substitute character */ +/* + * In lines for frequently called functions + */ +#define XFREE(cp) { \ + extern char end[]; \ + char stack; \ + if ((cp) >= end && (cp) < &stack) \ + free(cp); \ +} +char *alloctmp; +#define xalloc(i) ((alloctmp = malloc(i)) ? alloctmp : (char *)nomem(i)) + char *Dfix1(); -struct varent *adrof(), *adrof1(); char **blkcat(); char **blkcpy(); char **blkend(); char **blkspl(); char *calloc(); +char *malloc(); char *cname(); char **copyblk(); char **dobackp(); @@ -388,14 +407,17 @@ struct passwd *getpwnam(); struct wordent *gethent(); struct wordent *getsub(); char *getwd(); +char **glob(); char *globone(); +char *index(); struct biltins *isbfunc(); -char **glob(); +off_t lseek(); char *operate(); int phup(); int pintr(); int pchild(); char *putn(); +char *rindex(); char **saveblk(); char *savestr(); char *strcat(); @@ -412,9 +434,7 @@ struct command *syn1a(); struct command *syn1b(); struct command *syn2(); struct command *syn3(); -int tglob(); -int trim(); -char *value(), *value1(); +char *value1(); char *xhome(); char *xname(); char *xset(); @@ -425,7 +445,7 @@ char *xset(); * setname is a macro to save space (see sh.err.c) */ char *bname; -#define setname(a) bname = (a); +#define setname(a) (bname = (a)) #ifdef VFORK char *Vsav; diff --git a/usr/src/bin/csh/dir.c b/usr/src/bin/csh/dir.c index 069f45f733..c1f2a06817 100644 --- a/usr/src/bin/csh/dir.c +++ b/usr/src/bin/csh/dir.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)dir.c 4.2 %G%"; +#ifndef lint +static char *sccsid = "@(#)dir.c 4.3 (Berkeley) %G%"; +#endif #include "sh.h" #include "sh.dir.h" @@ -28,7 +30,7 @@ dinit(hp) else { cp = getwd(path); if (cp == NULL) { - write(2, path, strlen(path)); + (void) write(2, path, strlen(path)); exit(1); } } @@ -138,9 +140,9 @@ dfollow(cp) for (cdp = c->vec; *cdp; cdp++) { char buf[BUFSIZ]; - strcpy(buf, *cdp); - strcat(buf, "/"); - strcat(buf, cp); + (void) strcpy(buf, *cdp); + (void) strcat(buf, "/"); + (void) strcat(buf, cp); if (chdir(buf) >= 0) { printd = 1; xfree(cp); @@ -165,10 +167,10 @@ dfollow(cp) gotcha: if (*cp != '/') { - char *dp = calloc(strlen(cp) + strlen(dcwd->di_name) + 2, 1); - strcpy(dp, dcwd->di_name); - strcat(dp, "/"); - strcat(dp, cp); + char *dp = calloc((unsigned) (strlen(cp) + strlen(dcwd->di_name) + 2), 1); + (void) strcpy(dp, dcwd->di_name); + (void) strcat(dp, "/"); + (void) strcat(dp, cp); xfree(cp); cp = dp; } @@ -309,7 +311,7 @@ dcanon(cp) while(*++p == '/') /* flush extra slashes */ ; if (p != ++sp) - strcpy(sp, p); + (void) strcpy(sp, p); p = sp; /* save start of component */ slash = 0; while(*++p) /* find next slash or end of path */ @@ -325,7 +327,7 @@ dcanon(cp) *sp = '\0'; else if (eq(".", sp)) { if (slash) { - strcpy(sp, ++p); + (void) strcpy(sp, ++p); p = --sp; } else if (--sp != cp) *sp = '\0'; @@ -334,7 +336,7 @@ dcanon(cp) while (*--sp != '/') ; if (slash) { - strcpy(++sp, ++p); + (void) strcpy(++sp, ++p); p = --sp; } else if (cp == sp) *++sp = '\0'; diff --git a/usr/src/bin/csh/dir.h b/usr/src/bin/csh/dir.h index 444e1e5326..cfe65ee560 100644 --- a/usr/src/bin/csh/dir.h +++ b/usr/src/bin/csh/dir.h @@ -1,4 +1,4 @@ -/* dir.h 4.1 %G% */ +/* @(#)dir.h 4.2 (Berkeley) %G% */ /* * Structure for entries in directory stack. diff --git a/usr/src/bin/csh/dol.c b/usr/src/bin/csh/dol.c index e73a84bb47..313b12272e 100644 --- a/usr/src/bin/csh/dol.c +++ b/usr/src/bin/csh/dol.c @@ -1,6 +1,9 @@ -static char *sccsid = "@(#)dol.c 4.4 %G%"; +#ifndef lint +static char *sccsid = "@(#)dol.c 4.5 (Berkeley) %G%"; +#endif #include "sh.h" +#include "sh.char.h" /* * C shell @@ -12,7 +15,7 @@ static char *sccsid = "@(#)dol.c 4.4 %G%"; * input words. Here we expand variables and turn quoting via ' and " into * QUOTE bits on characters (which prevent further interpretation). * If the `:q' modifier was applied during history expansion, then - * some QUOTEing may have occurred already, so we dont "scan(,&trim)" here. + * some QUOTEing may have occurred already, so we dont "trim()" here. */ int Dpeekc, Dpeekrd; /* Peeks for DgetC and Dreadc */ @@ -22,7 +25,7 @@ char *Dcp, **Dvp; /* Input vector for Dreadc */ #define unDgetC(c) Dpeekc = c -char *QUOTES = "\\'`\""; +#define QUOTES (_Q|_Q1|_ESC) /* \ ' " ` */ /* * The following variables give the information about the current @@ -36,8 +39,6 @@ int dolcnt; /* Count of further words */ char dolmod; /* : modifier character */ int dolmcnt; /* :gx -> 10000, else 1 */ -int Dtest(); /* Test for \ " ` or ' */ - /* * Fix up the $ expansions and quotations in the * argument list to command t. @@ -45,14 +46,21 @@ int Dtest(); /* Test for \ " ` or ' */ Dfix(t) register struct command *t; { + register char **pp; + register char *p; if (noexec) return; - gflag = 0, rscan(t->t_dcom, Dtest); - if (gflag == 0) - return; - Dfix2(t->t_dcom); - blkfree(t->t_dcom), t->t_dcom = gargv, gargv = 0; + /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */ + for (pp = t->t_dcom; p = *pp++;) + while (*p) + if (cmap(*p++, _DOL|QUOTES)) { /* $, \, ', ", ` */ + Dfix2(t->t_dcom); /* found one */ + blkfree(t->t_dcom); + t->t_dcom = gargv; + gargv = 0; + return; + } } /* @@ -201,9 +209,9 @@ pack: } if (c == DEOF) goto deof; - if (any(c, " '`\"\t\n")) { + if (cmap(c, _SP|_NL|_Q|_Q1)) { /* sp \t\n'"` */ unDgetC(c); - if (any(c, QUOTES)) + if (cmap(c, QUOTES)) goto loop; *wp++ = 0; goto ret; @@ -240,7 +248,7 @@ top: goto top; } quotspec: - if (any(c, QUOTES)) + if (cmap(c, QUOTES)) return (c | QUOTE); return (c); } @@ -328,7 +336,7 @@ Dgetdol() goto syntax; case '*': - strcpy(name, "argv"); + (void) strcpy(name, "argv"); vp = adrof("argv"); subscr = -1; /* Prevent eating [...] */ break; @@ -419,7 +427,7 @@ syntax: i = i * 10 + *np++ - '0'; if ((i < 0 || i > upb) && !any(*np, "-*")) { oob: - setname(vp->name); + setname(vp->v_name); error("Subscript out of range"); } lwb = i; @@ -535,20 +543,11 @@ Dredc() return (' '); } -Dtest(c) - register int c; -{ - - /* Note that c isn't trimmed thus !...:q's aren't lost */ - if (any(c, "$\\'`\"")) - gflag = 1; -} - Dtestq(c) register int c; { - if (any(c, "\\'`\"")) + if (cmap(c, QUOTES)) gflag = 1; } @@ -570,17 +569,17 @@ heredoc(term) if (creat(shtemp, 0600) < 0) Perror(shtemp); - close(0); + (void) close(0); if (open(shtemp, 2) < 0) { int oerrno = errno; - unlink(shtemp); + (void) unlink(shtemp); errno = oerrno; Perror(shtemp); } - unlink(shtemp); /* 0 0 inode! */ + (void) unlink(shtemp); /* 0 0 inode! */ Dv[0] = term; Dv[1] = NOSTR; gflag = 0; - scan(Dv, trim); rscan(Dv, Dtestq); quoted = gflag; + trim(Dv); rscan(Dv, Dtestq); quoted = gflag; ocnt = BUFSIZ; obp = obuf; for (;;) { /* @@ -609,8 +608,8 @@ heredoc(term) * Compare to terminator -- before expansion */ if (eq(lbuf, term)) { - write(0, obuf, BUFSIZ - ocnt); - lseek(0, 0l, 0); + (void) write(0, obuf, BUFSIZ - ocnt); + (void) lseek(0, (off_t)0, 0); return; } @@ -622,7 +621,7 @@ heredoc(term) for (lbp = lbuf; c = *lbp++;) { *obp++ = c; if (--ocnt == 0) { - write(0, obuf, BUFSIZ); + (void) write(0, obuf, BUFSIZ); obp = obuf; ocnt = BUFSIZ; } } @@ -684,13 +683,13 @@ heredoc(term) for (mbp = *vp; *mbp; mbp++) { *obp++ = *mbp & TRIM; if (--ocnt == 0) { - write(0, obuf, BUFSIZ); + (void) write(0, obuf, BUFSIZ); obp = obuf; ocnt = BUFSIZ; } } *obp++ = '\n'; if (--ocnt == 0) { - write(0, obuf, BUFSIZ); + (void) write(0, obuf, BUFSIZ); obp = obuf; ocnt = BUFSIZ; } } diff --git a/usr/src/bin/csh/err.c b/usr/src/bin/csh/err.c index b52d85af75..23760c5373 100644 --- a/usr/src/bin/csh/err.c +++ b/usr/src/bin/csh/err.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)err.c 4.1 %G%"; +#ifndef lint +static char *sccsid = "@(#)err.c 4.2 (Berkeley) %G%"; +#endif #include "sh.h" #include @@ -20,6 +22,7 @@ char *onev[2] = { one, NOSTR }; * be closed in the routine process in sh.c which is the only * place error unwinds are ever caught. */ +/*VARARGS1*/ error(s, arg) char *s; { @@ -70,7 +73,7 @@ error(s, arg) setq("status", onev, &shvhed); if (tpgrp > 0) - ioctl(FSHTTY, TIOCSPGRP, &tpgrp); + (void) ioctl(FSHTTY, TIOCSPGRP, (char *)&tpgrp); reset(); /* Unwind */ } @@ -89,7 +92,7 @@ Perror(s) if (!didfds) { register int oerrno = errno; - dcopy(SHDIAG, 2); + (void) dcopy(SHDIAG, 2); errno = oerrno; } perror(s); diff --git a/usr/src/bin/csh/exec.c b/usr/src/bin/csh/exec.c index c15bb0770e..b72c256ba6 100644 --- a/usr/src/bin/csh/exec.c +++ b/usr/src/bin/csh/exec.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)exec.c 4.9 %G%"; +#ifndef lint +static char *sccsid = "@(#)exec.c 4.10 (Berkeley) %G%"; +#endif #include "sh.h" #include @@ -25,16 +27,25 @@ char *exerr; /* Execution error message */ char *expath; /* Path for exerr */ /* - * Xhash is an array of HSHSIZ chars, which are used to hash execs. - * If it is allocated, then to tell whether ``name'' is (possibly) - * present in the i'th component of the variable path, you look at - * the i'th bit of xhash[hash("name")]. This is setup automatically + * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used + * to hash execs. If it is allocated (havhash true), then to tell + * whether ``name'' is (possibly) present in the i'th component + * of the variable path, you look at the bit in xhash indexed by + * hash(hashname("name"), i). This is setup automatically * after .login is executed, and recomputed whenever ``path'' is * changed. + * The two part hash function is designed to let texec() call the + * more expensive hashname() only once and the simple hash() several + * times (once for each path component checked). + * Byte size is assumed to be 8. */ -int havhash; -#define HSHSIZ 511 -char xhash[HSHSIZ]; +#define HSHSIZ 8192 /* 1k bytes */ +#define HSHMASK (HSHSIZ - 1) +#define HSHMUL 243 +char xhash[HSHSIZ / 8]; +#define hash(a, b) ((a) * HSHMUL + (b) & HSHMASK) +#define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7)) /* bit test */ +#define bis(h, b) ((h)[(b) >> 3] |= 1 << ((b) & 7)) /* bit set */ #ifdef VFORK int hits, misses; #endif @@ -49,7 +60,7 @@ doexec(t) register char *dp, **pv, **av; register struct varent *v; bool slash = any('/', t->t_dcom[0]); - int hashval, i; + int hashval, hashval1, i; char *blk[2]; /* @@ -72,7 +83,7 @@ doexec(t) * Otherwise trim off the quote bits. */ gflag = 0; av = &t->t_dcom[1]; - rscan(av, tglob); + tglob(av); if (gflag) { av = glob(av); if (av == 0) @@ -84,19 +95,21 @@ doexec(t) #ifdef VFORK Vav = av; #endif - scan(av, trim); + trim(av); xechoit(av); /* Echo command if -x */ - closech(); /* Close random fd's */ + /* + * Since all internal file descriptors are set to close on exec, + * we don't need to close them explicitly here. Just reorient + * ourselves for error messages. + */ + SHIN = 0; SHOUT = 1; SHDIAG = 2; OLDSTD = 0; /* * We must do this AFTER any possible forking (like `foo` * in glob) so that this shell can still do subprocesses. */ -#ifdef notdef - sigsys(SIGCHLD, SIG_IGN); /* sigsys for vforks sake */ -#endif - sigsetmask(0); + (void) sigsetmask(0); /* * If no path, no words in path, or a / in the filename @@ -111,14 +124,17 @@ doexec(t) Vsav = sav; #endif if (havhash) - hashval = xhash[hash(*av)]; + hashval = hashname(*av); i = 0; #ifdef VFORK hits++; #endif do { - if (!slash && pv[0][0] == '/' && havhash && (hashval & (1 << (i % 8))) == 0) - goto cont; + if (!slash && pv[0][0] == '/' && havhash) { + hashval1 = hash(hashval, i); + if (!bit(xhash, hashval1)) + goto cont; + } if (pv[0][0] == 0 || eq(pv[0], ".")) /* don't make ./xxx */ texec(*av, av); else { @@ -147,7 +163,7 @@ cont: Vav = 0; #endif xfree(sav); - xfree(av); + xfree((char *)av); pexerr(); } @@ -162,9 +178,6 @@ pexerr() bferr("Command not found"); } -/* Last resort shell */ -char *lastsh[] = { SHELLPATH, 0 }; - /* * Execute command f, arg list t. * Record error message if not found. @@ -177,6 +190,7 @@ texec(f, t) register struct varent *v; register char **vp; extern char *sys_errlist[]; + char *lastsh[2]; execv(f, t); switch (errno) { @@ -200,7 +214,7 @@ texec(f, t) #ifdef OTHERSH if (ff != -1 && read(ff, &ch, 1) == 1 && ch != '#') vp[0] = OTHERSH; - close(ff); + (void) close(ff); #endif } else vp = v->vec; @@ -225,15 +239,16 @@ texec(f, t) } } +/*ARGSUSED*/ execash(t, kp) + char **t; register struct command *kp; { - didcch++; rechist(); - signal(SIGINT, parintr); - signal(SIGQUIT, parintr); - signal(SIGTERM, parterm); /* if doexec loses, screw */ + (void) signal(SIGINT, parintr); + (void) signal(SIGQUIT, parintr); + (void) signal(SIGTERM, parterm); /* if doexec loses, screw */ lshift(kp->t_dcom, 1); exiterr++; doexec(kp); @@ -247,12 +262,14 @@ xechoit(t) if (adrof("echo")) { flush(); haderr = 1; - blkpr(t), printf("\n"); + blkpr(t), putchar('\n'); haderr = 0; } } -dohash() +/*VARARGS0*//*ARGSUSED*/ +dohash(v) + char **v; { struct stat stb; DIR *dirp; @@ -261,13 +278,14 @@ dohash() int i = 0; struct varent *v = adrof("path"); char **pv; + int hashval; havhash = 1; - for (cnt = 0; cnt < HSHSIZ; cnt++) + for (cnt = 0; cnt < sizeof xhash; cnt++) xhash[cnt] = 0; if (v == 0) return; - for (pv = v->vec; *pv; pv++, i = (i + 1) % 8) { + for (pv = v->vec; *pv; pv++, i++) { if (pv[0][0] != '/') continue; dirp = opendir(*pv); @@ -280,7 +298,12 @@ dohash() while ((dp = readdir(dirp)) != NULL) { if (dp->d_ino == 0) continue; - xhash[hash(dp->d_name)] |= (1 << i); + if (dp->d_name[0] == '.' && + (dp->d_name[1] == '\0' || + dp->d_name[1] == '.' && dp->d_name[2] == '\0')) + continue; + hashval = hash(hashname(dp->d_name), i); + bis(xhash, hashval); } closedir(dirp); } @@ -297,20 +320,20 @@ hashstat() { if (hits+misses) - printf("%d hits, %d misses, %2d%%\n", hits, misses, 100 * hits / (hits + misses)); + printf("%d hits, %d misses, %d%%\n", + hits, misses, 100 * hits / (hits + misses)); } #endif -hash(cp) +/* + * Hash a command name. + */ +hashname(cp) register char *cp; { - register long hash = 0; - int retval; + register long h = 0; while (*cp) - hash += hash + *cp++; - if (hash < 0) - hash = -hash; - retval = hash % HSHSIZ; - return (retval); + h = hash(h, *cp++); + return ((int) h); } diff --git a/usr/src/bin/csh/exp.c b/usr/src/bin/csh/exp.c index 3bcf6c2977..2bcda7573b 100644 --- a/usr/src/bin/csh/exp.c +++ b/usr/src/bin/csh/exp.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)exp.c 4.1 %G%"; +#ifndef lint +static char *sccsid = "@(#)exp.c 4.2 (Berkeley) %G%"; +#endif #include "sh.h" @@ -512,31 +514,42 @@ isa(cp, what) if (cp == 0) return ((what & RESTOP) != 0); if (cp[1] == 0) { - if ((what & ADDOP) && any(cp[0], "+-")) + if (what & ADDOP && (*cp == '+' || *cp == '-')) return (1); - if ((what & MULOP) && any(cp[0], "*/%")) + if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%')) return (1); - if ((what & RESTOP) && any(cp[0], "()!~^")) + if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' || + *cp == '~' || *cp == '^' || *cp == '"')) return (1); + } else if (cp[2] == 0) { + if (what & RESTOP) { + if (cp[0] == '|' && cp[1] == '&') + return (1); + if (cp[0] == '<' && cp[1] == '<') + return (1); + if (cp[0] == '>' && cp[1] == '>') + return (1); + } + if (what & EQOP) { + if (cp[0] == '=') { + if (cp[1] == '=') + return (EQEQ); + if (cp[1] == '~') + return (EQMATCH); + } else if (cp[0] == '!') { + if (cp[1] == '=') + return (NOTEQ); + if (cp[1] == '~') + return (NOTEQMATCH); + } + } } - if ((what & RESTOP) && (any(cp[0], "|&") || eq(cp, "<<") || eq(cp, ">>"))) - return (1); - if (what & EQOP) { - if (eq(cp, "==")) - return (EQEQ); - if (eq(cp, "!=")) - return (NOTEQ); - if (eq(cp, "=~")) - return (EQMATCH); - if (eq(cp, "!~")) - return (NOTEQMATCH); + if (what & RELOP) { + if (*cp == '<') + return (LSS); + if (*cp == '>') + return (GTR); } - if (!(what & RELOP)) - return (0); - if (*cp == '<') - return (LSS); - if (*cp == '>') - return (GTR); return (0); } diff --git a/usr/src/bin/csh/file.c b/usr/src/bin/csh/file.c index 393d111be8..3fbd982b35 100644 --- a/usr/src/bin/csh/file.c +++ b/usr/src/bin/csh/file.c @@ -1,7 +1,8 @@ #ifndef lint -static char *sccsid = "@(#)file.c 1.5 (Berkeley from Hp Labs) %G%"; +static char *sccsid = "@(#)file.c 1.6 (Berkeley from Hp Labs) %G%"; #endif +#ifdef FILEC /* * Tenex style file name recognition, .. and more. * History: @@ -9,29 +10,28 @@ static char *sccsid = "@(#)file.c 1.5 (Berkeley from Hp Labs) %G%"; * Finally got around to adding to the Cshell., Ken Greer, Dec. 1981. */ -#include -#include +#include "sh.h" #include #include -#include #include -extern short SHIN, SHOUT; - #define TRUE 1 #define FALSE 0 #define ON 1 #define OFF 0 -#define FILSIZ 512 /* Max reasonable file name length */ #define ESC '\033' typedef enum {LIST, RECOGNIZE} COMMAND; -#define equal(a, b) (strcmp(a, b) == 0) - static struct tchars tchars; /* INT, QUIT, XON, XOFF, EOF, BRK */ +/* + * Put this here so the binary can be patched with adb to enable file + * completion by default. + */ +bool filec = 0; + static setup_tty(on) int on; @@ -41,9 +41,9 @@ setup_tty(on) omask = sigblock(sigmask(SIGINT)); if (on) { - ioctl(SHIN, TIOCGETC, &tchars); + (void) ioctl(SHIN, TIOCGETC, (char *)&tchars); tchars.t_brkc = ESC; - ioctl(SHIN, TIOCSETC, &tchars); + (void) ioctl(SHIN, TIOCSETC, (char *)&tchars); /* * This is a useful feature in it's own right... * The shell makes sure that the tty is not in some weird state @@ -51,16 +51,16 @@ setup_tty(on) * tenex routine will not work correctly in CBREAK or RAW mode * so this code below is, therefore, mandatory. */ - ioctl(SHIN, TIOCGETP, &sgtty); + (void) ioctl(SHIN, TIOCGETP, (char *)&sgtty); if (sgtty.sg_flags & (RAW|CBREAK)) { sgtty.sg_flags &= ~(RAW|CBREAK); - ioctl(SHIN, TIOCSETP, &sgtty); + (void) ioctl(SHIN, TIOCSETP, (char *)&sgtty); } } else { tchars.t_brkc = -1; - ioctl(SHIN, TIOCSETC, &tchars); + (void) ioctl(SHIN, TIOCSETC, (char *)&tchars); } - sigsetmask (omask); + (void) sigsetmask (omask); } /* @@ -73,13 +73,13 @@ back_to_col_1() int omask; omask = sigblock(sigmask(SIGINT)); - ioctl(SHIN, TIOCGETP, &tty); + (void) ioctl(SHIN, TIOCGETP, (char *)&tty); tty_normal = tty; tty.sg_flags &= ~CRMOD; - ioctl(SHIN, TIOCSETN, &tty); + (void) ioctl(SHIN, TIOCSETN, (char *)&tty); (void) write(SHOUT, "\r", 1); - ioctl(SHIN, TIOCSETN, &tty_normal); - sigsetmask(omask); + (void) ioctl(SHIN, TIOCSETN, (char *)&tty_normal); + (void) sigsetmask(omask); } /* @@ -94,15 +94,15 @@ pushback(string) int omask; omask = sigblock(sigmask(SIGINT)); - ioctl(SHOUT, TIOCGETP, &tty); + (void) ioctl(SHOUT, TIOCGETP, (char *)&tty); tty_normal = tty; tty.sg_flags &= ~ECHO; - ioctl(SHOUT, TIOCSETN, &tty); + (void) ioctl(SHOUT, TIOCSETN, (char *)&tty); for (p = string; *p; p++) - ioctl(SHOUT, TIOCSTI, p); - ioctl(SHOUT, TIOCSETN, &tty_normal); - sigsetmask(omask); + (void) ioctl(SHOUT, TIOCSTI, p); + (void) ioctl(SHOUT, TIOCSETN, (char *)&tty_normal); + (void) sigsetmask(omask); } /* @@ -162,10 +162,9 @@ filetype(dir, file) { char path[512]; struct stat statb; - extern char *strcpy (); if (dir) { - catn(strcpy (path, dir), file, sizeof path); + catn(strcpy(path, dir), file, sizeof path); if (stat(path, &statb) >= 0) { if (statb.st_mode & S_IFDIR) return ('/'); @@ -222,8 +221,6 @@ tilde(new, old) register char *o, *p; register struct passwd *pw; static char person[40]; - extern char *strcpy(); - extern struct passwd *getpwuid(), *getpwnam(); if (old[0] != '~') return (strcpy(new, old)); @@ -237,7 +234,7 @@ tilde(new, old) pw = getpwnam(person); if (pw == NULL) return (NULL); - strcpy(new, pw->pw_dir); + (void) strcpy(new, pw->pw_dir); (void) strcat(new, o); return (new); } @@ -250,7 +247,7 @@ retype() { int pending_input = LPENDIN; - ioctl(SHOUT, TIOCLBIS, &pending_input); + (void) ioctl(SHOUT, TIOCLBIS, (char *)&pending_input); } static @@ -296,9 +293,8 @@ extract_dir_and_name(path, dir, name) char *path, *dir, *name; { register char *p; - extern char *rindex(); - p = rindex (path, '/'); + p = rindex(path, '/'); if (p == NULL) { copyn(name, path, MAXNAMLEN); dir[0] = '\0'; @@ -314,7 +310,6 @@ getentry(dir_fd, looking_for_lognames) { register struct passwd *pw; register struct direct *dirp; - extern struct passwd *getpwent(); if (looking_for_lognames) { if ((pw = getpwent ()) == NULL) @@ -334,7 +329,7 @@ free_items(items) for (i = 0; items[i]; i++) free(items[i]); - free(items); + free((char *)items); } #define FREE_ITEMS(items) { \ @@ -343,7 +338,7 @@ free_items(items) omask = sigblock(sigmask(SIGINT));\ free_items(items);\ items = NULL;\ - sigsetmask(omask);\ + (void) sigsetmask(omask);\ } /* @@ -357,7 +352,7 @@ search(word, command, max_word_length) static char **items = NULL; register DIR *dir_fd; register numitems, name_length, looking_for_lognames; - char tilded_dir[FILSIZ + 1], dir[FILSIZ + 1]; + char tilded_dir[MAXPATHLEN + 1], dir[MAXPATHLEN + 1]; char name[MAXNAMLEN + 1], extended_name[MAXNAMLEN+1]; char *entry; #define MAXITEMS 1024 @@ -367,7 +362,7 @@ search(word, command, max_word_length) looking_for_lognames = (*word == '~') && (index(word, '/') == NULL); if (looking_for_lognames) { - setpwent(); + (void) setpwent(); copyn(name, &word[1], MAXNAMLEN); /* name sans ~ */ } else { extract_dir_and_name(word, dir, name); @@ -386,8 +381,6 @@ search(word, command, max_word_length) !looking_for_lognames) continue; if (command == LIST) { - extern char *malloc (); - if (numitems >= MAXITEMS) { printf ("\nYikes!! Too many %s!!\n", looking_for_lognames ? @@ -400,7 +393,7 @@ search(word, command, max_word_length) if (items == NULL) break; } - items[numitems] = malloc(strlen(entry) + 1); + items[numitems] = malloc((unsigned)strlen(entry) + 1); if (items[numitems] == NULL) { printf("out of mem\n"); break; @@ -413,7 +406,7 @@ search(word, command, max_word_length) break; } if (looking_for_lognames) - endpwent(); + (void) endpwent(); else closedir(dir_fd); if (command == RECOGNIZE && numitems > 0) { @@ -427,9 +420,7 @@ search(word, command, max_word_length) return (numitems); } if (command == LIST) { - register int i; - - qsort(items, numitems, sizeof(items[1]), fcompare); + qsort((char *)items, numitems, sizeof(items[1]), fcompare); print_by_column(looking_for_lognames ? NULL : tilded_dir, items, numitems); if (items != NULL) @@ -549,3 +540,4 @@ tenex(inputline, inputline_size) setup_tty (OFF); return (num_read); } +#endif FILEC diff --git a/usr/src/bin/csh/func.c b/usr/src/bin/csh/func.c index ff3300453c..2a647e649e 100644 --- a/usr/src/bin/csh/func.c +++ b/usr/src/bin/csh/func.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)func.c 4.12 84/08/31"; +#ifndef lint +static char *sccsid = "@(#)func.c 4.13 (Berkeley) %G%"; +#endif #include "sh.h" #include @@ -9,11 +11,10 @@ static char *sccsid = "@(#)func.c 4.12 84/08/31"; struct biltins * isbfunc(t) - register struct command *t; + struct command *t; { register char *cp = t->t_dcom[0]; - register char *dp; - register struct biltins *bp; + register struct biltins *bp, *bp1, *bp2; int dolabel(), dofg1(), dobg1(); static struct biltins label = { "", dolabel, 0, 0 }; static struct biltins foregnd = { "%job", dofg1, 0, 0 }; @@ -28,15 +29,26 @@ isbfunc(t) t->t_dflg &= ~FAND; backgnd.bname = cp; return (&backgnd); - } + } foregnd.bname = cp; return (&foregnd); } - for (bp = bfunc; dp = bp->bname; bp++) { - if (dp[0] == cp[0] && eq(dp, cp)) - return (bp); - if (dp[0] > cp[0]) - break; + /* + * Binary search + * Bp1 is the beginning of the current search range. + * Bp2 is one past the end. + */ + for (bp1 = bfunc, bp2 = bfunc + nbfunc; bp1 < bp2;) { + register i; + + bp = bp1 + (bp2 - bp1 >> 1); + if ((i = *cp - *bp->bname) == 0 && + (i = strcmp(cp, bp->bname)) == 0) + return bp; + if (i < 0) + bp2 = bp; + else + bp1 = bp + 1; } return (0); } @@ -75,16 +87,16 @@ doonintr(v) cp = gointr, gointr = 0, xfree(cp); if (vv == 0) { if (setintr) - sigblock(sigmask(SIGINT)); + (void) sigblock(sigmask(SIGINT)); else - signal(SIGINT, SIG_DFL); + (void) signal(SIGINT, SIG_DFL); gointr = 0; } else if (eq((vv = strip(vv)), "-")) { - signal(SIGINT, SIG_IGN); + (void) signal(SIGINT, SIG_IGN); gointr = "-"; } else { gointr = savestr(vv); - signal(SIGINT, pintr); + (void) signal(SIGINT, pintr); } } @@ -94,7 +106,7 @@ donohup() if (intty) bferr("Can't from terminal"); if (setintr == 0) { - signal(SIGHUP, SIG_IGN); + (void) signal(SIGHUP, SIG_IGN); #ifdef CC submit(getpid()); #endif @@ -156,7 +168,7 @@ dologin(v) islogin(); rechist(); - signal(SIGTERM, parterm); + (void) signal(SIGTERM, parterm); execl("/bin/login", "login", v[1], 0); untty(); exit(1); @@ -169,7 +181,7 @@ donewgrp(v) if (chkstop == 0 && setintr) panystop(0); - signal(SIGTERM, parterm); + (void) signal(SIGTERM, parterm); execl("/bin/newgrp", "newgrp", v[1], 0); execl("/usr/bin/newgrp", "newgrp", v[1], 0); untty(); @@ -317,7 +329,7 @@ doexit(v) } btoeof(); if (intty) - close(SHIN); + (void) close(SHIN); } doforeach(v) @@ -336,7 +348,7 @@ doforeach(v) if (v[0][0] != '(' || v[blklen(v) - 1][0] != ')') bferr("Words not ()'ed"); v++; - gflag = 0, rscan(v, tglob); + gflag = 0, tglob(v); v = glob(v); if (v == 0) bferr("No match"); @@ -400,10 +412,10 @@ preread() whyles->w_end = -1; if (setintr) - sigsetmask(sigblock(0) & ~sigmask(SIGINT)); + (void) sigsetmask(sigblock(0) & ~sigmask(SIGINT)); search(ZBREAK, 0); if (setintr) - sigblock(sigmask(SIGINT)); + (void) sigblock(sigmask(SIGINT)); whyles->w_end = btell(); } @@ -457,13 +469,13 @@ dorepeat(v, kp) lshift(v, 2); while (i > 0) { if (setintr) - sigsetmask(omask); + (void) sigsetmask(omask); reexecute(kp); --i; } donefds(); if (setintr) - sigsetmask(omask); + (void) sigsetmask(omask); } doswbrk() @@ -475,11 +487,24 @@ doswbrk() srchx(cp) register char *cp; { - register struct srch *sp; + register struct srch *sp, *sp1, *sp2; + register i; - for (sp = srchn; sp->s_name; sp++) - if (eq(cp, sp->s_name)) - return (sp->s_value); + /* + * Binary search + * Sp1 is the beginning of the current search range. + * Sp2 is one past the end. + */ + for (sp1 = srchn, sp2 = srchn + nsrchn; sp1 < sp2;) { + sp = sp1 + (sp2 - sp1 >> 1); + if ((i = *cp - *sp->s_name) == 0 && + (i = strcmp(cp, sp->s_name)) == 0) + return sp->s_value; + if (i < 0) + sp2 = sp; + else + sp1 = sp + 1; + } return (-1); } @@ -498,11 +523,12 @@ search(type, level, goal) Stype = type; Sgoal = goal; if (type == ZGOTO) - bseek(0l); + bseek((off_t)0); do { if (intty && fseekp == feobp) printf("? "), flush(); - aword[0] = 0, getword(aword); + aword[0] = 0; + (void) getword(aword); switch (srchx(aword)) { case ZELSE: @@ -561,7 +587,7 @@ search(type, level, goal) case ZCASE: if (type != ZSWITCH || level != 0) break; - getword(aword); + (void) getword(aword); if (lastchr(aword) == ':') aword[strlen(aword) - 1] = 0; cp = strip(Dfix1(aword)); @@ -575,7 +601,7 @@ search(type, level, goal) level = -1; break; } - getword(NOSTR); + (void) getword(NOSTR); } while (level >= 0); } @@ -607,7 +633,7 @@ getword(wp) c = readc(1); if (c == '\\' && (c = readc(1)) == '\n') c = ' '; - if (any(c, "'\"")) + if (c == '\'' || c == '"') if (d == 0) d = c; else if (d == c) @@ -699,17 +725,17 @@ echo(sep, v) int nonl = 0; if (setintr) - sigsetmask(sigblock(0) & ~sigmask(SIGINT)); + (void) sigsetmask(sigblock(0) & ~sigmask(SIGINT)); v++; if (*v == 0) return; - gflag = 0; rscan(v, tglob); + gflag = 0, tglob(v); if (gflag) { v = glob(v); if (v == 0) bferr("No match"); } else - scan(v, trim); + trim(v); if (sep == ' ' && !strcmp(*v, "-n")) nonl++, v++; while (cp = *v++) { @@ -725,7 +751,7 @@ echo(sep, v) else flush(); if (setintr) - sigblock(sigmask(SIGINT)); + (void) sigblock(sigmask(SIGINT)); if (gargv) blkfree(gargv), gargv = 0; } @@ -755,8 +781,8 @@ dounsetenv(v) while (*v); } -setenv(name, value) - char *name, *value; +setenv(name, val) + char *name, *val; { register char **ep = environ; register char *cp, *dp; @@ -767,17 +793,17 @@ setenv(name, value) continue; if (*cp != 0 || *dp != '=') continue; - cp = strspl("=", value); + cp = strspl("=", val); xfree(*ep); *ep = strspl(name, cp); xfree(cp); - scan(ep, trim); + trim(ep); return; } blk[0] = strspl(name, "="); blk[1] = 0; environ = blkspl(environ, blk); xfree((char *)oep); - setenv(name, value); + setenv(name, val); } unsetenv(name) @@ -810,7 +836,7 @@ doumask(v) if (cp == 0) { i = umask(0); - umask(i); + (void) umask(i); printf("%o\n", i); return; } @@ -819,7 +845,7 @@ doumask(v) i = i * 8 + *cp++ - '0'; if (*cp || i < 0 || i > 0777) bferr("Improper mask"); - umask(i); + (void) umask(i); } @@ -854,6 +880,7 @@ findlim(cp) if (res) return (res); bferr("No such limit"); + /*NOTREACHED*/ } dolimit(v) @@ -962,7 +989,7 @@ plim(lp) struct rlimit rlim; printf("%s \t", lp->limname); - getrlimit(lp->limconst, &rlim); + (void) getrlimit(lp->limconst, &rlim); if (rlim.rlim_cur == RLIM_INFINITY) printf("unlimited"); else if (lp->limconst == RLIMIT_CPU) @@ -980,12 +1007,12 @@ dounlimit(v) v++; if (*v == 0) { for (lp = limits+1; lp->limconst >= 0; lp++) - setlim(lp, RLIM_INFINITY); + setlim(lp, (int)RLIM_INFINITY); return; } while (*v) { lp = findlim(*v++); - setlim(lp, RLIM_INFINITY); + setlim(lp, (int)RLIM_INFINITY); } } @@ -994,11 +1021,11 @@ setlim(lp, limit) { struct rlimit rlim; - getrlimit(lp->limconst, &rlim); + (void) getrlimit(lp->limconst, &rlim); if(limit == RLIM_INFINITY && geteuid() != 0) rlim.rlim_cur = rlim.rlim_max; else - rlim.rlim_cur = limit; + rlim.rlim_cur = limit; if (setrlimit(lp->limconst, &rlim) < 0) Perror(bname); } @@ -1012,26 +1039,26 @@ dosuspend() error("Can't suspend a login shell (yet)"); untty(); old = signal(SIGTSTP, SIG_DFL); - kill(0, SIGTSTP); + (void) kill(0, SIGTSTP); /* the shell stops here */ - signal(SIGTSTP, old); + (void) signal(SIGTSTP, old); if (tpgrp != -1) { retry: - ioctl(FSHTTY, TIOCGPGRP, &ctpgrp); + (void) ioctl(FSHTTY, TIOCGPGRP, (char *)&ctpgrp); if (ctpgrp != opgrp) { old = signal(SIGTTIN, SIG_DFL); - kill(0, SIGTTIN); - signal(SIGTTIN, old); + (void) kill(0, SIGTTIN); + (void) signal(SIGTTIN, old); goto retry; } - ioctl(FSHTTY, TIOCSPGRP, &shpgrp); - setpgrp(0, shpgrp); + (void) ioctl(FSHTTY, TIOCSPGRP, (char *)&shpgrp); + (void) setpgrp(0, shpgrp); } - ioctl(FSHTTY, TIOCGETD, &oldisc); + (void) ioctl(FSHTTY, TIOCGETD, (char *)&oldisc); if (oldisc != NTTYDISC) { printf("Switching to new tty driver...\n"); ldisc = NTTYDISC; - ioctl(FSHTTY, TIOCSETD, &ldisc); + (void) ioctl(FSHTTY, TIOCSETD, (char *)&ldisc); } } @@ -1047,7 +1074,7 @@ doeval(v) v++; if (*v == 0) return; - gflag = 0; rscan(v, tglob); + gflag = 0, tglob(v); if (gflag) { gv = v = glob(v); gargv = 0; @@ -1055,7 +1082,7 @@ doeval(v) error("No match"); v = copyblk(v); } else - scan(v, trim); + trim(v); getexit(osetexit); reenter = 0; setexit(); diff --git a/usr/src/bin/csh/glob.c b/usr/src/bin/csh/glob.c index 4e87ad7ab4..9d229db124 100644 --- a/usr/src/bin/csh/glob.c +++ b/usr/src/bin/csh/glob.c @@ -1,6 +1,9 @@ -static char *sccsid = "@(#)glob.c 4.6 %G%"; +#ifndef lint +static char *sccsid = "@(#)glob.c 4.7 (Berkeley) %G%"; +#endif #include "sh.h" +#include "sh.char.h" #include /* @@ -9,8 +12,6 @@ static char *sccsid = "@(#)glob.c 4.6 %G%"; int globcnt; -char *globchars = "`{[*?"; - char *gpath, *gpathp, *lastgpathp; int globbed; bool noglob; @@ -63,7 +64,7 @@ collect(as) #ifdef GDEBUG printf("doing backp of %s\n", as); #endif - dobackp(as, 0); + (void) dobackp(as, 0); #ifdef GDEBUG printf("backp done, acollect'ing\n"); #endif @@ -135,13 +136,13 @@ expand(as) *gpathp = 0; if (gethdir(gpath + 1)) error("Unknown user: %s", gpath + 1); - strcpy(gpath, gpath + 1); + (void) strcpy(gpath, gpath + 1); } else - strcpy(gpath, value("home")); + (void) strcpy(gpath, value("home")); gpathp = strend(gpath); } } - while (!any(*cs, globchars)) { + while (!cmap(*cs, _GLOB)) { if (*cs == 0) { if (!globbed) Gcat(gpath, ""); @@ -160,7 +161,7 @@ expand(as) cs++, gpathp++; *gpathp = 0; if (*oldcs == '{') { - execbrc(cs, NOSTR); + (void) execbrc(cs, NOSTR); return; } matchdir(cs); @@ -174,8 +175,7 @@ matchdir(pattern) { struct stat stb; register struct direct *dp; - DIR *dirp; - register int cnt; + register DIR *dirp; dirp = opendir(gpath); if (dirp == NULL) { @@ -260,8 +260,8 @@ pend: doit: savec = *pm; *pm = 0; - strcpy(lm, pl); - strcat(restbuf, pe + 1); + (void) strcpy(lm, pl); + (void) strcat(restbuf, pe + 1); *pm = savec; if (s == 0) { sgpathp = gpathp; @@ -442,14 +442,24 @@ Gmatch(s, p) } Gcat(s1, s2) - register char *s1, *s2; + char *s1, *s2; { - - gnleft -= strlen(s1) + strlen(s2) + 1; + register char *p, *q; + int n; + + for (p = s1; *p++;) + ; + for (q = s2; *q++;) + ; + gnleft -= (n = (p - s1) + (q - s2) - 1); if (gnleft <= 0 || ++gargc >= GAVSIZ) error("Arguments too long"); gargv[gargc] = 0; - gargv[gargc - 1] = strspl(s1, s2); + p = gargv[gargc - 1] = xalloc((unsigned)n); + for (q = s1; *p++ = *q++;) + ; + for (p--, q = s2; *p++ = *q++;) + ; } addpath(c) @@ -466,52 +476,37 @@ rscan(t, f) register char **t; int (*f)(); { - register char *p, c; + register char *p; - while (p = *t++) { - if (f == tglob) - if (*p == '~') - gflag |= 2; - else if (eq(p, "{") || eq(p, "{}")) - continue; - while (c = *p++) - (*f)(c); - } + while (p = *t++) + while (*p) + (*f)(*p++); } -scan(t, f) +trim(t) register char **t; - int (*f)(); { - register char *p, c; + register char *p; while (p = *t++) - while (c = *p) - *p++ = (*f)(c); -} - -tglob(c) - register char c; -{ - - if (any(c, globchars)) - gflag |= c == '{' ? 2 : 1; - return (c); -} - -trim(c) - char c; -{ - - return (c & TRIM); + while (*p++ &= TRIM) + ; } -tback(c) - char c; +tglob(t) + register char **t; { + register char *p, c; - if (c == '`') - gflag = 1; + while (p = *t++) { + if (*p == '~') + gflag |= 2; + else if (*p == '{' && (p[1] == '\0' || p[1] == '}' && p[2] == '\0')) + continue; + while (c = *p++) + if (cmap(c, _GLOB)) + gflag |= c == '{' ? 2 : 1; + } } char * @@ -525,7 +520,7 @@ globone(str) gv[0] = str; gv[1] = 0; gflag = 0; - rscan(gv, tglob); + tglob(gv); if (gflag) { gvp = glob(gv); if (gvp == 0) { @@ -548,7 +543,7 @@ globone(str) */ xfree((char *)gargv); gargv = 0; } else { - scan(gv, trim); + trim(gv); cp = savestr(gv[0]); } return (cp); @@ -650,14 +645,14 @@ backeval(cp, literal) struct wordent paraml; struct command *t; - close(pvec[0]); - dmove(pvec[1], 1); - dmove(SHDIAG, 2); + (void) close(pvec[0]); + (void) dmove(pvec[1], 1); + (void) dmove(SHDIAG, 2); initdesc(); arginp = cp; while (*cp) *cp++ &= TRIM; - lex(¶ml); + (void) lex(¶ml); if (err) error(err); alias(¶ml); @@ -666,14 +661,14 @@ backeval(cp, literal) error(err); if (t) t->t_dflg |= FPAR; - signal(SIGTSTP, SIG_IGN); - signal(SIGTTIN, SIG_IGN); - signal(SIGTTOU, SIG_IGN); + (void) signal(SIGTSTP, SIG_IGN); + (void) signal(SIGTTIN, SIG_IGN); + (void) signal(SIGTTOU, SIG_IGN); execute(t, -1); exitstat(); } xfree(cp); - close(pvec[1]); + (void) close(pvec[1]); do { int cnt = 0; for (;;) { @@ -721,7 +716,7 @@ backeval(cp, literal) printf("done in backeval, pvec: %d %d\n", pvec[0], pvec[1]); printf("also c = %c <%o>\n", c, c); #endif - close(pvec[0]); + (void) close(pvec[0]); pwait(); prestjob(); } diff --git a/usr/src/bin/csh/hist.c b/usr/src/bin/csh/hist.c index 10437c2c61..fd3149b509 100644 --- a/usr/src/bin/csh/hist.c +++ b/usr/src/bin/csh/hist.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)hist.c 4.8 %G%"; +#ifndef lint +static char *sccsid = "@(#)hist.c 4.9 (Berkeley) %G%"; +#endif #include "sh.h" @@ -10,29 +12,30 @@ savehist(sp) struct wordent *sp; { register struct Hist *hp, *np; - int histlen; - register char *cp; + register int histlen = 0; + char *cp; - cp = value("history"); - if (*cp == 0) - histlen = 0; - else { - while (*cp && digit(*cp)) - cp++; - /* avoid a looping snafu */ - if (*cp) - set("history", "10"); - histlen = getn(value("history")); - } /* throw away null lines */ if (sp->next->word[0] == '\n') return; + cp = value("history"); + if (*cp) { + register char *p = cp; + + while (*p) { + if (!digit(*p)) { + histlen = 0; + break; + } + histlen = histlen * 10 + *p++ - '0'; + } + } for (hp = &Histlist; np = hp->Hnext;) if (eventno - np->Href >= histlen || histlen == 0) hp->Hnext = np->Hnext, hfree(np); else hp = np; - enthist(++eventno, sp, 1); + (void) enthist(++eventno, sp, 1); } struct Hist * @@ -43,7 +46,7 @@ enthist(event, lp, docopy) { register struct Hist *np; - np = (struct Hist *) calloc(1, sizeof *np); + np = (struct Hist *) xalloc(sizeof *np); np->Hnum = np->Href = event; if (docopy) copylex(&np->Hlex, lp); @@ -73,7 +76,7 @@ dohist(vp) if (getn(value("history")) == 0) return; if (setintr) - sigsetmask(sigblock(0) & ~sigmask(SIGINT)); + (void) sigsetmask(sigblock(0) & ~sigmask(SIGINT)); vp++; while (*vp && *vp[0] == '-') { if (*vp && eq(*vp, "-h")) diff --git a/usr/src/bin/csh/init.c b/usr/src/bin/csh/init.c index cc9f9bbb16..9ac10a072f 100644 --- a/usr/src/bin/csh/init.c +++ b/usr/src/bin/csh/init.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)init.c 4.3 %G%"; +#ifndef lint +static char *sccsid = "@(#)init.c 4.4 (Berkeley) %G%"; +#endif #include "sh.local.h" @@ -76,9 +78,7 @@ struct biltins { } bfunc[] = { "@", dolet, 0, INF, "alias", doalias, 0, INF, -#ifdef debug "alloc", showall, 0, 1, -#endif "bg", dobg, 0, INF, "break", dobreak, 0, 0, "breaksw", doswbrk, 0, 0, @@ -146,8 +146,8 @@ struct biltins { "unsetenv", dounsetenv, 1, INF, "wait", dowait, 0, 0, "while", dowhile, 1, INF, - 0, 0, 0, 0, }; +int nbfunc = sizeof bfunc / sizeof *bfunc; #define ZBREAK 0 #define ZBRKSW 1 @@ -190,8 +190,8 @@ struct srch { "set", ZSET, "switch", ZSWITCH, "while", ZWHILE, - 0, 0, }; +int nsrchn = sizeof srchn / sizeof *srchn; struct mesg { char *iname; @@ -213,18 +213,18 @@ struct mesg { "PIPE", "Broken pipe", "ALRM", "Alarm clock", "TERM", "Terminated", - 0, "Signal 16", + "URG", "Urgent I/O condition", "STOP", "Stopped (signal)", "TSTP", "Stopped", "CONT", "Continued", "CHLD", "Child exited", "TTIN", "Stopped (tty input)", "TTOU", "Stopped (tty output)", - "TINT", "Tty input interrupt", + "IO", "I/O possible", "XCPU", "Cputime limit exceeded", "XFSZ", "Filesize limit exceeded", - 0, "Signal 26", - 0, "Signal 27", + "VTALRM","Virtual timer expired", + "PROF", "Profiling timer expired", 0, "Signal 28", 0, "Signal 29", 0, "Signal 30", diff --git a/usr/src/bin/csh/lex.c b/usr/src/bin/csh/lex.c index 1a1ca3dc02..5fed216434 100644 --- a/usr/src/bin/csh/lex.c +++ b/usr/src/bin/csh/lex.c @@ -1,8 +1,9 @@ #ifndef lint -static char *sccsid = "@(#)lex.c 4.6 %G%"; +static char *sccsid = "@(#)lex.c 4.7 (Berkeley) %G%"; #endif #include "sh.h" +#include "sh.char.h" /* * C shell @@ -50,6 +51,8 @@ char *alvecp; /* "Globp" for alias resubstitution */ */ bool hadhist; +char getCtmp; +#define getC(f) ((getCtmp = peekc) ? (peekc = 0, getCtmp) : getC1(f)) #define ungetC(c) peekc = c #define ungetD(c) peekd = c @@ -78,8 +81,9 @@ lex(hp) * interrupted. */ do { - register struct wordent *new = (struct wordent *) calloc(1, sizeof *wdp); + register struct wordent *new = (struct wordent *) xalloc(sizeof *wdp); + new->word = 0; new->prev = wdp; new->next = hp; wdp->next = new; @@ -106,14 +110,14 @@ prlex(sp0) copylex(hp, fp) register struct wordent *hp; - struct wordent *fp; + register struct wordent *fp; { register struct wordent *wdp; wdp = hp; fp = fp->next; do { - register struct wordent *new = (struct wordent *) calloc(1, sizeof *wdp); + register struct wordent *new = (struct wordent *) xalloc(sizeof *wdp); new->prev = wdp; new->next = hp; @@ -133,14 +137,12 @@ freelex(vp) while (vp->next != vp) { fp = vp->next; vp->next = fp->next; - xfree(fp->word); - xfree((char *)fp); + XFREE(fp->word) + XFREE((char *)fp) } vp->prev = vp; } -char *WORDMETA = "# '`\"\t;&<>()|\n"; - char * word() { @@ -153,125 +155,113 @@ word() wp = wbuf; i = BUFSIZ - 4; loop: - c = getC(DOALL); - switch (c) { - - case ' ': - case '\t': - goto loop; + while ((c = getC(DOALL)) == ' ' || c == '\t') + ; + if (cmap(c, _META|_ESC)) + switch (c) { + case '&': + case '|': + case '<': + case '>': + *wp++ = c; + c1 = getC(DOALL); + if (c1 == c) + *wp++ = c1; + else + ungetC(c1); + goto ret; - case '`': - case '\'': - case '"': - *wp++ = c, --i, c1 = c; - dolflg = c == '"' ? DOALL : DOEXCL; - for (;;) { - c = getC(dolflg); - if (c == c1) + case '#': + if (intty) break; - if (c == '\n') { - seterrc("Unmatched ", c1); - ungetC(c); - goto ret; - } - if (c == '\\') { + c = 0; + do { + c1 = c; c = getC(0); - if (c == HIST) - c |= QUOTE; - else if (c == '\n') - c = ' '; - else - ungetC(c), c = '\\'; - } - if (--i <= 0) - goto toochars; - *wp++ = c; - } - *wp++ = c, --i; - goto pack; - - case '&': - case '|': - case '<': - case '>': - *wp++ = c; - c1 = getC(DOALL); - if (c1 == c) - *wp++ = c1; - else - ungetC(c1); - goto ret; + } while (c != '\n'); + if (c1 == '\\') + goto loop; + /* fall into ... */ - case '#': - if (intty) - break; - if (wp != wbuf) { - ungetC(c); + case ';': + case '(': + case ')': + case '\n': + *wp++ = c; goto ret; - } - c = 0; - do { - c1 = c; - c = getC(0); - } while (c != '\n'); - if (c1 == '\\') - goto loop; - /* fall into ... */ - case ';': - case '(': - case ')': - case '\n': - *wp++ = c; - goto ret; - -casebksl: - case '\\': - c = getC(0); - if (c == '\n') { - if (onelflg == 1) - onelflg = 2; - goto loop; - } - if (c != HIST) - *wp++ = '\\', --i; - c |= QUOTE; - break; - } - ungetC(c); -pack: - for (;;) { - c = getC(DOALL); - if (c == '\\') { + case '\\': c = getC(0); if (c == '\n') { if (onelflg == 1) onelflg = 2; - goto ret; + goto loop; } if (c != HIST) *wp++ = '\\', --i; c |= QUOTE; } - if (any(c, WORDMETA + intty)) { - ungetC(c); - if (any(c, "\"'`")) - goto loop; - goto ret; + c1 = 0; + dolflg = DOALL; + for (;;) { + if (c1) { + if (c == c1) { + c1 = 0; + dolflg = DOALL; + } else if (c == '\\') { + c = getC(0); + if (c == HIST) + c |= QUOTE; + else { + if (c == '\n') + /* + if (c1 == '`') + c = ' '; + else + */ + c |= QUOTE; + ungetC(c); + c = '\\'; + } + } else if (c == '\n') { + seterrc("Unmatched ", c1); + ungetC(c); + break; + } + } else if (cmap(c, _META|_Q|_Q1|_ESC)) { + if (c == '\\') { + c = getC(0); + if (c == '\n') { + if (onelflg == 1) + onelflg = 2; + break; + } + if (c != HIST) + *wp++ = '\\', --i; + c |= QUOTE; + } else if (cmap(c, _Q|_Q1)) { /* '"` */ + c1 = c; + dolflg = c == '"' ? DOALL : DOEXCL; + } else if (c != '#' || !intty) { + ungetC(c); + break; + } + } + if (--i > 0) { + *wp++ = c; + c = getC(dolflg); + } else { + seterr("Word too long"); + wp = &wbuf[1]; + break; } - if (--i <= 0) - goto toochars; - *wp++ = c; } -toochars: - seterr("Word too long"); - wp = &wbuf[1]; ret: *wp = 0; return (savestr(wbuf)); } -getC(flag) +getC1(flag) register int flag; { register char c; @@ -282,14 +272,13 @@ top: return (c); } if (lap) { - c = *lap++; - if (c == 0) { + if ((c = *lap++) == 0) lap = 0; - goto top; + else { + if (cmap(c, _META|_Q|_Q1)) + c |= QUOTE; + return (c); } - if (any(c, WORDMETA + intty)) - c |= QUOTE; - return (c); } if (c = peekd) { peekd = 0; @@ -435,10 +424,10 @@ addla(cp) return; } if (lap) - strcpy(buf, lap); - strcpy(labuf, cp); + (void) strcpy(buf, lap); + (void) strcpy(labuf, cp); if (lap) - strcat(labuf, buf); + (void) strcat(labuf, buf); lap = labuf; } @@ -559,7 +548,7 @@ getsub(en) seterr("No prev sub"); goto ret; } - strcpy(lhsb, slhs); + (void) strcpy(lhsb, slhs); break; /* @@ -583,7 +572,7 @@ bads: c = getC(0); if (c == '\n') { unreadc(c); - goto bads; + break; } if (c == delim) break; @@ -604,7 +593,7 @@ bads: goto ret; } cp = rhsb; - strcpy(orhsb, cp); + (void) strcpy(orhsb, cp); for (;;) { c = getC(0); if (c == '\n') { @@ -617,7 +606,7 @@ bads: if (c == '~') { if (&cp[strlen(orhsb)] > &rhsb[sizeof rhsb - 2]) goto toorhs; - strcpy(cp, orhsb); + (void) strcpy(cp, orhsb); cp = strend(cp); continue; } @@ -643,7 +632,7 @@ bads: seterrc("Bad ! modifier: ", c); goto ret; } - strcpy(slhs, lhsb); + (void) strcpy(slhs, lhsb); if (exclc) en = dosub(sc, en, global); ret: @@ -693,6 +682,7 @@ subword(cp, type, adid) switch (type) { case 'r': + case 'e': case 'h': case 't': case 'q': @@ -728,7 +718,7 @@ subword(cp, type, adid) if (i < 0) goto ovflo; *wp = 0; - strcat(wp, lhsb); + (void) strcat(wp, lhsb); wp = strend(wp); continue; } @@ -740,7 +730,7 @@ ovflo: return (""); } *wp = 0; - strcat(wp, mp); + (void) strcat(wp, mp); *adid = 1; return (savestr(wbuf)); } @@ -768,13 +758,12 @@ domod(cp, type) case 'h': case 't': - if (!any('/', cp)) /* what if :h :t are both the same? */ - return (0); + if (!any('/', cp)) + return (type == 't' ? savestr(cp) : 0); wp = strend(cp); while (*--wp != '/') continue; if (type == 'h') -take: xp = savestr(cp), xp[wp - cp] = 0; else xp = savestr(wp + 1); @@ -1007,9 +996,38 @@ findev(cp, anyarg) { register struct Hist *hp; - for (hp = Histlist.Hnext; hp; hp = hp->Hnext) - if (matchev(hp, cp, anyarg)) - return (hp); + for (hp = Histlist.Hnext; hp; hp = hp->Hnext) { + char *dp; + register char *p, *q; + register struct wordent *lp = hp->Hlex.next; + int argno = 0; + + if (lp->word[0] == '\n') + continue; + if (!anyarg) { + p = cp; + q = lp->word; + do + if (!*p) + return (hp); + while (*p++ == *q++); + continue; + } + do { + for (dp = lp->word; *dp; dp++) { + p = cp; + q = dp; + do + if (!*p) { + quesarg = argno; + return (hp); + } + while (*p++ == *q++); + } + lp = lp->next; + argno++; + } while (lp->word[0] != '\n'); + } noev(cp); return (0); } @@ -1021,32 +1039,6 @@ noev(cp) seterr2(cp, ": Event not found"); } -matchev(hp, cp, anyarg) - register struct Hist *hp; - char *cp; - bool anyarg; -{ - register char *dp; - struct wordent *lp = &hp->Hlex; - int argno = 0; - - for (;;) { - lp = lp->next; - if (lp->word[0] == '\n') - return (0); - for (dp = lp->word; *dp; dp++) { - if (matchs(dp, cp)) { - if (anyarg) - quesarg = argno; - return (1); - } - if (!anyarg) - return (0); - } - argno++; - } -} - setexclp(cp) register char *cp; { @@ -1133,17 +1125,19 @@ reread: if (wanteof) return (-1); /* was isatty but raw with ignoreeof yields problems */ - if (ioctl(SHIN, TIOCGETP, &tty)==0 && (tty.sg_flags & RAW) == 0) { + if (ioctl(SHIN, TIOCGETP, (char *)&tty) == 0 && + (tty.sg_flags & RAW) == 0) { /* was 'short' for FILEC */ int ctpgrp; if (++sincereal > 25) goto oops; if (tpgrp != -1 && - ioctl(FSHTTY, TIOCGPGRP, &ctpgrp) == 0 && + ioctl(FSHTTY, TIOCGPGRP, (char *)&ctpgrp) == 0 && tpgrp != ctpgrp) { - ioctl(FSHTTY, TIOCSPGRP, &tpgrp); - killpg(ctpgrp, SIGHUP); + (void) ioctl(FSHTTY, TIOCSPGRP, + (char *)&tpgrp); + (void) killpg(ctpgrp, SIGHUP); printf("Reset tty pgrp from %d to %d\n", ctpgrp, tpgrp); goto reread; } @@ -1180,7 +1174,7 @@ bgetc() if (cantell) { if (fseekp < fbobp || fseekp > feobp) { fbobp = feobp = fseekp; - lseek(SHIN, fseekp, 0); + (void) lseek(SHIN, fseekp, 0); } if (fseekp == feobp) { fbobp = feobp; @@ -1199,10 +1193,12 @@ bgetc() again: buf = (int) fseekp / BUFSIZ; if (buf >= fblocks) { - register char **nfbuf = (char **) calloc(fblocks+2, sizeof (char **)); + register char **nfbuf = + (char **) calloc((unsigned) (fblocks + 2), + sizeof (char **)); if (fbuf) { - blkcpy(nfbuf, fbuf); + (void) blkcpy(nfbuf, fbuf); xfree((char *)fbuf); } fbuf = nfbuf; @@ -1213,14 +1209,13 @@ again: if (fseekp >= feobp) { buf = (int) feobp / BUFSIZ; off = (int) feobp % BUFSIZ; -#ifdef FILEC - roomleft = BUFSIZ - off; -#endif - do #ifndef FILEC + do c = read(SHIN, fbuf[buf] + off, BUFSIZ - off); #else - if (intty) { + roomleft = BUFSIZ - off; + do + if (filec && intty) { c = numleft ? numleft : tenex(ttyline, BUFSIZ); if (c > roomleft) { /* start with fresh buffer */ @@ -1241,8 +1236,8 @@ again: #ifndef FILEC goto again; #else - if (!intty) - goto again; + if (filec && !intty) + goto again; #endif } c = fbuf[buf][(int) fseekp % BUFSIZ]; @@ -1264,7 +1259,7 @@ bfree() if (sb > 0) { for (i = 0; i < sb; i++) xfree(fbuf[i]); - blkcpy(fbuf, &fbuf[sb]); + (void) blkcpy(fbuf, &fbuf[sb]); fseekp -= BUFSIZ * sb; feobp -= BUFSIZ * sb; fblocks -= sb; @@ -1272,7 +1267,7 @@ bfree() } bseek(l) - long l; + off_t l; { register struct whyle *wp; @@ -1292,7 +1287,7 @@ bseek(l) } /* any similarity to bell telephone is purely accidental */ -long +off_t btell() { @@ -1302,7 +1297,7 @@ btell() btoeof() { - lseek(SHIN, 0l, 2); + (void) lseek(SHIN, (off_t)0, 2); fseekp = feobp; wfree(); bfree(); @@ -1315,12 +1310,12 @@ settell() cantell = 0; if (arginp || onelflg || intty) return; - if (lseek(SHIN, 0l, 1) < 0 || errno == ESPIPE) + if (lseek(SHIN, (off_t)0, 1) < 0 || errno == ESPIPE) return; fbuf = (char **) calloc(2, sizeof (char **)); fblocks = 1; fbuf[0] = calloc(BUFSIZ, sizeof (char)); - fseekp = fbobp = feobp = tell(SHIN); + fseekp = fbobp = feobp = lseek(SHIN, (off_t)0, 1); cantell = 1; } #endif diff --git a/usr/src/bin/csh/misc.c b/usr/src/bin/csh/misc.c index b60f27f2b5..98e2e3de75 100644 --- a/usr/src/bin/csh/misc.c +++ b/usr/src/bin/csh/misc.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)misc.c 4.3 %G%"; +#ifndef lint +static char *sccsid = "@(#)misc.c 4.4 (Berkeley) %G%"; +#endif #include "sh.h" @@ -37,31 +39,49 @@ any(c, s) return(0); } +onlyread(cp) + char *cp; +{ + extern char end[]; + + return (cp < end); +} + +xfree(cp) + char *cp; +{ + extern char end[]; + + if (cp >= end && cp < (char *) &cp) + free(cp); +} + +char * +savestr(s) + register char *s; +{ + char *n; + register char *p; + + if (s == 0) + s = ""; + for (p = s; *p++;) + ; + n = p = xalloc((unsigned) (p - s)); + while (*p++ = *s++) + ; + return (n); +} + char * calloc(i, j) register unsigned i; unsigned j; { register char *cp, *dp; -#ifdef debug - static char *av[2] = {0, 0}; -#endif i *= j; - cp = (char *) malloc(i); - if (cp == 0) { - child++; -#ifndef debug - error("Out of memory"); -#else - showall(av); - printf("i=%d, j=%d: ", i/j, j); - printf("Out of memory\n"); - chdir("/usr/bill/cshcore"); - abort(); -#endif - } - dp = cp; + dp = cp = xalloc(i); if (i != 0) do *dp++ = 0; @@ -69,6 +89,28 @@ calloc(i, j) return (cp); } +nomem(i) + unsigned i; +{ +#ifdef debug + static char *av[2] = {0, 0}; +#endif + + child++; +#ifndef debug + error("Out of memory"); +#ifdef lint + i = i; +#endif +#else + showall(av); + printf("i=%d: Out of memory\n", i); + chdir("/usr/bill/cshcore"); + abort(); +#endif + return 0; /* fool lint */ +} + char ** blkend(up) register char **up; @@ -117,7 +159,7 @@ blkcat(up, vp) char **up, **vp; { - blkcpy(blkend(up), vp); + (void) blkcpy(blkend(up), vp); return (up); } @@ -126,17 +168,17 @@ blkfree(av0) { register char **av = av0; - while (*av) - xfree(*av++); - xfree((char *)av0); + for (; *av; av++) + XFREE(*av) + XFREE((char *)av0) } char ** saveblk(v) register char **v; { - register int len = blklen(v) + 1; - register char **newv = (char **) calloc(len, sizeof (char **)); + register char **newv = + (char **) calloc((unsigned) (blklen(v) + 1), sizeof (char **)); char **onewv = newv; while (*v) @@ -146,12 +188,20 @@ saveblk(v) char * strspl(cp, dp) - register char *cp, *dp; + char *cp, *dp; { - register char *ep = calloc(1, strlen(cp) + strlen(dp) + 1); - - strcpy(ep, cp); - strcat(ep, dp); + char *ep; + register char *p, *q; + + for (p = cp; *p++;) + ; + for (q = dp; *q++;) + ; + ep = xalloc((unsigned) ((p - cp) + (q - dp) - 1)); + for (p = ep, q = cp; *p++ = *q++;) + ; + for (p--, q = dp; *p++ = *q++;) + ; return (ep); } @@ -159,9 +209,11 @@ char ** blkspl(up, vp) register char **up, **vp; { - register char **wp = (char **) calloc(blklen(up) + blklen(vp) + 1, sizeof (char **)); + register char **wp = + (char **) calloc((unsigned) (blklen(up) + blklen(vp) + 1), + sizeof (char **)); - blkcpy(wp, up); + (void) blkcpy(wp, up); return (blkcat(wp, vp)); } @@ -187,31 +239,15 @@ closem() for (f = 0; f < NOFILE; f++) if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD && f != FSHTTY) - close(f); -} - -/* - * Close files before executing a file. - * We could be MUCH more intelligent, since (on a version 7 system) - * we need only close files here during a source, the other - * shell fd's being in units 16-19 which are closed automatically! - */ -closech() -{ - register int f; - - if (didcch) - return; - didcch = 1; - SHIN = 0; SHOUT = 1; SHDIAG = 2; OLDSTD = 0; - for (f = 3; f < NOFILE; f++) - close(f); + (void) close(f); } donefds() { - close(0), close(1), close(2); + (void) close(0); + (void) close(1); + (void) close(2); didfds = 0; } @@ -226,15 +262,13 @@ dmove(i, j) if (i == j || i < 0) return (i); -#ifdef V7 if (j >= 0) { - dup2(i, j); + (void) dup2(i, j); return (j); - } else -#endif - j = dcopy(i, j); + } + j = dcopy(i, j); if (j != i) - close(i); + (void) close(i); return (j); } @@ -244,13 +278,11 @@ dcopy(i, j) if (i == j || i < 0 || j < 0 && i > 2) return (i); -#ifdef V7 if (j >= 0) { - dup2(i, j); + (void) dup2(i, j); return (j); } -#endif - close(j); + (void) close(j); return (renum(i, j)); } @@ -265,7 +297,7 @@ renum(i, j) return (k); if (k != j) { j = renum(k, j); - close(k); + (void) close(k); return (j); } return (k); @@ -297,7 +329,7 @@ lshift(v, c) while (*u && --c >= 0) xfree(*u++); - blkcpy(v, u); + (void) blkcpy(v, u); } number(cp) @@ -318,7 +350,8 @@ char ** copyblk(v) register char **v; { - register char **nv = (char **) calloc(blklen(v) + 1, sizeof (char **)); + register char **nv = + (char **) calloc((unsigned) (blklen(v) + 1), sizeof (char **)); return (blkcpy(nv, v)); } diff --git a/usr/src/bin/csh/parse.c b/usr/src/bin/csh/parse.c index c721592b40..7ca7ddad78 100644 --- a/usr/src/bin/csh/parse.c +++ b/usr/src/bin/csh/parse.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)parse.c 4.1 %G%"; +#ifndef lint +static char *sccsid = "@(#)parse.c 4.2 (Berkeley) %G%"; +#endif #include "sh.h" @@ -118,7 +120,7 @@ asyn3(p1, p2) char *cp = alout.next->word; alout.next->word = strspl("\200", cp); - xfree(cp); + XFREE(cp) } p1 = freenod(p1, redid ? p2 : p1->next); if (alout.next != &alout) { @@ -126,8 +128,8 @@ asyn3(p1, p2) alout.prev->prev->next = p1->next; alout.next->prev = p1; p1->next = alout.next; - xfree(alout.prev->word); - xfree((char *)(alout.prev)); + XFREE(alout.prev->word) + XFREE((char *)alout.prev) } reset(); /* throw! */ } @@ -139,9 +141,9 @@ freenod(p1, p2) register struct wordent *retp = p1->prev; while (p1 != p2) { - xfree(p1->word); + XFREE(p1->word) p1 = p1->next; - xfree((char *)(p1->prev)); + XFREE((char *)p1->prev) } retp->next = p2; p2->prev = retp; @@ -491,7 +493,7 @@ again: if (n < 0) n = 0; t = (struct command *) calloc(1, sizeof (*t)); - av = (char **) calloc(n + 1, sizeof (char **)); + av = (char **) calloc((unsigned) (n + 1), sizeof (char **)); t->t_dcom = av; n = 0; if (p2->word[0] == ')') @@ -599,8 +601,8 @@ freesyn(t) case TCOM: for (v = t->t_dcom; *v; v++) - xfree(*v); - xfree((char *)(t->t_dcom)); + XFREE(*v) + XFREE((char *)t->t_dcom) goto lr; case TPAR: @@ -608,7 +610,8 @@ freesyn(t) /* fall into ... */ lr: - xfree(t->t_dlef), xfree(t->t_drit); + XFREE(t->t_dlef) + XFREE(t->t_drit) break; case TAND: @@ -618,5 +621,5 @@ lr: freesyn(t->t_dcar), freesyn(t->t_dcdr); break; } - xfree((char *)t); + XFREE((char *)t) } diff --git a/usr/src/bin/csh/proc.c b/usr/src/bin/csh/proc.c index 107fb44c61..b93904d929 100644 --- a/usr/src/bin/csh/proc.c +++ b/usr/src/bin/csh/proc.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)proc.c 4.14 (Berkeley) 84/11/21"; +#ifndef lint +static char *sccsid = "@(#)proc.c 4.15 (Berkeley) %G%"; +#endif #include "sh.h" #include "sh.dir.h" @@ -28,11 +30,8 @@ pchild() int jobflags; struct rusage ru; - if (!timesdone) - timesdone++, times(&shtimes); loop: - pid = wait3(&w.w_status, (setintr ? WNOHANG|WUNTRACED:WNOHANG), - &ru); + pid = wait3(&w, (setintr ? WNOHANG|WUNTRACED:WNOHANG), &ru); if (pid <= 0) { if (errno == EINTR) { errno = 0; @@ -53,16 +52,8 @@ found: pp->p_flags |= PSTOPPED; pp->p_reason = w.w_stopsig; } else { - if (pp->p_flags & (PTIME|PPTIME) || adrof("time")) { - time_t oldcutimes, oldcstimes; - oldcutimes = shtimes.tms_cutime; - oldcstimes = shtimes.tms_cstime; - gettimeofday(&pp->p_etime, (struct timezone *)0); - times(&shtimes); - pp->p_utime = shtimes.tms_cutime - oldcutimes; - pp->p_stime = shtimes.tms_cstime - oldcstimes; - } else - times(&shtimes); + if (pp->p_flags & (PTIME|PPTIME) || adrof("time")) + (void) gettimeofday(&pp->p_etime, (struct timezone *)0); pp->p_rusage = ru; if (WIFSIGNALED(w)) { if (w.w_termsig == SIGINT) @@ -85,7 +76,7 @@ found: do { if ((fp->p_flags & (PPTIME|PRUNNING|PSTOPPED)) == 0 && !child && adrof("time") && - (fp->p_utime + fp->p_stime) / HZ >= + fp->p_rusage.ru_utime.tv_sec+fp->p_rusage.ru_stime.tv_sec >= atoi(value("time"))) fp->p_flags |= PTIME; jobflags |= fp->p_flags; @@ -124,7 +115,7 @@ found: } else { if (jobflags&PNOTIFY || adrof("notify")) { printf("\215\n"); - pprint(pp, NUMBER|NAME|REASON); + (void) pprint(pp, NUMBER|NAME|REASON); if ((jobflags&PSTOPPED) == 0) pflush(pp); } else { @@ -149,7 +140,7 @@ pnote() flags = pprint(pp, NUMBER|NAME|REASON); if ((flags&(PRUNNING|PSTOPPED)) == 0) pflush(pp); - sigsetmask(omask); + (void) sigsetmask(omask); } } } @@ -177,7 +168,7 @@ pwait() xfree((char *)pp); pp = fp; } - sigsetmask(omask); + (void) sigsetmask(omask); pjwait(pcurrjob); } @@ -190,7 +181,6 @@ pjwait(pp) { register struct process *fp; int jobflags, reason, omask; - int (*old)(); while (pp->p_pid != pp->p_jobid) pp = pp->p_friends; @@ -214,14 +204,14 @@ pjwait(pp) break; sigpause(0); } - sigsetmask(omask); - if (tpgrp > 0) - ioctl(FSHTTY, TIOCSPGRP, &tpgrp); /* get tty back */ + (void) sigsetmask(omask); + if (tpgrp > 0) /* get tty back */ + (void) ioctl(FSHTTY, TIOCSPGRP, (char *)&tpgrp); if ((jobflags&(PSIGNALED|PSTOPPED|PTIME)) || !eq(dcwd->di_name, fp->p_cwd->di_name)) { if (jobflags&PSTOPPED) printf("\n"); - pprint(pp, AREASON|SHELLDIR); + (void) pprint(pp, AREASON|SHELLDIR); } if ((jobflags&(PINTERRUPTED|PSTOPPED)) && setintr && (!gointr || !eq(gointr, "-"))) { @@ -260,7 +250,7 @@ loop: sigpause(0); goto loop; } - sigsetmask(omask); + (void) sigsetmask(omask); pjobs = 0; } @@ -399,7 +389,7 @@ palloc(pid, t) } pp->p_next = proclist.p_next; proclist.p_next = pp; - gettimeofday(&pp->p_btime, (struct timezone *)0); + (void) gettimeofday(&pp->p_btime, (struct timezone *)0); } padd(t) @@ -458,12 +448,12 @@ pads(cp) if (cmdlen >= PMAXLEN) return; if (cmdlen + i >= PMAXLEN) { - strcpy(cmdp, " ..."); + (void) strcpy(cmdp, " ..."); cmdlen = PMAXLEN; cmdp += 4; return; } - strcpy(cmdp, cp); + (void) strcpy(cmdp, cp); cmdp += i; cmdlen += i; } @@ -776,9 +766,9 @@ dokill(v) if (name = mesg[signum].iname) printf("%s ", name); if (signum == 16) - printf("\n"); + putchar('\n'); } - printf("\n"); + putchar('\n'); return; } if (digit(v[0][1])) { @@ -834,9 +824,9 @@ pkill(v, signum) goto cont; } } - killpg(pp->p_jobid, signum); + (void) killpg(pp->p_jobid, signum); if (signum == SIGTERM || signum == SIGHUP) - killpg(pp->p_jobid, SIGCONT); + (void) killpg(pp->p_jobid, SIGCONT); } else if (!digit(*cp)) bferr("Arguments should be jobs or process id's"); else { @@ -848,13 +838,13 @@ pkill(v, signum) goto cont; } if (signum == SIGTERM || signum == SIGHUP) - kill(pid, SIGCONT); + (void) kill(pid, SIGCONT); } cont: xfree(cp); v++; } - sigsetmask(omask); + (void) sigsetmask(omask); if (err) error(NOSTR); } @@ -884,12 +874,12 @@ pstart(pp, foregnd) } while((np = np->p_friends) != pp); if (!foregnd) pclrcurr(pp); - pprint(pp, foregnd ? NAME|JOBDIR : NUMBER|NAME|AMPERSAND); + (void) pprint(pp, foregnd ? NAME|JOBDIR : NUMBER|NAME|AMPERSAND); if (foregnd) - ioctl(FSHTTY, TIOCSPGRP, &pp->p_jobid); + (void) ioctl(FSHTTY, TIOCSPGRP, (char *)&pp->p_jobid); if (jobflags&PSTOPPED) - killpg(pp->p_jobid, SIGCONT); - sigsetmask(omask); + (void) killpg(pp->p_jobid, SIGCONT); + (void) sigsetmask(omask); } panystop(neednl) @@ -949,6 +939,7 @@ match: bferr("No job matches pattern"); else bferr("No such job"); + /*NOTREACHED*/ } /* @@ -1023,7 +1014,7 @@ pfork(t, wanttty) if (setintr == 0) sleep(FORKSLEEP); else { - sigsetmask(omask); + (void) sigsetmask(omask); error("No more processes"); } if (pid == 0) { @@ -1031,7 +1022,6 @@ pfork(t, wanttty) pgrp = pcurrjob ? pcurrjob->p_jobid : getpid(); pflushall(); pcurrjob = PNULL; - timesdone = 0; child++; if (setintr) { setintr = 0; /* until I think otherwise */ @@ -1039,23 +1029,23 @@ pfork(t, wanttty) * Children just get blown away on SIGINT, SIGQUIT * unless "onintr -" seen. */ - signal(SIGINT, ignint ? SIG_IGN : SIG_DFL); - signal(SIGQUIT, ignint ? SIG_IGN : SIG_DFL); + (void) signal(SIGINT, ignint ? SIG_IGN : SIG_DFL); + (void) signal(SIGQUIT, ignint ? SIG_IGN : SIG_DFL); if (wanttty >= 0) { /* make stoppable */ - signal(SIGTSTP, SIG_DFL); - signal(SIGTTIN, SIG_DFL); - signal(SIGTTOU, SIG_DFL); + (void) signal(SIGTSTP, SIG_DFL); + (void) signal(SIGTTIN, SIG_DFL); + (void) signal(SIGTTOU, SIG_DFL); } - signal(SIGTERM, parterm); + (void) signal(SIGTERM, parterm); } else if (tpgrp == -1 && (t->t_dflg&FINT)) { - signal(SIGINT, SIG_IGN); - signal(SIGQUIT, SIG_IGN); + (void) signal(SIGINT, SIG_IGN); + (void) signal(SIGQUIT, SIG_IGN); } if (wanttty > 0) - ioctl(FSHTTY, TIOCSPGRP, &pgrp); + (void) ioctl(FSHTTY, TIOCSPGRP, (char *)&pgrp); if (wanttty >= 0 && tpgrp >= 0) - setpgrp(0, pgrp); + (void) setpgrp(0, pgrp); if (tpgrp > 0) tpgrp = 0; /* gave tty away */ /* @@ -1064,12 +1054,12 @@ pfork(t, wanttty) * Then the parser would have to know about nice/nohup/time */ if (t->t_dflg & FNOHUP) - signal(SIGHUP, SIG_IGN); + (void) signal(SIGHUP, SIG_IGN); if (t->t_dflg & FNICE) - setpriority(PRIO_PROCESS, 0, t->t_nice); + (void) setpriority(PRIO_PROCESS, 0, t->t_nice); } else { palloc(pid, t); - sigsetmask(omask); + (void) sigsetmask(omask); } return (pid); diff --git a/usr/src/bin/csh/proc.h b/usr/src/bin/csh/proc.h index a2a7b643b8..54c023029a 100644 --- a/usr/src/bin/csh/proc.h +++ b/usr/src/bin/csh/proc.h @@ -1,4 +1,4 @@ -/* proc.h 4.4 %G% */ +/* @(#)proc.h 4.5 (Berkeley) %G% */ /* * C shell - process structure declarations @@ -25,8 +25,6 @@ struct process { struct timeval p_btime; /* begin time */ struct timeval p_etime; /* end time */ struct rusage p_rusage; - long p_utime; /* XXX */ - long p_stime; /* XXX */ char *p_command; /* first PMAXLEN chars of command */ }; @@ -74,8 +72,6 @@ struct process *pprevious; /* previous job in table */ short pmaxindex; /* current maximum job index */ -bool timesdone; /* shtimes buffer full ? */ - int psigint(); struct process *pgetcurr(); struct process *plookup(); diff --git a/usr/src/bin/csh/sem.c b/usr/src/bin/csh/sem.c index 88b475b357..c56f23f1d8 100644 --- a/usr/src/bin/csh/sem.c +++ b/usr/src/bin/csh/sem.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)sem.c 4.4 %G%"; +#ifndef lint +static char *sccsid = "@(#)sem.c 4.5 (Berkeley) %G%"; +#endif #include "sh.h" #include "sh.proc.h" @@ -26,7 +28,7 @@ execute(t, wanttty, pipein, pipeout) case TCOM: if ((t->t_dcom[0][0] & (QUOTE|TRIM)) == QUOTE) - strcpy(t->t_dcom[0], t->t_dcom[0] + 1); + (void) strcpy(t->t_dcom[0], t->t_dcom[0] + 1); if ((t->t_dflg & FREDO) == 0) Dfix(t); /* $ " ' \ */ if (t->t_dcom[0] == 0) @@ -42,10 +44,10 @@ execute(t, wanttty, pipein, pipeout) * If noexec then this is all we do. */ if (t->t_dflg & FHERE) { - close(0); + (void) close(0); heredoc(t->t_dlef); if (noexec) - close(0); + (void) close(0); } if (noexec) break; @@ -111,34 +113,34 @@ execute(t, wanttty, pipein, pipeout) #ifdef VFORK else { int vffree(); - int ochild, osetintr, ohaderr, odidfds, odidcch; + int ochild, osetintr, ohaderr, odidfds; int oSHIN, oSHOUT, oSHDIAG, oOLDSTD, otpgrp; int omask; omask = sigblock(sigmask(SIGCHLD)); ochild = child; osetintr = setintr; - ohaderr = haderr; odidfds = didfds; odidcch = didcch; + ohaderr = haderr; odidfds = didfds; oSHIN = SHIN; oSHOUT = SHOUT; oSHDIAG = SHDIAG; oOLDSTD = OLDSTD; otpgrp = tpgrp; Vsav = Vdp = 0; Vav = 0; pid = vfork(); if (pid < 0) { - sigsetmask(omask); + (void) sigsetmask(omask); error("No more processes"); } forked++; if (pid) { child = ochild; setintr = osetintr; haderr = ohaderr; didfds = odidfds; - didcch = odidcch; SHIN = oSHIN; + SHIN = oSHIN; SHOUT = oSHOUT; SHDIAG = oSHDIAG; OLDSTD = oOLDSTD; tpgrp = otpgrp; xfree(Vsav); Vsav = 0; xfree(Vdp); Vdp = 0; - xfree(Vav); Vav = 0; + xfree((char *)Vav); Vav = 0; /* this is from pfork() */ palloc(pid, t); - sigsetmask(omask); + (void) sigsetmask(omask); } else { /* this is from pfork() */ int pgrp; @@ -153,32 +155,34 @@ execute(t, wanttty, pipein, pipeout) if (setintr) { setintr = 0; #ifdef notdef - signal(SIGCHLD, SIG_DFL); + (void) signal(SIGCHLD, SIG_DFL); #endif - signal(SIGINT, ignint ? + (void) signal(SIGINT, ignint ? SIG_IGN : vffree); - signal(SIGQUIT, ignint ? + (void) signal(SIGQUIT, ignint ? SIG_IGN : SIG_DFL); if (wanttty >= 0) { - signal(SIGTSTP, SIG_DFL); - signal(SIGTTIN, SIG_DFL); - signal(SIGTTOU, SIG_DFL); + (void) signal(SIGTSTP, SIG_DFL); + (void) signal(SIGTTIN, SIG_DFL); + (void) signal(SIGTTOU, SIG_DFL); } - signal(SIGTERM, parterm); + (void) signal(SIGTERM, parterm); } else if (tpgrp == -1 && (t->t_dflg&FINT)) { - signal(SIGINT, SIG_IGN); - signal(SIGQUIT, SIG_IGN); + (void) signal(SIGINT, SIG_IGN); + (void) signal(SIGQUIT, SIG_IGN); } if (wanttty > 0) - ioctl(FSHTTY, TIOCSPGRP, &pgrp); + (void) ioctl(FSHTTY, TIOCSPGRP, + (char *)&pgrp); if (wanttty >= 0 && tpgrp >= 0) - setpgrp(0, pgrp); + (void) setpgrp(0, pgrp); if (tpgrp > 0) tpgrp = 0; if (t->t_dflg & FNOHUP) - signal(SIGHUP, SIG_IGN); + (void) signal(SIGHUP, SIG_IGN); if (t->t_dflg & FNICE) - setpriority(PRIO_PROCESS, 0, t->t_nice); + (void) setpriority(PRIO_PROCESS, + 0, t->t_nice); } } @@ -191,15 +195,19 @@ execute(t, wanttty, pipein, pipeout) * wait for the whole job anyway, but this test * doesn't really express our intentions. */ - if (didfds==0 && t->t_dflg&FPIN) - close(pipein[0]), close(pipein[1]); + if (didfds==0 && t->t_dflg&FPIN) { + (void) close(pipein[0]); + (void) close(pipein[1]); + } if ((t->t_dflg & (FPOU|FAND)) == 0) pwait(); break; } doio(t, pipein, pipeout); - if (t->t_dflg & FPOU) - close(pipeout[0]), close(pipeout[1]); + if (t->t_dflg & FPOU) { + (void) close(pipeout[0]); + (void) close(pipeout[1]); + } /* * Perform a builtin function. @@ -221,8 +229,9 @@ execute(t, wanttty, pipein, pipeout) OLDSTD = dcopy(0, FOLDSTD); SHOUT = dcopy(1, FSHOUT); SHDIAG = dcopy(2, FSHDIAG); - close(SHIN), SHIN = -1; - didcch = 0, didfds = 0; + (void) close(SHIN); + SHIN = -1; + didfds = 0; wanttty = -1; t->t_dspr->t_dflg |= t->t_dflg & FINT; execute(t->t_dspr, wanttty); @@ -288,9 +297,9 @@ vffree() register char **v; if (v = gargv) - gargv = 0, xfree(gargv); + gargv = 0, xfree((char *)v); if (v = pargv) - pargv = 0, xfree(pargv); + pargv = 0, xfree((char *)v); _exit(1); } #endif @@ -309,25 +318,28 @@ doio(t, pipein, pipeout) if (didfds || (flags & FREDO)) return; if ((flags & FHERE) == 0) { /* FHERE already done */ - close(0); + (void) close(0); if (cp = t->t_dlef) { cp = globone(Dfix1(cp)); xfree(cp); if (open(cp, 0) < 0) Perror(cp); - } else if (flags & FPIN) - dup(pipein[0]), close(pipein[0]), close(pipein[1]); - else if ((flags & FINT) && tpgrp == -1) - close(0), open("/dev/null", 0); - else - dup(OLDSTD); + } else if (flags & FPIN) { + (void) dup(pipein[0]); + (void) close(pipein[0]); + (void) close(pipein[1]); + } else if ((flags & FINT) && tpgrp == -1) { + (void) close(0); + (void) open("/dev/null", 0); + } else + (void) ioctl(dup(OLDSTD), FIONCLEX, (char *)0); } - close(1); + (void) close(1); if (cp = t->t_drit) { cp = globone(Dfix1(cp)); xfree(cp); if ((flags & FCAT) && open(cp, 1) >= 0) - lseek(1, 0l, 2); + (void) lseek(1, (off_t)0, 2); else { if (!(flags & FANY) && adrof("noclobber")) { if (flags & FCAT) @@ -338,12 +350,15 @@ doio(t, pipein, pipeout) Perror(cp); } } else if (flags & FPOU) - dup(pipeout[1]); + (void) dup(pipeout[1]); else - dup(SHOUT); + (void) ioctl(dup(SHOUT), FIONCLEX, (char *)0); - close(2); - dup((flags & FDIAG) ? 1 : SHDIAG); + (void) close(2); + if (flags & FDIAG) + (void) dup(1); + else + (void) ioctl(dup(SHDIAG), FIONCLEX, (char *)0); didfds = 1; } diff --git a/usr/src/bin/csh/set.c b/usr/src/bin/csh/set.c index 6d6c48c8ba..05b954fb0d 100644 --- a/usr/src/bin/csh/set.c +++ b/usr/src/bin/csh/set.c @@ -1,4 +1,6 @@ -static char *sccsid = "@(#)set.c 4.3 %G%"; +#ifndef lint +static char *sccsid = "@(#)set.c 4.4 (Berkeley) %G%"; +#endif #include "sh.h" @@ -79,6 +81,10 @@ setsyn: setenv("TERM", value(vp)); else if (eq(vp, "home")) setenv("HOME", value(vp)); +#ifdef FILEC + else if (eq(vp, "filec")) + filec = 1; +#endif } while (p = *v++); } @@ -110,6 +116,7 @@ asx(vp, subscr, p) struct varent * getvx(vp, subscr) + char *vp; { register struct varent *v = adrof(vp); @@ -122,7 +129,6 @@ getvx(vp, subscr) char plusplus[2] = { '1', 0 }; - dolet(v) char **v; { @@ -195,11 +201,13 @@ letsyn: #endif else set(vp, operate(op, value(vp), p)); - if (strcmp(vp, "path") == 0) + if (eq(vp, "path")) { + exportpath(adrof("path")->vec); dohash(); - xfree(vp); + } + XFREE(vp) if (c != '=') - xfree(p); + XFREE(p) } while (p = *v++); } @@ -245,35 +253,6 @@ operate(op, vp, p) return (putn(i)); } -onlyread(cp) - char *cp; -{ - extern char end[]; - - return (cp < end); -} - -xfree(cp) - char *cp; -{ - extern char end[]; - - if (cp >= end && cp < (char *) &cp) - free(cp); -} - -char * -savestr(s) - register char *s; -{ - register char *n; - - if (s == 0) - s = ""; - strcpy(n = calloc(1, strlen(s) + 1), s); - return (n); -} - static char *putp; char * @@ -337,14 +316,6 @@ badnum: return (0); } -char * -value(var) - char *var; -{ - - return (value1(var, &shvhed)); -} - char * value1(var, head) char *var; @@ -356,61 +327,49 @@ value1(var, head) return (vp == 0 || vp->vec[0] == 0 ? "" : vp->vec[0]); } -static struct varent *shprev; - -struct varent * -adrof(var) - char *var; -{ - - return (adrof1(var, &shvhed)); -} - struct varent * -madrof(pat, head) +madrof(pat, vp) char *pat; - struct varent *head; -{ register struct varent *vp; +{ + register struct varent *vp1; - shprev = head; - for (vp = shprev->link; vp != 0; vp = vp->link) { - if (Gmatch(vp->name, pat)) - return (vp); - shprev = vp; + for (; vp; vp = vp->v_right) { + if (vp->v_left && (vp1 = madrof(pat, vp->v_left))) + return vp1; + if (Gmatch(vp->v_name, pat)) + return vp; } - return (0); + return vp; } struct varent * -adrof1(var, head) - char *var; - struct varent *head; +adrof1(name, v) + register char *name; + register struct varent *v; { - register struct varent *vp; - int cmp; - - shprev = head; - for (vp = shprev->link; vp != 0; vp = vp->link) { - cmp = strcmp(vp->name, var); - if (cmp == 0) - return (vp); - else if (cmp > 0) - return (0); - shprev = vp; - } - return (0); + register cmp; + + v = v->v_left; + while (v && ((cmp = *name - *v->v_name) || + (cmp = strcmp(name, v->v_name)))) + if (cmp < 0) + v = v->v_left; + else + v = v->v_right; + return v; } /* * The caller is responsible for putting value in a safe place */ -set(var, value) - char *var, *value; +set(var, val) + char *var, *val; { - register char **vec = (char **) calloc(2, sizeof (char **)); + register char **vec = (char **) xalloc(2 * sizeof (char **)); - vec[0] = onlyread(value) ? savestr(value) : value; + vec[0] = onlyread(val) ? savestr(val) : val; + vec[1] = 0; set1(var, vec, &shvhed); } @@ -418,10 +377,9 @@ set1(var, vec, head) char *var, **vec; struct varent *head; { - register char **oldv = vec; - gflag = 0; rscan(oldv, tglob); + gflag = 0; tglob(oldv); if (gflag) { vec = glob(oldv); if (vec == 0) { @@ -435,27 +393,35 @@ set1(var, vec, head) setq(var, vec, head); } -setq(var, vec, head) - char *var, **vec; - struct varent *head; +setq(name, vec, p) + char *name, **vec; + register struct varent *p; { - register struct varent *vp; - - vp = adrof1(var, head); - if (vp == 0) { - vp = (struct varent *) calloc(1, sizeof *vp); - vp->name = savestr(var); - vp->link = shprev->link; - shprev->link = vp; + register struct varent *c; + register f; + + f = 0; /* tree hangs off the header's left link */ + while (c = p->v_link[f]) { + if ((f = *name - *c->v_name) == 0 && + (f = strcmp(name, c->v_name)) == 0) { + blkfree(c->vec); + goto found; + } + p = c; + f = f > 0; } - if (vp->vec) - blkfree(vp->vec); - scan(vec, trim); - vp->vec = vec; + p->v_link[f] = c = (struct varent *)xalloc(sizeof (struct varent)); + c->v_name = savestr(name); + c->v_bal = 0; + c->v_left = c->v_right = 0; + c->v_parent = p; + balance(p, f, 0); +found: + trim(c->vec = vec); } unset(v) - register char *v[]; + char *v[]; { unset1(v, &shvhed); @@ -463,47 +429,79 @@ unset(v) HIST = '!'; HISTSUB = '^'; } +#ifdef FILEC + if (adrof("filec") == 0) + filec = 0; +#endif } unset1(v, head) register char *v[]; struct varent *head; { - register char *var; register struct varent *vp; register int cnt; - v++; - while (var = *v++) { + while (*++v) { cnt = 0; - while (vp = madrof(var, head)) - unsetv1(vp->name, head), cnt++; + while (vp = madrof(*v, head->v_left)) + unsetv1(vp), cnt++; if (cnt == 0) - setname(var); + setname(*v); } } unsetv(var) char *var; { + register struct varent *vp; - unsetv1(var, &shvhed); + if ((vp = adrof1(var, &shvhed)) == 0) + udvar(var); + unsetv1(vp); } -unsetv1(var, head) - char *var; - struct varent *head; +unsetv1(p) + register struct varent *p; { - register struct varent *vp; - - vp = adrof1(var, head); - if (vp == 0) - udvar(var); - vp = shprev->link; - shprev->link = vp->link; - blkfree(vp->vec); - xfree(vp->name); - xfree((char *)vp); + register struct varent *c, *pp; + register f; + + /* + * Free associated memory first to avoid complications. + */ + blkfree(p->vec); + XFREE(p->v_name); + /* + * If p is missing one child, then we can move the other + * into where p is. Otherwise, we find the predecessor + * of p, which is guaranteed to have no right child, copy + * it into p, and move it's left child into it. + */ + if (p->v_right == 0) + c = p->v_left; + else if (p->v_left == 0) + c = p->v_right; + else { + for (c = p->v_left; c->v_right; c = c->v_right) + ; + p->v_name = c->v_name; + p->vec = c->vec; + p = c; + c = p->v_left; + } + /* + * Move c into where p is. + */ + pp = p->v_parent; + f = pp->v_right == p; + if (pp->v_link[f] = c) + c->v_parent = pp; + /* + * Free the deleted node, and rebalance. + */ + XFREE((char *)p); + balance(pp, f, 1); } setNS(cp) @@ -524,7 +522,7 @@ shift(v) if (name == 0) name = "argv"; else - strip(name); + (void) strip(name); argv = adrof(name); if (argv == 0) udvar(name); @@ -537,7 +535,6 @@ exportpath(val) char **val; { char exppath[BUFSIZ]; - register char *dir; exppath[0] = 0; if (val) @@ -546,10 +543,153 @@ char **val; printf("Warning: ridiculously long PATH truncated\n"); break; } - strcat(exppath, *val++); + (void) strcat(exppath, *val++); if (*val == 0 || eq(*val, ")")) break; - strcat(exppath, ":"); + (void) strcat(exppath, ":"); } setenv("PATH", exppath); } + + /* macros to do single rotations on node p */ +#define rright(p) (\ + t = (p)->v_left,\ + (t)->v_parent = (p)->v_parent,\ + ((p)->v_left = t->v_right) ? (t->v_right->v_parent = (p)) : 0,\ + (t->v_right = (p))->v_parent = t,\ + (p) = t) +#define rleft(p) (\ + t = (p)->v_right,\ + (t)->v_parent = (p)->v_parent,\ + ((p)->v_right = t->v_left) ? (t->v_left->v_parent = (p)) : 0,\ + (t->v_left = (p))->v_parent = t,\ + (p) = t) + +/* + * Rebalance a tree, starting at p and up. + * F == 0 means we've come from p's left child. + * D == 1 means we've just done a delete, otherwise an insert. + */ +balance(p, f, d) + register struct varent *p; + register f; +{ + register struct varent *pp; + register struct varent *t; /* used by the rotate macros */ + register ff; + + /* + * Ok, from here on, p is the node we're operating on; + * pp is it's parent; f is the branch of p from which we have come; + * ff is the branch of pp which is p. + */ + for (; pp = p->v_parent; p = pp, f = ff) { + ff = pp->v_right == p; + if (f ^ d) { /* right heavy */ + switch (p->v_bal) { + case -1: /* was left heavy */ + p->v_bal = 0; + break; + case 0: /* was balanced */ + p->v_bal = 1; + break; + case 1: /* was already right heavy */ + switch (p->v_right->v_bal) { + case 1: /* sigle rotate */ + pp->v_link[ff] = rleft(p); + p->v_left->v_bal = 0; + p->v_bal = 0; + break; + case 0: /* single rotate */ + pp->v_link[ff] = rleft(p); + p->v_left->v_bal = 1; + p->v_bal = -1; + break; + case -1: /* double rotate */ + rright(p->v_right); + pp->v_link[ff] = rleft(p); + p->v_left->v_bal = + p->v_bal < 1 ? 0 : -1; + p->v_right->v_bal = + p->v_bal > -1 ? 0 : 1; + p->v_bal = 0; + break; + } + break; + } + } else { /* left heavy */ + switch (p->v_bal) { + case 1: /* was right heavy */ + p->v_bal = 0; + break; + case 0: /* was balanced */ + p->v_bal = -1; + break; + case -1: /* was already left heavy */ + switch (p->v_left->v_bal) { + case -1: /* single rotate */ + pp->v_link[ff] = rright(p); + p->v_right->v_bal = 0; + p->v_bal = 0; + break; + case 0: /* signle rotate */ + pp->v_link[ff] = rright(p); + p->v_right->v_bal = -1; + p->v_bal = 1; + break; + case 1: /* double rotate */ + rleft(p->v_left); + pp->v_link[ff] = rright(p); + p->v_left->v_bal = + p->v_bal < 1 ? 0 : -1; + p->v_right->v_bal = + p->v_bal > -1 ? 0 : 1; + p->v_bal = 0; + break; + } + break; + } + } + /* + * If from insert, then we terminate when p is balanced. + * If from delete, then we terminate when p is unbalanced. + */ + if ((p->v_bal == 0) ^ d) + break; + } +} + +plist(p) + register struct varent *p; +{ + register struct varent *c; + register len; + + if (setintr) + (void) sigsetmask(sigblock(0) & ~ sigmask(SIGINT)); + for (;;) { + while (p->v_left) + p = p->v_left; + x: + if (p->v_parent == 0) /* is it the header? */ + return; + len = blklen(p->vec); + printf(p->v_name); + putchar('\t'); + if (len != 1) + putchar('('); + blkpr(p->vec); + if (len != 1) + putchar(')'); + putchar('\n'); + if (p->v_right) { + p = p->v_right; + continue; + } + do { + c = p; + p = p->v_parent; + } while (p->v_right == c); + goto x; + } +} diff --git a/usr/src/bin/csh/time.c b/usr/src/bin/csh/time.c index b507f45ab1..1e4c529205 100644 --- a/usr/src/bin/csh/time.c +++ b/usr/src/bin/csh/time.c @@ -1,5 +1,5 @@ #ifndef lint -static char sccsid[] = "@(#)time.c 4.5 (Berkeley) %G%"; +static char *sccsid = "@(#)time.c 4.6 (Berkeley) %G%"; #endif #include "sh.h" @@ -7,16 +7,14 @@ static char sccsid[] = "@(#)time.c 4.5 (Berkeley) %G%"; /* * C Shell - routines handling process timing and niceing */ -struct tms times0; -struct tms timesdol; settimes() { struct rusage ruch; - gettimeofday(&time0, (struct timezone *)0); - getrusage(RUSAGE_SELF, &ru0); - getrusage(RUSAGE_CHILDREN, &ruch); + (void) gettimeofday(&time0, (struct timezone *)0); + (void) getrusage(RUSAGE_SELF, &ru0); + (void) getrusage(RUSAGE_CHILDREN, &ruch); ruadd(&ru0, &ruch); } @@ -29,10 +27,10 @@ dotime() struct timeval timedol; struct rusage ru1, ruch; - getrusage(RUSAGE_SELF, &ru1); - getrusage(RUSAGE_CHILDREN, &ruch); + (void) getrusage(RUSAGE_SELF, &ru1); + (void) getrusage(RUSAGE_CHILDREN, &ruch); ruadd(&ru1, &ruch); - gettimeofday(&timedol, (struct timezone *)0); + (void) gettimeofday(&timedol, (struct timezone *)0); prusage(&ru0, &ru1, &timedol, &time0); } @@ -101,7 +99,7 @@ prusage(r0, r1, e, b) break; case 'E': - psecs(ms / 100); + psecs((long)(ms / 100)); break; case 'P': -- 2.20.1