17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
539e7390aSna195498 * Common Development and Distribution License (the "License").
639e7390aSna195498 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate /*
23*134a1f4eSCasper H.S. Dik * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
26965005c8Schin /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27965005c8Schin /* All Rights Reserved */
28965005c8Schin
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate * UNIX shell
317c478bd9Sstevel@tonic-gate */
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include "defs.h"
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <fcntl.h>
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #define ARGMK 01
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate static unsigned char *execs();
40965005c8Schin static void gsort();
417c478bd9Sstevel@tonic-gate static int split();
42965005c8Schin extern void makearg(struct argnod *);
437c478bd9Sstevel@tonic-gate extern short topfd;
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate * service routines for `execute'
487c478bd9Sstevel@tonic-gate */
49965005c8Schin short
initio(struct ionod * iop,int save)50965005c8Schin initio(struct ionod *iop, int save)
517c478bd9Sstevel@tonic-gate {
52965005c8Schin unsigned char *ion;
53965005c8Schin int iof, fd;
547c478bd9Sstevel@tonic-gate int ioufd;
557c478bd9Sstevel@tonic-gate short lastfd;
567c478bd9Sstevel@tonic-gate int newmode;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate lastfd = topfd;
597c478bd9Sstevel@tonic-gate while (iop) {
607c478bd9Sstevel@tonic-gate iof = iop->iofile;
617c478bd9Sstevel@tonic-gate ion = mactrim(iop->ioname);
627c478bd9Sstevel@tonic-gate ioufd = iof & IOUFD;
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate if (*ion && (flags&noexec) == 0) {
657c478bd9Sstevel@tonic-gate if (save) {
667c478bd9Sstevel@tonic-gate fdmap[topfd].org_fd = ioufd;
677c478bd9Sstevel@tonic-gate fdmap[topfd++].dup_fd = savefd(ioufd);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
701573d361Snakanon if (iof & IODOC_SUBST) {
717c478bd9Sstevel@tonic-gate struct tempblk tb;
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate subst(chkopen(ion, 0), (fd = tmpfil(&tb)));
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate * pushed in tmpfil() --
777c478bd9Sstevel@tonic-gate * bug fix for problem with
787c478bd9Sstevel@tonic-gate * in-line scripts
797c478bd9Sstevel@tonic-gate */
807c478bd9Sstevel@tonic-gate poptemp();
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate fd = chkopen(tmpout, 0);
837c478bd9Sstevel@tonic-gate unlink((const char *)tmpout);
847c478bd9Sstevel@tonic-gate } else if (iof & IOMOV) {
857c478bd9Sstevel@tonic-gate if (eq(minus, ion)) {
867c478bd9Sstevel@tonic-gate fd = -1;
877c478bd9Sstevel@tonic-gate close(ioufd);
887c478bd9Sstevel@tonic-gate } else if ((fd = stoi(ion)) >= USERIO) {
897c478bd9Sstevel@tonic-gate failed(ion, badfile);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate else
927c478bd9Sstevel@tonic-gate fd = dup(fd);
937c478bd9Sstevel@tonic-gate } else if (((iof & IOPUT) == 0) && ((iof & IORDW) == 0))
947c478bd9Sstevel@tonic-gate fd = chkopen(ion, 0);
957c478bd9Sstevel@tonic-gate else if (iof & IORDW) /* For <> */ {
967c478bd9Sstevel@tonic-gate newmode = O_RDWR|O_CREAT;
977c478bd9Sstevel@tonic-gate fd = chkopen(ion, newmode);
987c478bd9Sstevel@tonic-gate } else if (flags & rshflg) {
997c478bd9Sstevel@tonic-gate failed(ion, restricted);
1007c478bd9Sstevel@tonic-gate } else if (iof & IOAPP &&
1017c478bd9Sstevel@tonic-gate (fd = open((char *)ion, 1)) >= 0) {
1027c478bd9Sstevel@tonic-gate lseek(fd, (off_t)0, SEEK_END);
1037c478bd9Sstevel@tonic-gate } else {
1047c478bd9Sstevel@tonic-gate fd = create(ion);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate if (fd >= 0)
1077c478bd9Sstevel@tonic-gate renamef(fd, ioufd);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate iop = iop->ionxt;
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate return (lastfd);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate unsigned char *
simple(s)1167c478bd9Sstevel@tonic-gate simple(s)
1177c478bd9Sstevel@tonic-gate unsigned char *s;
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate unsigned char *sname;
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate sname = s;
1227c478bd9Sstevel@tonic-gate while (1) {
1237c478bd9Sstevel@tonic-gate if (any('/', sname))
1247c478bd9Sstevel@tonic-gate while (*sname++ != '/')
1257c478bd9Sstevel@tonic-gate ;
1267c478bd9Sstevel@tonic-gate else
1277c478bd9Sstevel@tonic-gate return (sname);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate unsigned char *
getpath(s)1327c478bd9Sstevel@tonic-gate getpath(s)
1337c478bd9Sstevel@tonic-gate unsigned char *s;
1347c478bd9Sstevel@tonic-gate {
135965005c8Schin unsigned char *path, *newpath;
136965005c8Schin int pathlen;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate if (any('/', s))
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate if (flags & rshflg)
1417c478bd9Sstevel@tonic-gate failed(s, restricted);
1427c478bd9Sstevel@tonic-gate else
1437c478bd9Sstevel@tonic-gate return ((unsigned char *)nullstr);
1447c478bd9Sstevel@tonic-gate } else if ((path = pathnod.namval) == 0)
1457c478bd9Sstevel@tonic-gate return ((unsigned char *)defpath);
1467c478bd9Sstevel@tonic-gate else {
1477c478bd9Sstevel@tonic-gate pathlen = length(path)-1;
1487c478bd9Sstevel@tonic-gate /* Add extra ':' if PATH variable ends in ':' */
1497c478bd9Sstevel@tonic-gate if (pathlen > 2 && path[pathlen - 1] == ':' &&
1507c478bd9Sstevel@tonic-gate path[pathlen - 2] != ':') {
1517c478bd9Sstevel@tonic-gate newpath = locstak();
1527c478bd9Sstevel@tonic-gate (void) memcpystak(newpath, path, pathlen);
1537c478bd9Sstevel@tonic-gate newpath[pathlen] = ':';
1547c478bd9Sstevel@tonic-gate endstak(newpath + pathlen + 1);
1557c478bd9Sstevel@tonic-gate return (newpath);
1567c478bd9Sstevel@tonic-gate } else
1577c478bd9Sstevel@tonic-gate return (cpystak(path));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
161965005c8Schin int
pathopen(unsigned char * path,unsigned char * name)162965005c8Schin pathopen(unsigned char *path, unsigned char *name)
1637c478bd9Sstevel@tonic-gate {
164965005c8Schin int f;
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate do
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate path = catpath(path, name);
1697c478bd9Sstevel@tonic-gate } while ((f = open((char *)curstak(), 0)) < 0 && path);
1707c478bd9Sstevel@tonic-gate return (f);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate unsigned char *
catpath(unsigned char * path,unsigned char * name)174965005c8Schin catpath(unsigned char *path, unsigned char *name)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate * leaves result on top of stack
1787c478bd9Sstevel@tonic-gate */
179965005c8Schin unsigned char *scanp = path;
180965005c8Schin unsigned char *argp = locstak();
1817c478bd9Sstevel@tonic-gate
182db397771Schin while (*scanp && *scanp != COLON) {
1837c478bd9Sstevel@tonic-gate if (argp >= brkend)
1847c478bd9Sstevel@tonic-gate growstak(argp);
1857c478bd9Sstevel@tonic-gate *argp++ = *scanp++;
1867c478bd9Sstevel@tonic-gate }
187db397771Schin if (scanp != path) {
1887c478bd9Sstevel@tonic-gate if (argp >= brkend)
1897c478bd9Sstevel@tonic-gate growstak(argp);
1907c478bd9Sstevel@tonic-gate *argp++ = '/';
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate if (*scanp == COLON)
1937c478bd9Sstevel@tonic-gate scanp++;
1947c478bd9Sstevel@tonic-gate path = (*scanp ? scanp : 0);
1957c478bd9Sstevel@tonic-gate scanp = name;
1967c478bd9Sstevel@tonic-gate do
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate if (argp >= brkend)
1997c478bd9Sstevel@tonic-gate growstak(argp);
2007c478bd9Sstevel@tonic-gate }
201*134a1f4eSCasper H.S. Dik while (*argp++ = *scanp++)
202*134a1f4eSCasper H.S. Dik ;
2037c478bd9Sstevel@tonic-gate return (path);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate unsigned char *
nextpath(unsigned char * path)207965005c8Schin nextpath(unsigned char *path)
2087c478bd9Sstevel@tonic-gate {
209965005c8Schin unsigned char *scanp = path;
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate while (*scanp && *scanp != COLON)
2127c478bd9Sstevel@tonic-gate scanp++;
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate if (*scanp == COLON)
2157c478bd9Sstevel@tonic-gate scanp++;
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate return (*scanp ? scanp : 0);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate
220965005c8Schin static const char *xecmsg;
2217c478bd9Sstevel@tonic-gate static unsigned char **xecenv;
2227c478bd9Sstevel@tonic-gate
223965005c8Schin void
execa(unsigned char * at[],short pos)224965005c8Schin execa(unsigned char *at[], short pos)
2257c478bd9Sstevel@tonic-gate {
226965005c8Schin unsigned char *path;
227965005c8Schin unsigned char **t = at;
2287c478bd9Sstevel@tonic-gate int cnt;
2297c478bd9Sstevel@tonic-gate
230db397771Schin if ((flags & noexec) == 0) {
231965005c8Schin xecmsg = notfound;
2327c478bd9Sstevel@tonic-gate path = getpath(*t);
2337c478bd9Sstevel@tonic-gate xecenv = local_setenv();
2347c478bd9Sstevel@tonic-gate
235db397771Schin if (pos > 0) {
2367c478bd9Sstevel@tonic-gate cnt = 1;
237db397771Schin while (cnt != pos) {
2387c478bd9Sstevel@tonic-gate ++cnt;
2397c478bd9Sstevel@tonic-gate path = nextpath(path);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate execs(path, t);
2427c478bd9Sstevel@tonic-gate path = getpath(*t);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate while (path = execs(path, t))
2457c478bd9Sstevel@tonic-gate ;
2467c478bd9Sstevel@tonic-gate failed(*t, xecmsg);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate static unsigned char *
execs(unsigned char * ap,unsigned char * t[])251965005c8Schin execs(unsigned char *ap, unsigned char *t[])
2527c478bd9Sstevel@tonic-gate {
253965005c8Schin unsigned char *p, *prefix;
2547c478bd9Sstevel@tonic-gate unsigned char *savptr;
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate prefix = catpath(ap, t[0]);
2577c478bd9Sstevel@tonic-gate trim(p = curstak());
2587c478bd9Sstevel@tonic-gate sigchk();
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate execve((const char *)p, (char *const *)&t[0],
2617c478bd9Sstevel@tonic-gate (char *const *)xecenv);
2627c478bd9Sstevel@tonic-gate
263db397771Schin switch (errno) {
2647c478bd9Sstevel@tonic-gate case ENOEXEC: /* could be a shell script */
2657c478bd9Sstevel@tonic-gate funcnt = 0;
2667c478bd9Sstevel@tonic-gate flags = 0;
2677c478bd9Sstevel@tonic-gate *flagadr = 0;
2687c478bd9Sstevel@tonic-gate comdiv = 0;
2697c478bd9Sstevel@tonic-gate ioset = 0;
2707c478bd9Sstevel@tonic-gate clearup(); /* remove open files and for loop junk */
2717c478bd9Sstevel@tonic-gate if (input)
2727c478bd9Sstevel@tonic-gate close(input);
2737c478bd9Sstevel@tonic-gate input = chkopen(p, 0);
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate #ifdef ACCT
2767c478bd9Sstevel@tonic-gate preacct(p); /* reset accounting */
2777c478bd9Sstevel@tonic-gate #endif
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate * set up new args
2817c478bd9Sstevel@tonic-gate */
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate setargs(t);
2847c478bd9Sstevel@tonic-gate longjmp(subshell, 1);
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate case ENOMEM:
2877c478bd9Sstevel@tonic-gate failed(p, toobig);
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate case E2BIG:
2907c478bd9Sstevel@tonic-gate failed(p, arglist);
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate case ETXTBSY:
2937c478bd9Sstevel@tonic-gate failed(p, txtbsy);
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate case ELIBACC:
2967c478bd9Sstevel@tonic-gate failed(p, libacc);
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate case ELIBBAD:
2997c478bd9Sstevel@tonic-gate failed(p, libbad);
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate case ELIBSCN:
3027c478bd9Sstevel@tonic-gate failed(p, libscn);
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate case ELIBMAX:
3057c478bd9Sstevel@tonic-gate failed(p, libmax);
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate default:
308965005c8Schin xecmsg = badexec;
3097c478bd9Sstevel@tonic-gate case ENOENT:
3107c478bd9Sstevel@tonic-gate return (prefix);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate BOOL nosubst;
3157c478bd9Sstevel@tonic-gate
316965005c8Schin void
trim(unsigned char * at)317965005c8Schin trim(unsigned char *at)
3187c478bd9Sstevel@tonic-gate {
319965005c8Schin unsigned char *last;
320965005c8Schin unsigned char *current;
321965005c8Schin unsigned char c;
3227c478bd9Sstevel@tonic-gate int len;
3237c478bd9Sstevel@tonic-gate wchar_t wc;
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate nosubst = 0;
326db397771Schin if (current = at) {
3277c478bd9Sstevel@tonic-gate last = at;
3287c478bd9Sstevel@tonic-gate while (c = *current) {
3297c478bd9Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)current,
3307c478bd9Sstevel@tonic-gate MB_LEN_MAX)) <= 0) {
3317c478bd9Sstevel@tonic-gate *last++ = c;
3327c478bd9Sstevel@tonic-gate current++;
3337c478bd9Sstevel@tonic-gate continue;
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate if (wc != '\\') {
3377c478bd9Sstevel@tonic-gate memcpy(last, current, len);
3387c478bd9Sstevel@tonic-gate last += len;
3397c478bd9Sstevel@tonic-gate current += len;
3407c478bd9Sstevel@tonic-gate continue;
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate /* remove \ and quoted nulls */
3447c478bd9Sstevel@tonic-gate nosubst = 1;
3457c478bd9Sstevel@tonic-gate current++;
3467c478bd9Sstevel@tonic-gate if (c = *current) {
3477c478bd9Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)current,
3487c478bd9Sstevel@tonic-gate MB_LEN_MAX)) <= 0) {
3497c478bd9Sstevel@tonic-gate *last++ = c;
3507c478bd9Sstevel@tonic-gate current++;
3517c478bd9Sstevel@tonic-gate continue;
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate memcpy(last, current, len);
3547c478bd9Sstevel@tonic-gate last += len;
3557c478bd9Sstevel@tonic-gate current += len;
3567c478bd9Sstevel@tonic-gate } else
3577c478bd9Sstevel@tonic-gate current++;
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate *last = 0;
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate /* Same as trim, but only removes backlashes before slashes */
365965005c8Schin void
trims(at)3667c478bd9Sstevel@tonic-gate trims(at)
3677c478bd9Sstevel@tonic-gate unsigned char *at;
3687c478bd9Sstevel@tonic-gate {
369965005c8Schin unsigned char *last;
370965005c8Schin unsigned char *current;
371965005c8Schin unsigned char c;
3727c478bd9Sstevel@tonic-gate int len;
3737c478bd9Sstevel@tonic-gate wchar_t wc;
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate if (current = at)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate last = at;
3787c478bd9Sstevel@tonic-gate while (c = *current) {
3797c478bd9Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)current,
3807c478bd9Sstevel@tonic-gate MB_LEN_MAX)) <= 0) {
3817c478bd9Sstevel@tonic-gate *last++ = c;
3827c478bd9Sstevel@tonic-gate current++;
3837c478bd9Sstevel@tonic-gate continue;
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate
3867c478bd9Sstevel@tonic-gate if (wc != '\\') {
3877c478bd9Sstevel@tonic-gate memcpy(last, current, len);
3887c478bd9Sstevel@tonic-gate last += len; current += len;
3897c478bd9Sstevel@tonic-gate continue;
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate /* remove \ and quoted nulls */
3937c478bd9Sstevel@tonic-gate current++;
3947c478bd9Sstevel@tonic-gate if (!(c = *current)) {
3957c478bd9Sstevel@tonic-gate current++;
3967c478bd9Sstevel@tonic-gate continue;
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate if (c == '/') {
4007c478bd9Sstevel@tonic-gate *last++ = c;
4017c478bd9Sstevel@tonic-gate current++;
4027c478bd9Sstevel@tonic-gate continue;
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate *last++ = '\\';
4067c478bd9Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)current,
4077c478bd9Sstevel@tonic-gate MB_LEN_MAX)) <= 0) {
4087c478bd9Sstevel@tonic-gate *last++ = c;
4097c478bd9Sstevel@tonic-gate current++;
4107c478bd9Sstevel@tonic-gate continue;
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate memcpy(last, current, len);
4137c478bd9Sstevel@tonic-gate last += len; current += len;
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate *last = 0;
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate unsigned char *
mactrim(s)4207c478bd9Sstevel@tonic-gate mactrim(s)
4217c478bd9Sstevel@tonic-gate unsigned char *s;
4227c478bd9Sstevel@tonic-gate {
423965005c8Schin unsigned char *t = macro(s);
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate trim(t);
4267c478bd9Sstevel@tonic-gate return (t);
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate unsigned char **
scan(argn)4307c478bd9Sstevel@tonic-gate scan(argn)
4317c478bd9Sstevel@tonic-gate int argn;
4327c478bd9Sstevel@tonic-gate {
433965005c8Schin struct argnod *argp =
4347c478bd9Sstevel@tonic-gate (struct argnod *)(Rcheat(gchain) & ~ARGMK);
435965005c8Schin unsigned char **comargn, **comargm;
4367c478bd9Sstevel@tonic-gate
4377c478bd9Sstevel@tonic-gate comargn = (unsigned char **)getstak(BYTESPERWORD * argn + BYTESPERWORD);
4387c478bd9Sstevel@tonic-gate comargm = comargn += argn;
4397c478bd9Sstevel@tonic-gate *comargn = ENDARGS;
4407c478bd9Sstevel@tonic-gate while (argp)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate *--comargn = argp->argval;
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate trim(*comargn);
4457c478bd9Sstevel@tonic-gate argp = argp->argnxt;
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate if (argp == 0 || Rcheat(argp) & ARGMK)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate gsort(comargn, comargm);
4507c478bd9Sstevel@tonic-gate comargm = comargn;
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate argp = (struct argnod *)(Rcheat(argp) & ~ARGMK);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate return (comargn);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate
457965005c8Schin static void
gsort(from,to)4587c478bd9Sstevel@tonic-gate gsort(from, to)
4597c478bd9Sstevel@tonic-gate unsigned char *from[], *to[];
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate int k, m, n;
462965005c8Schin int i, j;
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate if ((n = to - from) <= 1)
4657c478bd9Sstevel@tonic-gate return;
4667c478bd9Sstevel@tonic-gate for (j = 1; j <= n; j *= 2)
4677c478bd9Sstevel@tonic-gate ;
4687c478bd9Sstevel@tonic-gate for (m = 2 * j - 1; m /= 2; )
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate k = n - m;
4717c478bd9Sstevel@tonic-gate for (j = 0; j < k; j++)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate for (i = j; i >= 0; i -= m)
4747c478bd9Sstevel@tonic-gate {
475965005c8Schin unsigned char **fromi;
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate fromi = &from[i];
4787c478bd9Sstevel@tonic-gate if (cf(fromi[m], fromi[0]) > 0)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate break;
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate else
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate unsigned char *s;
4857c478bd9Sstevel@tonic-gate
4867c478bd9Sstevel@tonic-gate s = fromi[m];
4877c478bd9Sstevel@tonic-gate fromi[m] = fromi[0];
4887c478bd9Sstevel@tonic-gate fromi[0] = s;
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate * Argument list generation
4977c478bd9Sstevel@tonic-gate */
498965005c8Schin int
getarg(ac)4997c478bd9Sstevel@tonic-gate getarg(ac)
5007c478bd9Sstevel@tonic-gate struct comnod *ac;
5017c478bd9Sstevel@tonic-gate {
502965005c8Schin struct argnod *argp;
503965005c8Schin int count = 0;
504965005c8Schin struct comnod *c;
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate if (c = ac)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate argp = c->comarg;
5097c478bd9Sstevel@tonic-gate while (argp)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate count += split(macro(argp->argval), 1);
5127c478bd9Sstevel@tonic-gate argp = argp->argnxt;
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate return (count);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate static int
split(s)5197c478bd9Sstevel@tonic-gate split(s) /* blank interpretation routine */
5207c478bd9Sstevel@tonic-gate unsigned char *s;
5217c478bd9Sstevel@tonic-gate {
522965005c8Schin unsigned char *argp;
523965005c8Schin int c;
5247c478bd9Sstevel@tonic-gate int count = 0;
5257c478bd9Sstevel@tonic-gate for (;;)
5267c478bd9Sstevel@tonic-gate {
527965005c8Schin int length;
5287c478bd9Sstevel@tonic-gate sigchk();
5297c478bd9Sstevel@tonic-gate argp = locstak() + BYTESPERWORD;
5307c478bd9Sstevel@tonic-gate while (c = *s) {
5317c478bd9Sstevel@tonic-gate wchar_t wc;
5327c478bd9Sstevel@tonic-gate if ((length = mbtowc(&wc, (char *)s,
5337c478bd9Sstevel@tonic-gate MB_LEN_MAX)) <= 0) {
5347c478bd9Sstevel@tonic-gate wc = (unsigned char)*s;
5357c478bd9Sstevel@tonic-gate length = 1;
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate if (c == '\\') { /* skip over quoted characters */
5397c478bd9Sstevel@tonic-gate if (argp >= brkend)
5407c478bd9Sstevel@tonic-gate growstak(argp);
5417c478bd9Sstevel@tonic-gate *argp++ = c;
5427c478bd9Sstevel@tonic-gate s++;
5437c478bd9Sstevel@tonic-gate /* get rest of multibyte character */
5447c478bd9Sstevel@tonic-gate if ((length = mbtowc(&wc, (char *)s,
5457c478bd9Sstevel@tonic-gate MB_LEN_MAX)) <= 0) {
5467c478bd9Sstevel@tonic-gate wc = (unsigned char)*s;
5477c478bd9Sstevel@tonic-gate length = 1;
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate if (argp >= brkend)
5507c478bd9Sstevel@tonic-gate growstak(argp);
5517c478bd9Sstevel@tonic-gate *argp++ = *s++;
5527c478bd9Sstevel@tonic-gate while (--length > 0) {
5537c478bd9Sstevel@tonic-gate if (argp >= brkend)
5547c478bd9Sstevel@tonic-gate growstak(argp);
5557c478bd9Sstevel@tonic-gate *argp++ = *s++;
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate continue;
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate if (anys(s, ifsnod.namval)) {
5617c478bd9Sstevel@tonic-gate /* skip to next character position */
5627c478bd9Sstevel@tonic-gate s += length;
5637c478bd9Sstevel@tonic-gate break;
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate if (argp >= brkend)
5677c478bd9Sstevel@tonic-gate growstak(argp);
5687c478bd9Sstevel@tonic-gate *argp++ = c;
5697c478bd9Sstevel@tonic-gate s++;
5707c478bd9Sstevel@tonic-gate while (--length > 0) {
5717c478bd9Sstevel@tonic-gate if (argp >= brkend)
5727c478bd9Sstevel@tonic-gate growstak(argp);
5737c478bd9Sstevel@tonic-gate *argp++ = *s++;
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate if (argp == staktop + BYTESPERWORD)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate if (c)
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate continue;
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate else
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate return (count);
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate * file name generation
5897c478bd9Sstevel@tonic-gate */
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate argp = endstak(argp);
5927c478bd9Sstevel@tonic-gate trims(((struct argnod *)argp)->argval);
5937c478bd9Sstevel@tonic-gate if ((flags & nofngflg) == 0 &&
5947c478bd9Sstevel@tonic-gate (c = expand(((struct argnod *)argp)->argval, 0)))
5957c478bd9Sstevel@tonic-gate count += c;
5967c478bd9Sstevel@tonic-gate else
5977c478bd9Sstevel@tonic-gate {
598965005c8Schin makearg((struct argnod *)argp);
5997c478bd9Sstevel@tonic-gate count++;
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate gchain = (struct argnod *)((int)gchain | ARGMK);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate
6057c478bd9Sstevel@tonic-gate #ifdef ACCT
6067c478bd9Sstevel@tonic-gate #include <sys/types.h>
6077c478bd9Sstevel@tonic-gate #include <sys/acct.h>
6087c478bd9Sstevel@tonic-gate #include <sys/times.h>
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate struct acct sabuf;
6117c478bd9Sstevel@tonic-gate struct tms buffer;
6127c478bd9Sstevel@tonic-gate static clock_t before;
6137c478bd9Sstevel@tonic-gate static int shaccton; /* 0 implies do not write record on exit */
6147c478bd9Sstevel@tonic-gate /* 1 implies write acct record on exit */
615965005c8Schin static comp_t compress(clock_t);
6167c478bd9Sstevel@tonic-gate
6177c478bd9Sstevel@tonic-gate
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate * suspend accounting until turned on by preacct()
6207c478bd9Sstevel@tonic-gate */
621965005c8Schin void
suspacct(void)622965005c8Schin suspacct(void)
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate shaccton = 0;
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate
627965005c8Schin void
preacct(unsigned char * cmdadr)628965005c8Schin preacct(unsigned char *cmdadr)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate unsigned char *simple();
6317c478bd9Sstevel@tonic-gate
632db397771Schin if (acctnod.namval && *acctnod.namval) {
6337c478bd9Sstevel@tonic-gate sabuf.ac_btime = time((time_t *)0);
6347c478bd9Sstevel@tonic-gate before = times(&buffer);
6357c478bd9Sstevel@tonic-gate sabuf.ac_uid = getuid();
6367c478bd9Sstevel@tonic-gate sabuf.ac_gid = getgid();
6377c478bd9Sstevel@tonic-gate movstrn(simple(cmdadr), sabuf.ac_comm, sizeof (sabuf.ac_comm));
6387c478bd9Sstevel@tonic-gate shaccton = 1;
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate
642965005c8Schin void
doacct(void)643965005c8Schin doacct(void)
6447c478bd9Sstevel@tonic-gate {
6457c478bd9Sstevel@tonic-gate int fd;
6467c478bd9Sstevel@tonic-gate clock_t after;
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate if (shaccton) {
6497c478bd9Sstevel@tonic-gate after = times(&buffer);
6507c478bd9Sstevel@tonic-gate sabuf.ac_utime = compress(buffer.tms_utime + buffer.tms_cutime);
6517c478bd9Sstevel@tonic-gate sabuf.ac_stime = compress(buffer.tms_stime + buffer.tms_cstime);
6527c478bd9Sstevel@tonic-gate sabuf.ac_etime = compress(after - before);
6537c478bd9Sstevel@tonic-gate
6547c478bd9Sstevel@tonic-gate if ((fd = open((char *)acctnod.namval,
6557c478bd9Sstevel@tonic-gate O_WRONLY | O_APPEND | O_CREAT, 0666)) != -1) {
6567c478bd9Sstevel@tonic-gate write(fd, &sabuf, sizeof (sabuf));
6577c478bd9Sstevel@tonic-gate close(fd);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate
6627c478bd9Sstevel@tonic-gate /*
6637c478bd9Sstevel@tonic-gate * Produce a pseudo-floating point representation
6647c478bd9Sstevel@tonic-gate * with 3 bits base-8 exponent, 13 bits fraction
6657c478bd9Sstevel@tonic-gate */
6667c478bd9Sstevel@tonic-gate
667965005c8Schin static comp_t
compress(clock_t t)668965005c8Schin compress(clock_t t)
6697c478bd9Sstevel@tonic-gate {
670965005c8Schin int exp = 0;
671965005c8Schin int rund = 0;
6727c478bd9Sstevel@tonic-gate
673db397771Schin while (t >= 8192) {
6747c478bd9Sstevel@tonic-gate exp++;
6757c478bd9Sstevel@tonic-gate rund = t & 04;
6767c478bd9Sstevel@tonic-gate t >>= 3;
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate
679db397771Schin if (rund) {
6807c478bd9Sstevel@tonic-gate t++;
681db397771Schin if (t >= 8192) {
6827c478bd9Sstevel@tonic-gate t >>= 3;
6837c478bd9Sstevel@tonic-gate exp++;
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate return ((exp << 13) + t);
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate #endif
689