xref: /titanic_50/usr/src/lib/libshell/common/sh/main.c (revision 8cdd6a74847b5ae6ed26727528bdf6b139cf7552)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1982-2009 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                  David Korn <dgk@research.att.com>                   *
18 *                                                                      *
19 ***********************************************************************/
20 #pragma prototyped
21 /*
22  * UNIX shell
23  *
24  * S. R. Bourne
25  * Rewritten By David Korn
26  * AT&T Labs
27  *
28  */
29 
30 #include	<ast.h>
31 #include	<sfio.h>
32 #include	<stak.h>
33 #include	<ls.h>
34 #include	<fcin.h>
35 #include	"defs.h"
36 #include	"variables.h"
37 #include	"path.h"
38 #include	"io.h"
39 #include	"jobs.h"
40 #include	"shlex.h"
41 #include	"shnodes.h"
42 #include	"history.h"
43 #include	"timeout.h"
44 #include	"FEATURE/time"
45 #include	"FEATURE/pstat"
46 #include	"FEATURE/execargs"
47 #include	"FEATURE/externs"
48 #ifdef	_hdr_nc
49 #   include	<nc.h>
50 #endif	/* _hdr_nc */
51 
52 #define CMD_LENGTH	64
53 
54 /* These routines are referenced by this module */
55 static void	exfile(Shell_t*, Sfio_t*,int);
56 static void	chkmail(Shell_t *shp, char*);
57 #if defined(_lib_fork) && !defined(_NEXT_SOURCE)
58     static void	fixargs(char**,int);
59 #else
60 #   define fixargs(a,b)
61 #endif
62 
63 #ifndef environ
64     extern char	**environ;
65 #endif
66 
67 static struct stat lastmail;
68 static time_t	mailtime;
69 static char	beenhere = 0;
70 
71 #ifdef _lib_sigvec
72     void clearsigmask(register int sig)
73     {
74 	struct sigvec vec;
75 	if(sigvec(sig,NIL(struct sigvec*),&vec)>=0 && vec.sv_mask)
76 	{
77 		vec.sv_mask = 0;
78 		sigvec(sig,&vec,NIL(struct sigvec*));
79 	}
80     }
81 #endif /* _lib_sigvec */
82 
83 #ifdef _lib_fts_notify
84 #   include	<fts.h>
85     /* check for interrupts during tree walks */
86     static int fts_sigcheck(FTS* fp, FTSENT* ep, void* context)
87     {
88 	Shell_t *shp = (Shell_t*)context;
89 	NOT_USED(fp);
90 	NOT_USED(ep);
91 	if(shp->trapnote&SH_SIGSET)
92 	{
93 		errno = EINTR;
94 		return(-1);
95 	}
96 	return(0);
97     }
98 #endif /* _lib_fts_notify */
99 
100 #ifdef PATH_BFPATH
101 #define PATHCOMP	NIL(Pathcomp_t*)
102 #else
103 #define PATHCOMP	""
104 #endif
105 
106 /*
107  * search for file and exfile() it if it exists
108  * 1 returned if file found, 0 otherwise
109  */
110 
111 int sh_source(Shell_t *shp, Sfio_t *iop, const char *file)
112 {
113 	char*	oid;
114 	char*	nid;
115 	int	fd;
116 
117 	if (!file || !*file || (fd = path_open(file, PATHCOMP)) < 0)
118 	{
119 		REGRESS(source, "sh_source", ("%s:ENOENT", file));
120 		return 0;
121 	}
122 	oid = error_info.id;
123 	nid = error_info.id = strdup(file);
124 	shp->st.filename = path_fullname(stakptr(PATH_OFFSET));
125 	REGRESS(source, "sh_source", ("%s", file));
126 	exfile(shp, iop, fd);
127 	error_info.id = oid;
128 	free(nid);
129 	return 1;
130 }
131 
132 #ifdef S_ISSOCK
133 #define REMOTE(m)	(S_ISSOCK(m)||!(m))
134 #else
135 #define REMOTE(m)	!(m)
136 #endif
137 
138 int sh_main(int ac, char *av[], Shinit_f userinit)
139 {
140 	register char	*name;
141 	register int	fdin;
142 	register Sfio_t  *iop;
143 	register Shell_t *shp;
144 	struct stat	statb;
145 	int i, rshflag;		/* set for restricted shell */
146 	char *command;
147 #ifdef _lib_sigvec
148 	/* This is to clear mask that may be left on by rlogin */
149 	clearsigmask(SIGALRM);
150 	clearsigmask(SIGHUP);
151 	clearsigmask(SIGCHLD);
152 #endif /* _lib_sigvec */
153 #ifdef	_hdr_nc
154 	_NutConf(_NC_SET_SUFFIXED_SEARCHING, 1);
155 #endif	/* _hdr_nc */
156 	fixargs(av,0);
157 	shp = sh_init(ac,av,userinit);
158 	time(&mailtime);
159 	if(rshflag=sh_isoption(SH_RESTRICTED))
160 		sh_offoption(SH_RESTRICTED);
161 #ifdef _lib_fts_notify
162 	fts_notify(fts_sigcheck,(void*)shp);
163 #endif /* _lib_fts_notify */
164 	if(sigsetjmp(*((sigjmp_buf*)shp->jmpbuffer),0))
165 	{
166 		/* begin script execution here */
167 		sh_reinit((char**)0);
168 	}
169 	shp->fn_depth = shp->dot_depth = 0;
170 	command = error_info.id;
171 	/* set pidname '$$' */
172 	shp->pid = getpid();
173 	srand(shp->pid&0x7fff);
174 	shp->ppid = getppid();
175 	if(nv_isnull(PS4NOD))
176 		nv_putval(PS4NOD,e_traceprompt,NV_RDONLY);
177 	path_pwd(1);
178 	iop = (Sfio_t*)0;
179 #if SHOPT_BRACEPAT
180 	sh_onoption(SH_BRACEEXPAND);
181 #endif
182 	if((beenhere++)==0)
183 	{
184 		sh_onstate(SH_PROFILE);
185 		((Lex_t*)shp->lex_context)->nonstandard = 0;
186 		if(shp->ppid==1)
187 			shp->login_sh++;
188 		if(shp->login_sh >= 2)
189 			sh_onoption(SH_LOGIN_SHELL);
190 		/* decide whether shell is interactive */
191 		if(!sh_isoption(SH_INTERACTIVE) && !sh_isoption(SH_TFLAG) && !sh_isoption(SH_CFLAG) &&
192 		   sh_isoption(SH_SFLAG) && tty_check(0) && tty_check(ERRIO))
193 			sh_onoption(SH_INTERACTIVE);
194 		if(sh_isoption(SH_INTERACTIVE))
195 		{
196 			sh_onoption(SH_BGNICE);
197 			sh_onoption(SH_RC);
198 		}
199 		if(!sh_isoption(SH_RC) && (sh_isoption(SH_BASH) && !sh_isoption(SH_POSIX)
200 #if SHOPT_REMOTE
201 		   || !fstat(0, &statb) && REMOTE(statb.st_mode)
202 #endif
203 		  ))
204 			sh_onoption(SH_RC);
205 		for(i=0; i<elementsof(shp->offoptions.v); i++)
206 			shp->options.v[i] &= ~shp->offoptions.v[i];
207 		if(sh_isoption(SH_INTERACTIVE))
208 		{
209 #ifdef SIGXCPU
210 			signal(SIGXCPU,SIG_DFL);
211 #endif /* SIGXCPU */
212 #ifdef SIGXFSZ
213 			signal(SIGXFSZ,SIG_DFL);
214 #endif /* SIGXFSZ */
215 			sh_onoption(SH_MONITOR);
216 		}
217 		job_init(shp,sh_isoption(SH_LOGIN_SHELL));
218 		if(sh_isoption(SH_LOGIN_SHELL))
219 		{
220 			/*	system profile	*/
221 			sh_source(shp, iop, e_sysprofile);
222 			if(!sh_isoption(SH_NOUSRPROFILE) && !sh_isoption(SH_PRIVILEGED))
223 			{
224 				char **files = shp->login_files;
225 				while ((name = *files++) && !sh_source(shp, iop, sh_mactry(shp,name)));
226 			}
227 		}
228 		/* make sure PWD is set up correctly */
229 		path_pwd(1);
230 		if(!sh_isoption(SH_NOEXEC))
231 		{
232 			if(!sh_isoption(SH_NOUSRPROFILE) && !sh_isoption(SH_PRIVILEGED) && sh_isoption(SH_RC))
233 			{
234 #if SHOPT_BASH
235 				if(sh_isoption(SH_BASH) && !sh_isoption(SH_POSIX))
236 				{
237 #if SHOPT_SYSRC
238 					sh_source(shp, iop, e_bash_sysrc);
239 #endif
240 					sh_source(shp, iop, shp->rcfile ? shp->rcfile : sh_mactry(shp,(char*)e_bash_rc));
241 				}
242 				else
243 #endif
244 				{
245 					if(name = sh_mactry(shp,nv_getval(ENVNOD)))
246 						name = *name ? strdup(name) : (char*)0;
247 #if SHOPT_SYSRC
248 					if(!strmatch(name, "?(.)/./*"))
249 						sh_source(shp, iop, e_sysrc);
250 #endif
251 					if(name)
252 					{
253 						sh_source(shp, iop, name);
254 						free(name);
255 					}
256 				}
257 			}
258 			else if(sh_isoption(SH_INTERACTIVE) && sh_isoption(SH_PRIVILEGED))
259 				sh_source(shp, iop, e_suidprofile);
260 		}
261 		shp->st.cmdname = error_info.id = command;
262 		sh_offstate(SH_PROFILE);
263 		if(rshflag)
264 			sh_onoption(SH_RESTRICTED);
265 		/* open input file if specified */
266 		if(shp->comdiv)
267 		{
268 		shell_c:
269 			iop = sfnew(NIL(Sfio_t*),shp->comdiv,strlen(shp->comdiv),0,SF_STRING|SF_READ);
270 		}
271 		else
272 		{
273 			name = error_info.id;
274 			error_info.id = shp->shname;
275 			if(sh_isoption(SH_SFLAG))
276 				fdin = 0;
277 			else
278 			{
279 				char *sp;
280 				/* open stream should have been passed into shell */
281 				if(strmatch(name,e_devfdNN))
282 				{
283 #if !_WINIX
284 					char *cp;
285 					int type;
286 #endif
287 					fdin = (int)strtol(name+8, (char**)0, 10);
288 					if(fstat(fdin,&statb)<0)
289 						errormsg(SH_DICT,ERROR_system(1),e_open,name);
290 #if !_WINIX
291 					/*
292 					 * try to undo effect of solaris 2.5+
293 					 * change for argv for setuid scripts
294 					 */
295 					if(((type = sh_type(cp = av[0])) & SH_TYPE_SH) && (!(name = nv_getval(L_ARGNOD)) || !((type = sh_type(cp = name)) & SH_TYPE_SH)))
296 					{
297 						av[0] = (type & SH_TYPE_LOGIN) ? cp : path_basename(cp);
298 						/*  exec to change $0 for ps */
299 						execv(pathshell(),av);
300 						/* exec fails */
301 						shp->st.dolv[0] = av[0];
302 						fixargs(shp->st.dolv,1);
303 					}
304 #endif
305 					name = av[0];
306 					sh_offoption(SH_VERBOSE);
307 					sh_offoption(SH_XTRACE);
308 				}
309 				else
310 				{
311 					int isdir = 0;
312 					if((fdin=sh_open(name,O_RDONLY,0))>=0 &&(fstat(fdin,&statb)<0 || S_ISDIR(statb.st_mode)))
313 					{
314 						close(fdin);
315 						isdir = 1;
316 						fdin = -1;
317 					}
318 					else
319 						shp->st.filename = path_fullname(name);
320 					sp = 0;
321 					if(fdin < 0 && !strchr(name,'/'))
322 					{
323 #ifdef PATH_BFPATH
324 						if(path_absolute(name,NIL(Pathcomp_t*)))
325 							sp = stakptr(PATH_OFFSET);
326 #else
327 							sp = path_absolute(name,NIL(char*));
328 #endif
329 						if(sp)
330 						{
331 							if((fdin=sh_open(sp,O_RDONLY,0))>=0)
332 								shp->st.filename = path_fullname(sp);
333 						}
334 					}
335 					if(fdin<0)
336 					{
337 						if(isdir)
338 							errno = EISDIR;
339 						 error_info.id = av[0];
340 						if(sp || errno!=ENOENT)
341 							errormsg(SH_DICT,ERROR_system(ERROR_NOEXEC),e_open,name);
342 						/* try sh -c 'name "$@"' */
343 						sh_onoption(SH_CFLAG);
344 						shp->comdiv = (char*)malloc(strlen(name)+7);
345 						name = strcopy(shp->comdiv,name);
346 						if(shp->st.dolc)
347 							strcopy(name," \"$@\"");
348 						goto shell_c;
349 					}
350 					if(fdin==0)
351 						fdin = sh_iomovefd(fdin);
352 				}
353 				shp->readscript = shp->shname;
354 			}
355 			error_info.id = name;
356 			shp->comdiv--;
357 #if SHOPT_ACCT
358 			sh_accinit();
359 			if(fdin != 0)
360 				sh_accbegin(error_info.id);
361 #endif	/* SHOPT_ACCT */
362 		}
363 	}
364 	else
365 	{
366 		fdin = shp->infd;
367 		fixargs(shp->st.dolv,1);
368 	}
369 	if(sh_isoption(SH_INTERACTIVE))
370 		sh_onstate(SH_INTERACTIVE);
371 	nv_putval(IFSNOD,(char*)e_sptbnl,NV_RDONLY);
372 	exfile(shp,iop,fdin);
373 	sh_done(shp,0);
374 	/* NOTREACHED */
375 	return(0);
376 }
377 
378 /*
379  * iop is not null when the input is a string
380  * fdin is the input file descriptor
381  */
382 
383 static void	exfile(register Shell_t *shp, register Sfio_t *iop,register int fno)
384 {
385 	time_t curtime;
386 	Shnode_t *t;
387 	int maxtry=IOMAXTRY, tdone=0, execflags;
388 	int states,jmpval;
389 	struct checkpt buff;
390 	sh_pushcontext(&buff,SH_JMPERREXIT);
391 	/* open input stream */
392 	nv_putval(SH_PATHNAMENOD, shp->st.filename ,NV_NOFREE);
393 	if(!iop)
394 	{
395 		if(fno > 0)
396 		{
397 			int r;
398 			if(fno < 10 && ((r=sh_fcntl(fno,F_DUPFD,10))>=10))
399 			{
400 				shp->fdstatus[r] = shp->fdstatus[fno];
401 				sh_close(fno);
402 				fno = r;
403 			}
404 			fcntl(fno,F_SETFD,FD_CLOEXEC);
405 			shp->fdstatus[fno] |= IOCLEX;
406 			iop = sh_iostream((void*)shp,fno);
407 		}
408 		else
409 			iop = sfstdin;
410 	}
411 	else
412 		fno = -1;
413 	shp->infd = fno;
414 	if(sh_isstate(SH_INTERACTIVE))
415 	{
416 		if(nv_isnull(PS1NOD))
417 			nv_putval(PS1NOD,(shp->euserid?e_stdprompt:e_supprompt),NV_RDONLY);
418 		sh_sigdone();
419 		if(sh_histinit((void*)shp))
420 			sh_onoption(SH_HISTORY);
421 	}
422 	else
423 	{
424 		if(!sh_isstate(SH_PROFILE))
425 		{
426 			buff.mode = SH_JMPEXIT;
427 			sh_onoption(SH_TRACKALL);
428 			sh_offoption(SH_MONITOR);
429 		}
430 		sh_offstate(SH_INTERACTIVE);
431 		sh_offstate(SH_MONITOR);
432 		sh_offstate(SH_HISTORY);
433 		sh_offoption(SH_HISTORY);
434 	}
435 	states = sh_getstate();
436 	jmpval = sigsetjmp(buff.buff,0);
437 	if(jmpval)
438 	{
439 		Sfio_t *top;
440 		sh_iorestore((void*)shp,0,jmpval);
441 		hist_flush(shp->hist_ptr);
442 		sfsync(shp->outpool);
443 		shp->st.execbrk = shp->st.breakcnt = 0;
444 		/* check for return from profile or env file */
445 		if(sh_isstate(SH_PROFILE) && (jmpval==SH_JMPFUN || jmpval==SH_JMPEXIT))
446 			goto done;
447 		if(!sh_isoption(SH_INTERACTIVE) || sh_isstate(SH_FORKED) || (jmpval > SH_JMPERREXIT && job_close(shp) >=0))
448 		{
449 			sh_offstate(SH_INTERACTIVE);
450 			sh_offstate(SH_MONITOR);
451 			goto done;
452 		}
453 		/* skip over remaining input */
454 		if(top = fcfile())
455 		{
456 			while(fcget()>0);
457 			fcclose();
458 			while(top=sfstack(iop,SF_POPSTACK))
459 				sfclose(top);
460 		}
461 		/* make sure that we own the terminal */
462 #ifdef SIGTSTP
463 		tcsetpgrp(job.fd,shp->pid);
464 #endif /* SIGTSTP */
465 	}
466 	/* error return here */
467 	sfclrerr(iop);
468 	sh_setstate(states);
469 	shp->st.optindex = 1;
470 	opt_info.offset = 0;
471 	shp->st.loopcnt = 0;
472 	shp->trapnote = 0;
473 	shp->intrap = 0;
474 	error_info.line = 1;
475 	shp->inlineno = 1;
476 	shp->binscript = 0;
477 	if(sfeof(iop))
478 		goto eof_or_error;
479 	/* command loop */
480 	while(1)
481 	{
482 		shp->nextprompt = 1;
483 		sh_freeup(shp);
484 		stakset(NIL(char*),0);
485 		exitset();
486 		sh_offstate(SH_STOPOK);
487 		sh_offstate(SH_ERREXIT);
488 		sh_offstate(SH_VERBOSE);
489 		sh_offstate(SH_TIMING);
490 		sh_offstate(SH_GRACE);
491 		sh_offstate(SH_TTYWAIT);
492 		if(sh_isoption(SH_VERBOSE))
493 			sh_onstate(SH_VERBOSE);
494 		sh_onstate(SH_ERREXIT);
495 		/* -eim  flags don't apply to profiles */
496 		if(sh_isstate(SH_PROFILE))
497 		{
498 			sh_offstate(SH_INTERACTIVE);
499 			sh_offstate(SH_ERREXIT);
500 			sh_offstate(SH_MONITOR);
501 		}
502 		if(sh_isstate(SH_INTERACTIVE) && !tdone)
503 		{
504 			register char *mail;
505 #ifdef JOBS
506 			sh_offstate(SH_MONITOR);
507 			if(sh_isoption(SH_MONITOR))
508 				sh_onstate(SH_MONITOR);
509 			if(job.pwlist)
510 			{
511 				job_walk(sfstderr,job_list,JOB_NFLAG,(char**)0);
512 				job_wait((pid_t)0);
513 			}
514 #endif	/* JOBS */
515 			if((mail=nv_getval(MAILPNOD)) || (mail=nv_getval(MAILNOD)))
516 			{
517 				time(&curtime);
518 				if ((curtime - mailtime) >= sh_mailchk)
519 				{
520 					chkmail(shp,mail);
521 					mailtime = curtime;
522 				}
523 			}
524 			if(shp->hist_ptr)
525 				hist_eof(shp->hist_ptr);
526 			/* sets timeout for command entry */
527 			shp->timeout = shp->st.tmout;
528 #if SHOPT_TIMEOUT
529 			if(shp->timeout <= 0 || shp->timeout > SHOPT_TIMEOUT)
530 				shp->timeout = SHOPT_TIMEOUT;
531 #endif /* SHOPT_TIMEOUT */
532 			shp->inlineno = 1;
533 			error_info.line = 1;
534 			shp->exitval = 0;
535 			shp->trapnote = 0;
536 			if(buff.mode == SH_JMPEXIT)
537 			{
538 				buff.mode = SH_JMPERREXIT;
539 #ifdef DEBUG
540 				errormsg(SH_DICT,ERROR_warn(0),"%d: mode changed to JMP_EXIT",getpid());
541 #endif
542 			}
543 		}
544 		errno = 0;
545 		if(tdone || !sfreserve(iop,0,0))
546 		{
547 		eof_or_error:
548 			if(sh_isstate(SH_INTERACTIVE) && !sferror(iop))
549 			{
550 				if(--maxtry>0 && sh_isoption(SH_IGNOREEOF) &&
551 					 !sferror(sfstderr) && (shp->fdstatus[fno]&IOTTY))
552 				{
553 					sfclrerr(iop);
554 					errormsg(SH_DICT,0,e_logout);
555 					continue;
556 				}
557 				else if(job_close(shp)<0)
558 					continue;
559 			}
560 			if(errno==0 && sferror(iop) && --maxtry>0)
561 			{
562 				sfclrlock(iop);
563 				sfclrerr(iop);
564 				continue;
565 			}
566 			goto done;
567 		}
568 		maxtry = IOMAXTRY;
569 		if(sh_isstate(SH_INTERACTIVE) && shp->hist_ptr)
570 		{
571 			job_wait((pid_t)0);
572 			hist_eof(shp->hist_ptr);
573 			sfsync(sfstderr);
574 		}
575 		if(sh_isoption(SH_HISTORY))
576 			sh_onstate(SH_HISTORY);
577 		job.waitall = job.curpgid = 0;
578 		error_info.flags |= ERROR_INTERACTIVE;
579 		t = (Shnode_t*)sh_parse(shp,iop,0);
580 		if(!sh_isstate(SH_INTERACTIVE) && !sh_isstate(SH_CFLAG))
581 			error_info.flags &= ~ERROR_INTERACTIVE;
582 		shp->readscript = 0;
583 		if(sh_isstate(SH_INTERACTIVE) && shp->hist_ptr)
584 			hist_flush(shp->hist_ptr);
585 		sh_offstate(SH_HISTORY);
586 		if(t)
587 		{
588 			execflags = sh_state(SH_ERREXIT)|sh_state(SH_INTERACTIVE);
589 			/* The last command may not have to fork */
590 			if(!sh_isstate(SH_PROFILE) && !sh_isstate(SH_INTERACTIVE) &&
591 				(fno<0 || !(shp->fdstatus[fno]&(IOTTY|IONOSEEK)))
592 				&& !sfreserve(iop,0,0))
593 			{
594 					execflags |= sh_state(SH_NOFORK);
595 			}
596 			shp->st.execbrk = 0;
597 			sh_exec(t,execflags);
598 			if(shp->forked)
599 			{
600 				sh_offstate(SH_INTERACTIVE);
601 				goto done;
602 			}
603 			/* This is for sh -t */
604 			if(sh_isoption(SH_TFLAG) && !sh_isstate(SH_PROFILE))
605 				tdone++;
606 		}
607 	}
608 done:
609 	sh_popcontext(&buff);
610 	if(sh_isstate(SH_INTERACTIVE))
611 	{
612 		sfputc(sfstderr,'\n');
613 		job_close(shp);
614 	}
615 	if(jmpval == SH_JMPSCRIPT)
616 		siglongjmp(*shp->jmplist,jmpval);
617 	else if(jmpval == SH_JMPEXIT)
618 		sh_done(shp,0);
619 	if(fno>0)
620 		sh_close(fno);
621 	if(shp->st.filename)
622 		free((void*)shp->st.filename);
623 	shp->st.filename = 0;
624 }
625 
626 
627 /* prints out messages if files in list have been modified since last call */
628 static void chkmail(Shell_t *shp, char *files)
629 {
630 	register char *cp,*sp,*qp;
631 	register char save;
632 	struct argnod *arglist=0;
633 	int	offset = staktell();
634 	char 	*savstak=stakptr(0);
635 	struct stat	statb;
636 	if(*(cp=files) == 0)
637 		return;
638 	sp = cp;
639 	do
640 	{
641 		/* skip to : or end of string saving first '?' */
642 		for(qp=0;*sp && *sp != ':';sp++)
643 			if((*sp == '?' || *sp=='%') && qp == 0)
644 				qp = sp;
645 		save = *sp;
646 		*sp = 0;
647 		/* change '?' to end-of-string */
648 		if(qp)
649 			*qp = 0;
650 		do
651 		{
652 			/* see if time has been modified since last checked
653 			 * and the access time <= the modification time
654 			 */
655 			if(stat(cp,&statb) >= 0 && statb.st_mtime >= mailtime
656 				&& statb.st_atime <= statb.st_mtime)
657 			{
658 				/* check for directory */
659 				if(!arglist && S_ISDIR(statb.st_mode))
660 				{
661 					/* generate list of directory entries */
662 					path_complete(cp,"/*",&arglist);
663 				}
664 				else
665 				{
666 					/*
667 					 * If the file has shrunk,
668 					 * or if the size is zero
669 					 * then don't print anything
670 					 */
671 					if(statb.st_size &&
672 						(  statb.st_ino != lastmail.st_ino
673 						|| statb.st_dev != lastmail.st_dev
674 						|| statb.st_size > lastmail.st_size))
675 					{
676 						/* save and restore $_ */
677 						char *save = shp->lastarg;
678 						shp->lastarg = cp;
679 						errormsg(SH_DICT,0,sh_mactry(shp,qp?qp+1:(char*)e_mailmsg));
680 						shp->lastarg = save;
681 					}
682 					lastmail = statb;
683 					break;
684 				}
685 			}
686 			if(arglist)
687 			{
688 				cp = arglist->argval;
689 				arglist = arglist->argchn.ap;
690 			}
691 			else
692 				cp = 0;
693 		}
694 		while(cp);
695 		if(qp)
696 			*qp = '?';
697 		*sp++ = save;
698 		cp = sp;
699 	}
700 	while(save);
701 	stakset(savstak,offset);
702 }
703 
704 #undef EXECARGS
705 #undef PSTAT
706 #if defined(_hdr_execargs) && defined(pdp11)
707 #   include	<execargs.h>
708 #   define EXECARGS	1
709 #endif
710 
711 #if defined(_lib_pstat) && defined(_sys_pstat)
712 #   include	<sys/pstat.h>
713 #   define PSTAT	1
714 #endif
715 
716 #if defined(_lib_fork) && !defined(_NEXT_SOURCE)
717 /*
718  * fix up command line for ps command
719  * mode is 0 for initialization
720  */
721 static void fixargs(char **argv, int mode)
722 {
723 #if EXECARGS
724 	*execargs=(char *)argv;
725 #else
726 	static char *buff;
727 	static int command_len;
728 	register char *cp;
729 	int offset=0,size;
730 #   ifdef PSTAT
731 	union pstun un;
732 	if(mode==0)
733 	{
734 		struct pst_static st;
735 		un.pst_static = &st;
736 		if(pstat(PSTAT_STATIC, un, sizeof(struct pst_static), 1, 0)<0)
737 			return;
738 		command_len = st.command_length;
739 		return;
740 	}
741 	stakseek(command_len+2);
742 	buff = stakseek(0);
743 #   else
744 	if(mode==0)
745 	{
746 		buff = argv[0];
747 		while(cp = *argv++)
748 			command_len += strlen(cp)+1;
749 		if(environ && *environ==buff+command_len)
750 		{
751 			for(argv=environ; cp = *argv; cp++)
752 			{
753 				if(command_len > CMD_LENGTH)
754 				{
755 					command_len = CMD_LENGTH;
756 					break;
757 				}
758 				*argv++ = strdup(cp);
759 				command_len += strlen(cp)+1;
760 			}
761 		}
762 		command_len -= 1;
763 		return;
764 	}
765 #   endif /* PSTAT */
766 	if(command_len==0)
767 		return;
768 	while((cp = *argv++) && offset < command_len)
769 	{
770 		if(offset + (size=strlen(cp)) >= command_len)
771 			size = command_len - offset;
772 		memcpy(buff+offset,cp,size);
773 		offset += size;
774 		buff[offset++] = ' ';
775 	}
776 	buff[offset-1] = 0;
777 #   ifdef PSTAT
778 	un.pst_command = stakptr(0);
779 	pstat(PSTAT_SETCMD,un,0,0,0);
780 #   endif /* PSTAT */
781 #endif /* EXECARGS */
782 }
783 #endif /* _lib_fork */
784