1 static char *sccsid = "@(#)dosys.c 4.7 (Berkeley) 83/06/22"; 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" : "-ce"), comstring, 0); 48 #else 49 execl(SHELLCOM, "sh", (nohalt ? "-c" : "-ce"), comstring, 0); 50 #endif 51 fatal("Couldn't load Shell"); 52 } 53 54 return( await() ); 55 } 56 57 58 59 60 int 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 78 79 80 81 doclose() /* Close open directory files before exec'ing */ 82 { 83 register struct dirhdr *od; 84 85 for (od = firstod; od; od = od->nxtopendir) 86 if (od->dirfc != NULL) { 87 closedir(od->dirfc); 88 od->dirfc = NULL; 89 } 90 } 91 92 93 94 #define MAXARGV 400 95 96 doexec(str) 97 register char *str; 98 { 99 register char *t; 100 char *argv[MAXARGV]; 101 register char **p; 102 103 while( *str==' ' || *str=='\t' ) 104 ++str; 105 if( *str == '\0' ) 106 return(-1); /* no command */ 107 108 p = argv; 109 for(t = str ; *t ; ) 110 { 111 if (p >= argv + MAXARGV) 112 fatal1("%s: Too many arguments.", str); 113 *p++ = t; 114 while(*t!=' ' && *t!='\t' && *t!='\0') 115 ++t; 116 if(*t) 117 for( *t++ = '\0' ; *t==' ' || *t=='\t' ; ++t) 118 ; 119 } 120 121 *p = NULL; 122 123 if((waitpid = fork()) == 0) 124 { 125 enbint(SIG_DFL); 126 doclose(); 127 enbint(intrupt); 128 execvp(str, argv); 129 fatal1("Cannot load %s",str); 130 } 131 132 return( await() ); 133 } 134 135 #include <errno.h> 136 137 #include <sys/stat.h> 138 139 140 141 touch(force, name) 142 int force; 143 char *name; 144 { 145 struct stat stbuff; 146 char junk[1]; 147 int fd; 148 149 if( stat(name,&stbuff) < 0) 150 if(force) 151 goto create; 152 else 153 { 154 fprintf(stderr, "touch: file %s does not exist.\n", name); 155 return; 156 } 157 158 if(stbuff.st_size == 0) 159 goto create; 160 161 if( (fd = open(name, 2)) < 0) 162 goto bad; 163 164 if( read(fd, junk, 1) < 1) 165 { 166 close(fd); 167 goto bad; 168 } 169 lseek(fd, 0L, 0); 170 if( write(fd, junk, 1) < 1 ) 171 { 172 close(fd); 173 goto bad; 174 } 175 close(fd); 176 return; 177 178 bad: 179 fprintf(stderr, "Cannot touch %s\n", name); 180 return; 181 182 create: 183 if( (fd = creat(name, 0666)) < 0) 184 goto bad; 185 close(fd); 186 } 187