miscbltin.c revision 36150
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 3. All advertising materials mentioning features or use of this software 171556Srgrimes * must display the following acknowledgement: 181556Srgrimes * This product includes software developed by the University of 191556Srgrimes * California, Berkeley and its contributors. 201556Srgrimes * 4. Neither the name of the University nor the names of its contributors 211556Srgrimes * may be used to endorse or promote products derived from this software 221556Srgrimes * without specific prior written permission. 231556Srgrimes * 241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341556Srgrimes * SUCH DAMAGE. 351556Srgrimes */ 361556Srgrimes 371556Srgrimes#ifndef lint 3836150Scharnier#if 0 3936150Scharnierstatic char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95"; 4036150Scharnier#endif 4136150Scharnierstatic const char rcsid[] = 4236150Scharnier "$Id$"; 431556Srgrimes#endif /* not lint */ 441556Srgrimes 451556Srgrimes/* 461556Srgrimes * Miscelaneous builtins. 471556Srgrimes */ 481556Srgrimes 4917987Speter#include <sys/types.h> 5017987Speter#include <sys/stat.h> 5117987Speter#include <sys/time.h> 5217987Speter#include <sys/resource.h> 5317987Speter#include <unistd.h> 5417987Speter#include <ctype.h> 5518016Speter#include <errno.h> 5618018Speter#include <stdio.h> 5729983Smsmith#include <termios.h> 5817987Speter 591556Srgrimes#include "shell.h" 601556Srgrimes#include "options.h" 611556Srgrimes#include "var.h" 621556Srgrimes#include "output.h" 631556Srgrimes#include "memalloc.h" 641556Srgrimes#include "error.h" 651556Srgrimes#include "mystring.h" 661556Srgrimes 671556Srgrimes#undef eflag 681556Srgrimes 691556Srgrimesextern char **argptr; /* argument list for builtin command */ 701556Srgrimes 711556Srgrimes 721556Srgrimes/* 731556Srgrimes * The read builtin. The -e option causes backslashes to escape the 741556Srgrimes * following character. 751556Srgrimes * 761556Srgrimes * This uses unbuffered input, which may be avoidable in some cases. 771556Srgrimes */ 781556Srgrimes 7917987Speterint 8017987Speterreadcmd(argc, argv) 8125905Ssteve int argc __unused; 8225905Ssteve char **argv __unused; 8317987Speter{ 841556Srgrimes char **ap; 851556Srgrimes int backslash; 861556Srgrimes char c; 871556Srgrimes int eflag; 881556Srgrimes char *prompt; 891556Srgrimes char *ifs; 901556Srgrimes char *p; 911556Srgrimes int startword; 921556Srgrimes int status; 931556Srgrimes int i; 9429983Smsmith struct timeval tv; 9529983Smsmith char *tvptr; 9629983Smsmith fd_set ifds; 9729983Smsmith struct termios told, tnew; 9829983Smsmith int tsaved; 991556Srgrimes 1001556Srgrimes eflag = 0; 1011556Srgrimes prompt = NULL; 10229983Smsmith tv.tv_sec = -1; 10329983Smsmith tv.tv_usec = 0; 10429983Smsmith while ((i = nextopt("ep:t:")) != '\0') { 10529983Smsmith switch(i) { 10629983Smsmith case 'p': 1071556Srgrimes prompt = optarg; 10829983Smsmith break; 10929983Smsmith case 'e': 1101556Srgrimes eflag = 1; 11129983Smsmith break; 11229983Smsmith case 't': 11329983Smsmith tv.tv_sec = strtol(optarg, &tvptr, 0); 11429983Smsmith if (tvptr == optarg) 11529983Smsmith error("timeout value"); 11629983Smsmith switch(*tvptr) { 11729983Smsmith case 0: 11829983Smsmith case 's': 11929983Smsmith break; 12029983Smsmith case 'h': 12129983Smsmith tv.tv_sec *= 60; 12229983Smsmith /* FALLTHROUGH */ 12329983Smsmith case 'm': 12429983Smsmith tv.tv_sec *= 60; 12529983Smsmith break; 12629983Smsmith default: 12729983Smsmith error("timeout unit"); 12829983Smsmith } 12929983Smsmith break; 13029983Smsmith } 1311556Srgrimes } 1321556Srgrimes if (prompt && isatty(0)) { 1331556Srgrimes out2str(prompt); 1341556Srgrimes flushall(); 1351556Srgrimes } 1361556Srgrimes if (*(ap = argptr) == NULL) 1371556Srgrimes error("arg count"); 1381556Srgrimes if ((ifs = bltinlookup("IFS", 1)) == NULL) 1391556Srgrimes ifs = nullstr; 14029983Smsmith 14129983Smsmith if (tv.tv_sec >= 0) { 14229983Smsmith /* 14329983Smsmith * See if we can disable input processing; this will 14429983Smsmith * not give the desired result if we are in a pipeline 14529983Smsmith * and someone upstream is still in line-by-line mode. 14629983Smsmith */ 14729983Smsmith tsaved = 0; 14829983Smsmith if (tcgetattr(0, &told) == 0) { 14929983Smsmith memcpy(&tnew, &told, sizeof(told)); 15029983Smsmith cfmakeraw(&tnew); 15129983Smsmith tcsetattr(0, TCSANOW, &tnew); 15229983Smsmith tsaved = 1; 15329983Smsmith } 15429983Smsmith /* 15529983Smsmith * Wait for something to become available. 15629983Smsmith */ 15729983Smsmith FD_ZERO(&ifds); 15829983Smsmith FD_SET(0, &ifds); 15929983Smsmith status = select(1, &ifds, NULL, NULL, &tv); 16029983Smsmith if (tsaved) 16129983Smsmith tcsetattr(0, TCSANOW, &told); 16229983Smsmith /* 16329983Smsmith * If there's nothing ready, return an error. 16429983Smsmith */ 16529983Smsmith if (status <= 0) 16629983Smsmith return(1); 16729983Smsmith } 16829983Smsmith 1691556Srgrimes status = 0; 1701556Srgrimes startword = 1; 1711556Srgrimes backslash = 0; 1721556Srgrimes STARTSTACKSTR(p); 1731556Srgrimes for (;;) { 1741556Srgrimes if (read(0, &c, 1) != 1) { 1751556Srgrimes status = 1; 1761556Srgrimes break; 1771556Srgrimes } 1781556Srgrimes if (c == '\0') 1791556Srgrimes continue; 1801556Srgrimes if (backslash) { 1811556Srgrimes backslash = 0; 1821556Srgrimes if (c != '\n') 1831556Srgrimes STPUTC(c, p); 1841556Srgrimes continue; 1851556Srgrimes } 1861556Srgrimes if (eflag && c == '\\') { 1871556Srgrimes backslash++; 1881556Srgrimes continue; 1891556Srgrimes } 1901556Srgrimes if (c == '\n') 1911556Srgrimes break; 1921556Srgrimes if (startword && *ifs == ' ' && strchr(ifs, c)) { 1931556Srgrimes continue; 1941556Srgrimes } 1951556Srgrimes startword = 0; 1961556Srgrimes if (backslash && c == '\\') { 1971556Srgrimes if (read(0, &c, 1) != 1) { 1981556Srgrimes status = 1; 1991556Srgrimes break; 2001556Srgrimes } 2011556Srgrimes STPUTC(c, p); 2021556Srgrimes } else if (ap[1] != NULL && strchr(ifs, c) != NULL) { 2031556Srgrimes STACKSTRNUL(p); 2041556Srgrimes setvar(*ap, stackblock(), 0); 2051556Srgrimes ap++; 2061556Srgrimes startword = 1; 2071556Srgrimes STARTSTACKSTR(p); 2081556Srgrimes } else { 2091556Srgrimes STPUTC(c, p); 2101556Srgrimes } 2111556Srgrimes } 2121556Srgrimes STACKSTRNUL(p); 2131556Srgrimes setvar(*ap, stackblock(), 0); 2141556Srgrimes while (*++ap != NULL) 2151556Srgrimes setvar(*ap, nullstr, 0); 2161556Srgrimes return status; 2171556Srgrimes} 2181556Srgrimes 2191556Srgrimes 2201556Srgrimes 22117987Speterint 22217987Speterumaskcmd(argc, argv) 22325905Ssteve int argc __unused; 22420425Ssteve char **argv; 22517987Speter{ 22617987Speter char *ap; 2271556Srgrimes int mask; 2281556Srgrimes int i; 22917987Speter int symbolic_mode = 0; 2301556Srgrimes 23117987Speter while ((i = nextopt("S")) != '\0') { 23217987Speter symbolic_mode = 1; 2331556Srgrimes } 23411571Sjoerg 23517987Speter INTOFF; 23617987Speter mask = umask(0); 23717987Speter umask(mask); 23817987Speter INTON; 23911571Sjoerg 24017987Speter if ((ap = *argptr) == NULL) { 24117987Speter if (symbolic_mode) { 24217987Speter char u[4], g[4], o[4]; 24311571Sjoerg 24417987Speter i = 0; 24517987Speter if ((mask & S_IRUSR) == 0) 24617987Speter u[i++] = 'r'; 24717987Speter if ((mask & S_IWUSR) == 0) 24817987Speter u[i++] = 'w'; 24917987Speter if ((mask & S_IXUSR) == 0) 25017987Speter u[i++] = 'x'; 25117987Speter u[i] = '\0'; 25211571Sjoerg 25317987Speter i = 0; 25417987Speter if ((mask & S_IRGRP) == 0) 25517987Speter g[i++] = 'r'; 25617987Speter if ((mask & S_IWGRP) == 0) 25717987Speter g[i++] = 'w'; 25817987Speter if ((mask & S_IXGRP) == 0) 25917987Speter g[i++] = 'x'; 26017987Speter g[i] = '\0'; 26111571Sjoerg 26217987Speter i = 0; 26317987Speter if ((mask & S_IROTH) == 0) 26417987Speter o[i++] = 'r'; 26517987Speter if ((mask & S_IWOTH) == 0) 26617987Speter o[i++] = 'w'; 26717987Speter if ((mask & S_IXOTH) == 0) 26817987Speter o[i++] = 'x'; 26917987Speter o[i] = '\0'; 27011571Sjoerg 27117987Speter out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 27217987Speter } else { 27317987Speter out1fmt("%.4o\n", mask); 27417987Speter } 27517987Speter } else { 27617987Speter if (isdigit(*ap)) { 27717987Speter mask = 0; 27817987Speter do { 27917987Speter if (*ap >= '8' || *ap < '0') 28017987Speter error("Illegal number: %s", argv[1]); 28117987Speter mask = (mask << 3) + (*ap - '0'); 28217987Speter } while (*++ap != '\0'); 28317987Speter umask(mask); 28417987Speter } else { 28520425Ssteve void *set; 28617987Speter if ((set = setmode (ap)) == 0) 28717987Speter error("Illegal number: %s", ap); 28811571Sjoerg 28917987Speter mask = getmode (set, ~mask & 0777); 29017987Speter umask(~mask & 0777); 29117987Speter } 29217987Speter } 29311571Sjoerg return 0; 29411571Sjoerg} 29511571Sjoerg 29617987Speter/* 29717987Speter * ulimit builtin 29817987Speter * 29917987Speter * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and 30017987Speter * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with 30117987Speter * ash by J.T. Conklin. 30217987Speter * 30317987Speter * Public domain. 30417987Speter */ 30511571Sjoerg 30617987Speterstruct limits { 30717987Speter const char *name; 30818016Speter const char *units; 30917987Speter int cmd; 31017987Speter int factor; /* multiply by to get rlim_{cur,max} values */ 31117987Speter char option; 31217987Speter}; 31311571Sjoerg 31417987Speterstatic const struct limits limits[] = { 31517987Speter#ifdef RLIMIT_CPU 31618016Speter { "cpu time", "seconds", RLIMIT_CPU, 1, 't' }, 31717987Speter#endif 31817987Speter#ifdef RLIMIT_FSIZE 31918016Speter { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' }, 32017987Speter#endif 32117987Speter#ifdef RLIMIT_DATA 32218016Speter { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' }, 32317987Speter#endif 32417987Speter#ifdef RLIMIT_STACK 32518016Speter { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' }, 32617987Speter#endif 32717987Speter#ifdef RLIMIT_CORE 32818016Speter { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' }, 32917987Speter#endif 33017987Speter#ifdef RLIMIT_RSS 33118016Speter { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' }, 33217987Speter#endif 33317987Speter#ifdef RLIMIT_MEMLOCK 33418016Speter { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' }, 33517987Speter#endif 33617987Speter#ifdef RLIMIT_NPROC 33718016Speter { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' }, 33817987Speter#endif 33917987Speter#ifdef RLIMIT_NOFILE 34018016Speter { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' }, 34117987Speter#endif 34217987Speter#ifdef RLIMIT_VMEM 34318016Speter { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' }, 34417987Speter#endif 34517987Speter#ifdef RLIMIT_SWAP 34618016Speter { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' }, 34717987Speter#endif 34818016Speter { (char *) 0, (char *)0, 0, 0, '\0' } 34917987Speter}; 35017987Speter 35117987Speterint 35217987Speterulimitcmd(argc, argv) 35325905Ssteve int argc __unused; 35425905Ssteve char **argv __unused; 35517987Speter{ 35625222Ssteve int c; 35718018Speter quad_t val = 0; 35817987Speter enum { SOFT = 0x1, HARD = 0x2 } 35917987Speter how = SOFT | HARD; 36017987Speter const struct limits *l; 36117987Speter int set, all = 0; 36217987Speter int optc, what; 36317987Speter struct rlimit limit; 36411571Sjoerg 36517987Speter what = 'f'; 36618016Speter while ((optc = nextopt("HSatfdsmcnul")) != '\0') 36717987Speter switch (optc) { 36811571Sjoerg case 'H': 36917987Speter how = HARD; 37011571Sjoerg break; 37111571Sjoerg case 'S': 37217987Speter how = SOFT; 37311571Sjoerg break; 37411571Sjoerg case 'a': 37517987Speter all = 1; 37611571Sjoerg break; 37717987Speter default: 37817987Speter what = optc; 37911571Sjoerg } 38011571Sjoerg 38117987Speter for (l = limits; l->name && l->option != what; l++) 38217987Speter ; 38317987Speter if (!l->name) 38420425Ssteve error("ulimit: internal error (%c)", what); 38517987Speter 38617987Speter set = *argptr ? 1 : 0; 38717987Speter if (set) { 38817987Speter char *p = *argptr; 38917987Speter 39017987Speter if (all || argptr[1]) 39120425Ssteve error("ulimit: too many arguments"); 39217987Speter if (strcmp(p, "unlimited") == 0) 39311571Sjoerg val = RLIM_INFINITY; 39411571Sjoerg else { 39517987Speter val = (quad_t) 0; 39617987Speter 39717987Speter while ((c = *p++) >= '0' && c <= '9') 39817987Speter { 39917987Speter val = (val * 10) + (long)(c - '0'); 40017987Speter if (val < (quad_t) 0) 40117987Speter break; 40217987Speter } 40317987Speter if (c) 40420425Ssteve error("ulimit: bad number"); 40517987Speter val *= l->factor; 40611571Sjoerg } 40717987Speter } 40817987Speter if (all) { 40918016Speter for (l = limits; l->name; l++) { 41018016Speter char optbuf[40]; 41118016Speter if (getrlimit(l->cmd, &limit) < 0) 41220425Ssteve error("ulimit: can't get limit: %s", strerror(errno)); 41317987Speter if (how & SOFT) 41417987Speter val = limit.rlim_cur; 41517987Speter else if (how & HARD) 41617987Speter val = limit.rlim_max; 41717987Speter 41818016Speter if (l->units) 41918016Speter snprintf(optbuf, sizeof(optbuf), 42018019Speter "(%s, -%c) ", l->units, l->option); 42118016Speter else 42218016Speter snprintf(optbuf, sizeof(optbuf), 42318019Speter "(-%c) ", l->option); 42418019Speter out1fmt("%-18s %18s ", l->name, optbuf); 42517987Speter if (val == RLIM_INFINITY) 42617987Speter out1fmt("unlimited\n"); 42717987Speter else 42817987Speter { 42917987Speter val /= l->factor; 43018018Speter out1fmt("%qd\n", (quad_t) val); 43117987Speter } 43211571Sjoerg } 43317987Speter return 0; 43411571Sjoerg } 43517987Speter 43618016Speter if (getrlimit(l->cmd, &limit) < 0) 43720425Ssteve error("ulimit: can't get limit: %s", strerror(errno)); 43817987Speter if (set) { 43917987Speter if (how & SOFT) 44017987Speter limit.rlim_cur = val; 44117987Speter if (how & HARD) 44217987Speter limit.rlim_max = val; 44317987Speter if (setrlimit(l->cmd, &limit) < 0) 44420425Ssteve error("ulimit: bad limit: %s", strerror(errno)); 44517987Speter } else { 44617987Speter if (how & SOFT) 44717987Speter val = limit.rlim_cur; 44817987Speter else if (how & HARD) 44917987Speter val = limit.rlim_max; 45017987Speter 45117987Speter if (val == RLIM_INFINITY) 45217987Speter out1fmt("unlimited\n"); 45317987Speter else 45417987Speter { 45517987Speter val /= l->factor; 45618018Speter out1fmt("%qd\n", (quad_t) val); 45717987Speter } 45817987Speter } 45911571Sjoerg return 0; 46011571Sjoerg} 461