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

Last change on this file since 39a696b was 6262b5c, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Added include files for global functions and variables.

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