| [689] | 1 |  | 
|---|
|  | 2 | /*********************************************************************** | 
|---|
|  | 3 |  | 
|---|
|  | 4 | $Id: fsopen.c 907 2008-01-06 07:26:17Z stevenhl $ | 
|---|
|  | 5 |  | 
|---|
|  | 6 | _fsopen for IBMC | 
|---|
|  | 7 |  | 
|---|
|  | 8 | Copyright (c) 1993-98 M. Kimes | 
|---|
|  | 9 | Copyright (c) 2007 Steven H. Levine | 
|---|
|  | 10 |  | 
|---|
|  | 11 | 15 Oct 02 SHL Baseline | 
|---|
|  | 12 | 05 Jun 07 SHL Update for OpenWatcom | 
|---|
|  | 13 |  | 
|---|
|  | 14 | ***********************************************************************/ | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #if defined(__IBMC__) | 
|---|
|  | 17 |  | 
|---|
| [2] | 18 | #include <stdlib.h> | 
|---|
|  | 19 | #include <stdio.h> | 
|---|
|  | 20 | #include <string.h> | 
|---|
|  | 21 | #include <io.h> | 
|---|
|  | 22 | #include <fcntl.h> | 
|---|
|  | 23 | #include <share.h> | 
|---|
| [907] | 24 |  | 
|---|
|  | 25 | #define INCL_WIN | 
|---|
|  | 26 | #define INCL_DOS | 
|---|
|  | 27 | #define INCL_LONGLONG | 
|---|
|  | 28 |  | 
|---|
| [2] | 29 | #include "fm3dll.h" | 
|---|
|  | 30 |  | 
|---|
|  | 31 | #pragma alloc_text(FSOPEN,_fsopen) | 
|---|
|  | 32 |  | 
|---|
| [551] | 33 | FILE *_fsopen(CHAR * filename, CHAR * mode, INT sharemode, ...) | 
|---|
|  | 34 | { | 
|---|
| [2] | 35 |  | 
|---|
| [841] | 36 | ULONG openflag = OPEN_ACTION_OPEN_IF_EXISTS, openmode = 0; | 
|---|
|  | 37 | ULONGLONG action = 0; | 
|---|
| [2] | 38 | HFILE handle; | 
|---|
|  | 39 | FILE *fp; | 
|---|
| [551] | 40 | BOOL text = TRUE; | 
|---|
| [2] | 41 |  | 
|---|
| [551] | 42 | if (!stristr(mode, "b")) | 
|---|
| [2] | 43 | text = FALSE; | 
|---|
| [551] | 44 | if (stristr(mode, "r")) | 
|---|
| [2] | 45 | openmode |= OPEN_ACCESS_READONLY; | 
|---|
| [551] | 46 | else if (stristr(mode, "w")) { | 
|---|
| [2] | 47 | openmode |= OPEN_ACCESS_WRITEONLY; | 
|---|
|  | 48 | openflag |= (OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW); | 
|---|
|  | 49 | } | 
|---|
| [551] | 50 | if (stristr(mode, "a")) | 
|---|
| [2] | 51 | openmode |= OPEN_ACCESS_WRITEONLY; | 
|---|
| [551] | 52 | if (stristr(mode, "+")) { | 
|---|
| [2] | 53 | openmode &= (~(OPEN_ACCESS_READONLY | OPEN_ACCESS_WRITEONLY)); | 
|---|
|  | 54 | openmode |= OPEN_ACCESS_READWRITE; | 
|---|
|  | 55 | openflag |= OPEN_ACTION_CREATE_IF_NEW; | 
|---|
|  | 56 | } | 
|---|
| [551] | 57 | if (sharemode == SH_DENYRW) | 
|---|
| [2] | 58 | openmode |= OPEN_SHARE_DENYREADWRITE; | 
|---|
| [551] | 59 | else if (sharemode == SH_DENYWR) | 
|---|
| [2] | 60 | openmode |= OPEN_SHARE_DENYWRITE; | 
|---|
| [551] | 61 | else if (sharemode == SH_DENYRD) | 
|---|
| [2] | 62 | openmode |= OPEN_SHARE_DENYREAD; | 
|---|
|  | 63 | else | 
|---|
|  | 64 | openmode |= OPEN_SHARE_DENYNONE; | 
|---|
|  | 65 | openmode |= OPEN_FLAGS_FAIL_ON_ERROR; | 
|---|
| [551] | 66 | if (text) | 
|---|
| [2] | 67 | openmode |= OPEN_FLAGS_SEQUENTIAL; | 
|---|
|  | 68 | else | 
|---|
|  | 69 | openmode |= OPEN_FLAGS_RANDOMSEQUENTIAL; | 
|---|
| [844] | 70 | if (DosOpen(filename, &handle, &action, 0, FILE_NORMAL, openflag, openmode, | 
|---|
|  | 71 | (PEAOP2) 0)) | 
|---|
| [2] | 72 | return NULL; | 
|---|
| [551] | 73 | if (mode[strlen(mode) - 1] == 't') | 
|---|
|  | 74 | mode[strlen(mode) - 1] = 0;         /* bug bug bug */ | 
|---|
|  | 75 | fp = fdopen(handle, mode); | 
|---|
|  | 76 | if (!fp) { | 
|---|
| [2] | 77 | DosClose(handle); | 
|---|
| [551] | 78 | fp = fopen(filename, mode);         /* last ditch effort */ | 
|---|
| [2] | 79 | } | 
|---|
| [551] | 80 | if (fp) { | 
|---|
|  | 81 | if (text)                           /* line buffer text files */ | 
|---|
|  | 82 | setvbuf(fp, NULL, _IOLBF, BUFSIZ * 2); | 
|---|
| [2] | 83 | else | 
|---|
| [551] | 84 | setvbuf(fp, NULL, _IOFBF, BUFSIZ * 8); | 
|---|
|  | 85 | if (stristr(mode, "a")) | 
|---|
|  | 86 | fseek(fp, 0L, SEEK_END); | 
|---|
| [2] | 87 | } | 
|---|
|  | 88 | return fp; | 
|---|
|  | 89 | } | 
|---|
| [689] | 90 |  | 
|---|
|  | 91 | #endif | 
|---|