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