1 static char *sccsid = "@(#)dosys.c 4.3 (Berkeley) 82/04/20"; 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 95 96 doexec(str) 97 register char *str; 98 { 99 register char *t; 100 char *argv[200]; 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 *p++ = t; 112 while(*t!=' ' && *t!='\t' && *t!='\0') 113 ++t; 114 if(*t) 115 for( *t++ = '\0' ; *t==' ' || *t=='\t' ; ++t) 116 ; 117 } 118 119 *p = NULL; 120 121 if((waitpid = vfork()) == 0) 122 { 123 enbint(SIG_DFL); 124 doclose(); 125 enbint(intrupt); 126 execvp(str, argv); 127 fatal1("Cannot load %s",str); 128 } 129 130 return( await() ); 131 } 132 133 #include <errno.h> 134 135 #include <sys/stat.h> 136 137 138 139 140 touch(force, name) 141 int force; 142 char *name; 143 { 144 struct stat stbuff; 145 char junk[1]; 146 int fd; 147 148 #if vax 149 if (lstat(name, &stbuff) < 0) 150 #else 151 if( stat(name,&stbuff) < 0) 152 #endif 153 if(force) 154 goto create; 155 else 156 { 157 fprintf(stderr, "touch: file %s does not exist.\n", name); 158 return; 159 } 160 161 if(stbuff.st_size == 0) 162 goto create; 163 164 if( (fd = open(name, 2)) < 0) 165 goto bad; 166 167 if( read(fd, junk, 1) < 1) 168 { 169 close(fd); 170 goto bad; 171 } 172 lseek(fd, 0L, 0); 173 if( write(fd, junk, 1) < 1 ) 174 { 175 close(fd); 176 goto bad; 177 } 178 close(fd); 179 return; 180 181 bad: 182 fprintf(stderr, "Cannot touch %s\n", name); 183 return; 184 185 create: 186 if( (fd = creat(name, 0666)) < 0) 187 goto bad; 188 close(fd); 189 } 190