1 |
|
---|
2 | /***********************************************************************
|
---|
3 |
|
---|
4 | $Id: wrappers.c 841 2007-09-23 16:27:51Z 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 FILESTATUS3L or EAOP2 buffer
|
---|
69 | * @param ulInfoLevel FIL_STANDARDL 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 | FILESTATUS3L alt_fs3;
|
---|
81 | EAOP2 alt_eaop2;
|
---|
82 | if (rc == ERROR_INVALID_NAME) {
|
---|
83 | switch (ulInfoLevel) {
|
---|
84 | case FIL_STANDARDL:
|
---|
85 | alt_fs3 = *(PFILESTATUS3L)pInfoBuf; // Copy
|
---|
86 | rc = DosSetPathInfo(pszPathName, ulInfoLevel, &alt_fs3, sizeof(alt_fs3), flOptions);
|
---|
87 | break;
|
---|
88 | case FIL_QUERYEASIZE:
|
---|
89 | alt_eaop2 = *(PEAOP2)pInfoBuf; // Copy
|
---|
90 | rc = DosSetPathInfo(pszPathName, ulInfoLevel, &alt_eaop2, sizeof(alt_eaop2), flOptions);
|
---|
91 | break;
|
---|
92 | default:
|
---|
93 | Runtime_Error(pszSrcFile, __LINE__, "ulInfoLevel %u unexpected", ulInfoLevel);
|
---|
94 | rc = ERROR_INVALID_PARAMETER;
|
---|
95 | } // switch
|
---|
96 | }
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 | PSZ xfgets(PSZ pszBuf, size_t cMaxBytes, FILE * fp, PCSZ pszSrcFile,
|
---|
101 | UINT uiLineNumber)
|
---|
102 | {
|
---|
103 | PSZ psz = fgets(pszBuf, cMaxBytes, fp);
|
---|
104 |
|
---|
105 | if (!psz) {
|
---|
106 | if (ferror(fp))
|
---|
107 | Runtime_Error(pszSrcFile, uiLineNumber, "fgets");
|
---|
108 | }
|
---|
109 | else {
|
---|
110 | size_t c = strlen(psz);
|
---|
111 |
|
---|
112 | if (c + 1 > cMaxBytes)
|
---|
113 | Runtime_Error(pszSrcFile, uiLineNumber, "buffer overflow");
|
---|
114 | else if (!c || (psz[c - 1] != '\n' && psz[c - 1] != '\r'))
|
---|
115 | Runtime_Error(pszSrcFile, uiLineNumber, "missing EOL");
|
---|
116 | }
|
---|
117 | return psz;
|
---|
118 | }
|
---|
119 |
|
---|
120 | PSZ xfgets_bstripcr(PSZ pszBuf, size_t cMaxBytes, FILE * fp, PCSZ pszSrcFile,
|
---|
121 | UINT uiLineNumber)
|
---|
122 | {
|
---|
123 | PSZ psz = xfgets(pszBuf, cMaxBytes, fp, pszSrcFile, uiLineNumber);
|
---|
124 |
|
---|
125 | if (psz)
|
---|
126 | bstripcr(psz);
|
---|
127 | return psz;
|
---|
128 | }
|
---|
129 |
|
---|
130 | FILE *xfopen(PCSZ pszFileName, PCSZ pszMode, PCSZ pszSrcFile,
|
---|
131 | UINT uiLineNumber)
|
---|
132 | {
|
---|
133 | FILE *fp = fopen(pszFileName, pszMode);
|
---|
134 |
|
---|
135 | if (!fp)
|
---|
136 | Runtime_Error(pszSrcFile, uiLineNumber, "fopen");
|
---|
137 | return fp;
|
---|
138 | }
|
---|
139 |
|
---|
140 | FILE *xfsopen(PCSZ pszFileName, PCSZ pszMode, INT fSharemode, PCSZ pszSrcFile,
|
---|
141 | UINT uiLineNumber)
|
---|
142 | {
|
---|
143 | FILE *fp = _fsopen((PSZ) pszFileName, (PSZ) pszMode, fSharemode);
|
---|
144 |
|
---|
145 | if (!fp)
|
---|
146 | Runtime_Error(pszSrcFile, uiLineNumber, "_fsopen");
|
---|
147 | return fp;
|
---|
148 | }
|
---|
149 |
|
---|
150 | //== xfree - safe free ==
|
---|
151 |
|
---|
152 | VOID xfree(PVOID pv)
|
---|
153 | {
|
---|
154 | if (pv)
|
---|
155 | free(pv);
|
---|
156 | }
|
---|
157 |
|
---|
158 | //== xmalloc() malloc with error checking ==
|
---|
159 |
|
---|
160 | PVOID xmalloc(size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
|
---|
161 | {
|
---|
162 | PVOID pv = malloc(cBytes);
|
---|
163 |
|
---|
164 | if (!pv)
|
---|
165 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
|
---|
166 |
|
---|
167 | return pv;
|
---|
168 | }
|
---|
169 |
|
---|
170 | //== xmallocz() malloc and zero with error checking ==
|
---|
171 |
|
---|
172 | PVOID xmallocz(size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
|
---|
173 | {
|
---|
174 | PVOID pv = malloc(cBytes);
|
---|
175 |
|
---|
176 | if (!pv)
|
---|
177 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
|
---|
178 | else
|
---|
179 | memset(pv, 0, cBytes);
|
---|
180 |
|
---|
181 | return pv;
|
---|
182 | }
|
---|
183 |
|
---|
184 | //== xrealloc() realloc with error checking ==
|
---|
185 |
|
---|
186 | PVOID xrealloc(PVOID pvIn, size_t cBytes, PCSZ pszSrcFile, UINT uiLineNumber)
|
---|
187 | {
|
---|
188 | PVOID pv = realloc(pvIn, cBytes);
|
---|
189 |
|
---|
190 | if (!pv && cBytes)
|
---|
191 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
|
---|
192 |
|
---|
193 | return pv;
|
---|
194 |
|
---|
195 | }
|
---|
196 |
|
---|
197 | //== xstrdup() strdup with error checking ==
|
---|
198 |
|
---|
199 | PVOID xstrdup(PCSZ pszIn, PCSZ pszSrcFile, UINT uiLineNumber)
|
---|
200 | {
|
---|
201 | PSZ psz = strdup(pszIn);
|
---|
202 |
|
---|
203 | if (!psz)
|
---|
204 | Runtime_Error(pszSrcFile, uiLineNumber, GetPString(IDS_OUTOFMEMORY));
|
---|
205 |
|
---|
206 | return psz;
|
---|
207 | }
|
---|
208 |
|
---|
209 | #pragma alloc_text(WRAPPERS1,xfree,xfopen,xfsopen,xmalloc,xrealloc, xstrdup)
|
---|
210 | #pragma alloc_text(WRAPPERS2,xDosSetPathInfo,xDosFindFirst,xDosFindNext)
|
---|