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