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

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

Fixed console io_arg.

  • Property mode set to 100644
File size: 6.1 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 };
11static 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
19static 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
36int16_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
52fndchan: /* 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
104int16_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
118int16_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
133int16_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
148int16_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
163int16_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
176int16_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 /* search for an available fcb entry */
182
183 for (fp = _fcbtab; fp < (_fcbtab + MAXDFILE); ++fp)
184 if (fp->modefl EQ 0)
185 goto havefcb;
186
187 errno = ENFILE; /* no fcb space available for file */
188 return (FAILURE);
189
190havefcb:
191
192 /* setup the initial fcb */
193
194 if (_inifcb(fp, FilName(name, tmpname), FilExt(name, tmpext), flag)) {
195
196 errno = EINVAL; /* bad file name or flags */
197 return(FAILURE);
198 }
199
200 if (_opfcb(fp)) /* open the file */
201 return(FAILURE);
202
203 chp->c_arg = fp; /* allocate the channel */
204 chp->c_close = _filecl;
205
206 return(SUCCESS);
207}
208
209
Note: See TracBrowser for help on using the repository browser.