* add p cc
[mascara-docs.git] / compilers / pcc / pcc-libs-1.0.0 / libI77 / open.c
blob55175aed0f7d3d66fc4a4258ff3303060947bf7d
1 /* $Id: open.c,v 1.5 2008/05/06 10:09:52 ragge Exp $ */
2 /*
3 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * Redistributions of source code and documentation must retain the above
10 * copyright notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditionsand the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed or owned by Caldera
17 * International, Inc.
18 * Neither the name of Caldera International, Inc. nor the names of other
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
22 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
36 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 #include "fio.h"
45 static int isdev(char *s);
46 int canseek(FILE *f);
49 int
50 f_open(olist *a)
51 { unit *b;
52 int n;
53 char buf[256];
54 cllist x;
55 if(a->ounit>=MXUNIT || a->ounit<0)
56 err(a->oerr,101,"open")
57 b= &units[a->ounit];
58 if(b->ufd!=0) goto connected;
59 unconnected:
60 b->url=a->orl;
61 if(a->oblnk && *a->oblnk=='b') b->ublnk=1;
62 else b->ublnk=0;
63 if(a->ofm==0)
64 { if(b->url>0) b->ufmt=0;
65 else b->ufmt=1;
67 else if(*a->ofm=='f') b->ufmt=1;
68 else b->ufmt=0;
69 if(a->osta==0) goto unknown;
70 switch(*a->osta)
72 unknown:
73 default:
74 case 'o':
75 if(a->ofnm==0) err(a->oerr,107,"open")
76 g_char(a->ofnm,a->ofnmlen,buf);
77 b->uscrtch=0;
78 if(a->osta && *a->osta=='o' && access(buf,0))
79 err(a->oerr,errno,"open")
80 done:
81 b->ufnm=(char *) calloc(strlen(buf)+1,sizeof(char));
82 if(b->ufnm==NULL) err(a->oerr,113,"no space");
83 strcpy(b->ufnm,buf);
84 b->uend=0;
85 if(isdev(buf))
86 { b->ufd = fopen(buf,"r");
87 if(b->ufd==NULL) err(a->oerr,errno,buf)
88 else b->uwrt = 0;
90 else
91 { b->ufd = fopen(buf, "a");
92 if(b->ufd != NULL) b->uwrt = 1;
93 else if((b->ufd = fopen(buf, "r")) != NULL)
94 { fseek(b->ufd, 0L, 2);
95 b->uwrt = 0;
97 else err(a->oerr, errno, buf)
99 b->useek=canseek(b->ufd);
100 if((b->uinode=inode(buf))==-1)
101 err(a->oerr,108,"open")
102 if(a->orl && b->useek) rewind(b->ufd);
103 return(0);
104 case 's':
105 b->uscrtch=1;
106 strcpy(buf,"tmp.FXXXXXX");
107 close(mkstemp(buf));
108 goto done;
109 case 'n':
110 b->uscrtch=0;
111 if(a->ofnm==0) err(a->oerr,107,"open")
112 g_char(a->ofnm,a->ofnmlen,buf);
113 /*SYSDEP access*/
114 if(access(buf, 0) == 0) creat(buf, 0666);
115 goto done;
117 connected:
118 if(a->ofnm==0)
120 same: if(a->oblnk!= 0) b->ublnk= *a->oblnk== 'b'?0:1;
121 return(0);
123 g_char(a->ofnm,a->ofnmlen,buf);
124 if(inode(buf)==b->uinode) goto same;
125 x.cunit=a->ounit;
126 x.csta=0;
127 x.cerr=a->oerr;
128 if((n=f_clos(&x))!=0) return(n);
129 goto unconnected;
133 fk_open(int rd,int seq,int fmt, ftnint n)
135 char nbuf[10];
136 olist a;
137 sprintf(nbuf,"fort.%ld",n);
138 a.oerr=1;
139 a.ounit=n;
140 a.ofnm=nbuf;
141 a.ofnmlen=strlen(nbuf);
142 a.osta=NULL;
143 a.oacc= seq==SEQ?"s":"d";
144 a.ofm = fmt==FMT?"f":"u";
145 a.orl = seq==DIR?1:0;
146 a.oblnk=NULL;
147 return(f_open(&a));
151 isdev(char *s)
152 { struct stat x;
153 int j;
154 if(stat(s, &x) == -1) return(0);
155 if((j = (x.st_mode&S_IFMT)) == S_IFREG || j == S_IFDIR) return(0);
156 else return(1);