147129Sbostic /*- 260698Sbostic * Copyright (c) 1991, 1993 360698Sbostic * The Regents of the University of California. All rights reserved. 447129Sbostic * 547129Sbostic * This code is derived from software contributed to Berkeley by 647129Sbostic * Kenneth Almquist. 747129Sbostic * 847129Sbostic * %sccs.include.redist.c% 947129Sbostic */ 1047129Sbostic 1147129Sbostic #ifndef lint 12*68925Sbostic static char sccsid[] = "@(#)miscbltin.c 8.3 (Berkeley) 04/27/95"; 1347129Sbostic #endif /* not lint */ 1447129Sbostic 1547129Sbostic /* 1647129Sbostic * Miscelaneous builtins. 1747129Sbostic */ 1847129Sbostic 19*68925Sbostic #include <sys/types.h> 20*68925Sbostic #include <sys/stat.h> 2147129Sbostic #include "shell.h" 2247129Sbostic #include "options.h" 2347129Sbostic #include "var.h" 2447129Sbostic #include "output.h" 2547129Sbostic #include "memalloc.h" 2647129Sbostic #include "error.h" 2747129Sbostic #include "mystring.h" 2847129Sbostic 2947129Sbostic #undef eflag 3047129Sbostic 3147129Sbostic extern char **argptr; /* argument list for builtin command */ 3247129Sbostic 3347129Sbostic 3447129Sbostic /* 3547129Sbostic * The read builtin. The -e option causes backslashes to escape the 3647129Sbostic * following character. 3747129Sbostic * 3847129Sbostic * This uses unbuffered input, which may be avoidable in some cases. 3947129Sbostic */ 4047129Sbostic 4147129Sbostic readcmd(argc, argv) char **argv; { 4247129Sbostic char **ap; 4347129Sbostic int backslash; 4447129Sbostic char c; 4547129Sbostic int eflag; 4647129Sbostic char *prompt; 4747129Sbostic char *ifs; 4847129Sbostic char *p; 4947129Sbostic int startword; 5047129Sbostic int status; 5147129Sbostic int i; 5247129Sbostic 5347129Sbostic eflag = 0; 5447129Sbostic prompt = NULL; 5547297Smarc while ((i = nextopt("ep:")) != '\0') { 5647129Sbostic if (i == 'p') 5747129Sbostic prompt = optarg; 5847129Sbostic else 5947129Sbostic eflag = 1; 6047129Sbostic } 6147129Sbostic if (prompt && isatty(0)) { 6247129Sbostic out2str(prompt); 6347129Sbostic flushall(); 6447129Sbostic } 6566823Sbostic if (*(ap = argptr) == NULL) 6647129Sbostic error("arg count"); 6747129Sbostic if ((ifs = bltinlookup("IFS", 1)) == NULL) 6847129Sbostic ifs = nullstr; 6947129Sbostic status = 0; 7047129Sbostic startword = 1; 7147129Sbostic backslash = 0; 7247129Sbostic STARTSTACKSTR(p); 7347129Sbostic for (;;) { 7447129Sbostic if (read(0, &c, 1) != 1) { 7547129Sbostic status = 1; 7647129Sbostic break; 7747129Sbostic } 7847129Sbostic if (c == '\0') 7947129Sbostic continue; 8047129Sbostic if (backslash) { 8147129Sbostic backslash = 0; 8247129Sbostic if (c != '\n') 8347129Sbostic STPUTC(c, p); 8447129Sbostic continue; 8547129Sbostic } 8647129Sbostic if (eflag && c == '\\') { 8747129Sbostic backslash++; 8847129Sbostic continue; 8947129Sbostic } 9047129Sbostic if (c == '\n') 9147129Sbostic break; 9247129Sbostic if (startword && *ifs == ' ' && strchr(ifs, c)) { 9347129Sbostic continue; 9447129Sbostic } 9547129Sbostic startword = 0; 9647129Sbostic if (backslash && c == '\\') { 9747129Sbostic if (read(0, &c, 1) != 1) { 9847129Sbostic status = 1; 9947129Sbostic break; 10047129Sbostic } 10147129Sbostic STPUTC(c, p); 10247129Sbostic } else if (ap[1] != NULL && strchr(ifs, c) != NULL) { 10347129Sbostic STACKSTRNUL(p); 10447129Sbostic setvar(*ap, stackblock(), 0); 10547129Sbostic ap++; 10647129Sbostic startword = 1; 10747129Sbostic STARTSTACKSTR(p); 10847129Sbostic } else { 10947129Sbostic STPUTC(c, p); 11047129Sbostic } 11147129Sbostic } 11247129Sbostic STACKSTRNUL(p); 11347129Sbostic setvar(*ap, stackblock(), 0); 11447129Sbostic while (*++ap != NULL) 11547129Sbostic setvar(*ap, nullstr, 0); 11647129Sbostic return status; 11747129Sbostic } 11847129Sbostic 11947129Sbostic 12047129Sbostic 12147129Sbostic umaskcmd(argc, argv) char **argv; { 122*68925Sbostic extern void *setmode(); 123*68925Sbostic extern mode_t getmode(); 124*68925Sbostic char *ap; 12547129Sbostic int mask; 12647129Sbostic int i; 127*68925Sbostic int symbolic_mode = 0; 12847129Sbostic 129*68925Sbostic while ((i = nextopt("S")) != '\0') { 130*68925Sbostic symbolic_mode = 1; 131*68925Sbostic } 132*68925Sbostic 133*68925Sbostic INTOFF; 134*68925Sbostic mask = umask(0); 135*68925Sbostic umask(mask); 136*68925Sbostic INTON; 137*68925Sbostic 138*68925Sbostic if ((ap = *argptr) == NULL) { 139*68925Sbostic if (symbolic_mode) { 140*68925Sbostic char u[4], g[4], o[4]; 141*68925Sbostic 142*68925Sbostic i = 0; 143*68925Sbostic if ((mask & S_IRUSR) == 0) 144*68925Sbostic u[i++] = 'r'; 145*68925Sbostic if ((mask & S_IWUSR) == 0) 146*68925Sbostic u[i++] = 'w'; 147*68925Sbostic if ((mask & S_IXUSR) == 0) 148*68925Sbostic u[i++] = 'x'; 149*68925Sbostic u[i] = '\0'; 150*68925Sbostic 151*68925Sbostic i = 0; 152*68925Sbostic if ((mask & S_IRGRP) == 0) 153*68925Sbostic g[i++] = 'r'; 154*68925Sbostic if ((mask & S_IWGRP) == 0) 155*68925Sbostic g[i++] = 'w'; 156*68925Sbostic if ((mask & S_IXGRP) == 0) 157*68925Sbostic g[i++] = 'x'; 158*68925Sbostic g[i] = '\0'; 159*68925Sbostic 160*68925Sbostic i = 0; 161*68925Sbostic if ((mask & S_IROTH) == 0) 162*68925Sbostic o[i++] = 'r'; 163*68925Sbostic if ((mask & S_IWOTH) == 0) 164*68925Sbostic o[i++] = 'w'; 165*68925Sbostic if ((mask & S_IXOTH) == 0) 166*68925Sbostic o[i++] = 'x'; 167*68925Sbostic o[i] = '\0'; 168*68925Sbostic 169*68925Sbostic out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 170*68925Sbostic } else { 171*68925Sbostic out1fmt("%.4o\n", mask); 172*68925Sbostic } 17347129Sbostic } else { 174*68925Sbostic if (isdigit(*ap)) { 175*68925Sbostic mask = 0; 176*68925Sbostic do { 177*68925Sbostic if (*ap >= '8' || *ap < '0') 178*68925Sbostic error("Illegal number: %s", argv[1]); 179*68925Sbostic mask = (mask << 3) + (*ap - '0'); 180*68925Sbostic } while (*++ap != '\0'); 181*68925Sbostic umask(mask); 182*68925Sbostic } else { 183*68925Sbostic void *set; 184*68925Sbostic if ((set = setmode (ap)) == 0) 185*68925Sbostic error("Illegal number: %s", ap); 186*68925Sbostic 187*68925Sbostic mask = getmode (set, ~mask & 0777); 188*68925Sbostic umask(~mask & 0777); 189*68925Sbostic } 19047129Sbostic } 19147129Sbostic return 0; 19247129Sbostic } 193