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