can't use environ SHELL as variable shell
[unix-history] / usr / src / bin / csh / csh.c
index 7db93cf..35b9747 100644 (file)
@@ -1,4 +1,6 @@
-static char *sccsid = "@(#)csh.c 4.18 %G%";
+#ifndef lint
+static char *sccsid = "@(#)csh.c       4.21 (Berkeley) %G%";
+#endif
 
 #include "sh.h"
 #include <sys/ioctl.h>
 
 #include "sh.h"
 #include <sys/ioctl.h>
@@ -23,6 +25,7 @@ bool  nverbose;
 bool   nexececho;
 bool   quitit;
 bool   fast;
 bool   nexececho;
 bool   quitit;
 bool   fast;
+bool   batch;
 bool   prompt = 1;
 bool   enterhist = 0;
 
 bool   prompt = 1;
 bool   enterhist = 0;
 
@@ -41,7 +44,7 @@ main(c, av)
        uid = getuid();
        loginsh = **v == '-' && c == 1;
        if (loginsh)
        uid = getuid();
        loginsh = **v == '-' && c == 1;
        if (loginsh)
-               time(&chktim);
+               (void) time(&chktim);
 
        /*
         * Move the descriptors to safe places.
 
        /*
         * Move the descriptors to safe places.
@@ -77,28 +80,8 @@ main(c, av)
         */
        if ((cp = getenv("PATH")) == NOSTR)
                set1("path", saveblk(pathlist), &shvhed);
         */
        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);
-       }
+       else
+               importpath(cp);
        set("shell", SHELLPATH);
 
        doldol = putn(getpid());                /* For $$ */
        set("shell", SHELLPATH);
 
        doldol = putn(getpid());                /* For $$ */
@@ -111,14 +94,16 @@ main(c, av)
         * Our children inherit termination from our parent.
         * We catch it only if we are the login shell.
         */
         * 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;
        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) {
        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 */
        }
 
        /*
        }
 
        /*
@@ -126,16 +111,15 @@ main(c, av)
         *
         * Note that processing of -v/-x is actually delayed till after
         * script processing.
         *
         * Note that processing of -v/-x is actually delayed till after
         * script processing.
-        *
-        * We set the first character of our name to be '-' if we are
-        * a shell running interruptible commands.  Many programs which
-        * examine ps'es use this to filter such shells out.
         */
        c--, v++;
         */
        c--, v++;
-       while (c > 0 && (cp = v[0])[0] == '-') {
-               cp++;
+       while (c > 0 && (cp = v[0])[0] == '-' && *++cp != '\0' && !batch) {
                do switch (*cp++) {
 
                do switch (*cp++) {
 
+               case 'b':               /* -b   Next arg is input file */
+                       batch++;
+                       break;
+
                case 'c':               /* -c   Command input from arg */
                        if (c == 1)
                                exit(0);
                case 'c':               /* -c   Command input from arg */
                        if (c == 1)
                                exit(0);
@@ -197,10 +181,10 @@ main(c, av)
        }
 
        if (quitit)                     /* With all due haste, for debugging */
        }
 
        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
+        * Unless prevented by -c, -i, -s, or -t, if there
         * are remaining arguments the first of them is the name
         * of a shell file from which to read commands.
         */
         * are remaining arguments the first of them is the name
         * of a shell file from which to read commands.
         */
@@ -212,9 +196,15 @@ main(c, av)
                }
                file = v[0];
                SHIN = dmove(nofile, FSHIN);    /* Replace FSHIN */
                }
                file = v[0];
                SHIN = dmove(nofile, FSHIN);    /* Replace FSHIN */
+               (void) ioctl(SHIN, FIOCLEX, (char *)0);
                prompt = 0;
                c--, v++;
        }
                prompt = 0;
                c--, v++;
        }
+       if (!batch && uid != geteuid()) {
+               errno = EACCES;
+               child++;                        /* So this ... */
+               Perror("csh");                  /* ... doesn't return */
+       }
        /*
         * Consider input a tty if it really is or we are interactive.
         */
        /*
         * Consider input a tty if it really is or we are interactive.
         */
@@ -253,14 +243,14 @@ main(c, av)
        if (setintr) {
                **av = '-';
                if (!quitit)            /* Wary! */
        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) {
                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
                        /*
                         * Wait till in foreground, in case someone
                         * stupidly runs
@@ -276,31 +266,33 @@ main(c, av)
                        else
                                f = -1;
 retry:
                        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);
                                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;
                                }
                                        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;
                                        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;
                                } 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");
                        } else {
 notty:
   printf("Warning: no access to tty; thus no job control in this shell...\n");
@@ -310,7 +302,7 @@ notty:
        }
        if (setintr == 0 && parintr == SIG_DFL)
                setintr++;
        }
        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
 
        /*
         * Set an exit here in case of an interrupt or error reading
@@ -322,7 +314,7 @@ notty:
                reenter++;
                /* Will have value("home") here because set fast if don't */
                srccat(value("home"), "/.cshrc");
                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) {
                        dohash();
                dosource(loadhist);
                if (loginsh) {
@@ -351,7 +343,7 @@ notty:
         */
        if (loginsh) {
                printf("logout\n");
         */
        if (loginsh) {
                printf("logout\n");
-               close(SHIN);
+               (void) close(SHIN);
                child++;
                goodbye();
        }
                child++;
                goodbye();
        }
@@ -363,13 +355,13 @@ untty()
 {
 
        if (tpgrp > 0) {
 {
 
        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
                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);
                }
        }
 }
                }
        }
 }
@@ -391,7 +383,7 @@ importpath(cp)
         * There are i+1 directories in the path plus we need
         * room for a zero terminator.
         */
         * 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)
        dp = cp;
        i = 0;
        if (*dp)
@@ -420,7 +412,7 @@ srccat(cp, dp)
        register char *ep = strspl(cp, dp);
        register int unit = dmove(open(ep, 0), -1);
 
        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);
        xfree(ep);
 #ifdef INGRES
        srcunit(unit, 0, 0);
@@ -465,7 +457,7 @@ srcunit(unit, onlyown, hflg)
 
                if (fstat(unit, &stb) < 0 ||
                    (stb.st_uid != uid && stb.st_gid != getgid())) {
 
                if (fstat(unit, &stb) < 0 ||
                    (stb.st_uid != uid && stb.st_gid != getgid())) {
-                       close(unit);
+                       (void) close(unit);
                        return;
                }
        }
                        return;
                }
        }
@@ -503,14 +495,14 @@ srcunit(unit, onlyown, hflg)
                 * we let ourselves be interrupted.
                 */
                if (setintr)
                 * 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)
 #ifdef TELL
                settell();
 #endif
                process(0);             /* 0 -> blow away on errors */
        }
        if (setintr)
-               sigsetmask(omask);
+               (void) sigsetmask(omask);
        if (oSHIN >= 0) {
                register int i;
 
        if (oSHIN >= 0) {
                register int i;
 
@@ -523,7 +515,7 @@ srcunit(unit, onlyown, hflg)
                /* Reset input arena */
                copy((char *)&B, (char *)&saveB, sizeof B);
 
                /* 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;
                arginp = oarginp, onelflg = oonelflg;
                evalp = oevalp, evalvec = oevalvec;
                intty = oldintty, whyles = oldwhyl, gointr = ogointr;
@@ -552,8 +544,8 @@ rechist()
        if (!fast) {
                if (value("savehist")[0] == '\0')
                        return;
        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;
                fp = creat(buf, 0666);
                if (fp == -1)
                        return;
@@ -561,10 +553,10 @@ rechist()
                didfds = 0;
                ftmp = SHOUT;
                SHOUT = fp;
                didfds = 0;
                ftmp = SHOUT;
                SHOUT = fp;
-               strcpy(buf, value("savehist"));
+               (void) strcpy(buf, value("savehist"));
                dumphist[2] = buf;
                dohist(dumphist);
                dumphist[2] = buf;
                dohist(dumphist);
-               close(fp);
+               (void) close(fp);
                SHOUT = ftmp;
                didfds = oldidfds;
        }
                SHOUT = ftmp;
                didfds = oldidfds;
        }
@@ -573,9 +565,9 @@ rechist()
 goodbye()
 {
        if (loginsh) {
 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");
                setintr = 0;            /* No interrupts after "logout" */
                if (adrof("home"))
                        srccat(value("home"), "/.logout");
@@ -587,6 +579,9 @@ goodbye()
 exitstat()
 {
 
 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.
        /*
         * Note that if STATUS is corrupted (i.e. getn bombs)
         * then error will exit directly because we poke child here.
@@ -626,7 +621,7 @@ pintr1(wantnl)
 
        omask = sigblock(0);
        if (setintr) {
 
        omask = sigblock(0);
        if (setintr) {
-               sigsetmask(omask & ~sigmask(SIGINT));
+               (void) sigsetmask(omask & ~sigmask(SIGINT));
                if (pjobs) {
                        pjobs = 0;
                        printf("\n");
                if (pjobs) {
                        pjobs = 0;
                        printf("\n");
@@ -634,7 +629,7 @@ pintr1(wantnl)
                        bferr("Interrupted");
                }
        }
                        bferr("Interrupted");
                }
        }
-       sigsetmask(omask & ~sigmask(SIGCHLD));
+       (void) sigsetmask(omask & ~sigmask(SIGCHLD));
        draino();
 
        /*
        draino();
 
        /*
@@ -672,9 +667,8 @@ pintr1(wantnl)
 process(catch)
        bool catch;
 {
 process(catch)
        bool catch;
 {
-       register char *cp;
        jmp_buf osetexit;
        jmp_buf osetexit;
-       struct command *t;
+       register struct command *t;
 
        getexit(osetexit);
        for (;;) {
 
        getexit(osetexit);
        for (;;) {
@@ -689,7 +683,7 @@ process(catch)
                 * Interruptible during interactive reads
                 */
                if (setintr)
                 * Interruptible during interactive reads
                 */
                if (setintr)
-                       sigsetmask(sigblock(0) & ~sigmask(SIGINT));
+                       (void) sigsetmask(sigblock(0) & ~sigmask(SIGINT));
 
                /*
                 * For the sake of reset()
 
                /*
                 * For the sake of reset()
@@ -734,7 +728,6 @@ process(catch)
                         */
                        if (fseekp == feobp)
                                printprompt();
                         */
                        if (fseekp == feobp)
                                printprompt();
-                       flush();
                }
                err = 0;
 
                }
                err = 0;
 
@@ -753,7 +746,7 @@ process(catch)
                 * The parser may lose space if interrupted.
                 */
                if (setintr)
                 * The parser may lose space if interrupted.
                 */
                if (setintr)
-                       sigblock(sigmask(SIGINT));
+                       (void) sigblock(sigmask(SIGINT));
 
                /*
                 * Save input text on the history list if 
 
                /*
                 * Save input text on the history list if 
@@ -813,12 +806,13 @@ dosource(t)
                t++;
                hflg++;
        }
                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);
        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);
 }
 
        srcunit(u, 0, hflg);
 }
 
@@ -843,7 +837,7 @@ mailchk()
        v = adrof("mail");
        if (v == 0)
                return;
        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;
        vp = v->vec;
        cnt = blklen(vp);
        intvl = (cnt && number(*vp)) ? (--cnt, getn(*vp++)) : MAILINTVL;
@@ -881,7 +875,7 @@ gethdir(home)
 
        if (pp == 0)
                return (1);
 
        if (pp == 0)
                return (1);
-       strcpy(home, pp->pw_dir);
+       (void) strcpy(home, pp->pw_dir);
        return (0);
 }
 
        return (0);
 }
 
@@ -892,12 +886,11 @@ gethdir(home)
 initdesc()
 {
 
 initdesc()
 {
 
-       didcch = 0;                     /* Havent closed for child */
        didfds = 0;                     /* 0, 1, 2 aren't set up */
        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();
 }
 
        closem();
 }
 
@@ -910,11 +903,7 @@ exit(i)
 {
 
        untty();
 {
 
        untty();
-#ifdef PROF
-       monitor(0);
-#else
        _exit(i);
        _exit(i);
-#endif
 }
 
 printprompt()
 }
 
 printprompt()