1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
4 // Public Domain C Library - http://pdclib.sourceforge.net
5 // This code is Public Domain. Use, modify, and redistribute at will.
6 // ----------------------------------------------------------------------------
8 FILE * fopen( const char * restrict filename, const char * restrict mode ) { /* TODO */ };
10 /* PDPC code - unreviewed, verbatim.
11 NOTE THAT PDPC WAS MEANT TO RUN ON A HOST OS.
12 I thought the approach to "osfopen()" was close enough to what we have in mind
13 for pdclib to include the code here.
22 myfile = malloc(sizeof(FILE));
43 static void fopen2(void)
51 __userFiles[spareSpot] = myfile;
52 myfile->intFno = spareSpot;
59 static void fopen3(void)
61 myfile->intBuffer = malloc(BUFSIZ + 8);
62 if (myfile->intBuffer == NULL)
68 myfile->theirBuffer = 0;
69 myfile->fbuf = myfile->intBuffer + 2;
70 *myfile->fbuf++ = '\0';
71 *myfile->fbuf++ = '\0';
72 myfile->szfbuf = BUFSIZ;
74 myfile->quickText = 0;
77 myfile->endbuf = myfile->fbuf + myfile->szfbuf;
78 *myfile->endbuf = '\n';
80 myfile->upto = myfile->fbuf;
81 myfile->szfbuf = myfile->lrecl;
83 myfile->upto = myfile->endbuf;
85 myfile->bufStartR = -(long)myfile->szfbuf;
91 if (!myfile->textMode)
100 myfile->mode = __READ_MODE;
111 myfile->bufStartR = 0;
112 myfile->upto = myfile->fbuf;
113 myfile->mode = __WRITE_MODE;
130 static void findSpareSpot(void)
134 for (x = 0; x < __NFILE; x++)
136 if (__userFiles[x] == NULL)
152 /* checkMode - interpret mode string */
162 /* r+b or rb+ = 10 */
163 /* w+b or wb+ = 11 */
164 /* a+b or ab+ = 12 */
166 static void checkMode(void)
168 if (strncmp(modus, "r+b", 3) == 0)
172 else if (strncmp(modus, "rb+", 3) == 0)
176 else if (strncmp(modus, "w+b", 3) == 0)
180 else if (strncmp(modus, "wb+", 3) == 0)
184 else if (strncmp(modus, "a+b", 3) == 0)
188 else if (strncmp(modus, "ab+", 3) == 0)
192 else if (strncmp(modus, "r+", 2) == 0)
196 else if (strncmp(modus, "w+", 2) == 0)
200 else if (strncmp(modus, "a+", 2) == 0)
204 else if (strncmp(modus, "rb", 2) == 0)
208 else if (strncmp(modus, "wb", 2) == 0)
212 else if (strncmp(modus, "ab", 2) == 0)
216 else if (strncmp(modus, "r", 1) == 0)
220 else if (strncmp(modus, "w", 1) == 0)
224 else if (strncmp(modus, "a", 1) == 0)
240 myfile->textMode = 0;
244 myfile->textMode = 1;
249 static void osfopen(void)
256 ULONG openAction = 0;
259 if ((modeType == 1) || (modeType == 4) || (modeType == 7)
262 openAction |= OPEN_ACTION_FAIL_IF_NEW;
263 openAction |= OPEN_ACTION_OPEN_IF_EXISTS;
265 else if ((modeType == 2) || (modeType == 5) || (modeType == 8)
268 openAction |= OPEN_ACTION_CREATE_IF_NEW;
269 openAction |= OPEN_ACTION_REPLACE_IF_EXISTS;
271 else if ((modeType == 3) || (modeType == 6) || (modeType == 9)
274 openAction |= OPEN_ACTION_CREATE_IF_NEW;
275 openAction |= OPEN_ACTION_OPEN_IF_EXISTS;
277 openMode |= OPEN_SHARE_DENYWRITE;
278 if ((modeType == 1) || (modeType == 4))
280 openMode |= OPEN_ACCESS_READONLY;
282 else if ((modeType == 2) || (modeType == 3) || (modeType == 5)
285 openMode |= OPEN_ACCESS_WRITEONLY;
289 openMode |= OPEN_ACCESS_READWRITE;
291 if ((strlen(fnm) == 2)
293 && (openMode == OPEN_ACCESS_READONLY))
295 openMode |= OPEN_FLAGS_DASD;
297 rc = DosOpen((PSZ)fnm,
315 if ((modeType == 1) || (modeType == 4))
319 else if ((modeType == 2) || (modeType == 5))
327 myfile->hfile = __open(fnm, mode, &errind);
331 errno = myfile->hfile;
339 if ((modeType == 1) || (modeType == 4))
343 else if ((modeType == 2) || (modeType == 5))
351 p = strchr(fnm, ':');
352 if ((p != NULL) && (strncmp(fnm, "dd", 2) == 0))
360 strcpy(myfile->ddname, " ");
366 memcpy(myfile->ddname, p, len);
374 __aopen(myfile->ddname, mode, &myfile->recfm, &myfile->lrecl);
375 if ((modeType == 4) || (modeType == 5))
377 myfile->style = 0; /* binary */
381 myfile->style = 2; /* text */
383 myfile->style += myfile->recfm;
384 if (myfile->style == VARIABLE_TEXT)
386 myfile->quickText = 1;
388 if (myfile->style == FIXED_BINARY)
390 myfile->quickBin = 1;
394 myfile->quickBin = 0;