10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 22*527Schin 23*527Schin /* 24*527Schin * Copyright 1996 Sun Microsystems, Inc. All rights reserved. 25*527Schin * Use is subject to license terms. 26*527Schin */ 27*527Schin 280Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 290Sstevel@tonic-gate /* All Rights Reserved */ 300Sstevel@tonic-gate 31*527Schin #pragma ident "%Z%%M% %I% %E% SMI" 320Sstevel@tonic-gate /* 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * UNIX shell 350Sstevel@tonic-gate * 360Sstevel@tonic-gate */ 370Sstevel@tonic-gate 380Sstevel@tonic-gate 390Sstevel@tonic-gate #include "defs.h" 400Sstevel@tonic-gate #include <errno.h> 410Sstevel@tonic-gate #include "sym.h" 420Sstevel@tonic-gate #include "hash.h" 430Sstevel@tonic-gate #include <sys/types.h> 440Sstevel@tonic-gate #include <sys/times.h> 450Sstevel@tonic-gate 46*527Schin void 47*527Schin builtin(int type, int argc, unsigned char **argv, struct trenod *t) 480Sstevel@tonic-gate { 490Sstevel@tonic-gate short index = initio(t->treio, (type != SYSEXEC)); 500Sstevel@tonic-gate unsigned char *a1 = argv[1]; 510Sstevel@tonic-gate 520Sstevel@tonic-gate switch (type) 530Sstevel@tonic-gate { 540Sstevel@tonic-gate 550Sstevel@tonic-gate case SYSSUSP: 560Sstevel@tonic-gate syssusp(argc,argv); 570Sstevel@tonic-gate break; 580Sstevel@tonic-gate 590Sstevel@tonic-gate case SYSSTOP: 600Sstevel@tonic-gate sysstop(argc,argv); 610Sstevel@tonic-gate break; 620Sstevel@tonic-gate 630Sstevel@tonic-gate case SYSKILL: 640Sstevel@tonic-gate syskill(argc,argv); 650Sstevel@tonic-gate break; 660Sstevel@tonic-gate 670Sstevel@tonic-gate case SYSFGBG: 680Sstevel@tonic-gate sysfgbg(argc,argv); 690Sstevel@tonic-gate break; 700Sstevel@tonic-gate 710Sstevel@tonic-gate case SYSJOBS: 720Sstevel@tonic-gate sysjobs(argc,argv); 730Sstevel@tonic-gate break; 740Sstevel@tonic-gate 750Sstevel@tonic-gate case SYSDOT: 760Sstevel@tonic-gate if (a1) 770Sstevel@tonic-gate { 78*527Schin int f; 790Sstevel@tonic-gate 800Sstevel@tonic-gate if ((f = pathopen(getpath(a1), a1)) < 0) 810Sstevel@tonic-gate failed(a1, notfound); 820Sstevel@tonic-gate else 830Sstevel@tonic-gate execexp(0, f); 840Sstevel@tonic-gate } 850Sstevel@tonic-gate break; 860Sstevel@tonic-gate 870Sstevel@tonic-gate case SYSTIMES: 880Sstevel@tonic-gate { 890Sstevel@tonic-gate struct tms tms; 900Sstevel@tonic-gate 910Sstevel@tonic-gate times(&tms); 920Sstevel@tonic-gate prt(tms.tms_cutime); 930Sstevel@tonic-gate prc_buff(SPACE); 940Sstevel@tonic-gate prt(tms.tms_cstime); 950Sstevel@tonic-gate prc_buff(NL); 960Sstevel@tonic-gate } 970Sstevel@tonic-gate break; 980Sstevel@tonic-gate 990Sstevel@tonic-gate case SYSEXIT: 1000Sstevel@tonic-gate if ( tried_to_exit++ || endjobs(JOB_STOPPED) ){ 1010Sstevel@tonic-gate flags |= forcexit; /* force exit */ 1020Sstevel@tonic-gate exitsh(a1 ? stoi(a1) : retval); 1030Sstevel@tonic-gate } 1040Sstevel@tonic-gate break; 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate case SYSNULL: 1070Sstevel@tonic-gate t->treio = 0; 1080Sstevel@tonic-gate break; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate case SYSCONT: 1110Sstevel@tonic-gate if (loopcnt) 1120Sstevel@tonic-gate { 1130Sstevel@tonic-gate execbrk = breakcnt = 1; 1140Sstevel@tonic-gate if (a1) 1150Sstevel@tonic-gate breakcnt = stoi(a1); 1160Sstevel@tonic-gate if (breakcnt > loopcnt) 1170Sstevel@tonic-gate breakcnt = loopcnt; 1180Sstevel@tonic-gate else 1190Sstevel@tonic-gate breakcnt = -breakcnt; 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate break; 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate case SYSBREAK: 1240Sstevel@tonic-gate if (loopcnt) 1250Sstevel@tonic-gate { 1260Sstevel@tonic-gate execbrk = breakcnt = 1; 1270Sstevel@tonic-gate if (a1) 1280Sstevel@tonic-gate breakcnt = stoi(a1); 1290Sstevel@tonic-gate if (breakcnt > loopcnt) 1300Sstevel@tonic-gate breakcnt = loopcnt; 1310Sstevel@tonic-gate } 1320Sstevel@tonic-gate break; 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate case SYSTRAP: 1350Sstevel@tonic-gate systrap(argc,argv); 1360Sstevel@tonic-gate break; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate case SYSEXEC: 1390Sstevel@tonic-gate argv++; 1400Sstevel@tonic-gate ioset = 0; 1410Sstevel@tonic-gate if (a1 == 0) { 1420Sstevel@tonic-gate setmode(0); 1430Sstevel@tonic-gate break; 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate /* FALLTHROUGH */ 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate #ifdef RES /* Research includes login as part of the shell */ 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate case SYSLOGIN: 1500Sstevel@tonic-gate if (!endjobs(JOB_STOPPED|JOB_RUNNING)) 1510Sstevel@tonic-gate break; 1520Sstevel@tonic-gate oldsigs(); 1530Sstevel@tonic-gate execa(argv, -1); 1540Sstevel@tonic-gate done(0); 1550Sstevel@tonic-gate #else 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate case SYSNEWGRP: 1580Sstevel@tonic-gate if (flags & rshflg) 1590Sstevel@tonic-gate failed(argv[0], restricted); 1600Sstevel@tonic-gate else if (!endjobs(JOB_STOPPED|JOB_RUNNING)) 1610Sstevel@tonic-gate break; 1620Sstevel@tonic-gate else 1630Sstevel@tonic-gate { 1640Sstevel@tonic-gate flags |= forcexit; /* bad exec will terminate shell */ 1650Sstevel@tonic-gate oldsigs(); 1660Sstevel@tonic-gate rmtemp(0); 1670Sstevel@tonic-gate rmfunctmp(); 1680Sstevel@tonic-gate #ifdef ACCT 1690Sstevel@tonic-gate doacct(); 1700Sstevel@tonic-gate #endif 1710Sstevel@tonic-gate execa(argv, -1); 1720Sstevel@tonic-gate done(0); 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate #endif 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate case SYSCD: 1780Sstevel@tonic-gate if (flags & rshflg) 1790Sstevel@tonic-gate failed(argv[0], restricted); 1800Sstevel@tonic-gate else if ((a1 && *a1) || (a1 == 0 && (a1 = homenod.namval))) 1810Sstevel@tonic-gate { 1820Sstevel@tonic-gate unsigned char *cdpath; 1830Sstevel@tonic-gate unsigned char *dir; 1840Sstevel@tonic-gate int f; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate if ((cdpath = cdpnod.namval) == 0 || 1870Sstevel@tonic-gate *a1 == '/' || 1880Sstevel@tonic-gate cf(a1, ".") == 0 || 1890Sstevel@tonic-gate cf(a1, "..") == 0 || 1900Sstevel@tonic-gate (*a1 == '.' && (*(a1+1) == '/' || *(a1+1) == '.' && *(a1+2) == '/'))) 1910Sstevel@tonic-gate cdpath = (unsigned char *)nullstr; 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate do 1940Sstevel@tonic-gate { 1950Sstevel@tonic-gate dir = cdpath; 1960Sstevel@tonic-gate cdpath = catpath(cdpath,a1); 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate while ((f = (chdir((const char *) curstak()) < 0)) && 1990Sstevel@tonic-gate cdpath); 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate if (f) { 2020Sstevel@tonic-gate switch(errno) { 2030Sstevel@tonic-gate case EMULTIHOP: 2040Sstevel@tonic-gate failed(a1, emultihop); 2050Sstevel@tonic-gate break; 2060Sstevel@tonic-gate case ENOTDIR: 2070Sstevel@tonic-gate failed(a1, enotdir); 2080Sstevel@tonic-gate break; 2090Sstevel@tonic-gate case ENOENT: 2100Sstevel@tonic-gate failed(a1, enoent); 2110Sstevel@tonic-gate break; 2120Sstevel@tonic-gate case EACCES: 2130Sstevel@tonic-gate failed(a1, eacces); 2140Sstevel@tonic-gate break; 2150Sstevel@tonic-gate case ENOLINK: 2160Sstevel@tonic-gate failed(a1, enolink); 2170Sstevel@tonic-gate break; 2180Sstevel@tonic-gate default: 2190Sstevel@tonic-gate failed(a1, baddir); 2200Sstevel@tonic-gate break; 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate } 2230Sstevel@tonic-gate else 2240Sstevel@tonic-gate { 2250Sstevel@tonic-gate cwd(curstak()); 2260Sstevel@tonic-gate if (cf(nullstr, dir) && 2270Sstevel@tonic-gate *dir != ':' && 2280Sstevel@tonic-gate any('/', curstak()) && 2290Sstevel@tonic-gate flags & prompt) 2300Sstevel@tonic-gate { 2310Sstevel@tonic-gate prs_buff(cwdget()); 2320Sstevel@tonic-gate prc_buff(NL); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate zapcd(); 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate else 2380Sstevel@tonic-gate { 2390Sstevel@tonic-gate if (a1) 2400Sstevel@tonic-gate error(nulldir); 2410Sstevel@tonic-gate else 2420Sstevel@tonic-gate error(nohome); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate break; 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate case SYSSHFT: 2480Sstevel@tonic-gate { 2490Sstevel@tonic-gate int places; 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate places = a1 ? stoi(a1) : 1; 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate if ((dolc -= places) < 0) 2540Sstevel@tonic-gate { 2550Sstevel@tonic-gate dolc = 0; 2560Sstevel@tonic-gate error(badshift); 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate else 2590Sstevel@tonic-gate dolv += places; 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate break; 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate case SYSWAIT: 2650Sstevel@tonic-gate syswait(argc,argv); 2660Sstevel@tonic-gate break; 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate case SYSREAD: 2690Sstevel@tonic-gate if(argc < 2) 2700Sstevel@tonic-gate failed(argv[0],mssgargn); 2710Sstevel@tonic-gate rwait = 1; 2720Sstevel@tonic-gate exitval = readvar(&argv[1]); 2730Sstevel@tonic-gate rwait = 0; 2740Sstevel@tonic-gate break; 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate case SYSSET: 2770Sstevel@tonic-gate if (a1) 2780Sstevel@tonic-gate { 2790Sstevel@tonic-gate int cnt; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate cnt = options(argc, argv); 2820Sstevel@tonic-gate if (cnt > 1) 2830Sstevel@tonic-gate setargs(argv + argc - cnt); 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate else if (comptr(t)->comset == 0) 2860Sstevel@tonic-gate { 2870Sstevel@tonic-gate /* 2880Sstevel@tonic-gate * scan name chain and print 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate namscan(printnam); 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate break; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate case SYSRDONLY: 2950Sstevel@tonic-gate exitval = 0; 2960Sstevel@tonic-gate if (a1) 2970Sstevel@tonic-gate { 2980Sstevel@tonic-gate while (*++argv) 2990Sstevel@tonic-gate attrib(lookup(*argv), N_RDONLY); 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate else 3020Sstevel@tonic-gate namscan(printro); 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate break; 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate case SYSXPORT: 3070Sstevel@tonic-gate { 3080Sstevel@tonic-gate struct namnod *n; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate exitval = 0; 3110Sstevel@tonic-gate if (a1) 3120Sstevel@tonic-gate { 3130Sstevel@tonic-gate while (*++argv) 3140Sstevel@tonic-gate { 3150Sstevel@tonic-gate n = lookup(*argv); 3160Sstevel@tonic-gate if (n->namflg & N_FUNCTN) 3170Sstevel@tonic-gate error(badexport); 3180Sstevel@tonic-gate else 3190Sstevel@tonic-gate attrib(n, N_EXPORT); 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate else 3230Sstevel@tonic-gate namscan(printexp); 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate break; 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate case SYSEVAL: 3280Sstevel@tonic-gate if (a1) 3290Sstevel@tonic-gate execexp(a1, &argv[2]); 3300Sstevel@tonic-gate break; 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate #ifndef RES 3330Sstevel@tonic-gate case SYSULIMIT: 3340Sstevel@tonic-gate sysulimit(argc, argv); 3350Sstevel@tonic-gate break; 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate case SYSUMASK: 3380Sstevel@tonic-gate if (a1) 3390Sstevel@tonic-gate { 3400Sstevel@tonic-gate int c; 3410Sstevel@tonic-gate mode_t i; 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate i = 0; 3440Sstevel@tonic-gate while ((c = *a1++) >= '0' && c <= '7') 3450Sstevel@tonic-gate i = (i << 3) + c - '0'; 3460Sstevel@tonic-gate umask(i); 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate else 3490Sstevel@tonic-gate { 3500Sstevel@tonic-gate mode_t i; 3510Sstevel@tonic-gate int j; 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate umask(i = umask(0)); 3540Sstevel@tonic-gate prc_buff('0'); 3550Sstevel@tonic-gate for (j = 6; j >= 0; j -= 3) 3560Sstevel@tonic-gate prc_buff(((i >> j) & 07) +'0'); 3570Sstevel@tonic-gate prc_buff(NL); 3580Sstevel@tonic-gate } 3590Sstevel@tonic-gate break; 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate #endif 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate case SYSTST: 3640Sstevel@tonic-gate exitval = test(argc, argv); 3650Sstevel@tonic-gate break; 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate case SYSECHO: 3680Sstevel@tonic-gate exitval = echo(argc, argv); 3690Sstevel@tonic-gate break; 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate case SYSHASH: 3720Sstevel@tonic-gate exitval = 0; 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate if (a1) 3750Sstevel@tonic-gate { 3760Sstevel@tonic-gate if (a1[0] == '-') 3770Sstevel@tonic-gate { 3780Sstevel@tonic-gate if (a1[1] == 'r') 3790Sstevel@tonic-gate zaphash(); 3800Sstevel@tonic-gate else 3810Sstevel@tonic-gate error(badopt); 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate else 3840Sstevel@tonic-gate { 3850Sstevel@tonic-gate while (*++argv) 3860Sstevel@tonic-gate { 3870Sstevel@tonic-gate if (hashtype(hash_cmd(*argv)) == NOTFOUND) 3880Sstevel@tonic-gate failed(*argv, notfound); 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate else 3930Sstevel@tonic-gate hashpr(); 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate break; 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate case SYSPWD: 3980Sstevel@tonic-gate { 3990Sstevel@tonic-gate exitval = 0; 4000Sstevel@tonic-gate cwdprint(); 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate break; 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate case SYSRETURN: 4050Sstevel@tonic-gate if (funcnt == 0) 4060Sstevel@tonic-gate error(badreturn); 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate execbrk = 1; 4090Sstevel@tonic-gate exitval = (a1 ? stoi(a1) : retval); 4100Sstevel@tonic-gate break; 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate case SYSTYPE: 4130Sstevel@tonic-gate exitval = 0; 4140Sstevel@tonic-gate if (a1) 4150Sstevel@tonic-gate { 4160Sstevel@tonic-gate /* return success only if all names are found */ 4170Sstevel@tonic-gate while (*++argv) 4180Sstevel@tonic-gate exitval |= what_is_path(*argv); 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate break; 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate case SYSUNS: 4230Sstevel@tonic-gate exitval = 0; 4240Sstevel@tonic-gate if (a1) 4250Sstevel@tonic-gate { 4260Sstevel@tonic-gate while (*++argv) 4270Sstevel@tonic-gate unset_name(*argv); 4280Sstevel@tonic-gate } 4290Sstevel@tonic-gate break; 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate case SYSGETOPT: { 4320Sstevel@tonic-gate int getoptval; 4330Sstevel@tonic-gate struct namnod *n; 4340Sstevel@tonic-gate extern unsigned char numbuf[]; 4350Sstevel@tonic-gate unsigned char *varnam = argv[2]; 4360Sstevel@tonic-gate unsigned char c[2]; 4370Sstevel@tonic-gate if(argc < 3) { 4380Sstevel@tonic-gate failure(argv[0],mssgargn); 4390Sstevel@tonic-gate break; 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate exitval = 0; 4420Sstevel@tonic-gate n = lookup("OPTIND"); 4430Sstevel@tonic-gate optind = stoi(n->namval); 4440Sstevel@tonic-gate if(argc > 3) { 4450Sstevel@tonic-gate argv[2] = dolv[0]; 4460Sstevel@tonic-gate getoptval = getopt(argc-2, (char **)&argv[2], (char *)argv[1]); 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate else 4490Sstevel@tonic-gate getoptval = getopt(dolc+1, (char **)dolv, (char *)argv[1]); 4500Sstevel@tonic-gate if(getoptval == -1) { 4510Sstevel@tonic-gate itos(optind); 4520Sstevel@tonic-gate assign(n, numbuf); 4530Sstevel@tonic-gate n = lookup(varnam); 454*527Schin assign(n, (unsigned char *)nullstr); 4550Sstevel@tonic-gate exitval = 1; 4560Sstevel@tonic-gate break; 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate argv[2] = varnam; 4590Sstevel@tonic-gate itos(optind); 4600Sstevel@tonic-gate assign(n, numbuf); 4610Sstevel@tonic-gate c[0] = getoptval; 4620Sstevel@tonic-gate c[1] = 0; 4630Sstevel@tonic-gate n = lookup(varnam); 4640Sstevel@tonic-gate assign(n, c); 4650Sstevel@tonic-gate n = lookup("OPTARG"); 466*527Schin assign(n, (unsigned char *)optarg); 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate break; 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate default: 4710Sstevel@tonic-gate prs_buff("unknown builtin\n"); 4720Sstevel@tonic-gate } 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate flushb(); 4760Sstevel@tonic-gate restore(index); 4770Sstevel@tonic-gate chktrap(); 4780Sstevel@tonic-gate } 479