source: buchla-68k/libcio/open.c@ 9519422

Last change on this file since 9519422 was 7258c6a, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Use standard integer types.

  • Property mode set to 100644
File size: 6.6 KB
Line 
1/*
2 =============================================================================
3 open.c -- open a file for the Buchla 700 C I/O library functions
4 Version 11 -- 1988-01-31 -- D.N. Lynx Crowe
5 =============================================================================
6*/
7
8#include "biosdefs.h"
9#include "errno.h"
10#include "fcntl.h"
11#include "io.h"
12#include "stddefs.h"
13
14extern int16_t _badfd(void);
15extern int16_t _noper(void);
16extern int16_t _inifcb(struct fcb *fcp, int8_t *name, int8_t *ext, int16_t mode);
17extern int16_t _opfcb(struct fcb *fcp);
18
19extern int8_t *FilName(int8_t *s, int8_t *p);
20extern int8_t *FilExt(int8_t *s, int8_t *p);
21
22int16_t _fileop(int8_t *name, int16_t flag, int16_t mode, struct channel *chp, struct devtabl *dp);
23int16_t _filecl(struct fcb *fp);
24
25static struct device condev = { 2, 2, 1, 0, _noper };
26static struct device filedev = { 1, 1, 0, 1, _fileop };
27
28extern struct fcb _fcbtab[];
29
30/*
31 =============================================================================
32 device table: contains names and pointers to device control structures
33 =============================================================================
34*/
35
36static struct devtabl devtabl[] = {
37
38 { "con:", &condev, 2 }, /* console device */
39 { "CON:", &condev, 2 },
40
41 { 0, &filedev, 0 } /* this MUST be the last entry */
42};
43
44/*
45
46*/
47
48/*
49 =============================================================================
50 open(name, flag, mode) -- Opens file 'name' with flags 'flag'
51 and access mode 'mode'. File will be ASCII unless opened with O_RAW.
52 Returns a file descriptor (small positive integer) if successful, or
53 FAILURE (-1) if an error occurred.
54 =============================================================================
55*/
56
57int16_t open(int8_t *name, int16_t flag, int16_t mode)
58{
59 register struct devtabl *dp;
60 register struct channel *chp;
61 register struct device *dev;
62 int16_t fd, mdmask;
63
64 /* search for a free channel */
65
66 for (chp = chantab, fd = 0 ; fd < MAXCHAN ; ++chp, ++fd)
67 if (chp->c_close EQ _badfd)
68 goto fndchan;
69
70 errno = EMFILE; /* no channels available */
71 return(FAILURE);
72
73/*
74
75*/
76
77fndchan: /* found a channel to use */
78
79 for (dp = devtabl; dp->d_name; ++dp) /* search for the device */
80 if (strcmp(dp->d_name, name) EQ 0)
81 break;
82
83 dev = dp->d_dev;
84 mdmask = (flag & 3) + 1;
85
86 if (mdmask & 1) { /* see if device is readable */
87
88 if ((chp->c_read = dev->d_read) EQ 0) {
89
90 errno = EACCES; /* can't read */
91 return(FAILURE);
92 }
93 }
94
95 if (mdmask & 2) { /* see if device is writeable */
96
97 if ((chp->c_write = dev->d_write) EQ 0) {
98
99 errno = EACCES; /* can't write */
100 return(FAILURE);
101 }
102 }
103
104 /* setup the channel table entries */
105
106 chp->c_arg = dp->d_arg;
107 chp->c_ioctl = dev->d_ioctl;
108 chp->c_seek = dev->d_seek;
109 chp->c_close = _noper;
110
111 if ((*dev->d_open)(name, flag, mode, chp, dp) < 0) { /* open */
112
113 chp->c_close = _badfd; /* couldn't open for some reason */
114 return(FAILURE);
115 }
116
117 return(fd);
118}
119
120/*
121
122*/
123
124/*
125 =============================================================================
126 opena(name, flag, mode) -- Opens ASCII file 'name' with flags 'flag'
127 and access mode 'mode'. Newline translation will be done.
128 Returns a file descriptor (small positive integer) if successful, or
129 FAILURE (-1) if an error occurred.
130 =============================================================================
131*/
132
133int16_t opena(int8_t *name, int16_t flag, int16_t mode)
134{
135 return(open(name, flag, mode));
136}
137
138/*
139 =============================================================================
140 openb(name, flag, mode) -- Opens binary file 'name' with flags 'flag'
141 and access mode 'mode'. No newline translation is done.
142 Returns a file descriptor (small positive integer) if successful, or
143 FAILURE (-1) if an error occurred.
144 =============================================================================
145*/
146
147int16_t openb(int8_t *name, int16_t flag, int16_t mode)
148{
149 return(open(name, flag|O_RAW, mode));
150}
151
152/*
153
154*/
155
156/*
157 =============================================================================
158 creat(name, mode) -- Creates file 'name' with access mode 'mode'.
159 The created file is initially open for writing only. The file
160 will be ASCII unless mode contains O_RAW.
161 Returns a file descriptor (small positive integer) if successful, or
162 FAILURE (-1) if an error occurred.
163 =============================================================================
164*/
165
166int16_t creat(int8_t *name, int16_t mode)
167{
168 return(open(name, O_WRONLY|O_TRUNC|O_CREAT, mode));
169}
170
171/*
172
173*/
174
175/*
176 =============================================================================
177 creata(name, mode) -- Creates ASCII file 'name' with access mode 'mode'.
178 The created file is initially open for writing only.
179 Files created with creata() do newline translations.
180 Returns a file descriptor (small positive integer) if successful, or
181 FAILURE (-1) if an error occurred.
182 =============================================================================
183*/
184
185int16_t creata(int8_t *name, int16_t mode)
186{
187 return(open(name, O_WRONLY|O_TRUNC|O_CREAT, mode));
188}
189
190/*
191 =============================================================================
192 creatb(name, mode) -- create binary file 'name' with access mode 'mode'.
193 The created file is initially open for writing only.
194 Files created with creatb don't do newline translations.
195 Returns a file descriptor (small positive integer) if successful, or
196 FAILURE (-1) if an error occurred.
197 =============================================================================
198*/
199
200int16_t creatb(int8_t *name, int16_t mode)
201{
202 return(open(name, O_WRONLY|O_TRUNC|O_CREAT|O_RAW, mode));
203}
204
205/*
206
207*/
208
209/*
210 =============================================================================
211 _fileop(name, flag, mode, chp, dp) -- Opens disk file 'name' with
212 flags 'flag' in mode 'mode' with channel pointer 'chp' and
213 device pointer 'dp'. Returns SUCCESS (0) or FAILURE (-1).
214 =============================================================================
215*/
216
217int16_t _fileop(int8_t *name, int16_t flag, int16_t mode, struct channel *chp, struct devtabl *dp)
218{
219 register struct fcb *fp;
220 int8_t tmpname[9], tmpext[4];
221
222 /* search for an available fcb entry */
223
224 for (fp = _fcbtab; fp < (_fcbtab + MAXDFILE); ++fp)
225 if (fp->modefl EQ 0)
226 goto havefcb;
227
228 errno = ENFILE; /* no fcb space available for file */
229 return (FAILURE);
230
231havefcb:
232
233 /* setup the initial fcb */
234
235 if (_inifcb(fp, FilName(name, tmpname), FilExt(name, tmpext), flag)) {
236
237 errno = EINVAL; /* bad file name or flags */
238 return(FAILURE);
239 }
240
241 if (_opfcb(fp)) /* open the file */
242 return(FAILURE);
243
244 chp->c_arg = fp; /* allocate the channel */
245 chp->c_close = _filecl;
246
247 return(SUCCESS);
248}
249
Note: See TracBrowser for help on using the repository browser.