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