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

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

Minor cleanup.

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