Annotation of 40BSD/lib/libI77/open.c, revision 1.1.1.1

1.1       root        1: #include       "sys/types.h"
                      2: #include       "sys/stat.h"
                      3: #include "fio.h"
                      4: extern char *mktemp(), *malloc(), *strcpy();
                      5: f_open(a) olist *a;
                      6: {      unit *b;
                      7:        int n;
                      8:        char buf[256];
                      9:        cllist x;
                     10:        if(a->ounit>=MXUNIT || a->ounit<0)
                     11:                err(a->oerr,101,"open")
                     12:        b= &units[a->ounit];
                     13:        if(b->ufd!=0) goto connected;
                     14: unconnected:
                     15:        b->url=a->orl;
                     16:        if(a->oblnk && *a->oblnk=='b') b->ublnk=1;
                     17:        else b->ublnk=0;
                     18:        if(a->ofm==0)
                     19:        {       if(b->url>0) b->ufmt=0;
                     20:                else b->ufmt=1;
                     21:        }
                     22:        else if(*a->ofm=='f') b->ufmt=1;
                     23:        else b->ufmt=0;
                     24:        if(a->osta==0) goto unknown;
                     25:        switch(*a->osta)
                     26:        {
                     27:        unknown:
                     28:        default:
                     29:        case 'o':
                     30:                if(a->ofnm==0) err(a->oerr,107,"open")
                     31:                g_char(a->ofnm,a->ofnmlen,buf);
                     32:                b->uscrtch=0;
                     33:                if(a->osta == 0)
                     34:                        goto done;
                     35:                if(*a->osta=='o' && access(buf,0))
                     36:                        err(a->oerr,errno,"open")
                     37:        done:
                     38:                b->ufnm=(char *) malloc((unsigned int)(strlen(buf)+1));
                     39:                if(b->ufnm==NULL) err(a->oerr,113,"no space");
                     40:                (void) strcpy(b->ufnm,buf);
                     41:                b->uend=0;
                     42:                if(isdev(buf))
                     43:                {       b->ufd = fopen(buf,"r");
                     44:                        if(b->ufd==NULL) err(a->oerr,errno,buf)
                     45:                        else    b->uwrt = 0;
                     46:                }
                     47:                else
                     48:                {       b->ufd = fopen(buf, "a");
                     49:                        if(b->ufd != NULL) b->uwrt = 1;
                     50:                        else if((b->ufd = fopen(buf, "r")) != NULL)
                     51:                        {       (void) fseek(b->ufd, 0L, 2);
                     52:                                b->uwrt = 0;
                     53:                        }
                     54:                        else    err(a->oerr, errno, buf)
                     55:                }
                     56:                b->useek=canseek(b->ufd);
                     57:                if((b->uinode=inode(buf))==-1)
                     58:                        err(a->oerr,108,"open")
                     59:                if(a->orl && b->useek) rewind(b->ufd);
                     60:                return(0);
                     61:         case 's':
                     62:                b->uscrtch=1;
                     63:                (void) strcpy(buf,"tmp.FXXXXXX");
                     64:                (void) mktemp(buf);
                     65:                goto done;
                     66:        case 'n':
                     67:                b->uscrtch=0;
                     68:                if(a->ofnm==0) err(a->oerr,107,"open")
                     69:                g_char(a->ofnm,a->ofnmlen,buf);
                     70:                /*SYSDEP access*/
                     71:                if(access(buf, 0) == -1) (void) close(creat(buf, 0666));
                     72:                goto done;
                     73:        }
                     74: connected:
                     75:        if(a->ofnm==0)
                     76:        {
                     77:        same:   if(a->oblnk!= 0) b->ublnk= *a->oblnk== 'b'?0:1;
                     78:                return(0);
                     79:        }
                     80:        g_char(a->ofnm,a->ofnmlen,buf);
                     81:        if(inode(buf)==b->uinode) goto same;
                     82:        x.cunit=a->ounit;
                     83:        x.csta=0;
                     84:        x.cerr=a->oerr;
                     85:        if((n=f_clos(&x))!=0) return(n);
                     86:        goto unconnected;
                     87: }
                     88: fk_open(seq,fmt,n) ftnint n;
                     89: {      char nbuf[10];
                     90:        olist a;
                     91:        (void) sprintf(nbuf,"fort.%ld",n);
                     92:        a.oerr=1;
                     93:        a.ounit=n;
                     94:        a.ofnm=nbuf;
                     95:        a.ofnmlen=strlen(nbuf);
                     96:        a.osta=NULL;
                     97:        a.oacc= seq==SEQ?"s":"d";
                     98:        a.ofm = fmt==FMT?"f":"u";
                     99:        a.orl = seq==DIR?1:0;
                    100:        a.oblnk=NULL;
                    101:        return(f_open(&a));
                    102: }
                    103: isdev(s) char *s;
                    104: {      struct stat x;
                    105:        int j;
                    106:        if(stat(s, &x) == -1) return(0);
                    107:        if((j = (x.st_mode&S_IFMT)) == S_IFREG || j == S_IFDIR) return(0);
                    108:        else    return(1);
                    109: }

unix.superglobalmegacorp.com

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