1 static char *sccsid = "@(#)dosys.c 4.13 (Berkeley) 02/28/91"; 2 #include "defs" 3 #include <signal.h> 4 5 dosys(comstring,nohalt) 6 register char *comstring; 7 int nohalt; 8 { 9 register int status; 10 11 if(metas(comstring)) 12 status = doshell(comstring,nohalt); 13 else status = doexec(comstring); 14 15 return(status); 16 } 17 18 19 20 metas(s) /* Are there are any Shell meta-characters? */ 21 register char *s; 22 { 23 register char c; 24 25 while( (funny[c = *s++] & META) == 0 ) 26 ; 27 return( c ); 28 } 29 30 doshell(comstring,nohalt) 31 char *comstring; 32 int nohalt; 33 { 34 #ifdef SHELLENV 35 char *getenv(), *rindex(); 36 char *shellcom = getenv("SHELL"); 37 char *shellstr; 38 #endif 39 if((waitpid = vfork()) == 0) 40 { 41 enbint(SIG_DFL); 42 doclose(); 43 44 #ifdef SHELLENV 45 if (shellcom == 0) shellcom = SHELLCOM; 46 shellstr = rindex(shellcom, '/') + 1; 47 execl(shellcom, shellstr, (nohalt ? "-c" : "-ec"), comstring, 0); 48 #else 49 execl(SHELLCOM, "sh", (nohalt ? "-c" : "-ec"), comstring, 0); 50 #endif 51 fatal("Couldn't load Shell"); 52 } 53 54 return( await() ); 55 } 56 57 58 59 60 void intrupt(); 61 62 await() 63 { 64 int status; 65 register int pid; 66 67 enbint(SIG_IGN); 68 while( (pid = wait(&status)) != waitpid) 69 if(pid == -1) 70 fatal("bad wait code"); 71 waitpid = 0; 72 enbint(intrupt); 73 return(status); 74 } 75 76 /* 77 * Close open directory files before exec'ing 78 */ 79 doclose() 80 { 81 register struct dirhdr *od; 82 83 for (od = firstod; od; od = od->nxtopendir) 84 if (od->dirfc != NULL) 85 /* 86 * vfork kludge... 87 * we cannot call closedir since this will modify 88 * the parents data space; just call close directly. 89 */ 90 (void)close(dirfd(od->dirfc)); 91 } 92 93 94 95 #define MAXARGV 400 96 97 doexec(str) 98 register char *str; 99 { 100 register char *t; 101 char *argv[MAXARGV]; 102 register char **p; 103 104 while( *str==' ' || *str=='\t' ) 105 ++str; 106 if( *str == '\0' ) 107 return(-1); /* no command */ 108 109 p = argv; 110 for(t = str ; *t ; ) 111 { 112 if (p >= argv + MAXARGV) 113 fatal1("%s: Too many arguments.", str); 114 *p++ = t; 115 while(*t!=' ' && *t!='\t' && *t!='\0') 116 ++t; 117 if(*t) 118 for( *t++ = '\0' ; *t==' ' || *t=='\t' ; ++t) 119 ; 120 } 121 122 *p = NULL; 123 124 if((waitpid = vfork()) == 0) 125 { 126 enbint(SIG_DFL); 127 doclose(); 128 enbint(intrupt); 129 execvp(str, argv); 130 fatal1("Cannot load %s",str); 131 } 132 133 return( await() ); 134 } 135 136 #include <errno.h> 137 138 #include <sys/stat.h> 139 140 141 142 touch(force, name) 143 int force; 144 char *name; 145 { 146 struct stat stbuff; 147 char junk[1]; 148 int fd; 149 150 if( stat(name,&stbuff) < 0) 151 if(force) 152 goto create; 153 else 154 { 155 fprintf(stderr, "touch: file %s does not exist.\n", name); 156 return; 157 } 158 159 if(stbuff.st_size == 0) 160 goto create; 161 162 if( (fd = open(name, 2)) < 0) 163 goto bad; 164 165 if( read(fd, junk, 1) < 1) 166 { 167 close(fd); 168 goto bad; 169 } 170 lseek(fd, 0L, 0); 171 if( write(fd, junk, 1) < 1 ) 172 { 173 close(fd); 174 goto bad; 175 } 176 close(fd); 177 return; 178 179 bad: 180 fprintf(stderr, "Cannot touch %s\n", name); 181 return; 182 183 create: 184 if( (fd = creat(name, 0666)) < 0) 185 goto bad; 186 close(fd); 187 } 188