Annotation of researchv9/jtools/src/sam/io.c, revision 1.1.1.1

1.1       root        1: #include "sam.h"
                      2: #include <sys/ioctl.h>
                      3: #ifdef SUN
                      4: /* a hack to get around redef of ushort in sys/types.h .
                      5:    this depends on non-use of ushort in this file */
                      6: #define ushort USHORT
                      7: #endif
                      8: #include <sys/types.h>
                      9: #include <sys/stat.h>
                     10: #ifndef SUN
                     11: #include "/usr/jerq/include/jioctl.h"
                     12: #endif
                     13: 
                     14: writef(f)
                     15:        register File *f;
                     16: {
                     17:        uchar c;
                     18:        Posn n;
                     19:        struct stat statb;
                     20:        int newfile=0;
                     21:        if(stat((char *)genstr.s, &statb)==-1)
                     22:                newfile++;
                     23:        else if(strcmp(genstr.s, f->name.s)==0 &&
                     24:                (f->inumber!=statb.st_ino || f->date<statb.st_mtime)){
                     25:                f->inumber=statb.st_ino;
                     26:                f->date=statb.st_mtime;
                     27:                warn_s(Wdate, (char *)genstr.s);
                     28:                return;
                     29:        }
                     30:        if((io=creat((char *)genstr.s, 0666))<0)
                     31:                error_s(Ecreate, (char *)genstr.s);
                     32:        dprint("%s: ", (char *)genstr.s);
                     33:        n=writeio(f);
                     34:        if(f->name.s[0]==0 || strcmp(genstr.s, f->name.s)==0)
                     35:                state(f, addr.r.p1==0 && addr.r.p2==f->nbytes? Clean : Dirty);
                     36:        if(newfile)
                     37:                dprint("(new file) ");
                     38:        if(addr.r.p2>0 && Fchars(f, &c, addr.r.p2-1, addr.r.p2) && c!='\n')
                     39:                warn(Wnotnewline);
                     40:        if(f->name.s[0]==0 || strcmp(genstr.s, f->name.s)==0){
                     41:                fstat(io, &statb);
                     42:                f->inumber=statb.st_ino;
                     43:                f->date=statb.st_mtime;
                     44:        }
                     45:        closeio(n);
                     46: }
                     47: Posn
                     48: readio(f, nonascii, setdate)
                     49:        register File *f;
                     50:        int *nonascii;
                     51: {
                     52:        register n;
                     53:        register Posn nt;
                     54:        register Posn p=addr.r.p2;
                     55:        struct stat statb;
                     56:        if(nonascii)
                     57:                *nonascii=FALSE;
                     58:        for(nt=0; (n=read(io, (char *)genbuf, BLOCKSIZE))>0; nt+=n){
                     59:                n=clean(genbuf, n, nonascii);
                     60:                Finsert(f, tempstr(genbuf, n), p);
                     61:        }
                     62:        if(nonascii && *nonascii)
                     63:                warn(Wnonascii);
                     64:        if(setdate){
                     65:                fstat(io, &statb);
                     66:                f->inumber=statb.st_ino;
                     67:                f->date=statb.st_mtime;
                     68:        }
                     69:        return nt;
                     70: }
                     71: Posn
                     72: writeio(f)
                     73:        register File *f;
                     74: {
                     75:        register n;
                     76:        register Posn p=addr.r.p1;
                     77:        while(p<addr.r.p2){
                     78:                if(addr.r.p2-p>BLOCKSIZE)
                     79:                        n=BLOCKSIZE;
                     80:                else
                     81:                        n=addr.r.p2-p;
                     82:                if(Fchars(f, genbuf, p, p+n)!=n)
                     83:                        panic("writef read");
                     84:                (void)Write(io, genbuf, n);
                     85:                p+=n;
                     86:        }
                     87:        return addr.r.p2-addr.r.p1;
                     88: }
                     89: closeio(p)
                     90:        Posn p;
                     91: {
                     92:        (void)close(io);
                     93:        io=0;
                     94:        if(p>=0)
                     95:                dprint("#%lud\n", p);
                     96: }
                     97: clean(s, n, nonascii)
                     98:        register uchar *s;
                     99:        register n;
                    100:        int *nonascii;
                    101: {
                    102:        register uchar *p, *q;
                    103:        register i;
                    104:        /* for speed, just look first */
                    105:        p=s;
                    106:        if(i=n)
                    107:                do
                    108:                        if(*p==0 || (*p++&HIGHBIT))
                    109:                                goto Cleanup;
                    110:                while(--i);
                    111:        return n;       /* usual case */
                    112:     Cleanup:
                    113:        for(q=p=s, i=0; i<n; i++, p++)
                    114:                if(*p!=0 && (*p&HIGHBIT)==0)
                    115:                        *q++= *p;
                    116:        if(nonascii)
                    117:                *nonascii=TRUE;
                    118:        return q-s;
                    119: }
                    120: #ifndef SUN
                    121: bootterm(zflag)
                    122: {
                    123:        if(system("/usr/jerq/bin/32ld", "32ld",
                    124: #ifdef DIST
                    125:            zflag? "-z" : "-", "/usr/jerq/mbin/sam.m")){
                    126: #else
                    127:            zflag? "-z" : "-", "/n/ikeya/usr/rob/sam/term/a.out")){
                    128: #endif
                    129:                dprint("sam: can't boot terminal program\n");
                    130:                return 0;
                    131:        }
                    132:        return 1;
                    133: }
                    134: #endif !SUN
                    135: rawmode(raw)
                    136: {
                    137:        struct sgttyb buf;
                    138:        ioctl(0, TIOCGETP, &buf);
                    139:        if(raw){
                    140:                ioctl(0, TIOCEXCL, (struct sgttyb *)0);
                    141:                buf.sg_flags|=RAW|ANYP;
                    142:        }else{
                    143:                ioctl(0, TIOCNXCL, (struct sgttyb *)0);
                    144: #ifndef SUN
                    145:                ioctl(0, JTERM, (struct sgttyb *)0);
                    146: #endif
                    147:                buf.sg_flags&=~(RAW|ANYP);
                    148:        }
                    149:        ioctl(0, TIOCSETP, &buf);
                    150: }
                    151: system(s, t, u, v)
                    152:        char *s, *t, *u, *v;
                    153: {
                    154:        int status, pid, l;
                    155:        if((pid=fork()) == 0) {
                    156:                execl(s, t, u, v, 0);
                    157:                _exit(127);
                    158:        }
                    159:        do; while((l=wait(&status))!=pid && l!=-1);
                    160:        if(l==-1)
                    161:                status= -1;
                    162:        return(status);
                    163: }
                    164: closeonexec(fd){
                    165:        ioctl(fd, FIOCLEX, (struct sgttyb *)0);
                    166: }
                    167: #ifndef SUN
                    168: static int     remotefd;
                    169: connectto(machine)
                    170:        char *machine;
                    171: {
                    172:        if(strncmp((uchar *)machine, (uchar *)"inet/", 5)==0)
                    173:                remotefd=tcpexec(machine+5, "exec /usr/jerq/bin/sam -R");
                    174:        else
                    175:                remotefd=tdkexec(machine, "exec /usr/jerq/bin/sam -R");
                    176:        if(remotefd<0){
                    177:                dprint("sam: can't call %s\n", machine);
                    178:                exit(1);
                    179:        }
                    180: }
                    181: join(){
                    182:        switch(fork()){
                    183:        case 0:
                    184:                close(0);
                    185:                dup(remotefd);
                    186:                execl("/bin/cat", 0);
                    187:        default:
                    188:                close(1);
                    189:                dup(remotefd);
                    190:                execl("/bin/cat", 0);
                    191:        case -1:
                    192:                dprint("sam: can't fork\n");
                    193:        }
                    194:        exit(1);
                    195: }
                    196: 
                    197: int
                    198: tcpexec(host, cmd)
                    199:        char *host;
                    200:        char *cmd;
                    201: {
                    202:        int pfd[2];
                    203: 
                    204:        if (pipe(pfd)<0)
                    205:                return -1;
                    206:        switch(fork()) {
                    207:        case -1:
                    208:                return -1;
                    209:        case 0:
                    210:                break;
                    211:        default:
                    212:                close(pfd[0]);
                    213:                return(pfd[1]);
                    214:        }
                    215:        close(pfd[1]);
                    216:        if (dup2(pfd[0], 0)<0 || dup2(pfd[0], 1)<0 || dup2(pfd[0], 2)<0) {
                    217:                dprint("sam: can't redirect\n");
                    218:                exit(1);
                    219:        }
                    220:        close(pfd[0]);
                    221:        execl("/usr/inet/bin/rsh", host, cmd, 0);
                    222:        dprint("sam: can't exec rsh\n");
                    223:        exit(1);
                    224:        return -1;      /* for cyntax */
                    225: }
                    226: #endif !SUN

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.