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