|
|
1.1 ! root 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: /* cant use closedir - it changes our parent's data */ ! 87: if (od->dirfc != NULL) ! 88: close(od->dirfc->dd_fd); ! 89: } ! 90: ! 91: ! 92: ! 93: #define MAXARGV 400 ! 94: ! 95: doexec(str) ! 96: register char *str; ! 97: { ! 98: register char *t; ! 99: char *argv[MAXARGV]; ! 100: register char **p; ! 101: ! 102: while( *str==' ' || *str=='\t' ) ! 103: ++str; ! 104: if( *str == '\0' ) ! 105: return(-1); /* no command */ ! 106: ! 107: p = argv; ! 108: for(t = str ; *t ; ) ! 109: { ! 110: if (p >= argv + MAXARGV) ! 111: fatal1("%s: Too many arguments.", str); ! 112: *p++ = t; ! 113: while(*t!=' ' && *t!='\t' && *t!='\0') ! 114: ++t; ! 115: if(*t) ! 116: for( *t++ = '\0' ; *t==' ' || *t=='\t' ; ++t) ! 117: ; ! 118: } ! 119: ! 120: *p = NULL; ! 121: ! 122: if((waitpid = fork()) == 0) ! 123: { ! 124: enbint(SIG_DFL); ! 125: doclose(); ! 126: enbint(intrupt); ! 127: execvp(str, argv); ! 128: fatal1("Cannot load %s",str); ! 129: } ! 130: ! 131: return( await() ); ! 132: } ! 133: ! 134: #include <errno.h> ! 135: ! 136: #include <sys/stat.h> ! 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( stat(name,&stbuff) < 0) ! 149: if(force) ! 150: goto create; ! 151: else ! 152: { ! 153: fprintf(stderr, "touch: file %s does not exist.\n", name); ! 154: return; ! 155: } ! 156: ! 157: if(stbuff.st_size == 0) ! 158: goto create; ! 159: ! 160: if( (fd = open(name, 2)) < 0) ! 161: goto bad; ! 162: ! 163: if( read(fd, junk, 1) < 1) ! 164: { ! 165: close(fd); ! 166: goto bad; ! 167: } ! 168: lseek(fd, 0L, 0); ! 169: if( write(fd, junk, 1) < 1 ) ! 170: { ! 171: close(fd); ! 172: goto bad; ! 173: } ! 174: close(fd); ! 175: return; ! 176: ! 177: bad: ! 178: fprintf(stderr, "Cannot touch %s\n", name); ! 179: return; ! 180: ! 181: create: ! 182: if( (fd = creat(name, 0666)) < 0) ! 183: goto bad; ! 184: close(fd); ! 185: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.