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