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