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