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