[395] | 1 |
|
---|
| 2 | /***********************************************************************
|
---|
| 3 |
|
---|
| 4 | $Id: wrappers.c 826 2007-09-01 21:50:33Z gyoung $
|
---|
| 5 |
|
---|
| 6 | Wrappers with error checking
|
---|
| 7 |
|
---|
| 8 | Copyright (c) 2006 Steven H.Levine
|
---|
| 9 |
|
---|
| 10 | 22 Jul 06 SHL Baseline
|
---|
[400] | 11 | 29 Jul 06 SHL Add xgets_stripped
|
---|
[438] | 12 | 18 Aug 06 SHL Correct Runtime_Error line number report
|
---|
[794] | 13 | 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
|
---|
[826] | 14 | 01 Sep 07 GKY Add xDosSetPathInfo to fix case where FS3 buffer crosses 64k boundry
|
---|
[395] | 15 |
|
---|
| 16 | ***********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #define INCL_WIN
|
---|
[826] | 19 | #define INCL_DOS
|
---|
| 20 | #define INCL_DOSERRORS
|
---|
[395] | 21 | #include <os2.h>
|
---|
| 22 |
|
---|
| 23 | #include <stdio.h>
|
---|
| 24 | #include <stdlib.h>
|
---|
| 25 | #include <string.h>
|
---|
| 26 |
|
---|
| 27 | #include "fm3dll.h"
|
---|
| 28 | #include "fm3str.h"
|
---|
| 29 |
|
---|
[826] | 30 | APIRET APIENTRY xDosSetPathInfo(PSZ pszPathName,
|
---|
| 31 | ULONG ulInfoLevel,
|
---|
| 32 | PVOID pInfoBuf,
|
---|
| 33 | ULONG cbInfoBuf,
|
---|
| 34 | ULONG flOptions)
|
---|
| 35 | {
|
---|
| 36 | APIRET rc;
|
---|
[395] | 37 |
|
---|
[826] | 38 | rc = DosSetPathInfo(pszPathName, ulInfoLevel, pInfoBuf, cbInfoBuf, flOptions);
|
---|
| 39 | if (rc)
|
---|
| 40 | /* Some kernels to do not handle fs3 buffers that cross 64K boundaries
|
---|
| 41 | and return ERROR_INVALID_NAME
|
---|
| 42 | If code works around the problem because if fs3 crosses the boundary
|
---|
| 43 | fsa2 will not because we don't have enough data on the stack for this
|
---|
| 44 | to occur 25 Aug 07 SHL
|
---|
| 45 | */
|
---|
| 46 | if (rc == ERROR_INVALID_NAME){
|
---|
| 47 | if (ulInfoLevel == FIL_STANDARD){
|
---|
| 48 | FILESTATUS3 fs3x;
|
---|
| 49 | fs3x = *(PFILESTATUS3) pInfoBuf;
|
---|
| 50 | rc = DosSetPathInfo(pszPathName, ulInfoLevel, &fs3x, sizeof(fs3x), flOptions);
|
---|
| 51 | }
|
---|
| 52 | else {
|
---|
| 53 | EAOP2 eaop2x;
|
---|
| 54 | eaop2x = *(PEAOP2) pInfoBuf;
|
---|
| 55 | rc = DosSetPathInfo(pszPathName, ulInfoLevel, &eaop2x, sizeof(eaop2x), flOptions);
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | return rc;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[551] | 61 | PSZ xfgets(PSZ pszBuf, size_t cMaxBytes, FILE * fp, PCSZ pszSrcFile,
|
---|
| 62 | UINT uiLineNumber)
|
---|
[400] | 63 | {
|
---|
[551] | 64 | PSZ psz = fgets(pszBuf, cMaxBytes, fp);
|
---|
| 65 |
|
---|
[400] | 66 | if (!psz) {
|
---|
| 67 | if (ferror(fp))
|
---|
| 68 | Runtime_Error(pszSrcFile, uiLineNumber, "fgets");
|
---|
| 69 | }
|
---|
| 70 | else {
|
---|
| 71 | size_t c = strlen(psz);
|
---|
[551] | 72 |
|
---|
[400] | 73 | if (c + 1 > cMaxBytes)
|
---|
| 74 | Runtime_Error(pszSrcFile, uiLineNumber, "buffer overflow");
|
---|
[551] | 75 | else if (!c || (psz[c - 1] != '\n' && psz[c - 1] != '\r'))
|
---|
[400] | 76 | Runtime_Error(pszSrcFile, uiLineNumber, "missing EOL");
|
---|
| 77 | }
|
---|
| 78 | return psz;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[551] | 81 | PSZ xfgets_bstripcr(PSZ pszBuf, size_t cMaxBytes, FILE * fp, PCSZ pszSrcFile,
|
---|
| 82 | UINT uiLineNumber)
|
---|
[400] | 83 | {
|
---|
[551] | 84 | PSZ psz = xfgets(pszBuf, cMaxBytes, fp, pszSrcFile, uiLineNumber);
|
---|
| 85 |
|
---|
[400] | 86 | if (psz)
|
---|
| 87 | bstripcr(psz);
|
---|
| 88 | return psz;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[551] | 91 | FILE *xfopen(PCSZ pszFileName, PCSZ pszMode, PCSZ pszSrcFile,
|
---|
| 92 | UINT uiLineNumber)
|
---|
[395] | 93 | {
|
---|
[551] | 94 | FILE *fp = fopen(pszFileName, pszMode);
|
---|
| 95 |
|
---|
[395] | 96 | if (!fp)
|
---|
| 97 | Runtime_Error(pszSrcFile, uiLineNumber, "fopen");
|
---|
| 98 | return fp;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[551] | 101 | FILE *xfsopen(PCSZ pszFileName, PCSZ pszMode, INT fSharemode, PCSZ pszSrcFile,
|
---|
| 102 | UINT uiLineNumber)
|
---|
[395] | 103 | {
|
---|
[551] | 104 | FILE *fp = _fsopen((PSZ) pszFileName, (PSZ) pszMode, fSharemode);
|
---|
| 105 |
|
---|
[395] | 106 | if (!fp)
|
---|
| 107 | Runtime_Error(pszSrcFile, uiLineNumber, "_fsopen");
|
---|
| 108 | return fp;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | //== xfree - safe free ==
|
---|
| 112 |
|
---|
[551] | 113 | VOID xfree(PVOID pv)
|
---|
[395] | 114 | {
|
---|
| 115 | if (pv)
|
---|
| 116 | free(pv);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | //== xmalloc() malloc with error checking ==
|
---|
| 120 |
|
---|
| 121 | PVOID xmalloc(size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
|
---|
| 122 | {
|
---|
| 123 | PVOID pv = malloc(cBytes);
|
---|
| 124 |
|
---|
| 125 | if (!pv)
|
---|
[551] | 126 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
|
---|
[395] | 127 |
|
---|
| 128 | return pv;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | //== xmallocz() malloc and zero with error checking ==
|
---|
| 132 |
|
---|
| 133 | PVOID xmallocz(size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
|
---|
| 134 | {
|
---|
| 135 | PVOID pv = malloc(cBytes);
|
---|
| 136 |
|
---|
| 137 | if (!pv)
|
---|
[551] | 138 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
|
---|
[395] | 139 | else
|
---|
| 140 | memset(pv, 0, cBytes);
|
---|
| 141 |
|
---|
| 142 | return pv;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | //== xrealloc() realloc with error checking ==
|
---|
| 146 |
|
---|
| 147 | PVOID xrealloc(PVOID pvIn, size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
|
---|
| 148 | {
|
---|
| 149 | PVOID pv = realloc(pvIn, cBytes);
|
---|
| 150 |
|
---|
| 151 | if (!pv && cBytes)
|
---|
[551] | 152 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
|
---|
[395] | 153 |
|
---|
| 154 | return pv;
|
---|
| 155 |
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | //== xstrdup() strdup with error checking ==
|
---|
| 159 |
|
---|
| 160 | PVOID xstrdup(PCSZ pszIn, PCSZ pszSrcFile, UINT uiLineNumber)
|
---|
| 161 | {
|
---|
| 162 | PSZ psz = strdup(pszIn);
|
---|
| 163 |
|
---|
| 164 | if (!psz)
|
---|
[551] | 165 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
|
---|
[395] | 166 |
|
---|
| 167 | return psz;
|
---|
| 168 | }
|
---|
[794] | 169 |
|
---|
| 170 | #pragma alloc_text(WRAPPERS1,xfree,xfopen,xfsopen,xmalloc,xrealloc, xstrdup)
|
---|
| 171 |
|
---|