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