#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993\
The Regents of the University of California. All rights reserved.");
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)csh.c 8.2 (Berkeley) 10/12/93";
#else
__RCSID("$NetBSD: csh.c,v 1.56 2022/09/15 11:35:06 martin Exp $");
#endif
#endif
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <paths.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <vis.h>
#include "csh.h"
#include "extern.h"
#include "pathnames.h"
#include "proc.h"
FILE *cshin, *cshout, *csherr;
struct timespec time0;
struct rusage ru0;
struct varent shvhed, aliases;
Char HISTSUB;
int editing;
int child;
int chkstop;
int didfds;
int doneinp;
int exiterr;
int haderr;
int havhash;
int intact;
int intty;
int justpr;
int loginsh;
int neednote;
int noexec;
int pjobs;
int setintr;
int timflg;
Char *arginp;
Char *ffile;
int onelflg;
Char *shtemp;
time_t chktim;
Char *doldol;
pid_t backpid;
gid_t egid, gid;
uid_t euid, uid;
int shpgrp;
int tpgrp;
int opgrp;
int SHIN;
int SHOUT;
int SHERR;
int OLDSTD;
jmp_buf reslab;
Char *gointr;
sig_t parintr;
sig_t parterm;
struct Bin B;
struct Ain lineloc;
int cantell;
Char *lap;
struct whyle *whyles;
struct wordent *alhistp,*alhistt;
int AsciiOnly;
int gflag;
long pnleft;
Char *pargs;
Char *pargcp;
struct Hist Histlist;
struct wordent paraml;
int eventno;
int lastev;
Char HIST;
Char HISTSUB;
const char *bname;
Char *Vsav;
Char *Vdp;
Char *Vexpath;
char **Vt;
Char **evalvec;
Char *evalp;
Char *word_chars;
Char *STR_SHELLPATH;
#ifdef _PATH_BSHELL
Char *STR_BSHELL;
#endif
Char *STR_WORD_CHARS;
Char **STR_environ;
#ifdef EDIT
EditLine *el;
History *hi;
#endif
int editing;
Char *dumphist[] = {STRhistory, STRmh, 0, 0};
Char *tildehist[] = {STRsource, STRmh, STRtildothist, 0};
int nofile = 0;
int batch = 0;
int enterhist = 0;
int fast = 0;
int mflag = 0;
int nexececho = 0;
int nverbose = 0;
int prompt = 1;
int quitit = 0;
int reenter = 0;
extern char **environ;
static ssize_t readf(void *, void *, size_t);
static off_t seekf(void *, off_t, int);
static ssize_t writef(void *, const void *, size_t);
static int closef(void *);
static int srccat(Char *, Char *);
static int srcfile(const char *, int, int);
__dead static void phup(int);
static void srcunit(int, int, int);
static void mailchk(void);
#ifndef _PATH_DEFPATH
static Char **defaultpath(void);
#endif
int
main(int argc, char *argv[])
{
struct sigaction oact;
Char *cp;
char *tcp, **tempv;
const char *ecp;
sigset_t nsigset;
int f;
cshin = stdin;
cshout = stdout;
csherr = stderr;
setprogname(argv[0]);
settimes();
#ifdef _PATH_BSHELL
STR_BSHELL = SAVE(_PATH_BSHELL);
#endif
#ifdef _PATH_CSHELL
STR_SHELLPATH = SAVE(_PATH_CSHELL);
#endif
STR_environ = blk2short(environ);
environ = short2blk(STR_environ);
STR_WORD_CHARS = SAVE(WORD_CHARS);
HIST = '!';
HISTSUB = '^';
word_chars = STR_WORD_CHARS;
tempv = argv;
if (eq(str2short(tempv[0]), STRaout))
quitit = 1;
uid = getuid();
gid = getgid();
euid = geteuid();
egid = getegid();
loginsh = (**tempv == '-' && argc == 1) ||
(argc == 2 && tempv[1][0] == '-' && tempv[1][1] == 'l' &&
tempv[1][2] == '\0');
if (loginsh && **tempv != '-') {
tempv[1][0] = '\0';
tempv[1][1] = '\0';
tempv[1] = NULL;
for (tcp = *tempv; *tcp++;)
continue;
for (tcp--; tcp >= *tempv; tcp--)
tcp[1] = tcp[0];
*++tcp = '-';
argc--;
}
if (loginsh)
(void)time(&chktim);
AsciiOnly = 1;
#ifdef NLS
(void)setlocale(LC_ALL, "");
{
int k;
for (k = 0200; k <= 0377 && !Isprint(k); k++)
continue;
AsciiOnly = k > 0377;
}
#else
AsciiOnly = getenv("LANG") == NULL && getenv("LC_CTYPE") == NULL;
#endif
initdesc();
(void)fclose(cshin);
(void)fclose(cshout);
(void)fclose(csherr);
if (!(cshin = funopen2((void *) &SHIN, readf, writef, seekf, NULL,
closef)))
exit(1);
if (!(cshout = funopen2((void *) &SHOUT, readf, writef, seekf, NULL,
closef)))
exit(1);
if (!(csherr = funopen2((void *) &SHERR, readf, writef, seekf, NULL,
closef)))
exit(1);
(void)setvbuf(cshin, NULL, _IOLBF, 0);
(void)setvbuf(cshout, NULL, _IOLBF, 0);
(void)setvbuf(csherr, NULL, _IOLBF, 0);
set(STRstatus, Strsave(STR0));
if ((ecp = getenv("HOME")) != NULL)
cp = quote(SAVE(ecp));
else
cp = NULL;
if (cp == NULL)
fast = 1;
else
set(STRhome, cp);
dinit(cp);
if ((ecp = getenv("LOGNAME")) != NULL ||
(ecp = getenv("USER")) != NULL)
set(STRuser, quote(SAVE(ecp)));
if ((ecp = getenv("TERM")) != NULL)
set(STRterm, quote(SAVE(ecp)));
if ((ecp = getenv("PATH")) == NULL) {
#ifdef _PATH_DEFPATH
importpath(str2short(_PATH_DEFPATH));
#else
setq(STRpath, defaultpath(), &shvhed);
#endif
} else {
importpath(str2short(ecp));
}
set(STRshell, Strsave(STR_SHELLPATH));
doldol = putn((int) getpid());
shtemp = Strspl(STRtmpsh, doldol);
(void)sigaction(SIGINT, NULL, &oact);
parintr = oact.sa_handler;
(void)sigaction(SIGTERM, NULL, &oact);
parterm = oact.sa_handler;
(void)signal(SIGHUP, phup);
(void)signal(SIGXCPU, phup);
(void)signal(SIGXFSZ, phup);
argc--, tempv++;
while (argc > 0 && (tcp = tempv[0])[0] == '-' && *++tcp != '\0' && !batch) {
do
switch (*tcp++) {
case 0:
prompt = 0;
setintr = 1;
nofile = 1;
break;
case 'b':
batch = 1;
break;
case 'c':
if (argc == 1)
xexit(0);
argc--, tempv++;
arginp = SAVE(tempv[0]);
prompt = 0;
nofile = 1;
break;
case 'e':
exiterr = 1;
break;
case 'f':
fast = 1;
break;
case 'i':
intact = 1;
nofile = 1;
break;
case 'm':
mflag = 1;
break;
case 'n':
noexec = 1;
break;
case 'q':
quitit = 1;
break;
case 's':
nofile = 1;
break;
case 't':
onelflg = 2;
prompt = 0;
nofile = 1;
break;
case 'v':
nverbose = 1;
break;
case 'x':
nexececho = 1;
break;
case 'V':
setNS(STRverbose);
break;
case 'X':
setNS(STRecho);
break;
} while (*tcp);
tempv++, argc--;
}
if (quitit)
(void)signal(SIGQUIT, SIG_DFL);
if (nofile == 0 && argc > 0) {
nofile = open(tempv[0], O_RDONLY);
if (nofile < 0) {
child = 1;
stderror(ERR_SYSTEM, tempv[0], strerror(errno));
}
ffile = SAVE(tempv[0]);
if ((SHIN = dmove(nofile, FSHIN)) < 0)
switch(nofile) {
case 0:
SHIN = FSHIN;
break;
case 1:
SHIN = FSHOUT;
break;
case 2:
SHIN = FSHERR;
break;
default:
stderror(ERR_SYSTEM, tempv[0], strerror(errno));
}
(void)ioctl(SHIN, FIOCLEX, NULL);
prompt = 0;
tempv++;
}
intty = isatty(SHIN);
intty |= intact;
if (intty || (intact && isatty(SHOUT))) {
if (!batch && (uid != euid || gid != egid)) {
errno = EACCES;
child = 1;
stderror(ERR_SYSTEM, "csh", strerror(errno));
}
}
if (loginsh || intact || (intty && isatty(SHOUT)))
setintr = 1;
settell();
setq(STRargv, blk2short(tempv), &shvhed);
if (prompt) {
set(STRprompt, Strsave(uid == 0 ? STRsymhash : STRsymcent));
set(STRprompt2, Strsave(STRmquestion));
}
shpgrp = getpgrp();
opgrp = tpgrp = -1;
if (setintr) {
**argv = '-';
if (!quitit)
(void)signal(SIGQUIT, SIG_IGN);
(void)signal(SIGINT, pintr);
sigemptyset(&nsigset);
(void)sigaddset(&nsigset, SIGINT);
(void)sigprocmask(SIG_BLOCK, &nsigset, NULL);
(void)signal(SIGTERM, SIG_IGN);
if (quitit == 0 && arginp == 0) {
(void)signal(SIGTSTP, SIG_IGN);
(void)signal(SIGTTIN, SIG_IGN);
(void)signal(SIGTTOU, SIG_IGN);
if (isatty(FSHERR))
f = FSHERR;
else if (isatty(FSHOUT))
f = FSHOUT;
else if (isatty(OLDSTD))
f = OLDSTD;
else
f = -1;
retry:
if ((tpgrp = tcgetpgrp(f)) != -1) {
if (tpgrp != shpgrp) {
sig_t old = signal(SIGTTIN, SIG_DFL);
(void)kill(0, SIGTTIN);
(void)signal(SIGTTIN, old);
goto retry;
}
opgrp = shpgrp;
shpgrp = getpid();
tpgrp = shpgrp;
if (opgrp != shpgrp)
if (setpgid(0, shpgrp) == -1)
goto notty;
if (tcsetpgrp(f, shpgrp) == -1)
goto notty;
(void)ioctl(dcopy(f, FSHTTY), FIOCLEX, NULL);
}
if (tpgrp == -1) {
notty:
(void)fprintf(csherr, "Warning: no access to tty (%s).\n",
strerror(errno));
(void)fprintf(csherr, "Thus no job control in this shell.\n");
}
}
}
if ((setintr == 0) && (parintr == SIG_DFL))
setintr = 1;
(void)signal(SIGCHLD, pchild);
reenter = setexit();
haderr = 0;
if (!fast && reenter == 0) {
{
sig_t oparintr;
sigset_t osigset;
int osetintr;
oparintr = parintr;
osetintr = setintr;
sigemptyset(&nsigset);
(void)sigaddset(&nsigset, SIGINT);
(void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
setintr = 0;
parintr = SIG_IGN;
#ifdef _PATH_DOTCSHRC
(void)srcfile(_PATH_DOTCSHRC, 0, 0);
#endif
if (!fast && !arginp && !onelflg)
dohash(NULL, NULL);
#ifdef _PATH_DOTLOGIN
if (loginsh)
(void)srcfile(_PATH_DOTLOGIN, 0, 0);
#endif
(void)sigprocmask(SIG_SETMASK, &osigset, NULL);
setintr = osetintr;
parintr = oparintr;
}
(void)srccat(value(STRhome), STRsldotcshrc);
if (!fast && !arginp && !onelflg && !havhash)
dohash(NULL, NULL);
if ((cp = value(STRhistfile)) != STRNULL)
tildehist[2] = cp;
dosource(tildehist, NULL);
if (loginsh)
(void)srccat(value(STRhome), STRsldotlogin);
}
if (nverbose)
setNS(STRverbose);
if (nexececho)
setNS(STRecho);
process(setintr);
if (intty) {
if (loginsh) {
(void)fprintf(cshout, "logout\n");
(void)close(SHIN);
child = 1;
goodbye();
}
else {
(void)fprintf(cshout, "exit\n");
}
}
rechist();
exitstat();
}
void
untty(void)
{
if (tpgrp > 0) {
(void)setpgid(0, opgrp);
(void)tcsetpgrp(FSHTTY, opgrp);
}
}
void
importpath(Char *cp)
{
Char *dp, **pv;
int c, i;
i = 0;
for (dp = cp; *dp; dp++)
if (*dp == ':')
i++;
pv = xcalloc((size_t) (i + 2), sizeof(*pv));
dp = cp;
i = 0;
if (*dp)
for (;;) {
if ((c = *dp) == ':' || c == 0) {
*dp = 0;
pv[i++] = Strsave(*cp ? cp : STRdot);
if (c) {
cp = dp + 1;
*dp = ':';
}
else
break;
}
dp++;
}
pv[i] = 0;
setq(STRpath, pv, &shvhed);
}
static int
srccat(Char *cp, Char *dp)
{
Char *ep;
char *ptr;
ep = Strspl(cp, dp);
ptr = short2str(ep);
free(ep);
return srcfile(ptr, mflag ? 0 : 1, 0);
}
static int
srcfile(const char *f, int onlyown, int flag)
{
int unit;
if ((unit = open(f, O_RDONLY)) == -1)
return 0;
unit = dmove(unit, -1);
(void) ioctl(unit, FIOCLEX, NULL);
srcunit(unit, onlyown, flag);
return 1;
}
int insource;
static void
srcunit(int unit, int onlyown, int hflg)
{
struct whyle *oldwhyl;
struct Bin saveB;
sigset_t nsigset, osigset;
jmp_buf oldexit;
Char *oarginp, *oevalp, **oevalvec, *ogointr;
Char OHIST;
int oSHIN, oinsource, oldintty, oonelflg;
int oenterhist, otell;
int my_reenter;
oSHIN = -1;
oldintty = intty;
oinsource = insource;
oldwhyl = whyles;
ogointr = gointr;
oarginp = arginp;
oevalp = evalp;
oevalvec = evalvec;
oonelflg = onelflg;
oenterhist = enterhist;
OHIST = HIST;
otell = cantell;
if (unit < 0)
return;
if (didfds)
donefds();
if (onlyown) {
struct stat stb;
if (fstat(unit, &stb) < 0) {
(void)close(unit);
return;
}
}
insource = 1;
getexit(oldexit);
if (setintr) {
sigemptyset(&nsigset);
(void)sigaddset(&nsigset, SIGINT);
(void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
}
(void)memcpy(&saveB, &B, sizeof(B));
fbuf = NULL;
fseekp = feobp = fblocks = 0;
oSHIN = SHIN, SHIN = unit, arginp = 0, onelflg = 0;
intty = isatty(SHIN), whyles = 0, gointr = 0;
evalvec = 0;
evalp = 0;
enterhist = hflg;
if (enterhist)
HIST = '\0';
if (setintr)
(void)sigprocmask(SIG_SETMASK, &osigset, NULL);
settell();
if ((my_reenter = setexit()) == 0)
process(0);
if (setintr)
(void)sigprocmask(SIG_SETMASK, &osigset, NULL);
if (oSHIN >= 0) {
int i;
for (i = 0; i < fblocks; i++)
free(fbuf[i]);
free(fbuf);
(void)memcpy(&B, &saveB, sizeof(B));
(void)close(SHIN), SHIN = oSHIN;
arginp = oarginp, onelflg = oonelflg;
evalp = oevalp, evalvec = oevalvec;
intty = oldintty, whyles = oldwhyl, gointr = ogointr;
if (enterhist)
HIST = OHIST;
enterhist = oenterhist;
cantell = otell;
}
resexit(oldexit);
if (my_reenter)
stderror(ERR_SILENT);
insource = oinsource;
}
void
rechist(void)
{
Char buf[BUFSIZE], hbuf[BUFSIZE], *hfile;
int fp, ftmp, oldidfds;
struct varent *shist;
if (!fast) {
if ((shist = adrof(STRsavehist)) != NULL) {
if (shist->vec[0][0] != '\0')
(void)Strcpy(hbuf, shist->vec[0]);
else if ((shist = adrof(STRhistory)) && shist->vec[0][0] != '\0')
(void)Strcpy(hbuf, shist->vec[0]);
else
return;
}
else
return;
if ((hfile = value(STRhistfile)) == STRNULL) {
hfile = Strcpy(buf, value(STRhome));
(void) Strcat(buf, STRsldthist);
}
if ((fp = open(short2str(hfile), O_WRONLY | O_CREAT | O_TRUNC,
0600)) == -1)
return;
oldidfds = didfds;
didfds = 0;
ftmp = SHOUT;
SHOUT = fp;
dumphist[2] = hbuf;
dohist(dumphist, NULL);
SHOUT = ftmp;
(void)close(fp);
didfds = oldidfds;
}
}
void
goodbye(void)
{
rechist();
if (loginsh) {
(void)signal(SIGQUIT, SIG_IGN);
(void)signal(SIGINT, SIG_IGN);
(void)signal(SIGTERM, SIG_IGN);
setintr = 0;
if (!(adrof(STRlogout)))
set(STRlogout, STRnormal);
#ifdef _PATH_DOTLOGOUT
(void)srcfile(_PATH_DOTLOGOUT, 0, 0);
#endif
if (adrof(STRhome))
(void)srccat(value(STRhome), STRsldtlogout);
}
exitstat();
}
__dead void
exitstat(void)
{
Char *s;
#ifdef PROF
monitor(0);
#endif
child = 1;
s = value(STRstatus);
xexit(s ? getn(s) : 0);
}
static void
phup(int sig)
{
rechist();
{
struct process *pp, *np;
for (pp = proclist.p_next; pp; pp = pp->p_next) {
np = pp;
do
if ((np->p_flags & PFOREGND) != 0 && np->p_jobid != shpgrp &&
kill(-np->p_jobid, SIGHUP) != -1) {
(void)kill(-np->p_jobid, SIGCONT);
break;
}
while ((np = np->p_friends) != pp);
}
}
xexit(sig);
}
Char *jobargv[2] = {STRjobs, 0};
void
pintr(int notused)
{
pintr1(1);
}
void
pintr1(int wantnl)
{
Char **v;
sigset_t nsigset, osigset;
sigemptyset(&nsigset);
(void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
if (setintr) {
nsigset = osigset;
(void)sigdelset(&nsigset, SIGINT);
(void)sigprocmask(SIG_SETMASK, &nsigset, NULL);
if (pjobs) {
pjobs = 0;
(void)fprintf(cshout, "\n");
dojobs(jobargv, NULL);
stderror(ERR_NAME | ERR_INTR);
}
}
(void)sigdelset(&osigset, SIGCHLD);
(void)sigprocmask(SIG_SETMASK, &osigset, NULL);
(void)fpurge(cshout);
(void)endpwent();
if (gointr) {
gotolab(gointr);
timflg = 0;
if ((v = pargv) != NULL)
pargv = 0, blkfree(v);
if ((v = gargv) != NULL)
gargv = 0, blkfree(v);
reset();
}
else if (intty && wantnl) {
(void)fputc('\r', cshout);
(void)fputc('\n', cshout);
}
stderror(ERR_SILENT);
}
static struct command *savet = NULL;
void
process(int catch)
{
struct command *t;
jmp_buf osetexit;
sigset_t nsigset;
t = savet;
savet = NULL;
getexit(osetexit);
for (;;) {
pendjob();
paraml.next = paraml.prev = ¶ml;
paraml.word = STRNULL;
(void)setexit();
justpr = enterhist;
if (setintr) {
sigemptyset(&nsigset);
(void)sigaddset(&nsigset, SIGINT);
(void)sigprocmask(SIG_UNBLOCK, &nsigset, NULL);
}
freelex(¶ml);
if (savet)
freesyn(savet), savet = NULL;
if (haderr) {
if (!catch) {
doneinp = 0;
resexit(osetexit);
savet = t;
reset();
}
haderr = 0;
closem();
continue;
}
if (doneinp) {
doneinp = 0;
break;
}
if (chkstop)
chkstop--;
if (neednote)
pnote();
if (intty && prompt && evalvec == 0) {
mailchk();
#ifdef EDIT
updateediting();
#endif
if (aret == F_SEEK && fseekp == feobp)
printprompt();
(void)fflush(cshout);
}
if (seterr) {
free(seterr);
seterr = NULL;
}
if ((lex(¶ml) && !seterr && intty) || adrof(STRverbose)) {
int odidfds = didfds;
fflush(csherr);
didfds = 0;
prlex(csherr, ¶ml);
fflush(csherr);
didfds = odidfds;
}
if (setintr)
(void)sigprocmask(SIG_BLOCK, &nsigset, NULL);
if (enterhist || (catch && intty && !whyles))
savehist(¶ml);
if (!enterhist && seterr)
stderror(ERR_OLD);
if (justpr)
reset();
alias(¶ml);
savet = syntax(paraml.next, ¶ml, 0);
if (seterr)
stderror(ERR_OLD);
execute(savet, (tpgrp > 0 ? tpgrp : -1), NULL, NULL);
freelex(¶ml);
freesyn(savet), savet = NULL;
}
resexit(osetexit);
savet = t;
}
void
dosource(Char **v, struct command *t)
{
Char buf[BUFSIZE], *f;
int hflg;
hflg = 0;
v++;
if (*v && eq(*v, STRmh)) {
if (*++v == NULL)
stderror(ERR_NAME | ERR_HFLAG);
hflg++;
}
(void)Strcpy(buf, *v);
f = globone(buf, G_ERROR);
(void)strcpy((char *)buf, short2str(f));
free(f);
if (!srcfile((char *)buf, 0, hflg) && !hflg)
stderror(ERR_SYSTEM, (char *)buf, strerror(errno));
}
static void
mailchk(void)
{
struct stat stb;
struct varent *v;
Char **vp;
time_t t;
int cnt, intvl;
int new;
v = adrof(STRmail);
if (v == 0)
return;
(void)time(&t);
vp = v->vec;
cnt = blklen(vp);
intvl = (cnt && number(*vp)) ? (--cnt, getn(*vp++)) : MAILINTVL;
if (intvl < 1)
intvl = 1;
if (chktim + intvl > t)
return;
for (; *vp; vp++) {
if (stat(short2str(*vp), &stb) < 0)
continue;
new = stb.st_mtime > time0.tv_sec;
if (stb.st_size == 0 || stb.st_atime > stb.st_mtime ||
(stb.st_atime < chktim && stb.st_mtime < chktim) ||
(loginsh && !new))
continue;
if (cnt == 1)
(void)fprintf(cshout, "You have %smail.\n", new ? "new " : "");
else
(void)fprintf(cshout, "%s in %s.\n", new ? "New mail" : "Mail",
vis_str(*vp));
}
chktim = t;
}
int
gethdir(Char *home)
{
struct passwd *pw;
Char *h;
if (*home == '\0') {
if ((h = value(STRhome)) != NULL) {
(void)Strcpy(home, h);
return 0;
}
else
return 1;
}
if ((pw = getpwnam(short2str(home))) != NULL) {
(void)Strcpy(home, str2short(pw->pw_dir));
return 0;
}
else
return 1;
}
#define DESC(a) (*((int *) (a)) - (didfds && *((int *) a) >= FSHIN ? FSHIN : 0))
static ssize_t
readf(void *oreo, void *buf, size_t siz)
{
return read(DESC(oreo), buf, siz);
}
static ssize_t
writef(void *oreo, const void *buf, size_t siz)
{
return write(DESC(oreo), buf, siz);
}
static off_t
seekf(void *oreo, off_t off, int whence)
{
return lseek(DESC(oreo), off, whence);
}
static int
closef(void *oreo)
{
return close(DESC(oreo));
}
int
vis_fputc(int ch, FILE *fp)
{
char uenc[5];
if (ch & QUOTE)
return fputc(ch & TRIM, fp);
(void)vis(uenc, ch & TRIM, VIS_NOSLASH, 0);
return (fputs(uenc, fp));
}
void
initdesc(void)
{
didfds = 0;
(void)ioctl(SHIN = dcopy(0, FSHIN), FIOCLEX, NULL);
(void)ioctl(SHOUT = dcopy(1, FSHOUT), FIOCLEX, NULL);
(void)ioctl(SHERR = dcopy(2, FSHERR), FIOCLEX, NULL);
(void)ioctl(OLDSTD = dcopy(SHIN, FOLDSTD), FIOCLEX, NULL);
closem();
}
__dead void
#ifdef PROF
done(int i)
#else
xexit(int i)
#endif
{
untty();
_exit(i);
}
#ifndef _PATH_DEFPATH
static Char **
defaultpath(void)
{
struct stat stb;
Char **blk, **blkp;
char *ptr;
blkp = blk = xmalloc((size_t) sizeof(Char *) * 10);
#define DIRAPPEND(a) \
if (stat(ptr = a, &stb) == 0 && S_ISDIR(stb.st_mode)) \
*blkp++ = SAVE(ptr)
#ifdef RESCUEDIR
DIRAPPEND(RESCUEDIR);
#endif
DIRAPPEND(_PATH_BIN);
DIRAPPEND(_PATH_USRBIN);
#undef DIRAPPEND
#if 0
if (euid != 0 && uid != 0)
*blkp++ = Strsave(STRdot);
#endif
*blkp = NULL;
return (blk);
}
#endif
void
printprompt(void)
{
Char *cp;
if (editing)
return;
if (!whyles) {
for (cp = value(STRprompt); *cp; cp++)
if (*cp == HIST)
(void)fprintf(cshout, "%d", eventno + 1);
else {
if (*cp == '\\' && cp[1] == HIST)
cp++;
(void)vis_fputc(*cp | QUOTE, cshout);
}
}
else
(void)fprintf(cshout, "? ");
(void)fflush(cshout);
}
#ifdef EDIT
char *
printpromptstr(EditLine *elx) {
static char pbuf[1024];
static char qspace[] = "? ";
Char *cp;
size_t i;
if (whyles)
return qspace;
i = 0;
for (cp = value(STRprompt); *cp; cp++) {
if (i >= sizeof(pbuf))
break;
if (*cp == HIST) {
int r;
r = snprintf(pbuf + i, sizeof(pbuf) - i, "%d", eventno + 1);
if (r > 0)
i += (size_t)r;
} else
pbuf[i++] = (char)*cp;
}
if (i >= sizeof(pbuf))
i = sizeof(pbuf) - 1;
pbuf[i] = '\0';
return pbuf;
}
#endif