[2] | 1 | #define INCL_WIN
|
---|
| 2 | #define INCL_DOS
|
---|
| 3 |
|
---|
| 4 | #include <os2.h>
|
---|
| 5 | #include <stdlib.h>
|
---|
| 6 | #include <stdio.h>
|
---|
| 7 | #include <string.h>
|
---|
| 8 | #include <io.h>
|
---|
| 9 | #include <fcntl.h>
|
---|
| 10 | #include <share.h>
|
---|
| 11 | #include "fm3dll.h"
|
---|
| 12 |
|
---|
| 13 | #pragma alloc_text(FSOPEN,_fsopen)
|
---|
| 14 |
|
---|
[551] | 15 | FILE *_fsopen(CHAR * filename, CHAR * mode, INT sharemode, ...)
|
---|
| 16 | {
|
---|
[2] | 17 |
|
---|
[551] | 18 | ULONG openflag = OPEN_ACTION_OPEN_IF_EXISTS, openmode = 0, action = 0;
|
---|
[2] | 19 | HFILE handle;
|
---|
| 20 | FILE *fp;
|
---|
[551] | 21 | BOOL text = TRUE;
|
---|
[2] | 22 |
|
---|
[551] | 23 | if (!stristr(mode, "b"))
|
---|
[2] | 24 | text = FALSE;
|
---|
[551] | 25 | if (stristr(mode, "r"))
|
---|
[2] | 26 | openmode |= OPEN_ACCESS_READONLY;
|
---|
[551] | 27 | else if (stristr(mode, "w")) {
|
---|
[2] | 28 | openmode |= OPEN_ACCESS_WRITEONLY;
|
---|
| 29 | openflag |= (OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW);
|
---|
| 30 | }
|
---|
[551] | 31 | if (stristr(mode, "a"))
|
---|
[2] | 32 | openmode |= OPEN_ACCESS_WRITEONLY;
|
---|
[551] | 33 | if (stristr(mode, "+")) {
|
---|
[2] | 34 | openmode &= (~(OPEN_ACCESS_READONLY | OPEN_ACCESS_WRITEONLY));
|
---|
| 35 | openmode |= OPEN_ACCESS_READWRITE;
|
---|
| 36 | openflag |= OPEN_ACTION_CREATE_IF_NEW;
|
---|
| 37 | }
|
---|
[551] | 38 | if (sharemode == SH_DENYRW)
|
---|
[2] | 39 | openmode |= OPEN_SHARE_DENYREADWRITE;
|
---|
[551] | 40 | else if (sharemode == SH_DENYWR)
|
---|
[2] | 41 | openmode |= OPEN_SHARE_DENYWRITE;
|
---|
[551] | 42 | else if (sharemode == SH_DENYRD)
|
---|
[2] | 43 | openmode |= OPEN_SHARE_DENYREAD;
|
---|
| 44 | else
|
---|
| 45 | openmode |= OPEN_SHARE_DENYNONE;
|
---|
| 46 | openmode |= OPEN_FLAGS_FAIL_ON_ERROR;
|
---|
[551] | 47 | if (text)
|
---|
[2] | 48 | openmode |= OPEN_FLAGS_SEQUENTIAL;
|
---|
| 49 | else
|
---|
| 50 | openmode |= OPEN_FLAGS_RANDOMSEQUENTIAL;
|
---|
[551] | 51 | if (DosOpen(filename, &handle, &action, 0L, FILE_NORMAL, openflag, openmode,
|
---|
| 52 | (PEAOP2) 0))
|
---|
[2] | 53 | return NULL;
|
---|
[551] | 54 | if (mode[strlen(mode) - 1] == 't')
|
---|
| 55 | mode[strlen(mode) - 1] = 0; /* bug bug bug */
|
---|
| 56 | fp = fdopen(handle, mode);
|
---|
| 57 | if (!fp) {
|
---|
[2] | 58 | DosClose(handle);
|
---|
[551] | 59 | fp = fopen(filename, mode); /* last ditch effort */
|
---|
[2] | 60 | }
|
---|
[551] | 61 | if (fp) {
|
---|
| 62 | if (text) /* line buffer text files */
|
---|
| 63 | setvbuf(fp, NULL, _IOLBF, BUFSIZ * 2);
|
---|
[2] | 64 | else
|
---|
[551] | 65 | setvbuf(fp, NULL, _IOFBF, BUFSIZ * 8);
|
---|
| 66 | if (stristr(mode, "a"))
|
---|
| 67 | fseek(fp, 0L, SEEK_END);
|
---|
[2] | 68 | }
|
---|
| 69 | return fp;
|
---|
| 70 | }
|
---|