1 static char *sccsid = "@(#)dosys.c 4.8 (Berkeley) 84/03/21"; 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 43 #ifdef SHELLENV 44 if (shellcom == 0) shellcom = SHELLCOM; 45 shellstr = rindex(shellcom, '/') + 1; 46 execl(shellcom, shellstr, (nohalt ? "-c" : "-ce"), comstring, 0); 47 #else 48 execl(SHELLCOM, "sh", (nohalt ? "-c" : "-ce"), comstring, 0); 49 #endif 50 fatal("Couldn't load Shell"); 51 } 52 53 return( await() ); 54 } 55 56 57 58 59 int intrupt(); 60 61 await() 62 { 63 int status; 64 register int pid; 65 66 enbint(SIG_IGN); 67 while( (pid = wait(&status)) != waitpid) 68 if(pid == -1) 69 fatal("bad wait code"); 70 waitpid = 0; 71 enbint(intrupt); 72 return(status); 73 } 74 75 76 77 #define MAXARGV 400 78 79 doexec(str) 80 register char *str; 81 { 82 register char *t; 83 char *argv[MAXARGV]; 84 register char **p; 85 86 while( *str==' ' || *str=='\t' ) 87 ++str; 88 if( *str == '\0' ) 89 return(-1); /* no command */ 90 91 p = argv; 92 for(t = str ; *t ; ) 93 { 94 if (p >= argv + MAXARGV) 95 fatal1("%s: Too many arguments.", str); 96 *p++ = t; 97 while(*t!=' ' && *t!='\t' && *t!='\0') 98 ++t; 99 if(*t) 100 for( *t++ = '\0' ; *t==' ' || *t=='\t' ; ++t) 101 ; 102 } 103 104 *p = NULL; 105 106 if((waitpid = vfork()) == 0) 107 { 108 enbint(SIG_DFL); 109 enbint(intrupt); 110 execvp(str, argv); 111 fatal1("Cannot load %s",str); 112 } 113 114 return( await() ); 115 } 116 117 #include <errno.h> 118 119 #include <sys/stat.h> 120 121 122 123 touch(force, name) 124 int force; 125 char *name; 126 { 127 struct stat stbuff; 128 char junk[1]; 129 int fd; 130 131 if( stat(name,&stbuff) < 0) 132 if(force) 133 goto create; 134 else 135 { 136 fprintf(stderr, "touch: file %s does not exist.\n", name); 137 return; 138 } 139 140 if(stbuff.st_size == 0) 141 goto create; 142 143 if( (fd = open(name, 2)) < 0) 144 goto bad; 145 146 if( read(fd, junk, 1) < 1) 147 { 148 close(fd); 149 goto bad; 150 } 151 lseek(fd, 0L, 0); 152 if( write(fd, junk, 1) < 1 ) 153 { 154 close(fd); 155 goto bad; 156 } 157 close(fd); 158 return; 159 160 bad: 161 fprintf(stderr, "Cannot touch %s\n", name); 162 return; 163 164 create: 165 if( (fd = creat(name, 0666)) < 0) 166 goto bad; 167 close(fd); 168 } 169