| 1 |
|
|---|
| 2 | /***********************************************************************
|
|---|
| 3 |
|
|---|
| 4 | $Id: wrappers.c 847 2007-09-29 18:45:16Z gyoung $
|
|---|
| 5 |
|
|---|
| 6 | Wrappers with error checking
|
|---|
| 7 |
|
|---|
| 8 | Copyright (c) 2006 Steven H.Levine
|
|---|
| 9 |
|
|---|
| 10 | 22 Jul 06 SHL Baseline
|
|---|
| 11 | 29 Jul 06 SHL Add xgets_stripped
|
|---|
| 12 | 18 Aug 06 SHL Correct Runtime_Error line number report
|
|---|
| 13 | 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
|
|---|
| 14 | 01 Sep 07 GKY Add xDosSetPathInfo to fix case where FS3 buffer crosses 64k boundry
|
|---|
| 15 |
|
|---|
| 16 | ***********************************************************************/
|
|---|
| 17 |
|
|---|
| 18 | #define INCL_WIN
|
|---|
| 19 | #define INCL_DOS
|
|---|
| 20 | #define INCL_DOSERRORS
|
|---|
| 21 | #define INCL_LONGLONG
|
|---|
| 22 | #include <os2.h>
|
|---|
| 23 |
|
|---|
| 24 | #include <stdio.h>
|
|---|
| 25 | #include <stdlib.h>
|
|---|
| 26 | #include <string.h>
|
|---|
| 27 |
|
|---|
| 28 | #include "fm3dll.h"
|
|---|
| 29 | #include "fm3str.h"
|
|---|
| 30 |
|
|---|
| 31 | static PSZ pszSrcFile = __FILE__;
|
|---|
| 32 |
|
|---|
| 33 | APIRET xDosFindFirst(PSZ pszFileSpec,
|
|---|
| 34 | PHDIR phdir,
|
|---|
| 35 | ULONG flAttribute,
|
|---|
| 36 | PVOID pfindbuf,
|
|---|
| 37 | ULONG cbBuf,
|
|---|
| 38 | PULONG pcFileNames,
|
|---|
| 39 | ULONG ulInfoLevel)
|
|---|
| 40 | {
|
|---|
| 41 | APIRET rc;
|
|---|
| 42 |
|
|---|
| 43 | rc = DosFindFirst(pszFileSpec, phdir, flAttribute, pfindbuf, cbBuf,
|
|---|
| 44 | pcFileNames, ulInfoLevel);
|
|---|
| 45 | return rc;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | APIRET xDosFindNext(HDIR hDir,
|
|---|
| 49 | PVOID pfindbuf,
|
|---|
| 50 | ULONG cbfindbuf,
|
|---|
| 51 | PULONG pcFilenames)
|
|---|
| 52 | {
|
|---|
| 53 | APIRET rc;
|
|---|
| 54 |
|
|---|
| 55 | rc = DosFindNext(hDir, pfindbuf, cbfindbuf, pcFilenames);
|
|---|
| 56 | return rc;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * Wrap DosSetPathInfo to avoid spurious ERROR_INVALID_NAME returns
|
|---|
| 61 | * Some kernels to do not correctly handle FILESTATUS3 and PEAOP2 buffers
|
|---|
| 62 | * that cross a 64K boundary.
|
|---|
| 63 | * When this occurs, they return ERROR_INVALID_NAME.
|
|---|
| 64 | * This code works around the problem because if the passed buffer crosses
|
|---|
| 65 | * the boundary the alternate buffer will not because both are on the stack
|
|---|
| 66 | * and we don't put enough additional data on the stack for this to occur.
|
|---|
| 67 | * It is caller's responsitibility to report errors
|
|---|
| 68 | * @param pInfoBuf pointer to FILESTATUS3(L) or EAOP2 buffer
|
|---|
| 69 | * @param ulInfoLevel FIL_STANDARD(L) or FIL_QUERYEASIZE
|
|---|
| 70 | * @returns Same as DosSetPathInfo
|
|---|
| 71 | */
|
|---|
| 72 |
|
|---|
| 73 | APIRET xDosSetPathInfo(PSZ pszPathName,
|
|---|
| 74 | ULONG ulInfoLevel,
|
|---|
| 75 | PVOID pInfoBuf,
|
|---|
| 76 | ULONG cbInfoBuf,
|
|---|
| 77 | ULONG flOptions)
|
|---|
| 78 | {
|
|---|
| 79 | APIRET rc = DosSetPathInfo(pszPathName, ulInfoLevel, pInfoBuf, cbInfoBuf, flOptions);
|
|---|
| 80 | FILESTATUS3 alt_fs3;
|
|---|
| 81 | FILESTATUS3L alt_fs3L;
|
|---|
| 82 | EAOP2 alt_eaop2;
|
|---|
| 83 | if (rc == ERROR_INVALID_NAME) {
|
|---|
| 84 | switch (ulInfoLevel) {
|
|---|
| 85 | case FIL_STANDARD:
|
|---|
| 86 | alt_fs3 = *(PFILESTATUS3)pInfoBuf; // Copy
|
|---|
| 87 | rc = DosSetPathInfo(pszPathName, ulInfoLevel, &alt_fs3, sizeof(alt_fs3), flOptions);
|
|---|
| 88 | break;
|
|---|
| 89 | case FIL_STANDARDL:
|
|---|
| 90 | alt_fs3L = *(PFILESTATUS3L)pInfoBuf; // Copy
|
|---|
| 91 | rc = DosSetPathInfo(pszPathName, ulInfoLevel, &alt_fs3L, sizeof(alt_fs3L), flOptions);
|
|---|
| 92 | break;
|
|---|
| 93 | case FIL_QUERYEASIZE:
|
|---|
| 94 | alt_eaop2 = *(PEAOP2)pInfoBuf; // Copy
|
|---|
| 95 | rc = DosSetPathInfo(pszPathName, ulInfoLevel, &alt_eaop2, sizeof(alt_eaop2), flOptions);
|
|---|
| 96 | break;
|
|---|
| 97 | default:
|
|---|
| 98 | Runtime_Error(pszSrcFile, __LINE__, "ulInfoLevel %u unexpected", ulInfoLevel);
|
|---|
| 99 | rc = ERROR_INVALID_PARAMETER;
|
|---|
| 100 | } // switch
|
|---|
| 101 | }
|
|---|
| 102 | return rc;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | PSZ xfgets(PSZ pszBuf, size_t cMaxBytes, FILE * fp, PCSZ pszSrcFile,
|
|---|
| 106 | UINT uiLineNumber)
|
|---|
| 107 | {
|
|---|
| 108 | PSZ psz = fgets(pszBuf, cMaxBytes, fp);
|
|---|
| 109 |
|
|---|
| 110 | if (!psz) {
|
|---|
| 111 | if (ferror(fp))
|
|---|
| 112 | Runtime_Error(pszSrcFile, uiLineNumber, "fgets");
|
|---|
| 113 | }
|
|---|
| 114 | else {
|
|---|
| 115 | size_t c = strlen(psz);
|
|---|
| 116 |
|
|---|
| 117 | if (c + 1 > cMaxBytes)
|
|---|
| 118 | Runtime_Error(pszSrcFile, uiLineNumber, "buffer overflow");
|
|---|
| 119 | else if (!c || (psz[c - 1] != '\n' && psz[c - 1] != '\r'))
|
|---|
| 120 | Runtime_Error(pszSrcFile, uiLineNumber, "missing EOL");
|
|---|
| 121 | }
|
|---|
| 122 | return psz;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | PSZ xfgets_bstripcr(PSZ pszBuf, size_t cMaxBytes, FILE * fp, PCSZ pszSrcFile,
|
|---|
| 126 | UINT uiLineNumber)
|
|---|
| 127 | {
|
|---|
| 128 | PSZ psz = xfgets(pszBuf, cMaxBytes, fp, pszSrcFile, uiLineNumber);
|
|---|
| 129 |
|
|---|
| 130 | if (psz)
|
|---|
| 131 | bstripcr(psz);
|
|---|
| 132 | return psz;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | FILE *xfopen(PCSZ pszFileName, PCSZ pszMode, PCSZ pszSrcFile,
|
|---|
| 136 | UINT uiLineNumber)
|
|---|
| 137 | {
|
|---|
| 138 | FILE *fp = fopen(pszFileName, pszMode);
|
|---|
| 139 |
|
|---|
| 140 | if (!fp)
|
|---|
| 141 | Runtime_Error(pszSrcFile, uiLineNumber, "fopen");
|
|---|
| 142 | return fp;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | FILE *xfsopen(PCSZ pszFileName, PCSZ pszMode, INT fSharemode, PCSZ pszSrcFile,
|
|---|
| 146 | UINT uiLineNumber)
|
|---|
| 147 | {
|
|---|
| 148 | FILE *fp = _fsopen((PSZ) pszFileName, (PSZ) pszMode, fSharemode);
|
|---|
| 149 |
|
|---|
| 150 | if (!fp)
|
|---|
| 151 | Runtime_Error(pszSrcFile, uiLineNumber, "_fsopen");
|
|---|
| 152 | return fp;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | //== xfree - safe free ==
|
|---|
| 156 |
|
|---|
| 157 | VOID xfree(PVOID pv)
|
|---|
| 158 | {
|
|---|
| 159 | if (pv)
|
|---|
| 160 | free(pv);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | //== xmalloc() malloc with error checking ==
|
|---|
| 164 |
|
|---|
| 165 | PVOID xmalloc(size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
|
|---|
| 166 | {
|
|---|
| 167 | PVOID pv = malloc(cBytes);
|
|---|
| 168 |
|
|---|
| 169 | if (!pv)
|
|---|
| 170 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
|
|---|
| 171 |
|
|---|
| 172 | return pv;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | //== xmallocz() malloc and zero with error checking ==
|
|---|
| 176 |
|
|---|
| 177 | PVOID xmallocz(size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
|
|---|
| 178 | {
|
|---|
| 179 | PVOID pv = malloc(cBytes);
|
|---|
| 180 |
|
|---|
| 181 | if (!pv)
|
|---|
| 182 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
|
|---|
| 183 | else
|
|---|
| 184 | memset(pv, 0, cBytes);
|
|---|
| 185 |
|
|---|
| 186 | return pv;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | //== xrealloc() realloc with error checking ==
|
|---|
| 190 |
|
|---|
| 191 | PVOID xrealloc(PVOID pvIn, size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
|
|---|
| 192 | {
|
|---|
| 193 | PVOID pv = realloc(pvIn, cBytes);
|
|---|
| 194 |
|
|---|
| 195 | if (!pv && cBytes)
|
|---|
| 196 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
|
|---|
| 197 |
|
|---|
| 198 | return pv;
|
|---|
| 199 |
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | //== xstrdup() strdup with error checking ==
|
|---|
| 203 |
|
|---|
| 204 | PVOID xstrdup(PCSZ pszIn, PCSZ pszSrcFile, UINT uiLineNumber)
|
|---|
| 205 | {
|
|---|
| 206 | PSZ psz = strdup(pszIn);
|
|---|
| 207 |
|
|---|
| 208 | if (!psz)
|
|---|
| 209 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
|
|---|
| 210 |
|
|---|
| 211 | return psz;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | #pragma alloc_text(WRAPPERS1,xfree,xfopen,xfsopen,xmalloc,xrealloc, xstrdup)
|
|---|
| 215 | #pragma alloc_text(WRAPPERS2,xDosSetPathInfo,xDosFindFirst,xDosFindNext)
|
|---|