| 1 | 
 | 
|---|
| 2 | /***********************************************************************
 | 
|---|
| 3 | 
 | 
|---|
| 4 |   $Id: wrappers.c 827 2007-09-02 18:32:14Z stevenhl $
 | 
|---|
| 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 | #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 | 
 | 
|---|
| 30 | static PSZ pszSrcFile = __FILE__;
 | 
|---|
| 31 | 
 | 
|---|
| 32 | /**
 | 
|---|
| 33 |  * Wrap DosSetPathInfo to avoid spurious ERROR_INVALID_NAME returns
 | 
|---|
| 34 |  * Some kernels to do not correctly handle FILESTATUS3 and PEAOP2 buffers
 | 
|---|
| 35 |  * that cross a 64K boundary.
 | 
|---|
| 36 |  * When this occurs, they return ERROR_INVALID_NAME.
 | 
|---|
| 37 |  * This code works around the problem because if the passed buffer crosses
 | 
|---|
| 38 |  * the boundary the alternate buffer will not because both are on the stack
 | 
|---|
| 39 |  * and we don't put enough additional data on the stack for this to occur.
 | 
|---|
| 40 |  * It is caller's responsitibility to report errors
 | 
|---|
| 41 |  * @param pInfoBuf pointer to FILESTATUS3 or EAOP2 buffer
 | 
|---|
| 42 |  * @param ulInfoLevel FIL_STANDARD or FIL_QUERYEASIZE
 | 
|---|
| 43 |  * @returns Same as DosSetPathInfo
 | 
|---|
| 44 |  */
 | 
|---|
| 45 | 
 | 
|---|
| 46 | APIRET APIENTRY xDosSetPathInfo(PSZ pszPathName,
 | 
|---|
| 47 |                                 ULONG ulInfoLevel,
 | 
|---|
| 48 |                                 PVOID pInfoBuf,
 | 
|---|
| 49 |                                 ULONG cbInfoBuf,
 | 
|---|
| 50 |                                 ULONG flOptions)
 | 
|---|
| 51 | {
 | 
|---|
| 52 |     APIRET rc = DosSetPathInfo(pszPathName, ulInfoLevel, pInfoBuf, cbInfoBuf, flOptions);
 | 
|---|
| 53 |     FILESTATUS3 alt_fs3;
 | 
|---|
| 54 |     EAOP2 alt_eaop2;
 | 
|---|
| 55 |     if (rc == ERROR_INVALID_NAME) {
 | 
|---|
| 56 |       switch (ulInfoLevel) {
 | 
|---|
| 57 |       case FIL_STANDARD:
 | 
|---|
| 58 |         alt_fs3 = *(PFILESTATUS3)pInfoBuf;      // Copy
 | 
|---|
| 59 |         rc = DosSetPathInfo(pszPathName, ulInfoLevel, &alt_fs3, sizeof(alt_fs3), flOptions);
 | 
|---|
| 60 |         break;
 | 
|---|
| 61 |       case FIL_QUERYEASIZE:
 | 
|---|
| 62 |         alt_eaop2 = *(PEAOP2)pInfoBuf;  // Copy
 | 
|---|
| 63 |         rc = DosSetPathInfo(pszPathName, ulInfoLevel, &alt_eaop2, sizeof(alt_eaop2), flOptions);
 | 
|---|
| 64 |         break;
 | 
|---|
| 65 |       default:
 | 
|---|
| 66 |         Runtime_Error(pszSrcFile, __LINE__, "ulInfoLevel %u unexpected", ulInfoLevel);
 | 
|---|
| 67 |         rc = ERROR_INVALID_PARAMETER;
 | 
|---|
| 68 |       } // switch
 | 
|---|
| 69 |     }
 | 
|---|
| 70 |     return rc;
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | PSZ xfgets(PSZ pszBuf, size_t cMaxBytes, FILE * fp, PCSZ pszSrcFile,
 | 
|---|
| 74 |            UINT uiLineNumber)
 | 
|---|
| 75 | {
 | 
|---|
| 76 |   PSZ psz = fgets(pszBuf, cMaxBytes, fp);
 | 
|---|
| 77 | 
 | 
|---|
| 78 |   if (!psz) {
 | 
|---|
| 79 |     if (ferror(fp))
 | 
|---|
| 80 |       Runtime_Error(pszSrcFile, uiLineNumber, "fgets");
 | 
|---|
| 81 |   }
 | 
|---|
| 82 |   else {
 | 
|---|
| 83 |     size_t c = strlen(psz);
 | 
|---|
| 84 | 
 | 
|---|
| 85 |     if (c + 1 > cMaxBytes)
 | 
|---|
| 86 |       Runtime_Error(pszSrcFile, uiLineNumber, "buffer overflow");
 | 
|---|
| 87 |     else if (!c || (psz[c - 1] != '\n' && psz[c - 1] != '\r'))
 | 
|---|
| 88 |       Runtime_Error(pszSrcFile, uiLineNumber, "missing EOL");
 | 
|---|
| 89 |   }
 | 
|---|
| 90 |   return psz;
 | 
|---|
| 91 | }
 | 
|---|
| 92 | 
 | 
|---|
| 93 | PSZ xfgets_bstripcr(PSZ pszBuf, size_t cMaxBytes, FILE * fp, PCSZ pszSrcFile,
 | 
|---|
| 94 |                     UINT uiLineNumber)
 | 
|---|
| 95 | {
 | 
|---|
| 96 |   PSZ psz = xfgets(pszBuf, cMaxBytes, fp, pszSrcFile, uiLineNumber);
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   if (psz)
 | 
|---|
| 99 |     bstripcr(psz);
 | 
|---|
| 100 |   return psz;
 | 
|---|
| 101 | }
 | 
|---|
| 102 | 
 | 
|---|
| 103 | FILE *xfopen(PCSZ pszFileName, PCSZ pszMode, PCSZ pszSrcFile,
 | 
|---|
| 104 |              UINT uiLineNumber)
 | 
|---|
| 105 | {
 | 
|---|
| 106 |   FILE *fp = fopen(pszFileName, pszMode);
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   if (!fp)
 | 
|---|
| 109 |     Runtime_Error(pszSrcFile, uiLineNumber, "fopen");
 | 
|---|
| 110 |   return fp;
 | 
|---|
| 111 | }
 | 
|---|
| 112 | 
 | 
|---|
| 113 | FILE *xfsopen(PCSZ pszFileName, PCSZ pszMode, INT fSharemode, PCSZ pszSrcFile,
 | 
|---|
| 114 |               UINT uiLineNumber)
 | 
|---|
| 115 | {
 | 
|---|
| 116 |   FILE *fp = _fsopen((PSZ) pszFileName, (PSZ) pszMode, fSharemode);
 | 
|---|
| 117 | 
 | 
|---|
| 118 |   if (!fp)
 | 
|---|
| 119 |     Runtime_Error(pszSrcFile, uiLineNumber, "_fsopen");
 | 
|---|
| 120 |   return fp;
 | 
|---|
| 121 | }
 | 
|---|
| 122 | 
 | 
|---|
| 123 | //== xfree - safe free ==
 | 
|---|
| 124 | 
 | 
|---|
| 125 | VOID xfree(PVOID pv)
 | 
|---|
| 126 | {
 | 
|---|
| 127 |   if (pv)
 | 
|---|
| 128 |     free(pv);
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | //== xmalloc() malloc with error checking ==
 | 
|---|
| 132 | 
 | 
|---|
| 133 | PVOID xmalloc(size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
 | 
|---|
| 134 | {
 | 
|---|
| 135 |   PVOID pv = malloc(cBytes);
 | 
|---|
| 136 | 
 | 
|---|
| 137 |   if (!pv)
 | 
|---|
| 138 |     Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
 | 
|---|
| 139 | 
 | 
|---|
| 140 |   return pv;
 | 
|---|
| 141 | }
 | 
|---|
| 142 | 
 | 
|---|
| 143 | //== xmallocz() malloc and zero with error checking ==
 | 
|---|
| 144 | 
 | 
|---|
| 145 | PVOID xmallocz(size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
 | 
|---|
| 146 | {
 | 
|---|
| 147 |   PVOID pv = malloc(cBytes);
 | 
|---|
| 148 | 
 | 
|---|
| 149 |   if (!pv)
 | 
|---|
| 150 |     Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
 | 
|---|
| 151 |   else
 | 
|---|
| 152 |     memset(pv, 0, cBytes);
 | 
|---|
| 153 | 
 | 
|---|
| 154 |   return pv;
 | 
|---|
| 155 | }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | //== xrealloc() realloc with error checking ==
 | 
|---|
| 158 | 
 | 
|---|
| 159 | PVOID xrealloc(PVOID pvIn, size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
 | 
|---|
| 160 | {
 | 
|---|
| 161 |   PVOID pv = realloc(pvIn, cBytes);
 | 
|---|
| 162 | 
 | 
|---|
| 163 |   if (!pv && cBytes)
 | 
|---|
| 164 |     Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
 | 
|---|
| 165 | 
 | 
|---|
| 166 |   return pv;
 | 
|---|
| 167 | 
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | //== xstrdup() strdup with error checking ==
 | 
|---|
| 171 | 
 | 
|---|
| 172 | PVOID xstrdup(PCSZ pszIn, PCSZ pszSrcFile, UINT uiLineNumber)
 | 
|---|
| 173 | {
 | 
|---|
| 174 |   PSZ psz = strdup(pszIn);
 | 
|---|
| 175 | 
 | 
|---|
| 176 |   if (!psz)
 | 
|---|
| 177 |     Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
 | 
|---|
| 178 | 
 | 
|---|
| 179 |   return psz;
 | 
|---|
| 180 | }
 | 
|---|
| 181 | 
 | 
|---|
| 182 | #pragma alloc_text(WRAPPERS1,xfree,xfopen,xfsopen,xmalloc,xrealloc, xstrdup)
 | 
|---|
| 183 | 
 | 
|---|