1 |
|
---|
2 | /*
|
---|
3 | *@@sourcefile dosh.c:
|
---|
4 | * dosh.c contains Control Program helper functions.
|
---|
5 | *
|
---|
6 | * This file has miscellaneous system functions,
|
---|
7 | * drive helpers, file helpers, and partition functions.
|
---|
8 | *
|
---|
9 | * Usage: All OS/2 programs.
|
---|
10 | *
|
---|
11 | * Function prefixes (new with V0.81):
|
---|
12 | * -- dosh* Dos (Control Program) helper functions
|
---|
13 | *
|
---|
14 | * These funcs are forward-declared in dosh.h, which
|
---|
15 | * must be #include'd first.
|
---|
16 | *
|
---|
17 | * The resulting dosh.obj object file can be linked
|
---|
18 | * against any application object file. As opposed to
|
---|
19 | * the code in dosh2.c, it does not require any other
|
---|
20 | * code from the helpers.
|
---|
21 | *
|
---|
22 | * dosh.obj can also be used with the VAC subsystem
|
---|
23 | * library (/rn compiler option).
|
---|
24 | *
|
---|
25 | * Note: Version numbering in this file relates to XWorkplace version
|
---|
26 | * numbering.
|
---|
27 | *
|
---|
28 | *@@header "helpers\dosh.h"
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*
|
---|
32 | * This file Copyright (C) 1997-2000 Ulrich Mller.
|
---|
33 | * This file is part of the "XWorkplace helpers" source package.
|
---|
34 | * This is free software; you can redistribute it and/or modify
|
---|
35 | * it under the terms of the GNU General Public License as published
|
---|
36 | * by the Free Software Foundation, in version 2 as it comes in the
|
---|
37 | * "COPYING" file of the XWorkplace main distribution.
|
---|
38 | * This program is distributed in the hope that it will be useful,
|
---|
39 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
40 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
41 | * GNU General Public License for more details.
|
---|
42 | */
|
---|
43 |
|
---|
44 | #define OS2EMX_PLAIN_CHAR
|
---|
45 | // this is needed for "os2emx.h"; if this is defined,
|
---|
46 | // emx will define PSZ as _signed_ char, otherwise
|
---|
47 | // as unsigned char
|
---|
48 |
|
---|
49 | #define INCL_DOSMODULEMGR
|
---|
50 | #define INCL_DOSPROCESS
|
---|
51 | #define INCL_DOSSESMGR
|
---|
52 | #define INCL_DOSQUEUES
|
---|
53 | #define INCL_DOSSEMAPHORES
|
---|
54 | #define INCL_DOSMISC
|
---|
55 | #define INCL_DOSDEVICES
|
---|
56 | #define INCL_DOSDEVIOCTL
|
---|
57 | #define INCL_DOSERRORS
|
---|
58 |
|
---|
59 | #define INCL_KBD
|
---|
60 | #include <os2.h>
|
---|
61 |
|
---|
62 | #include <stdlib.h>
|
---|
63 | #include <string.h>
|
---|
64 | #include <stdio.h>
|
---|
65 | #include <stdarg.h>
|
---|
66 | #include <ctype.h>
|
---|
67 |
|
---|
68 | #include "setup.h" // code generation and debugging options
|
---|
69 |
|
---|
70 | #include "helpers\dosh.h"
|
---|
71 | #include "helpers\standards.h"
|
---|
72 |
|
---|
73 | #pragma hdrstop
|
---|
74 |
|
---|
75 | // static const CHAR G_acDriveLetters[28] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
---|
76 |
|
---|
77 | /*
|
---|
78 | *@@category: Helpers\Control program helpers\Miscellaneous
|
---|
79 | * Miscellaneous helpers in dosh.c that didn't fit into any other
|
---|
80 | * category.
|
---|
81 | */
|
---|
82 |
|
---|
83 | /* ******************************************************************
|
---|
84 | *
|
---|
85 | * Miscellaneous
|
---|
86 | *
|
---|
87 | ********************************************************************/
|
---|
88 |
|
---|
89 | /*
|
---|
90 | *@@ doshGetChar:
|
---|
91 | * reads a single character from the keyboard.
|
---|
92 | * Useful for VIO sessions, since there's great
|
---|
93 | * confusion between the various C dialects about
|
---|
94 | * how to use getc(), and getc() doesn't work
|
---|
95 | * with the VAC subsystem library.
|
---|
96 | *
|
---|
97 | *@@added V0.9.4 (2000-07-27) [umoeller]
|
---|
98 | */
|
---|
99 |
|
---|
100 | CHAR doshGetChar(VOID)
|
---|
101 | {
|
---|
102 | // CHAR c;
|
---|
103 | // ULONG ulRead = 0;
|
---|
104 |
|
---|
105 | KBDKEYINFO kki;
|
---|
106 | KbdCharIn(&kki,
|
---|
107 | 0, // wait
|
---|
108 | 0);
|
---|
109 |
|
---|
110 | return (kki.chChar);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /*
|
---|
114 | *@@ doshQueryShiftState:
|
---|
115 | * returns TRUE if any of the SHIFT keys are
|
---|
116 | * currently pressed. Useful for checks during
|
---|
117 | * WM_COMMAND messages from menus.
|
---|
118 | *
|
---|
119 | *@@changed V0.9.5 (2000-09-27) [umoeller]: added error checking
|
---|
120 | */
|
---|
121 |
|
---|
122 | BOOL doshQueryShiftState(VOID)
|
---|
123 | {
|
---|
124 | BOOL brc = FALSE;
|
---|
125 | APIRET arc = NO_ERROR;
|
---|
126 | HFILE hfKbd;
|
---|
127 | ULONG ulAction;
|
---|
128 |
|
---|
129 | arc = DosOpen("KBD$", &hfKbd, &ulAction, 0,
|
---|
130 | FILE_NORMAL, FILE_OPEN,
|
---|
131 | OPEN_ACCESS_READONLY | OPEN_SHARE_DENYWRITE,
|
---|
132 | (PEAOP2)NULL);
|
---|
133 | if (arc == NO_ERROR)
|
---|
134 | {
|
---|
135 | SHIFTSTATE ShiftState;
|
---|
136 | ULONG cbDataLen = sizeof(ShiftState);
|
---|
137 |
|
---|
138 | arc = DosDevIOCtl(hfKbd, IOCTL_KEYBOARD, KBD_GETSHIFTSTATE,
|
---|
139 | NULL, 0, NULL, // no parameters
|
---|
140 | &ShiftState, cbDataLen, &cbDataLen);
|
---|
141 | if (arc == NO_ERROR)
|
---|
142 | brc = ((ShiftState.fsState & 3) != 0);
|
---|
143 |
|
---|
144 | DosClose(hfKbd);
|
---|
145 | }
|
---|
146 |
|
---|
147 | return brc;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /*
|
---|
151 | *@@ doshIsWarp4:
|
---|
152 | * checks the OS/2 system version number.
|
---|
153 | *
|
---|
154 | * Returns:
|
---|
155 | *
|
---|
156 | * -- 0 (FALSE): OS/2 2.x or Warp 3 is running.
|
---|
157 | *
|
---|
158 | * -- 1: Warp 4.0 is running.
|
---|
159 | *
|
---|
160 | * -- 2: Warp 4.5 is running (WSeB or Warp 4 FP 13+ or eCS
|
---|
161 | * or ACP/MCP), or even something newer.
|
---|
162 | *
|
---|
163 | *@@changed V0.9.2 (2000-03-05) [umoeller]: reported TRUE on Warp 3 also; fixed
|
---|
164 | *@@changed V0.9.6 (2000-10-16) [umoeller]: patched for speed
|
---|
165 | *@@changed V0.9.9 (2001-04-04) [umoeller]: now returning 2 for Warp 4.5 and above
|
---|
166 | */
|
---|
167 |
|
---|
168 | ULONG doshIsWarp4(VOID)
|
---|
169 | {
|
---|
170 | static BOOL s_fQueried = FALSE;
|
---|
171 | static ULONG s_ulrc = 0;
|
---|
172 |
|
---|
173 | if (!s_fQueried)
|
---|
174 | {
|
---|
175 | // first call:
|
---|
176 | ULONG aulBuf[3];
|
---|
177 |
|
---|
178 | DosQuerySysInfo(QSV_VERSION_MAJOR, // 11
|
---|
179 | QSV_VERSION_MINOR, // 12
|
---|
180 | &aulBuf, sizeof(aulBuf));
|
---|
181 | // Warp 3 is reported as 20.30
|
---|
182 | // Warp 4 is reported as 20.40
|
---|
183 | // Aurora is reported as 20.45
|
---|
184 |
|
---|
185 | if ( (aulBuf[0] > 20) // major > 20; not the case with Warp 3, 4, 5
|
---|
186 | || ( (aulBuf[0] == 20) // major == 20 and minor >= 45
|
---|
187 | && (aulBuf[1] >= 45)
|
---|
188 | )
|
---|
189 | )
|
---|
190 | // Warp 4.5 or newer:
|
---|
191 | s_ulrc = 2;
|
---|
192 | else if ( (aulBuf[0] == 20) // major == 20 and minor == 40
|
---|
193 | && (aulBuf[1] == 40)
|
---|
194 | )
|
---|
195 | // Warp 4:
|
---|
196 | s_ulrc = 1;
|
---|
197 |
|
---|
198 | s_fQueried = TRUE;
|
---|
199 | }
|
---|
200 |
|
---|
201 | return (s_ulrc);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /*
|
---|
205 | *@@ doshQuerySysErrorMsg:
|
---|
206 | * this retrieves the error message for a system error
|
---|
207 | * (APIRET) from the system error message file (OSO001.MSG).
|
---|
208 | * This file better be on the DPATH (it normally is).
|
---|
209 | *
|
---|
210 | * This returns the string in the "SYSxxx: blahblah" style,
|
---|
211 | * which is normally displayed on the command line when
|
---|
212 | * errors occur.
|
---|
213 | *
|
---|
214 | * The error message is returned in a newly allocated
|
---|
215 | * buffer, which should be free()'d afterwards.
|
---|
216 | *
|
---|
217 | * Returns NULL upon errors.
|
---|
218 | */
|
---|
219 |
|
---|
220 | PSZ doshQuerySysErrorMsg(APIRET arc) // in: DOS error code
|
---|
221 | {
|
---|
222 | PSZ pszReturn = 0;
|
---|
223 | CHAR szDosError[1000];
|
---|
224 | ULONG cbDosError = 0;
|
---|
225 | DosGetMessage(NULL, 0, // no string replacements
|
---|
226 | szDosError, sizeof(szDosError),
|
---|
227 | arc,
|
---|
228 | "OSO001.MSG", // default OS/2 message file
|
---|
229 | &cbDosError);
|
---|
230 | if (cbDosError > 2)
|
---|
231 | {
|
---|
232 | szDosError[cbDosError - 2] = 0;
|
---|
233 | pszReturn = strdup(szDosError);
|
---|
234 | }
|
---|
235 | return (pszReturn);
|
---|
236 | }
|
---|
237 |
|
---|
238 | /*
|
---|
239 | *@@ doshQuerySysUptime:
|
---|
240 | * returns the system uptime in milliseconds.
|
---|
241 | * This can be used for time comparisons.
|
---|
242 | *
|
---|
243 | *@@added V0.9.12 (2001-05-18) [umoeller]
|
---|
244 | */
|
---|
245 |
|
---|
246 | ULONG doshQuerySysUptime(VOID)
|
---|
247 | {
|
---|
248 | ULONG ulms;
|
---|
249 | DosQuerySysInfo(QSV_MS_COUNT,
|
---|
250 | QSV_MS_COUNT,
|
---|
251 | &ulms,
|
---|
252 | sizeof(ulms));
|
---|
253 | return (ulms);
|
---|
254 | }
|
---|
255 |
|
---|
256 | /*
|
---|
257 | *@@ doshDevIOCtl:
|
---|
258 | *
|
---|
259 | * Works with those IOCtls where the buffer
|
---|
260 | * size parameters are always the same anyway,
|
---|
261 | * which applies to all IOCtls I have seen
|
---|
262 | * so far.
|
---|
263 | *
|
---|
264 | *@@added V0.9.13 (2001-06-14) [umoeller]
|
---|
265 | */
|
---|
266 |
|
---|
267 | APIRET doshDevIOCtl(HFILE hf,
|
---|
268 | ULONG ulCategory,
|
---|
269 | ULONG ulFunction,
|
---|
270 | PVOID pvParams,
|
---|
271 | ULONG cbParams,
|
---|
272 | PVOID pvData,
|
---|
273 | ULONG cbData)
|
---|
274 | {
|
---|
275 | return (DosDevIOCtl(hf,
|
---|
276 | ulCategory,
|
---|
277 | ulFunction,
|
---|
278 | pvParams, cbParams, &cbParams,
|
---|
279 | pvData, cbData, &cbData));
|
---|
280 | }
|
---|
281 |
|
---|
282 | /*
|
---|
283 | *@@category: Helpers\Control program helpers\Shared memory management
|
---|
284 | * helpers for allocating and requesting shared memory.
|
---|
285 | */
|
---|
286 |
|
---|
287 | /* ******************************************************************
|
---|
288 | *
|
---|
289 | * Memory helpers
|
---|
290 | *
|
---|
291 | ********************************************************************/
|
---|
292 |
|
---|
293 | /*
|
---|
294 | *@@ Allocate:
|
---|
295 | * wrapper around malloc() which automatically
|
---|
296 | * sets ERROR_NOT_ENOUGH_MEMORY.
|
---|
297 | *
|
---|
298 | *@@added V0.9.16 (2001-10-19) [umoeller]
|
---|
299 | */
|
---|
300 |
|
---|
301 | PVOID doshMalloc(ULONG cb,
|
---|
302 | APIRET *parc)
|
---|
303 | {
|
---|
304 | PVOID pv;
|
---|
305 | *parc = NO_ERROR;
|
---|
306 | if (!(pv = malloc(cb)))
|
---|
307 | *parc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
308 |
|
---|
309 | return (pv);
|
---|
310 | }
|
---|
311 |
|
---|
312 | /*
|
---|
313 | *@@ doshAllocSharedMem:
|
---|
314 | * wrapper for DosAllocSharedMem which has
|
---|
315 | * a malloc()-like syntax. Just due to my
|
---|
316 | * lazyness.
|
---|
317 | *
|
---|
318 | * Note that ulSize is always rounded up to the
|
---|
319 | * next 4KB value, so don't use this hundreds of times.
|
---|
320 | *
|
---|
321 | * Returns NULL upon errors. Possible errors include
|
---|
322 | * that a memory block calle pcszName has already been
|
---|
323 | * allocated.
|
---|
324 | *
|
---|
325 | * Use DosFreeMem(pvrc) to free the memory. The memory
|
---|
326 | * will only be freed if no other process has requested
|
---|
327 | * access.
|
---|
328 | *
|
---|
329 | *@@added V0.9.3 (2000-04-18) [umoeller]
|
---|
330 | */
|
---|
331 |
|
---|
332 | PVOID doshAllocSharedMem(ULONG ulSize, // in: requested mem block size (rounded up to 4KB)
|
---|
333 | const char* pcszName) // in: name of block ("\\SHAREMEM\\xxx") or NULL
|
---|
334 | {
|
---|
335 | PVOID pvrc = NULL;
|
---|
336 | APIRET arc = DosAllocSharedMem((PVOID*)&pvrc,
|
---|
337 | (PSZ)pcszName,
|
---|
338 | ulSize,
|
---|
339 | PAG_COMMIT | PAG_READ | PAG_WRITE);
|
---|
340 | if (arc == NO_ERROR)
|
---|
341 | return (pvrc);
|
---|
342 |
|
---|
343 | return (NULL);
|
---|
344 | }
|
---|
345 |
|
---|
346 | /*
|
---|
347 | *@@ doshRequestSharedMem:
|
---|
348 | * requests access to a block of named shared memory
|
---|
349 | * allocated by doshAllocSharedMem.
|
---|
350 | *
|
---|
351 | * Returns NULL upon errors.
|
---|
352 | *
|
---|
353 | * Use DosFreeMem(pvrc) to free the memory. The memory
|
---|
354 | * will only be freed if no other process has requested
|
---|
355 | * access.
|
---|
356 | *
|
---|
357 | *@@added V0.9.3 (2000-04-19) [umoeller]
|
---|
358 | */
|
---|
359 |
|
---|
360 | PVOID doshRequestSharedMem(PCSZ pcszName)
|
---|
361 | {
|
---|
362 | PVOID pvrc = NULL;
|
---|
363 | APIRET arc = DosGetNamedSharedMem((PVOID*)pvrc,
|
---|
364 | (PSZ)pcszName,
|
---|
365 | PAG_READ | PAG_WRITE);
|
---|
366 | if (arc == NO_ERROR)
|
---|
367 | return (pvrc);
|
---|
368 |
|
---|
369 | return (NULL);
|
---|
370 | }
|
---|
371 |
|
---|
372 | /*
|
---|
373 | *@@category: Helpers\Control program helpers\Drive management
|
---|
374 | * functions for managing drives... enumerating, testing,
|
---|
375 | * querying etc.
|
---|
376 | */
|
---|
377 |
|
---|
378 | /* ******************************************************************
|
---|
379 | *
|
---|
380 | * Drive helpers
|
---|
381 | *
|
---|
382 | ********************************************************************/
|
---|
383 |
|
---|
384 | /*
|
---|
385 | *@@ doshIsFixedDisk:
|
---|
386 | * checks whether a disk is fixed or removeable.
|
---|
387 | * ulLogicalDrive must be 1 for drive A:, 2 for B:, ...
|
---|
388 | * The result is stored in *pfFixed.
|
---|
389 | * Returns DOS error code.
|
---|
390 | *
|
---|
391 | * From my testing, this function does _not_ provoke
|
---|
392 | * "drive not ready" popups, even if the disk is not
|
---|
393 | * ready.
|
---|
394 | *
|
---|
395 | * Warning: This uses DosDevIOCtl, which has proved
|
---|
396 | * to cause problems with some device drivers for
|
---|
397 | * removeable disks.
|
---|
398 | *
|
---|
399 | *@@changed V0.9.14 (2001-08-03) [umoeller]: added extra fix for A: and B:
|
---|
400 | */
|
---|
401 |
|
---|
402 | APIRET doshIsFixedDisk(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
403 | PBOOL pfFixed) // out: TRUE for fixed disks
|
---|
404 | {
|
---|
405 | APIRET arc = ERROR_INVALID_DRIVE;
|
---|
406 |
|
---|
407 | if ( (ulLogicalDrive == 1)
|
---|
408 | || (ulLogicalDrive == 2)
|
---|
409 | )
|
---|
410 | {
|
---|
411 | // drive A: and B: can never be fixed V0.9.14 (2001-08-03) [umoeller]
|
---|
412 | *pfFixed = FALSE;
|
---|
413 | return NO_ERROR;
|
---|
414 | }
|
---|
415 |
|
---|
416 | if (ulLogicalDrive)
|
---|
417 | {
|
---|
418 | // parameter packet
|
---|
419 | #pragma pack(1)
|
---|
420 | struct {
|
---|
421 | UCHAR command,
|
---|
422 | drive;
|
---|
423 | } parms;
|
---|
424 | #pragma pack()
|
---|
425 |
|
---|
426 | // data packet
|
---|
427 | UCHAR ucNonRemoveable;
|
---|
428 |
|
---|
429 | parms.drive = (UCHAR)(ulLogicalDrive-1);
|
---|
430 | arc = doshDevIOCtl((HFILE)-1,
|
---|
431 | IOCTL_DISK, // 0x08
|
---|
432 | DSK_BLOCKREMOVABLE, // 0x20
|
---|
433 | &parms, sizeof(parms),
|
---|
434 | &ucNonRemoveable, sizeof(ucNonRemoveable));
|
---|
435 |
|
---|
436 | if (arc == NO_ERROR)
|
---|
437 | *pfFixed = (BOOL)ucNonRemoveable;
|
---|
438 | }
|
---|
439 |
|
---|
440 | return (arc);
|
---|
441 | }
|
---|
442 |
|
---|
443 | /*
|
---|
444 | *@@ doshQueryDiskParams:
|
---|
445 | * this retrieves more information about a given drive,
|
---|
446 | * which is stored in the specified BIOSPARAMETERBLOCK
|
---|
447 | * structure.
|
---|
448 | *
|
---|
449 | * BIOSPARAMETERBLOCK is defined in the Toolkit headers,
|
---|
450 | * and from my testing, it's the same with the Toolkits
|
---|
451 | * 3 and 4.5.
|
---|
452 | *
|
---|
453 | * If NO_ERROR is returned, the bDeviceType field can
|
---|
454 | * be one of the following (according to CPREF):
|
---|
455 | *
|
---|
456 | * -- 0: 48 TPI low-density diskette drive
|
---|
457 | * -- 1: 96 TPI high-density diskette drive
|
---|
458 | * -- 2: 3.5-inch 720KB diskette drive
|
---|
459 | * -- 3: 8-Inch single-density diskette drive
|
---|
460 | * -- 4: 8-Inch double-density diskette drive
|
---|
461 | * -- 5: Fixed disk
|
---|
462 | * -- 6: Tape drive
|
---|
463 | * -- 7: Other (includes 1.44MB 3.5-inch diskette drive)
|
---|
464 | * -- 8: R/W optical disk
|
---|
465 | * -- 9: 3.5-inch 4.0MB diskette drive (2.88MB formatted)
|
---|
466 | *
|
---|
467 | * From my testing, this function does _not_ provoke
|
---|
468 | * "drive not ready" popups, even if the disk is not
|
---|
469 | * ready.
|
---|
470 | *
|
---|
471 | * Warning: This uses DosDevIOCtl, which has proved
|
---|
472 | * to cause problems with some device drivers for
|
---|
473 | * removeable disks.
|
---|
474 | *
|
---|
475 | * This returns the DOS error code of DosDevIOCtl.
|
---|
476 | *
|
---|
477 | *@@added V0.9.0 [umoeller]
|
---|
478 | *@@changed V0.9.13 (2001-06-14) [umoeller]: changed prototype to use BIOSPARAMETERBLOCK directly
|
---|
479 | *@@changed V0.9.13 (2001-06-14) [umoeller]: now querying standard media, no redetermine
|
---|
480 | */
|
---|
481 |
|
---|
482 | APIRET doshQueryDiskParams(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
483 | PBIOSPARAMETERBLOCK pdp) // out: drive parameters
|
---|
484 | {
|
---|
485 | APIRET arc = ERROR_INVALID_DRIVE;
|
---|
486 |
|
---|
487 | if (ulLogicalDrive)
|
---|
488 | {
|
---|
489 | #pragma pack(1)
|
---|
490 | // parameter packet
|
---|
491 | struct {
|
---|
492 | UCHAR ucCommand,
|
---|
493 | ucDrive;
|
---|
494 | } parms;
|
---|
495 | #pragma pack()
|
---|
496 |
|
---|
497 | parms.ucCommand = 0; // 0 = return standard media,
|
---|
498 | // 1 = read currently inserted media
|
---|
499 | // (1 doesn't work any more, returns arc 87
|
---|
500 | // V0.9.13 (2001-06-14) [umoeller])
|
---|
501 | parms.ucDrive=(UCHAR)(ulLogicalDrive-1);
|
---|
502 |
|
---|
503 | // zero the structure V0.9.13 (2001-06-14) [umoeller]
|
---|
504 | memset(pdp, 0, sizeof(BIOSPARAMETERBLOCK));
|
---|
505 |
|
---|
506 | arc = doshDevIOCtl((HFILE)-1,
|
---|
507 | IOCTL_DISK, // 0x08
|
---|
508 | DSK_GETDEVICEPARAMS, // 0x63
|
---|
509 | &parms, sizeof(parms),
|
---|
510 | pdp, sizeof(BIOSPARAMETERBLOCK));
|
---|
511 |
|
---|
512 | /* if (!arc)
|
---|
513 | {
|
---|
514 | _Pmpf((" bDeviceType: %d", pdp->bDeviceType));
|
---|
515 | _Pmpf((" bytes per sector: %d", pdp->usBytesPerSector));
|
---|
516 | _Pmpf((" sectors per track: %d", pdp->usSectorsPerTrack));
|
---|
517 | } */
|
---|
518 | }
|
---|
519 |
|
---|
520 | return (arc);
|
---|
521 | }
|
---|
522 |
|
---|
523 | /*
|
---|
524 | *@@ doshIsCDROM:
|
---|
525 | * tests the specified BIOSPARAMETERBLOCK
|
---|
526 | * for whether it represents a CD-ROM drive.
|
---|
527 | *
|
---|
528 | * The BIOSPARAMETERBLOCK must be filled
|
---|
529 | * first using doshQueryDiskParams.
|
---|
530 | *
|
---|
531 | *@@added V0.9.13 (2001-06-14) [umoeller]
|
---|
532 | */
|
---|
533 |
|
---|
534 | BOOL doshIsCDROM(PBIOSPARAMETERBLOCK pdp)
|
---|
535 | {
|
---|
536 | return ( (pdp)
|
---|
537 | && (pdp->bDeviceType == 7) // "other"
|
---|
538 | && (pdp->usBytesPerSector == 2048)
|
---|
539 | && (pdp->usSectorsPerTrack == (USHORT)-1)
|
---|
540 | );
|
---|
541 | }
|
---|
542 |
|
---|
543 | /*
|
---|
544 | *@@ doshHasAudioCD:
|
---|
545 | * sets *pfAudio to whether ulLogicalDrive
|
---|
546 | * currently has an audio CD inserted.
|
---|
547 | *
|
---|
548 | * Better call this only if you're sure that
|
---|
549 | * ulLogicalDrive is a CD-ROM drive. Use
|
---|
550 | * doshIsCDROM to check.
|
---|
551 | *
|
---|
552 | *@@added V0.9.14 (2001-08-01) [umoeller]
|
---|
553 | */
|
---|
554 |
|
---|
555 | APIRET doshHasAudioCD(ULONG ulLogicalDrive,
|
---|
556 | HFILE hfDrive, // in: DASD open
|
---|
557 | BOOL fMixedModeCD,
|
---|
558 | PBOOL pfAudio)
|
---|
559 | {
|
---|
560 | APIRET arc = NO_ERROR;
|
---|
561 |
|
---|
562 | ULONG ulAudioTracks = 0,
|
---|
563 | ulDataTracks = 0;
|
---|
564 |
|
---|
565 | CHAR cds1[4] = { 'C', 'D', '0', '1' };
|
---|
566 | CHAR cds2[4];
|
---|
567 |
|
---|
568 | *pfAudio = FALSE;
|
---|
569 |
|
---|
570 | // check for proper driver signature
|
---|
571 | if (!(arc = doshDevIOCtl(hfDrive,
|
---|
572 | IOCTL_CDROMDISK,
|
---|
573 | CDROMDISK_GETDRIVER,
|
---|
574 | &cds1, sizeof(cds1),
|
---|
575 | &cds2, sizeof(cds2))))
|
---|
576 | {
|
---|
577 | if (memcmp(&cds1, &cds2, 4))
|
---|
578 | // this is not a CD-ROM then:
|
---|
579 | arc = NO_ERROR;
|
---|
580 | else
|
---|
581 | {
|
---|
582 | struct {
|
---|
583 | UCHAR ucFirstTrack,
|
---|
584 | ucLastTrack;
|
---|
585 | ULONG ulLeadOut;
|
---|
586 | } cdat;
|
---|
587 |
|
---|
588 | // get track count
|
---|
589 | if (!(arc = doshDevIOCtl(hfDrive,
|
---|
590 | IOCTL_CDROMAUDIO,
|
---|
591 | CDROMAUDIO_GETAUDIODISK,
|
---|
592 | &cds1, sizeof(cds1),
|
---|
593 | &cdat, sizeof(cdat))))
|
---|
594 | {
|
---|
595 | // still no error: build the audio TOC
|
---|
596 | ULONG i;
|
---|
597 | for (i = cdat.ucFirstTrack;
|
---|
598 | i <= cdat.ucLastTrack;
|
---|
599 | i++)
|
---|
600 | {
|
---|
601 | BYTE cdtp[5] =
|
---|
602 | { 'C', 'D', '0', '1', (UCHAR)i };
|
---|
603 |
|
---|
604 | struct {
|
---|
605 | ULONG ulTrackAddress;
|
---|
606 | BYTE bFlags;
|
---|
607 | } trackdata;
|
---|
608 |
|
---|
609 | if (!(arc = doshDevIOCtl(hfDrive,
|
---|
610 | IOCTL_CDROMAUDIO,
|
---|
611 | CDROMAUDIO_GETAUDIOTRACK,
|
---|
612 | &cdtp, sizeof(cdtp),
|
---|
613 | &trackdata, sizeof(trackdata))))
|
---|
614 | {
|
---|
615 | if (trackdata.bFlags & 64)
|
---|
616 | ulDataTracks++;
|
---|
617 | else
|
---|
618 | {
|
---|
619 | ulAudioTracks++;
|
---|
620 |
|
---|
621 | if (!fMixedModeCD)
|
---|
622 | {
|
---|
623 | // caller doesn't want mixed mode:
|
---|
624 | // stop here
|
---|
625 | ulDataTracks = 0;
|
---|
626 | break;
|
---|
627 | }
|
---|
628 | }
|
---|
629 | }
|
---|
630 | }
|
---|
631 |
|
---|
632 | // _Pmpf((" got %d audio, %d data tracks",
|
---|
633 | // ulAudioTracks, ulDataTracks));
|
---|
634 |
|
---|
635 | if (!ulDataTracks)
|
---|
636 | *pfAudio = TRUE;
|
---|
637 | }
|
---|
638 | else
|
---|
639 | {
|
---|
640 | // not audio disk:
|
---|
641 | // go on then
|
---|
642 | // _Pmpf((" CDROMAUDIO_GETAUDIODISK returned %d", arc));
|
---|
643 | arc = NO_ERROR;
|
---|
644 | }
|
---|
645 | }
|
---|
646 | }
|
---|
647 | else
|
---|
648 | {
|
---|
649 | // not CD-ROM: go on then
|
---|
650 | // _Pmpf((" CDROMDISK_GETDRIVER returned %d", arc));
|
---|
651 | arc = NO_ERROR;
|
---|
652 | }
|
---|
653 |
|
---|
654 | return (arc);
|
---|
655 | }
|
---|
656 |
|
---|
657 | /*
|
---|
658 | *@@ doshEnumDrives:
|
---|
659 | * this function enumerates all valid drive letters on
|
---|
660 | * the system by composing a string of drive letters
|
---|
661 | * in the buffer pointed to by pszBuffer, which should
|
---|
662 | * be 27 characters in size to hold information for
|
---|
663 | * all drives. The buffer will be null-terminated.
|
---|
664 | *
|
---|
665 | * If (pcszFileSystem != NULL), only drives matching
|
---|
666 | * the specified file system type (e.g. "HPFS") will
|
---|
667 | * be enumerated. If (pcszFileSystem == NULL), all
|
---|
668 | * drives will be enumerated.
|
---|
669 | *
|
---|
670 | * If (fSkipRemovables == TRUE), removeable drives will
|
---|
671 | * be skipped. This applies to floppy, CD-ROM, and
|
---|
672 | * virtual floppy drives. This will start the search
|
---|
673 | * at drive letter C: so that drives A: and B: will
|
---|
674 | * never be checked (to avoid the hardware bumps).
|
---|
675 | *
|
---|
676 | * Otherwise, the search starts at drive A:. Still,
|
---|
677 | * removeable drives will only be added if valid media
|
---|
678 | * is inserted.
|
---|
679 | *
|
---|
680 | *@@changed V0.9.4 (2000-07-03) [umoeller]: this stopped at the first invalid drive letter; fixed
|
---|
681 | *@@changed V0.9.4 (2000-07-03) [umoeller]: added fSkipRemoveables
|
---|
682 | */
|
---|
683 |
|
---|
684 | VOID doshEnumDrives(PSZ pszBuffer, // out: drive letters
|
---|
685 | PCSZ pcszFileSystem, // in: FS's to match or NULL
|
---|
686 | BOOL fSkipRemoveables) // in: if TRUE, only non-removeable disks will be returned
|
---|
687 | {
|
---|
688 | CHAR szName[5] = "";
|
---|
689 | ULONG ulLogicalDrive = 1, // start with drive A:
|
---|
690 | ulFound = 0; // found drives count
|
---|
691 | APIRET arc = NO_ERROR; // return code
|
---|
692 |
|
---|
693 | if (fSkipRemoveables)
|
---|
694 | // start with drive C:
|
---|
695 | ulLogicalDrive = 3;
|
---|
696 |
|
---|
697 | // go thru the drives, start with C: (== 3), stop after Z: (== 26)
|
---|
698 | while (ulLogicalDrive <= 26)
|
---|
699 | {
|
---|
700 | #pragma pack(1)
|
---|
701 | struct
|
---|
702 | {
|
---|
703 | UCHAR dummy,drive;
|
---|
704 | } parms;
|
---|
705 | #pragma pack()
|
---|
706 |
|
---|
707 | // data packet
|
---|
708 | UCHAR nonRemovable=0;
|
---|
709 |
|
---|
710 | parms.drive=(UCHAR)(ulLogicalDrive-1);
|
---|
711 | arc = doshDevIOCtl((HFILE)-1,
|
---|
712 | IOCTL_DISK,
|
---|
713 | DSK_BLOCKREMOVABLE,
|
---|
714 | &parms, sizeof(parms),
|
---|
715 | &nonRemovable, sizeof(nonRemovable));
|
---|
716 |
|
---|
717 | if ( // fixed disk and non-removeable
|
---|
718 | ((arc == NO_ERROR) && (nonRemovable))
|
---|
719 | // or network drive:
|
---|
720 | || (arc == ERROR_NOT_SUPPORTED)
|
---|
721 | )
|
---|
722 | {
|
---|
723 | ULONG ulOrdinal = 0; // ordinal of entry in name list
|
---|
724 | BYTE fsqBuffer[sizeof(FSQBUFFER2) + (3 * CCHMAXPATH)] = {0};
|
---|
725 | ULONG cbBuffer = sizeof(fsqBuffer); // Buffer length)
|
---|
726 | PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)fsqBuffer;
|
---|
727 |
|
---|
728 | szName[0] = ulLogicalDrive + 'A' - 1;
|
---|
729 | szName[1] = ':';
|
---|
730 | szName[2] = '\0';
|
---|
731 |
|
---|
732 | arc = DosQueryFSAttach(szName, // logical drive of attached FS
|
---|
733 | ulOrdinal, // ignored for FSAIL_QUERYNAME
|
---|
734 | FSAIL_QUERYNAME, // return data for a Drive or Device
|
---|
735 | pfsqBuffer, // returned data
|
---|
736 | &cbBuffer); // returned data length
|
---|
737 |
|
---|
738 | if (arc == NO_ERROR)
|
---|
739 | {
|
---|
740 | // The data for the last three fields in the FSQBUFFER2
|
---|
741 | // structure are stored at the offset of fsqBuffer.szName.
|
---|
742 | // Each data field following fsqBuffer.szName begins
|
---|
743 | // immediately after the previous item.
|
---|
744 | CHAR* pszFSDName = (PSZ)&(pfsqBuffer->szName) + (pfsqBuffer->cbName) + 1;
|
---|
745 | if (pcszFileSystem == NULL)
|
---|
746 | {
|
---|
747 | // enum-all mode: always copy
|
---|
748 | pszBuffer[ulFound] = szName[0]; // drive letter
|
---|
749 | ulFound++;
|
---|
750 | }
|
---|
751 | else if (strcmp(pszFSDName, pcszFileSystem) == 0)
|
---|
752 | {
|
---|
753 | pszBuffer[ulFound] = szName[0]; // drive letter
|
---|
754 | ulFound++;
|
---|
755 | }
|
---|
756 | }
|
---|
757 | }
|
---|
758 |
|
---|
759 | ulLogicalDrive++;
|
---|
760 | } // end while (G_acDriveLetters[ulLogicalDrive] <= 'Z')
|
---|
761 |
|
---|
762 | pszBuffer[ulFound] = '\0';
|
---|
763 | }
|
---|
764 |
|
---|
765 | /*
|
---|
766 | *@@ doshQueryBootDrive:
|
---|
767 | * returns the letter of the boot drive as a
|
---|
768 | * single (capital) character, which is useful for
|
---|
769 | * constructing file names using sprintf and such.
|
---|
770 | */
|
---|
771 |
|
---|
772 | CHAR doshQueryBootDrive(VOID)
|
---|
773 | {
|
---|
774 | ULONG ulBootDrive;
|
---|
775 | DosQuerySysInfo(QSV_BOOT_DRIVE, QSV_BOOT_DRIVE,
|
---|
776 | &ulBootDrive,
|
---|
777 | sizeof(ulBootDrive));
|
---|
778 | return (ulBootDrive + 'A' - 1);
|
---|
779 | }
|
---|
780 |
|
---|
781 | /*
|
---|
782 | *@@ doshAssertDrive:
|
---|
783 | * this checks for whether the given drive
|
---|
784 | * is currently available without provoking
|
---|
785 | * those ugly white "Drive not ready" popups.
|
---|
786 | *
|
---|
787 | * "fl" can specify additional flags for testing
|
---|
788 | * and can be any combination of:
|
---|
789 | *
|
---|
790 | * -- ASSERTFL_MIXEDMODECD: whether to allow
|
---|
791 | * mixed-mode CD-ROMs. See error codes below.
|
---|
792 | *
|
---|
793 | * This returns (from my testing):
|
---|
794 | *
|
---|
795 | * -- NO_ERROR: drive is available.
|
---|
796 | *
|
---|
797 | * -- ERROR_INVALID_DRIVE (15): drive letter does not exist.
|
---|
798 | *
|
---|
799 | * -- ERROR_NOT_READY (21): drive exists, but is not ready.
|
---|
800 | * This is produced by floppies and CD-ROM drives
|
---|
801 | * which do not have valid media inserted.
|
---|
802 | *
|
---|
803 | * -- ERROR_AUDIO_CD_ROM (10000): special error code returned
|
---|
804 | * only by this function if a CD-ROM drive has audio
|
---|
805 | * media inserted.
|
---|
806 | *
|
---|
807 | * If ASSERTFL_MIXEDMODECD was specified, ERROR_AUDIO_CD_ROM
|
---|
808 | * is returned _only_ if _no_ data tracks are
|
---|
809 | * present on a CD-ROM. Since OS/2 is not very
|
---|
810 | * good at handling mixed-mode CDs, this might not
|
---|
811 | * be desireable.
|
---|
812 | *
|
---|
813 | * If ASSERTFL_MIXEDMODECD was not set, ERROR_AUDIO_CD_ROM
|
---|
814 | * will be returned already if _one_ audio track is present.
|
---|
815 | *
|
---|
816 | *@@changed V0.9.1 (99-12-13) [umoeller]: rewritten, prototype changed. Now using DosOpen on the drive instead of DosError.
|
---|
817 | *@@changed V0.9.1 (2000-01-08) [umoeller]: DosClose was called even if DosOpen failed, which messed up OS/2 error handling.
|
---|
818 | *@@changed V0.9.1 (2000-02-09) [umoeller]: this didn't work for network drives, including RAMFS; fixed.
|
---|
819 | *@@changed V0.9.3 (2000-03-28) [umoeller]: added check for network drives, which weren't working
|
---|
820 | *@@changed V0.9.4 (2000-08-03) [umoeller]: more network fixes
|
---|
821 | *@@changed V0.9.9 (2001-03-19) [pr]: validate drive number
|
---|
822 | *@@changed V0.9.11 (2001-04-23) [umoeller]: added an extra check for floppies
|
---|
823 | *@@changed V0.9.13 (2001-06-14) [umoeller]: added "fl" parameter and lots of CD-ROM checks
|
---|
824 | */
|
---|
825 |
|
---|
826 | APIRET doshAssertDrive(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
827 | ULONG fl) // in: ASSERTFL_* flags
|
---|
828 | {
|
---|
829 | APIRET arc = NO_ERROR;
|
---|
830 | BOOL fFixed = FALSE,
|
---|
831 | fCDROM = FALSE;
|
---|
832 |
|
---|
833 | if ((ulLogicalDrive < 1) || (ulLogicalDrive > 26))
|
---|
834 | return(ERROR_PATH_NOT_FOUND);
|
---|
835 |
|
---|
836 | arc = doshIsFixedDisk(ulLogicalDrive,
|
---|
837 | &fFixed); // V0.9.13 (2001-06-14) [umoeller]
|
---|
838 |
|
---|
839 | // _Pmpf((__FUNCTION__ ": doshIsFixedDisk returned %d for disk %d", arc, ulLogicalDrive));
|
---|
840 | // _Pmpf((" fFixed is %d", fFixed));
|
---|
841 |
|
---|
842 | if (!arc)
|
---|
843 | if (!fFixed)
|
---|
844 | {
|
---|
845 | // removeable disk:
|
---|
846 | // check if it's a CD-ROM
|
---|
847 | BIOSPARAMETERBLOCK bpb;
|
---|
848 | arc = doshQueryDiskParams(ulLogicalDrive,
|
---|
849 | &bpb);
|
---|
850 | // _Pmpf((" doshQueryDiskParams returned %d", arc));
|
---|
851 |
|
---|
852 | if ( (!arc)
|
---|
853 | && (doshIsCDROM(&bpb))
|
---|
854 | )
|
---|
855 | {
|
---|
856 | // _Pmpf((" --> is CD-ROM"));
|
---|
857 | fCDROM = TRUE;
|
---|
858 | }
|
---|
859 | }
|
---|
860 |
|
---|
861 | if (!arc)
|
---|
862 | {
|
---|
863 | HFILE hfDrive = NULLHANDLE;
|
---|
864 |
|
---|
865 | ULONG ulTemp = 0;
|
---|
866 | CHAR szDrive[3] = "C:";
|
---|
867 | szDrive[0] = 'A' + ulLogicalDrive - 1;
|
---|
868 |
|
---|
869 | arc = DosOpen(szDrive, // "C:", "D:", ...
|
---|
870 | &hfDrive,
|
---|
871 | &ulTemp,
|
---|
872 | 0,
|
---|
873 | FILE_NORMAL,
|
---|
874 | OPEN_ACTION_FAIL_IF_NEW
|
---|
875 | | OPEN_ACTION_OPEN_IF_EXISTS,
|
---|
876 | OPEN_FLAGS_DASD
|
---|
877 | | OPEN_FLAGS_FAIL_ON_ERROR
|
---|
878 | | OPEN_FLAGS_NOINHERIT // V0.9.6 (2000-11-25) [pr]
|
---|
879 | // | OPEN_ACCESS_READONLY // V0.9.13 (2001-06-14) [umoeller]
|
---|
880 | | OPEN_SHARE_DENYNONE,
|
---|
881 | NULL);
|
---|
882 |
|
---|
883 | // _Pmpf((" DosOpen(OPEN_FLAGS_DASD) returned %d", arc));
|
---|
884 |
|
---|
885 | // this still returns NO_ERROR for audio CDs in a
|
---|
886 | // CD-ROM drive...
|
---|
887 | // however, the WPS then attempts to read in the
|
---|
888 | // root directory for audio CDs, which produces
|
---|
889 | // a "sector not found" error box...
|
---|
890 |
|
---|
891 | if ( (!arc)
|
---|
892 | && (hfDrive)
|
---|
893 | && (fCDROM)
|
---|
894 | )
|
---|
895 | {
|
---|
896 | BOOL fAudio;
|
---|
897 | if ( (!(arc = doshHasAudioCD(ulLogicalDrive,
|
---|
898 | hfDrive,
|
---|
899 | ((fl & ASSERTFL_MIXEDMODECD) != 0),
|
---|
900 | &fAudio)))
|
---|
901 | && (fAudio)
|
---|
902 | )
|
---|
903 | arc = ERROR_AUDIO_CD_ROM; // special private error code (10000)
|
---|
904 | }
|
---|
905 |
|
---|
906 | if (hfDrive)
|
---|
907 | DosClose(hfDrive);
|
---|
908 | }
|
---|
909 |
|
---|
910 | switch (arc)
|
---|
911 | {
|
---|
912 | case ERROR_NETWORK_ACCESS_DENIED: // 65
|
---|
913 | // added V0.9.3 (2000-03-27) [umoeller];
|
---|
914 | // according to user reports, this is returned
|
---|
915 | // by all network drives, which apparently don't
|
---|
916 | // support DASD DosOpen
|
---|
917 | case ERROR_ACCESS_DENIED: // 5
|
---|
918 | // added V0.9.4 (2000-07-10) [umoeller]
|
---|
919 | // LAN drives still didn't work... apparently
|
---|
920 | // the above only works for NFS drives
|
---|
921 | case ERROR_PATH_NOT_FOUND: // 3
|
---|
922 | // added V0.9.4 (2000-08-03) [umoeller]:
|
---|
923 | // this is returned by some other network types...
|
---|
924 | // sigh...
|
---|
925 | case ERROR_NOT_SUPPORTED: // 50
|
---|
926 | // this is returned by file systems which don't
|
---|
927 | // support DASD DosOpen;
|
---|
928 | // use some other method then, this isn't likely
|
---|
929 | // to fail -- V0.9.1 (2000-02-09) [umoeller]
|
---|
930 |
|
---|
931 | // but don't do this for floppies
|
---|
932 | // V0.9.11 (2001-04-23) [umoeller]
|
---|
933 | if (ulLogicalDrive > 2)
|
---|
934 | {
|
---|
935 | FSALLOCATE fsa;
|
---|
936 | arc = DosQueryFSInfo(ulLogicalDrive,
|
---|
937 | FSIL_ALLOC,
|
---|
938 | &fsa,
|
---|
939 | sizeof(fsa));
|
---|
940 | // _Pmpf((" re-checked, DosQueryFSInfo returned %d", arc));
|
---|
941 | }
|
---|
942 | break;
|
---|
943 | }
|
---|
944 |
|
---|
945 | return (arc);
|
---|
946 | }
|
---|
947 |
|
---|
948 | /*
|
---|
949 | *@@ doshSetLogicalMap:
|
---|
950 | * sets the mapping of logical floppy drives onto a single
|
---|
951 | * physical floppy drive.
|
---|
952 | * This means selecting either drive A: or drive B: to refer
|
---|
953 | * to the physical drive.
|
---|
954 | *
|
---|
955 | *@@added V0.9.6 (2000-11-24) [pr]
|
---|
956 | */
|
---|
957 |
|
---|
958 | APIRET doshSetLogicalMap(ULONG ulLogicalDrive)
|
---|
959 | {
|
---|
960 | CHAR name[3] = "?:";
|
---|
961 | ULONG fd = 0,
|
---|
962 | action = 0;
|
---|
963 | // paramsize = 0;
|
---|
964 | // datasize = 0;
|
---|
965 | APIRET rc = NO_ERROR;
|
---|
966 | USHORT data,
|
---|
967 | param;
|
---|
968 |
|
---|
969 | name[0] = doshQueryBootDrive();
|
---|
970 | rc = DosOpen(name,
|
---|
971 | &fd,
|
---|
972 | &action,
|
---|
973 | 0,
|
---|
974 | 0,
|
---|
975 | OPEN_ACTION_FAIL_IF_NEW
|
---|
976 | | OPEN_ACTION_OPEN_IF_EXISTS,
|
---|
977 | OPEN_FLAGS_DASD
|
---|
978 | | OPEN_FLAGS_FAIL_ON_ERROR
|
---|
979 | | OPEN_FLAGS_NOINHERIT
|
---|
980 | | OPEN_ACCESS_READONLY
|
---|
981 | | OPEN_SHARE_DENYNONE,
|
---|
982 | 0);
|
---|
983 |
|
---|
984 | if (rc == NO_ERROR)
|
---|
985 | {
|
---|
986 | param = 0;
|
---|
987 | data = (USHORT)ulLogicalDrive;
|
---|
988 | // paramsize = sizeof(param);
|
---|
989 | // datasize = sizeof(data);
|
---|
990 | rc = doshDevIOCtl(fd,
|
---|
991 | IOCTL_DISK, DSK_SETLOGICALMAP,
|
---|
992 | ¶m, sizeof(param),
|
---|
993 | &data, sizeof(data));
|
---|
994 | DosClose(fd);
|
---|
995 | }
|
---|
996 |
|
---|
997 | return(rc);
|
---|
998 | }
|
---|
999 |
|
---|
1000 | /*
|
---|
1001 | *@@ doshQueryDiskSize:
|
---|
1002 | * returns the size of the specified disk in bytes.
|
---|
1003 | *
|
---|
1004 | * Note: This returns a "double" value, because a ULONG
|
---|
1005 | * can only hold values of some 4 billion, which would
|
---|
1006 | * lead to funny results for drives > 4 GB.
|
---|
1007 | *
|
---|
1008 | *@@added V0.9.11 (2001-04-18) [umoeller]
|
---|
1009 | */
|
---|
1010 |
|
---|
1011 | APIRET doshQueryDiskSize(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
1012 | double *pdSize)
|
---|
1013 | {
|
---|
1014 | APIRET arc = NO_ERROR;
|
---|
1015 | FSALLOCATE fsa;
|
---|
1016 | // double dbl = -1;
|
---|
1017 |
|
---|
1018 | if (!(arc = DosQueryFSInfo(ulLogicalDrive, FSIL_ALLOC, &fsa, sizeof(fsa))))
|
---|
1019 | *pdSize = ((double)fsa.cSectorUnit * fsa.cbSector * fsa.cUnit);
|
---|
1020 |
|
---|
1021 | return (arc);
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | /*
|
---|
1025 | *@@ doshQueryDiskFree:
|
---|
1026 | * returns the number of bytes remaining on the disk
|
---|
1027 | * specified by the given logical drive.
|
---|
1028 | *
|
---|
1029 | * Note: This returns a "double" value, because a ULONG
|
---|
1030 | * can only hold values of some 4 billion, which would
|
---|
1031 | * lead to funny results for drives > 4 GB.
|
---|
1032 | *
|
---|
1033 | *@@changed V0.9.0 [umoeller]: fixed another > 4 GB bug (thanks to Rdiger Ihle)
|
---|
1034 | *@@changed V0.9.7 (2000-12-01) [umoeller]: changed prototype
|
---|
1035 | */
|
---|
1036 |
|
---|
1037 | APIRET doshQueryDiskFree(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
1038 | double *pdFree)
|
---|
1039 | {
|
---|
1040 | APIRET arc = NO_ERROR;
|
---|
1041 | FSALLOCATE fsa;
|
---|
1042 | // double dbl = -1;
|
---|
1043 |
|
---|
1044 | if (!(arc = DosQueryFSInfo(ulLogicalDrive, FSIL_ALLOC, &fsa, sizeof(fsa))))
|
---|
1045 | *pdFree = ((double)fsa.cSectorUnit * fsa.cbSector * fsa.cUnitAvail);
|
---|
1046 | // ^ fixed V0.9.0
|
---|
1047 |
|
---|
1048 | return (arc);
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | /*
|
---|
1052 | *@@ doshQueryDiskFSType:
|
---|
1053 | * copies the file-system type of the given disk object
|
---|
1054 | * (HPFS, FAT, CDFS etc.) to pszBuf.
|
---|
1055 | * Returns the DOS error code.
|
---|
1056 | *
|
---|
1057 | *@@changed V0.9.1 (99-12-12) [umoeller]: added cbBuf to prototype
|
---|
1058 | *@@changed V0.9.14 (2001-08-01) [umoeller]: fixed, this never respected cbBuf
|
---|
1059 | *@@changed V0.9.16 (2001-10-02) [umoeller]: added check for valid logical disk no
|
---|
1060 | */
|
---|
1061 |
|
---|
1062 | APIRET doshQueryDiskFSType(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
1063 | PSZ pszBuf, // out: buffer for FS type
|
---|
1064 | ULONG cbBuf) // in: size of that buffer
|
---|
1065 | {
|
---|
1066 | APIRET arc = NO_ERROR;
|
---|
1067 | CHAR szName[5];
|
---|
1068 |
|
---|
1069 | BYTE fsqBuffer[sizeof(FSQBUFFER2) + (3 * CCHMAXPATH)] = {0};
|
---|
1070 | ULONG cbBuffer = sizeof(fsqBuffer); // Buffer length)
|
---|
1071 | PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)fsqBuffer;
|
---|
1072 |
|
---|
1073 | // compose "D:"-type string from logical drive letter
|
---|
1074 | if (ulLogicalDrive > 0 && ulLogicalDrive < 27)
|
---|
1075 | {
|
---|
1076 | szName[0] = ulLogicalDrive + 'A' - 1;
|
---|
1077 | szName[1] = ':';
|
---|
1078 | szName[2] = '\0';
|
---|
1079 |
|
---|
1080 | arc = DosQueryFSAttach(szName, // logical drive of attached FS ("D:"-style)
|
---|
1081 | 0, // ulOrdinal, ignored for FSAIL_QUERYNAME
|
---|
1082 | FSAIL_QUERYNAME, // return name for a drive or device
|
---|
1083 | pfsqBuffer, // buffer for returned data
|
---|
1084 | &cbBuffer); // sizeof(*pfsqBuffer)
|
---|
1085 |
|
---|
1086 | if (arc == NO_ERROR)
|
---|
1087 | {
|
---|
1088 | if (pszBuf)
|
---|
1089 | {
|
---|
1090 | // The data for the last three fields in the FSQBUFFER2
|
---|
1091 | // structure are stored at the offset of fsqBuffer.szName.
|
---|
1092 | // Each data field following fsqBuffer.szName begins
|
---|
1093 | // immediately after the previous item.
|
---|
1094 | strncpy(pszBuf,
|
---|
1095 | (CHAR*)(&pfsqBuffer->szName) + pfsqBuffer->cbName + 1,
|
---|
1096 | cbBuf); // V0.9.14 (2001-08-01) [umoeller]
|
---|
1097 | *(pszBuf + cbBuf) = '\0';
|
---|
1098 | }
|
---|
1099 | }
|
---|
1100 | }
|
---|
1101 | else
|
---|
1102 | arc = ERROR_INVALID_PARAMETER; // V0.9.16 (2001-10-02) [umoeller]
|
---|
1103 |
|
---|
1104 | return (arc);
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | /*
|
---|
1108 | *@@ doshQueryDiskLabel:
|
---|
1109 | * this returns the label of a disk into
|
---|
1110 | * *pszVolumeLabel, which must be 12 bytes
|
---|
1111 | * in size.
|
---|
1112 | *
|
---|
1113 | * This function was added because the Toolkit
|
---|
1114 | * information for DosQueryFSInfo is only partly
|
---|
1115 | * correct. On OS/2 2.x, that function does not
|
---|
1116 | * take an FSINFO structure as input, but a VOLUMELABEL.
|
---|
1117 | * On Warp, this does take an FSINFO.
|
---|
1118 | *
|
---|
1119 | * DosSetFSInfo is even worse. See doshSetDiskLabel.
|
---|
1120 | *
|
---|
1121 | * See http://zebra.asta.fh-weingarten.de/os2/Snippets/Bugi6787.HTML
|
---|
1122 | * for details.
|
---|
1123 | *
|
---|
1124 | *@@added V0.9.0 [umoeller]
|
---|
1125 | *@@changed V0.9.11 (2001-04-22) [umoeller]: this copied even with errors, fixed
|
---|
1126 | */
|
---|
1127 |
|
---|
1128 | APIRET doshQueryDiskLabel(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
1129 | PSZ pszVolumeLabel) // out: volume label (must be 12 chars in size)
|
---|
1130 | {
|
---|
1131 | APIRET arc;
|
---|
1132 |
|
---|
1133 | #ifdef __OS2V2X__
|
---|
1134 | VOLUMELABEL FSInfoBuf;
|
---|
1135 | #else
|
---|
1136 | FSINFO FSInfoBuf;
|
---|
1137 | #endif
|
---|
1138 |
|
---|
1139 | arc = DosQueryFSInfo(ulLogicalDrive,
|
---|
1140 | FSIL_VOLSER,
|
---|
1141 | &FSInfoBuf,
|
---|
1142 | sizeof(FSInfoBuf)); // depends
|
---|
1143 |
|
---|
1144 | if (!arc) // V0.9.11 (2001-04-22) [umoeller]
|
---|
1145 | {
|
---|
1146 | #ifdef __OS2V2X__
|
---|
1147 | strcpy(pszVolumeLabel, FSInfoBuf.szVolLabel);
|
---|
1148 | #else
|
---|
1149 | strcpy(pszVolumeLabel, FSInfoBuf.vol.szVolLabel);
|
---|
1150 | #endif
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | return (arc);
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | /*
|
---|
1157 | *@@ doshSetDiskLabel:
|
---|
1158 | * this sets the label of a disk.
|
---|
1159 | *
|
---|
1160 | * This function was added because the Toolkit
|
---|
1161 | * information for DosSetFSInfo is flat out wrong.
|
---|
1162 | * That function does not take an FSINFO structure
|
---|
1163 | * as input, but a VOLUMELABEL. As a result, using
|
---|
1164 | * that function with the Toolkit's calling specs
|
---|
1165 | * results in ERROR_LABEL_TOO_LONG always.
|
---|
1166 | *
|
---|
1167 | * See http://zebra.asta.fh-weingarten.de/os2/Snippets/Bugi6787.HTML
|
---|
1168 | * for details.
|
---|
1169 | *
|
---|
1170 | *@@added V0.9.0 [umoeller]
|
---|
1171 | */
|
---|
1172 |
|
---|
1173 | APIRET doshSetDiskLabel(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
1174 | PSZ pszNewLabel)
|
---|
1175 | {
|
---|
1176 | VOLUMELABEL FSInfoBuf;
|
---|
1177 |
|
---|
1178 | // check length; 11 chars plus null byte allowed
|
---|
1179 | FSInfoBuf.cch = (BYTE)strlen(pszNewLabel);
|
---|
1180 | if (FSInfoBuf.cch < sizeof(FSInfoBuf.szVolLabel))
|
---|
1181 | {
|
---|
1182 | strcpy(FSInfoBuf.szVolLabel, pszNewLabel);
|
---|
1183 |
|
---|
1184 | return (DosSetFSInfo(ulLogicalDrive,
|
---|
1185 | FSIL_VOLSER,
|
---|
1186 | &FSInfoBuf,
|
---|
1187 | sizeof(FSInfoBuf)));
|
---|
1188 | }
|
---|
1189 | else
|
---|
1190 | return (ERROR_LABEL_TOO_LONG);
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | /*
|
---|
1194 | *@@category: Helpers\Control program helpers\File name parsing
|
---|
1195 | */
|
---|
1196 |
|
---|
1197 | /* ******************************************************************
|
---|
1198 | *
|
---|
1199 | * File name parsing
|
---|
1200 | *
|
---|
1201 | ********************************************************************/
|
---|
1202 |
|
---|
1203 | /*
|
---|
1204 | *@@ doshGetDriveSpec:
|
---|
1205 | * returns the drive specification in pcszFullFile,
|
---|
1206 | * if any is present. This is useful for UNC support.
|
---|
1207 | *
|
---|
1208 | * This returns:
|
---|
1209 | *
|
---|
1210 | * -- NO_ERROR: drive spec was given, and the output
|
---|
1211 | * fields have been set.
|
---|
1212 | *
|
---|
1213 | * -- ERROR_INVALID_NAME: incorrect UNC syntax.
|
---|
1214 | *
|
---|
1215 | * -- ERROR_INVALID_DRIVE: second char is ':', but
|
---|
1216 | * drive letter is not in the range [A-Z].
|
---|
1217 | *
|
---|
1218 | * -- ERROR_INVALID_PARAMETER: no drive spec given
|
---|
1219 | * at all; apparently pcszFullFile is not fully
|
---|
1220 | * qualified in the first place, or it is NULL,
|
---|
1221 | * or its length is <= 2.
|
---|
1222 | *
|
---|
1223 | *@@added V0.9.16 (2001-10-25) [umoeller]
|
---|
1224 | */
|
---|
1225 |
|
---|
1226 | APIRET doshGetDriveSpec(PCSZ pcszFullFile, // in: fully q'fied file spec
|
---|
1227 | PSZ pszDrive, // out: drive spec ("C:" or "\\SERVER\RESOURCE"; ptr can be NULL)
|
---|
1228 | PULONG pulDriveLen, // out: length of drive spec (2 if local drive; ptr can be NULL)
|
---|
1229 | PBOOL pfIsUNC) // out: set to TRUE if UNC name, FALSE otherwise (ptr can be NULL)
|
---|
1230 | {
|
---|
1231 | APIRET arc = NO_ERROR;
|
---|
1232 | ULONG ulFileSpecLength;
|
---|
1233 |
|
---|
1234 | if ( (pcszFullFile)
|
---|
1235 | && (ulFileSpecLength = strlen(pcszFullFile))
|
---|
1236 | && (ulFileSpecLength >= 2)
|
---|
1237 | )
|
---|
1238 | {
|
---|
1239 | // upper-case the drive letter
|
---|
1240 | if (pcszFullFile[1] == ':')
|
---|
1241 | {
|
---|
1242 | CHAR cDrive = toupper(*pcszFullFile);
|
---|
1243 | // local drive specified:
|
---|
1244 | if ( (cDrive >= 'A')
|
---|
1245 | && (cDrive <= 'Z')
|
---|
1246 | )
|
---|
1247 | {
|
---|
1248 | if (pszDrive)
|
---|
1249 | {
|
---|
1250 | pszDrive[0] = cDrive;
|
---|
1251 | pszDrive[1] = ':';
|
---|
1252 | pszDrive[2] = '\0';
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | if (pulDriveLen)
|
---|
1256 | *pulDriveLen = 2;
|
---|
1257 | if (pfIsUNC)
|
---|
1258 | *pfIsUNC = FALSE;
|
---|
1259 | }
|
---|
1260 | else
|
---|
1261 | // this is not a valid drive:
|
---|
1262 | arc = ERROR_INVALID_DRIVE;
|
---|
1263 | }
|
---|
1264 | else if ( (pcszFullFile[0] == '\\')
|
---|
1265 | && (pcszFullFile[1] == '\\')
|
---|
1266 | )
|
---|
1267 | {
|
---|
1268 | // UNC drive specified:
|
---|
1269 | // this better be a full \\SERVER\RESOURCE string
|
---|
1270 | PCSZ pResource;
|
---|
1271 | if (pResource = strchr(pcszFullFile + 3, '\\'))
|
---|
1272 | {
|
---|
1273 | // we got at least \\SERVER\:
|
---|
1274 | ULONG ulLength;
|
---|
1275 | PCSZ p;
|
---|
1276 |
|
---|
1277 | // check if more stuff is coming
|
---|
1278 | if (p = strchr(pResource + 1, '\\'))
|
---|
1279 | {
|
---|
1280 | // yes: copy server and resource excluding that backslash
|
---|
1281 | if (p == pResource + 1)
|
---|
1282 | // "\\SERVER\\" is invalid
|
---|
1283 | arc = ERROR_INVALID_NAME;
|
---|
1284 | else
|
---|
1285 | // we got "\\SERVER\something\":
|
---|
1286 | // drop the last backslash
|
---|
1287 | ulLength = p - pcszFullFile;
|
---|
1288 | }
|
---|
1289 | else
|
---|
1290 | // "\\SERVER\something" only:
|
---|
1291 | ulLength = ulFileSpecLength;
|
---|
1292 |
|
---|
1293 | if (!arc)
|
---|
1294 | {
|
---|
1295 | if (pszDrive)
|
---|
1296 | {
|
---|
1297 | memcpy(pszDrive,
|
---|
1298 | pcszFullFile,
|
---|
1299 | ulLength);
|
---|
1300 | pszDrive[ulLength] = '\0';
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | if (pulDriveLen)
|
---|
1304 | *pulDriveLen = ulLength;
|
---|
1305 | if (pfIsUNC)
|
---|
1306 | *pfIsUNC = TRUE;
|
---|
1307 | }
|
---|
1308 | }
|
---|
1309 | else
|
---|
1310 | // invalid UNC name:
|
---|
1311 | arc = ERROR_INVALID_NAME;
|
---|
1312 | }
|
---|
1313 | else
|
---|
1314 | // neither local, nor UNC:
|
---|
1315 | arc = ERROR_INVALID_PARAMETER;
|
---|
1316 | }
|
---|
1317 | else
|
---|
1318 | arc = ERROR_INVALID_PARAMETER;
|
---|
1319 |
|
---|
1320 | return (arc);
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 | /*
|
---|
1324 | *@@ doshGetExtension:
|
---|
1325 | * finds the file name extension of pszFilename,
|
---|
1326 | * which can be a file name only or a fully
|
---|
1327 | * qualified filename.
|
---|
1328 | *
|
---|
1329 | * This returns a pointer into pszFilename to
|
---|
1330 | * the character after the last dot.
|
---|
1331 | *
|
---|
1332 | * Returns NULL if not found (e.g. if the filename
|
---|
1333 | * has no dot in it).
|
---|
1334 | *
|
---|
1335 | *@@added V0.9.6 (2000-10-16) [umoeller]
|
---|
1336 | *@@changed V0.9.7 (2000-12-10) [umoeller]: fixed "F:filename.ext" case
|
---|
1337 | */
|
---|
1338 |
|
---|
1339 | PSZ doshGetExtension(PCSZ pcszFilename)
|
---|
1340 | {
|
---|
1341 | PSZ pReturn = NULL;
|
---|
1342 |
|
---|
1343 | if (pcszFilename)
|
---|
1344 | {
|
---|
1345 | // find filename
|
---|
1346 | PCSZ p2 = strrchr(pcszFilename + 2, '\\'),
|
---|
1347 | // works on "C:\blah" or "\\unc\blah"
|
---|
1348 | pStartOfName = NULL,
|
---|
1349 | pExtension = NULL;
|
---|
1350 |
|
---|
1351 | if (p2)
|
---|
1352 | pStartOfName = p2 + 1;
|
---|
1353 | else
|
---|
1354 | {
|
---|
1355 | // no backslash found:
|
---|
1356 | // maybe only a drive letter was specified:
|
---|
1357 | if (pcszFilename[1] == ':')
|
---|
1358 | // yes:
|
---|
1359 | pStartOfName = pcszFilename + 2;
|
---|
1360 | else
|
---|
1361 | // then this is not qualified at all...
|
---|
1362 | // use start of filename
|
---|
1363 | pStartOfName = (PSZ)pcszFilename;
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | // find last dot in filename
|
---|
1367 | pExtension = strrchr(pStartOfName, '.');
|
---|
1368 | if (pExtension)
|
---|
1369 | pReturn = (PSZ)pExtension + 1;
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | return (pReturn);
|
---|
1373 | }
|
---|
1374 |
|
---|
1375 | /*
|
---|
1376 | *@@category: Helpers\Control program helpers\File management
|
---|
1377 | */
|
---|
1378 |
|
---|
1379 | /* ******************************************************************
|
---|
1380 | *
|
---|
1381 | * File helpers
|
---|
1382 | *
|
---|
1383 | ********************************************************************/
|
---|
1384 |
|
---|
1385 | /*
|
---|
1386 | *@@ doshIsFileOnFAT:
|
---|
1387 | * returns TRUE if pszFileName resides on
|
---|
1388 | * a FAT drive. Note that pszFileName must
|
---|
1389 | * be fully qualified (i.e. the drive letter
|
---|
1390 | * must be the first character), or this will
|
---|
1391 | * return garbage.
|
---|
1392 | */
|
---|
1393 |
|
---|
1394 | BOOL doshIsFileOnFAT(const char* pcszFileName)
|
---|
1395 | {
|
---|
1396 | BOOL brc = FALSE;
|
---|
1397 | CHAR szName[5];
|
---|
1398 |
|
---|
1399 | APIRET rc = NO_ERROR; // return code
|
---|
1400 | BYTE fsqBuffer[sizeof(FSQBUFFER2) + (3 * CCHMAXPATH)] = {0};
|
---|
1401 | ULONG cbBuffer = sizeof(fsqBuffer); // Buffer length)
|
---|
1402 | PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)fsqBuffer;
|
---|
1403 |
|
---|
1404 | szName[0] = pcszFileName[0]; // copy drive letter
|
---|
1405 | szName[1] = ':';
|
---|
1406 | szName[2] = '\0';
|
---|
1407 |
|
---|
1408 | rc = DosQueryFSAttach(szName, // logical drive of attached FS
|
---|
1409 | 0, // ulOrdinal, ignored for FSAIL_QUERYNAME
|
---|
1410 | FSAIL_QUERYNAME, // return data for a Drive or Device
|
---|
1411 | pfsqBuffer, // returned data
|
---|
1412 | &cbBuffer); // returned data length
|
---|
1413 |
|
---|
1414 | if (rc == NO_ERROR)
|
---|
1415 | {
|
---|
1416 | // The data for the last three fields in the FSQBUFFER2
|
---|
1417 | // structure are stored at the offset of fsqBuffer.szName.
|
---|
1418 | // Each data field following fsqBuffer.szName begins
|
---|
1419 | // immediately after the previous item.
|
---|
1420 | if (strncmp((PSZ)&(pfsqBuffer->szName) + pfsqBuffer->cbName + 1,
|
---|
1421 | "FAT",
|
---|
1422 | 3)
|
---|
1423 | == 0)
|
---|
1424 | brc = TRUE;
|
---|
1425 | }
|
---|
1426 |
|
---|
1427 | return (brc);
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | /*
|
---|
1431 | *@@ doshQueryFileSize:
|
---|
1432 | * returns the size of an already opened file
|
---|
1433 | * or 0 upon errors.
|
---|
1434 | * Use doshQueryPathSize to query the size of
|
---|
1435 | * any file.
|
---|
1436 | *
|
---|
1437 | *@@changed V0.9.16 (2001-10-19) [umoeller]: now returning APIRET
|
---|
1438 | */
|
---|
1439 |
|
---|
1440 | APIRET doshQueryFileSize(HFILE hFile,
|
---|
1441 | PULONG pulSize)
|
---|
1442 | {
|
---|
1443 | APIRET arc;
|
---|
1444 | FILESTATUS3 fs3;
|
---|
1445 | if (!(arc = DosQueryFileInfo(hFile, FIL_STANDARD, &fs3, sizeof(fs3))))
|
---|
1446 | *pulSize = fs3.cbFile;
|
---|
1447 | return (arc);
|
---|
1448 | }
|
---|
1449 |
|
---|
1450 | /*
|
---|
1451 | *@@ doshQueryPathSize:
|
---|
1452 | * returns the size of any file,
|
---|
1453 | * or 0 if the file could not be
|
---|
1454 | * found.
|
---|
1455 | * Use doshQueryFileSize instead to query the
|
---|
1456 | * size if you have a HFILE.
|
---|
1457 | *
|
---|
1458 | *@@changed V0.9.16 (2001-10-19) [umoeller]: now returning APIRET
|
---|
1459 | */
|
---|
1460 |
|
---|
1461 | APIRET doshQueryPathSize(PCSZ pcszFile,
|
---|
1462 | PULONG pulSize)
|
---|
1463 | {
|
---|
1464 | APIRET arc;
|
---|
1465 | FILESTATUS3 fs3;
|
---|
1466 | if (!(arc = DosQueryPathInfo((PSZ)pcszFile, FIL_STANDARD, &fs3, sizeof(fs3))))
|
---|
1467 | *pulSize = fs3.cbFile;
|
---|
1468 | return (arc);
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | /*
|
---|
1472 | *@@ doshQueryPathAttr:
|
---|
1473 | * returns the file attributes of pszFile,
|
---|
1474 | * which can be fully qualified. The
|
---|
1475 | * attributes will be stored in *pulAttr.
|
---|
1476 | * pszFile can also specify a directory,
|
---|
1477 | * although not all attributes make sense
|
---|
1478 | * for directories.
|
---|
1479 | *
|
---|
1480 | * fAttr can be:
|
---|
1481 | * -- FILE_ARCHIVED
|
---|
1482 | * -- FILE_READONLY
|
---|
1483 | * -- FILE_SYSTEM
|
---|
1484 | * -- FILE_HIDDEN
|
---|
1485 | *
|
---|
1486 | * This returns the APIRET of DosQueryPathInfo.
|
---|
1487 | * *pulAttr is only valid if NO_ERROR is
|
---|
1488 | * returned.
|
---|
1489 | *
|
---|
1490 | *@@added V0.9.0 [umoeller]
|
---|
1491 | */
|
---|
1492 |
|
---|
1493 | APIRET doshQueryPathAttr(const char* pcszFile, // in: file or directory name
|
---|
1494 | PULONG pulAttr) // out: attributes
|
---|
1495 | {
|
---|
1496 | FILESTATUS3 fs3;
|
---|
1497 | APIRET arc = DosQueryPathInfo((PSZ)pcszFile,
|
---|
1498 | FIL_STANDARD,
|
---|
1499 | &fs3,
|
---|
1500 | sizeof(fs3));
|
---|
1501 | if (arc == NO_ERROR)
|
---|
1502 | {
|
---|
1503 | if (pulAttr)
|
---|
1504 | *pulAttr = fs3.attrFile;
|
---|
1505 | }
|
---|
1506 |
|
---|
1507 | return (arc);
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | /*
|
---|
1511 | *@@ doshSetPathAttr:
|
---|
1512 | * sets the file attributes of pszFile,
|
---|
1513 | * which can be fully qualified.
|
---|
1514 | * pszFile can also specify a directory,
|
---|
1515 | * although not all attributes make sense
|
---|
1516 | * for directories.
|
---|
1517 | *
|
---|
1518 | * fAttr can be:
|
---|
1519 | * -- FILE_ARCHIVED
|
---|
1520 | * -- FILE_READONLY
|
---|
1521 | * -- FILE_SYSTEM
|
---|
1522 | * -- FILE_HIDDEN
|
---|
1523 | *
|
---|
1524 | * Note that this simply sets all the given
|
---|
1525 | * attributes; the existing attributes
|
---|
1526 | * are lost.
|
---|
1527 | *
|
---|
1528 | * This returns the APIRET of DosQueryPathInfo.
|
---|
1529 | */
|
---|
1530 |
|
---|
1531 | APIRET doshSetPathAttr(const char* pcszFile, // in: file or directory name
|
---|
1532 | ULONG ulAttr) // in: new attributes
|
---|
1533 | {
|
---|
1534 | FILESTATUS3 fs3;
|
---|
1535 | APIRET rc = DosQueryPathInfo((PSZ)pcszFile,
|
---|
1536 | FIL_STANDARD,
|
---|
1537 | &fs3,
|
---|
1538 | sizeof(fs3));
|
---|
1539 |
|
---|
1540 | if (rc == NO_ERROR)
|
---|
1541 | {
|
---|
1542 | fs3.attrFile = ulAttr;
|
---|
1543 | rc = DosSetPathInfo((PSZ)pcszFile,
|
---|
1544 | FIL_STANDARD,
|
---|
1545 | &fs3,
|
---|
1546 | sizeof(fs3),
|
---|
1547 | DSPI_WRTTHRU);
|
---|
1548 | }
|
---|
1549 | return (rc);
|
---|
1550 | }
|
---|
1551 |
|
---|
1552 | /*
|
---|
1553 | *@@ doshOpenExisting:
|
---|
1554 | * opens an existing file for read-write access. Does
|
---|
1555 | * not create a new file if the file doesn't exist.
|
---|
1556 | *
|
---|
1557 | * This is just a simple wrapper around DosOpen.
|
---|
1558 | *
|
---|
1559 | * ulOpenFlags is passed to DosOpen. Should be one
|
---|
1560 | * of:
|
---|
1561 | *
|
---|
1562 | * -- for read-only access:
|
---|
1563 | *
|
---|
1564 | + OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY
|
---|
1565 | *
|
---|
1566 | * -- for read-write access:
|
---|
1567 | *
|
---|
1568 | + OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE
|
---|
1569 | *
|
---|
1570 | * In addition, you can specify
|
---|
1571 | *
|
---|
1572 | + OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_RANDOM
|
---|
1573 | + | OPEN_FLAGS_NOINHERIT
|
---|
1574 | *
|
---|
1575 | *@@added V0.9.13 (2001-06-14) [umoeller]
|
---|
1576 | */
|
---|
1577 |
|
---|
1578 | APIRET doshOpenExisting(PCSZ pcszFilename, // in: file name
|
---|
1579 | ULONG ulOpenFlags, // in: open flags
|
---|
1580 | HFILE *phf) // out: OS/2 file handle
|
---|
1581 | {
|
---|
1582 | ULONG ulAction;
|
---|
1583 | return (DosOpen((PSZ)pcszFilename,
|
---|
1584 | phf,
|
---|
1585 | &ulAction,
|
---|
1586 | 0, // cbFile
|
---|
1587 | 0, // attributes
|
---|
1588 | OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
|
---|
1589 | ulOpenFlags,
|
---|
1590 | NULL)); // EAs
|
---|
1591 | }
|
---|
1592 |
|
---|
1593 | /*
|
---|
1594 | *@@ doshWriteAt:
|
---|
1595 | * writes cb bytes (pointed to by pbData) to the
|
---|
1596 | * position specified by ulMethod and lOffset into
|
---|
1597 | * the file specified by hf.
|
---|
1598 | *
|
---|
1599 | * If ulMethod is FILE_BEGIN, lOffset specifies the
|
---|
1600 | * offset from the beginning of the file. With
|
---|
1601 | * FILE_CURRENT, lOffset is considered from the
|
---|
1602 | * current file pointer, and with FILE_END, it is
|
---|
1603 | * considered from the end of the file.
|
---|
1604 | *
|
---|
1605 | *@@added V0.9.13 (2001-06-14) [umoeller]
|
---|
1606 | */
|
---|
1607 |
|
---|
1608 | APIRET doshWriteAt(HFILE hf, // in: OS/2 file handle
|
---|
1609 | LONG lOffset, // in: offset to write at (depends on ulMethod)
|
---|
1610 | ULONG ulMethod, // in: one of FILE_BEGIN, FILE_CURRENT, FILE_END
|
---|
1611 | ULONG cb, // in: bytes to write
|
---|
1612 | PBYTE pbData) // in: ptr to bytes to write (must be cb bytes)
|
---|
1613 | {
|
---|
1614 | APIRET arc;
|
---|
1615 | ULONG ulDummy;
|
---|
1616 | if (!(arc = DosSetFilePtr(hf,
|
---|
1617 | lOffset,
|
---|
1618 | ulMethod,
|
---|
1619 | &ulDummy)))
|
---|
1620 | arc = DosWrite(hf,
|
---|
1621 | pbData,
|
---|
1622 | cb,
|
---|
1623 | &ulDummy);
|
---|
1624 |
|
---|
1625 | return (arc);
|
---|
1626 | }
|
---|
1627 |
|
---|
1628 | /*
|
---|
1629 | *@@ doshReadAt:
|
---|
1630 | * reads cb bytes from the position specified by
|
---|
1631 | * ulMethod and lOffset into the buffer pointed to
|
---|
1632 | * by pbData, which should be cb bytes in size.
|
---|
1633 | *
|
---|
1634 | * Use lOffset and ulMethod as with doshWriteAt.
|
---|
1635 | *
|
---|
1636 | *@@added V0.9.13 (2001-06-14) [umoeller]
|
---|
1637 | */
|
---|
1638 |
|
---|
1639 | APIRET doshReadAt(HFILE hf, // in: OS/2 file handle
|
---|
1640 | LONG lOffset, // in: offset to write at (depends on ulMethod)
|
---|
1641 | ULONG ulMethod, // in: one of FILE_BEGIN, FILE_CURRENT, FILE_END
|
---|
1642 | ULONG cb, // in: bytes to write
|
---|
1643 | PBYTE pbData) // out: read buffer (must be cb bytes)
|
---|
1644 | {
|
---|
1645 | APIRET arc;
|
---|
1646 | ULONG ulDummy;
|
---|
1647 | if (!(arc = DosSetFilePtr(hf,
|
---|
1648 | lOffset,
|
---|
1649 | ulMethod,
|
---|
1650 | &ulDummy)))
|
---|
1651 | arc = DosRead(hf,
|
---|
1652 | pbData,
|
---|
1653 | cb,
|
---|
1654 | &ulDummy);
|
---|
1655 |
|
---|
1656 | return (arc);
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | /*
|
---|
1660 | * doshOpen:
|
---|
1661 | * wrapper around DosOpen for simpler opening
|
---|
1662 | * of files.
|
---|
1663 | *
|
---|
1664 | * ulOpenMode determines the mode to open the
|
---|
1665 | * file in:
|
---|
1666 | *
|
---|
1667 | + +-------------------------+------+-----------+-----------+
|
---|
1668 | + | | | | |
|
---|
1669 | + | ulOpenMode | mode | if exists | if new |
|
---|
1670 | + +-------------------------+------+-----------+-----------+
|
---|
1671 | + | XOPEN_READ_EXISTING | read | opens | fails |
|
---|
1672 | + +-------------------------+------+-----------+-----------+
|
---|
1673 | + | XOPEN_READWRITE_APPEND | r/w | opens, | creates |
|
---|
1674 | + | | | appends | |
|
---|
1675 | + +-------------------------+------+-----------+-----------+
|
---|
1676 | + | XOPEN_READWRITE_NEW | r/w | replaces | creates |
|
---|
1677 | + +-------------------------+------+-----------+-----------+
|
---|
1678 | *
|
---|
1679 | * *ppFile receives a new XFILE structure describing
|
---|
1680 | * the open file, if NO_ERROR is returned.
|
---|
1681 | *
|
---|
1682 | *@@added V0.9.16 (2001-10-19) [umoeller]
|
---|
1683 | */
|
---|
1684 |
|
---|
1685 | APIRET doshOpen(PCSZ pcszFilename, // in: filename to open
|
---|
1686 | ULONG ulOpenMode, // in: XOPEN_* mode
|
---|
1687 | PULONG pcbFile, // in: new file size (if new file is created)
|
---|
1688 | // out: file size
|
---|
1689 | PXFILE *ppFile)
|
---|
1690 | {
|
---|
1691 | APIRET arc = NO_ERROR;
|
---|
1692 |
|
---|
1693 | ULONG fsOpenFlags = 0,
|
---|
1694 | fsOpenMode = OPEN_FLAGS_FAIL_ON_ERROR
|
---|
1695 | | OPEN_FLAGS_NO_LOCALITY
|
---|
1696 | | OPEN_FLAGS_NOINHERIT;
|
---|
1697 |
|
---|
1698 | switch (ulOpenMode)
|
---|
1699 | {
|
---|
1700 | case XOPEN_READ_EXISTING:
|
---|
1701 | fsOpenFlags = OPEN_ACTION_FAIL_IF_NEW
|
---|
1702 | | OPEN_ACTION_OPEN_IF_EXISTS;
|
---|
1703 | fsOpenMode |= OPEN_SHARE_DENYWRITE
|
---|
1704 | | OPEN_ACCESS_READONLY;
|
---|
1705 | break;
|
---|
1706 |
|
---|
1707 | case XOPEN_READWRITE_APPEND:
|
---|
1708 | fsOpenFlags = OPEN_ACTION_CREATE_IF_NEW
|
---|
1709 | | OPEN_ACTION_OPEN_IF_EXISTS;
|
---|
1710 | fsOpenMode |= OPEN_SHARE_DENYREADWRITE
|
---|
1711 | | OPEN_ACCESS_READWRITE;
|
---|
1712 | break;
|
---|
1713 |
|
---|
1714 | case XOPEN_READWRITE_NEW:
|
---|
1715 | fsOpenFlags = OPEN_ACTION_CREATE_IF_NEW
|
---|
1716 | | OPEN_ACTION_REPLACE_IF_EXISTS;
|
---|
1717 | fsOpenMode |= OPEN_SHARE_DENYREADWRITE
|
---|
1718 | | OPEN_ACCESS_READWRITE;
|
---|
1719 | break;
|
---|
1720 | }
|
---|
1721 |
|
---|
1722 | if (pcszFilename && fsOpenFlags && pcbFile && ppFile)
|
---|
1723 | {
|
---|
1724 | PXFILE pFile;
|
---|
1725 | if (pFile = NEW(XFILE))
|
---|
1726 | {
|
---|
1727 | ULONG ulAction;
|
---|
1728 |
|
---|
1729 | ZERO(pFile);
|
---|
1730 |
|
---|
1731 | if (!(arc = DosOpen((PSZ)pcszFilename,
|
---|
1732 | &pFile->hf,
|
---|
1733 | &ulAction,
|
---|
1734 | *pcbFile,
|
---|
1735 | FILE_ARCHIVED,
|
---|
1736 | fsOpenFlags,
|
---|
1737 | fsOpenMode,
|
---|
1738 | NULL))) // EAs
|
---|
1739 | {
|
---|
1740 | // alright, got the file:
|
---|
1741 |
|
---|
1742 | if ( (ulAction == FILE_EXISTED)
|
---|
1743 | && (ulOpenMode == XOPEN_READWRITE_APPEND)
|
---|
1744 | )
|
---|
1745 | // get its size and set ptr to end for append
|
---|
1746 | arc = DosSetFilePtr(pFile->hf,
|
---|
1747 | 0,
|
---|
1748 | FILE_END,
|
---|
1749 | pcbFile);
|
---|
1750 | else
|
---|
1751 | arc = doshQueryFileSize(pFile->hf,
|
---|
1752 | pcbFile);
|
---|
1753 | // file ptr is at beginning
|
---|
1754 |
|
---|
1755 | // store file size
|
---|
1756 | pFile->cbInitial
|
---|
1757 | = pFile->cbCurrent
|
---|
1758 | = *pcbFile;
|
---|
1759 | }
|
---|
1760 |
|
---|
1761 | if (arc)
|
---|
1762 | doshClose(&pFile);
|
---|
1763 | else
|
---|
1764 | *ppFile = pFile;
|
---|
1765 | }
|
---|
1766 | else
|
---|
1767 | arc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
1768 | }
|
---|
1769 | else
|
---|
1770 | arc = ERROR_INVALID_PARAMETER;
|
---|
1771 |
|
---|
1772 | return (arc);
|
---|
1773 | }
|
---|
1774 |
|
---|
1775 | /*
|
---|
1776 | *@@ doshLockFile:
|
---|
1777 | *
|
---|
1778 | *@@added V0.9.16 (2001-10-19) [umoeller]
|
---|
1779 | */
|
---|
1780 |
|
---|
1781 | APIRET doshLockFile(PXFILE pFile)
|
---|
1782 | {
|
---|
1783 | if (!pFile)
|
---|
1784 | return (ERROR_INVALID_PARAMETER);
|
---|
1785 |
|
---|
1786 | if (!pFile->hmtx)
|
---|
1787 | // first call:
|
---|
1788 | return (DosCreateMutexSem(NULL,
|
---|
1789 | &pFile->hmtx,
|
---|
1790 | 0,
|
---|
1791 | TRUE)); // request!
|
---|
1792 |
|
---|
1793 | return (DosRequestMutexSem(pFile->hmtx, SEM_INDEFINITE_WAIT));
|
---|
1794 | }
|
---|
1795 |
|
---|
1796 | /*
|
---|
1797 | *@@ doshUnlockFile:
|
---|
1798 | *
|
---|
1799 | *@@added V0.9.16 (2001-10-19) [umoeller]
|
---|
1800 | */
|
---|
1801 |
|
---|
1802 | APIRET doshUnlockFile(PXFILE pFile)
|
---|
1803 | {
|
---|
1804 | if (pFile)
|
---|
1805 | return (DosReleaseMutexSem(pFile->hmtx));
|
---|
1806 |
|
---|
1807 | return (ERROR_INVALID_PARAMETER);
|
---|
1808 | }
|
---|
1809 |
|
---|
1810 | /*
|
---|
1811 | *@@ doshWrite:
|
---|
1812 | * writes the specified data to the file.
|
---|
1813 | * If (cb == 0), this runs strlen on pcsz
|
---|
1814 | * to find out the length.
|
---|
1815 | *
|
---|
1816 | *@@added V0.9.16 (2001-10-19) [umoeller]
|
---|
1817 | */
|
---|
1818 |
|
---|
1819 | APIRET doshWrite(PXFILE pFile,
|
---|
1820 | PCSZ pcsz,
|
---|
1821 | ULONG cb)
|
---|
1822 | {
|
---|
1823 | APIRET arc;
|
---|
1824 | if (!pcsz)
|
---|
1825 | arc = ERROR_INVALID_PARAMETER;
|
---|
1826 | else
|
---|
1827 | {
|
---|
1828 | if (!cb)
|
---|
1829 | cb = strlen(pcsz);
|
---|
1830 |
|
---|
1831 | if (!cb)
|
---|
1832 | arc = ERROR_INVALID_PARAMETER;
|
---|
1833 | else
|
---|
1834 | if (!(arc = doshLockFile(pFile))) // this checks for pFile
|
---|
1835 | {
|
---|
1836 | ULONG cbWritten;
|
---|
1837 | if (!(arc = DosWrite(pFile->hf,
|
---|
1838 | (PSZ)pcsz,
|
---|
1839 | cb,
|
---|
1840 | &cbWritten)))
|
---|
1841 | pFile->cbCurrent += cbWritten;
|
---|
1842 |
|
---|
1843 | doshUnlockFile(pFile);
|
---|
1844 | }
|
---|
1845 | }
|
---|
1846 |
|
---|
1847 | return (arc);
|
---|
1848 | }
|
---|
1849 |
|
---|
1850 | /*
|
---|
1851 | *@@ doshWriteLogEntry
|
---|
1852 | * writes a log string to an XFILE, adding a
|
---|
1853 | * leading timestamp before the line.
|
---|
1854 | *
|
---|
1855 | * The internal string buffer is limited to 2000
|
---|
1856 | * characters. \n is NOT translated to \r\n before
|
---|
1857 | * writing.
|
---|
1858 | */
|
---|
1859 |
|
---|
1860 | APIRET doshWriteLogEntry(PXFILE pFile,
|
---|
1861 | const char* pcszFormat,
|
---|
1862 | ...)
|
---|
1863 | {
|
---|
1864 | APIRET arc;
|
---|
1865 |
|
---|
1866 | DATETIME dt;
|
---|
1867 | CHAR szTemp[2000];
|
---|
1868 | ULONG ulLength;
|
---|
1869 |
|
---|
1870 | DosGetDateTime(&dt);
|
---|
1871 | if (ulLength = sprintf(szTemp,
|
---|
1872 | "%04d-%02d-%02d %02d:%02d:%02d:%02d ",
|
---|
1873 | dt.year, dt.month, dt.day,
|
---|
1874 | dt.hours, dt.minutes, dt.seconds, dt.hundredths))
|
---|
1875 | {
|
---|
1876 | if (!(arc = doshWrite(pFile,
|
---|
1877 | szTemp,
|
---|
1878 | ulLength)))
|
---|
1879 | {
|
---|
1880 | va_list arg_ptr;
|
---|
1881 | va_start(arg_ptr, pcszFormat);
|
---|
1882 | ulLength = vsprintf(szTemp, pcszFormat, arg_ptr);
|
---|
1883 | va_end(arg_ptr);
|
---|
1884 |
|
---|
1885 | szTemp[ulLength++] = '\r';
|
---|
1886 | szTemp[ulLength++] = '\n';
|
---|
1887 |
|
---|
1888 | arc = doshWrite(pFile,
|
---|
1889 | (PCSZ)szTemp,
|
---|
1890 | ulLength);
|
---|
1891 | }
|
---|
1892 | }
|
---|
1893 |
|
---|
1894 | return (arc);
|
---|
1895 | }
|
---|
1896 |
|
---|
1897 | /*
|
---|
1898 | * doshClose:
|
---|
1899 | *
|
---|
1900 | *@@added V0.9.16 (2001-10-19) [umoeller]
|
---|
1901 | */
|
---|
1902 |
|
---|
1903 | APIRET doshClose(PXFILE *ppFile)
|
---|
1904 | {
|
---|
1905 | APIRET arc = NO_ERROR;
|
---|
1906 | PXFILE pFile;
|
---|
1907 |
|
---|
1908 | if ( (ppFile)
|
---|
1909 | && (pFile = *ppFile)
|
---|
1910 | )
|
---|
1911 | {
|
---|
1912 | // request the mutex so that we won't be
|
---|
1913 | // taking the file away under someone's butt
|
---|
1914 | if (!(arc = doshLockFile(pFile)))
|
---|
1915 | {
|
---|
1916 | HMTX hmtx = pFile->hmtx;
|
---|
1917 | pFile->hmtx = NULLHANDLE;
|
---|
1918 |
|
---|
1919 | // now that the file is locked,
|
---|
1920 | // set the ptr to NULL
|
---|
1921 | *ppFile = NULL;
|
---|
1922 |
|
---|
1923 | if (pFile->hf)
|
---|
1924 | {
|
---|
1925 | DosSetFileSize(pFile->hf, pFile->cbCurrent);
|
---|
1926 | DosClose(pFile->hf);
|
---|
1927 | pFile->hf = NULLHANDLE;
|
---|
1928 | }
|
---|
1929 |
|
---|
1930 | doshUnlockFile(pFile);
|
---|
1931 | DosCloseMutexSem(pFile->hmtx);
|
---|
1932 | }
|
---|
1933 |
|
---|
1934 | free(pFile);
|
---|
1935 | }
|
---|
1936 | else
|
---|
1937 | arc = ERROR_INVALID_PARAMETER;
|
---|
1938 |
|
---|
1939 | return (arc);
|
---|
1940 | }
|
---|
1941 |
|
---|
1942 | /*
|
---|
1943 | *@@ doshLoadTextFile:
|
---|
1944 | * reads a text file from disk, allocates memory
|
---|
1945 | * via malloc() and sets a pointer to this
|
---|
1946 | * buffer (or NULL upon errors).
|
---|
1947 | *
|
---|
1948 | * This returns the APIRET of DosOpen and DosRead.
|
---|
1949 | * If any error occured, no buffer was allocated.
|
---|
1950 | * Otherwise, you should free() the buffer when
|
---|
1951 | * no longer needed.
|
---|
1952 | *
|
---|
1953 | *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from doshReadTextFile
|
---|
1954 | */
|
---|
1955 |
|
---|
1956 | APIRET doshLoadTextFile(PCSZ pcszFile, // in: file name to read
|
---|
1957 | PSZ* ppszContent) // out: newly allocated buffer with file's content
|
---|
1958 | {
|
---|
1959 | ULONG ulSize,
|
---|
1960 | ulBytesRead = 0,
|
---|
1961 | ulAction, ulLocal;
|
---|
1962 | HFILE hFile;
|
---|
1963 | PSZ pszContent = NULL;
|
---|
1964 |
|
---|
1965 | APIRET arc;
|
---|
1966 |
|
---|
1967 | *ppszContent = 0;
|
---|
1968 |
|
---|
1969 | if (!(arc = DosOpen((PSZ)pcszFile,
|
---|
1970 | &hFile,
|
---|
1971 | &ulAction, // action taken
|
---|
1972 | 5000L, // primary allocation size
|
---|
1973 | FILE_ARCHIVED | FILE_NORMAL, // file attribute
|
---|
1974 | OPEN_ACTION_OPEN_IF_EXISTS, // open flags
|
---|
1975 | OPEN_FLAGS_NOINHERIT
|
---|
1976 | | OPEN_SHARE_DENYNONE
|
---|
1977 | | OPEN_ACCESS_READONLY, // read-only mode
|
---|
1978 | NULL))) // no EAs
|
---|
1979 | {
|
---|
1980 | if (!(arc = doshQueryFileSize(hFile, &ulSize)))
|
---|
1981 | {
|
---|
1982 | pszContent = (PSZ)malloc(ulSize+1);
|
---|
1983 |
|
---|
1984 | if (!(arc = DosSetFilePtr(hFile,
|
---|
1985 | 0L,
|
---|
1986 | FILE_BEGIN,
|
---|
1987 | &ulLocal)))
|
---|
1988 | if (!(arc = DosRead(hFile,
|
---|
1989 | pszContent,
|
---|
1990 | ulSize,
|
---|
1991 | &ulBytesRead)))
|
---|
1992 | {
|
---|
1993 | *(pszContent+ulBytesRead) = 0;
|
---|
1994 | // set output buffer pointer
|
---|
1995 | *ppszContent = pszContent;
|
---|
1996 | }
|
---|
1997 |
|
---|
1998 | if (arc)
|
---|
1999 | free(pszContent);
|
---|
2000 | }
|
---|
2001 | DosClose(hFile);
|
---|
2002 | }
|
---|
2003 |
|
---|
2004 | return (arc);
|
---|
2005 | }
|
---|
2006 |
|
---|
2007 | /*
|
---|
2008 | *@@ doshCreateBackupFileName:
|
---|
2009 | * creates a valid backup filename of pszExisting
|
---|
2010 | * with a numerical file name extension which does
|
---|
2011 | * not exist in the directory where pszExisting
|
---|
2012 | * resides.
|
---|
2013 | * Returns a PSZ to a new buffer which was allocated
|
---|
2014 | * using malloc().
|
---|
2015 | *
|
---|
2016 | * <B>Example:</B> returns "C:\CONFIG.002" for input
|
---|
2017 | * "C:\CONFIG.SYS" if "C:\CONFIG.001" already exists.
|
---|
2018 | *
|
---|
2019 | *@@changed V0.9.1 (99-12-13) [umoeller]: this crashed if pszExisting had no file extension
|
---|
2020 | */
|
---|
2021 |
|
---|
2022 | PSZ doshCreateBackupFileName(const char* pszExisting)
|
---|
2023 | {
|
---|
2024 | CHAR szFilename[CCHMAXPATH];
|
---|
2025 | PSZ pszLastDot;
|
---|
2026 | ULONG ulCount = 1;
|
---|
2027 | CHAR szCount[5];
|
---|
2028 | ULONG ulDummy;
|
---|
2029 |
|
---|
2030 | strcpy(szFilename, pszExisting);
|
---|
2031 | pszLastDot = strrchr(szFilename, '.');
|
---|
2032 | if (!pszLastDot)
|
---|
2033 | // no dot in filename:
|
---|
2034 | pszLastDot = szFilename + strlen(szFilename);
|
---|
2035 | do
|
---|
2036 | {
|
---|
2037 | sprintf(szCount, ".%03lu", ulCount);
|
---|
2038 | strcpy(pszLastDot, szCount);
|
---|
2039 | ulCount++;
|
---|
2040 | } while (!doshQueryPathSize(szFilename, &ulDummy));
|
---|
2041 |
|
---|
2042 | return (strdup(szFilename));
|
---|
2043 | }
|
---|
2044 |
|
---|
2045 | /*
|
---|
2046 | *@@ doshCreateTempFileName:
|
---|
2047 | * produces a file name in the the specified directory
|
---|
2048 | * or $(TEMP) which presently doesn't exist. This
|
---|
2049 | * checks the directory for existing files, but does
|
---|
2050 | * not open the temp file.
|
---|
2051 | *
|
---|
2052 | * If (pcszDir != NULL), we look into that directory.
|
---|
2053 | * Otherwise we look into the directory specified
|
---|
2054 | * by the $(TEMP) environment variable.
|
---|
2055 | * If $(TEMP) is not set, $(TMP) is tried next.
|
---|
2056 | *
|
---|
2057 | * If the directory thus specified does not exist, the
|
---|
2058 | * root directory of the boot drive is used instead.
|
---|
2059 | * As a result, this function should be fairly bomb-proof.
|
---|
2060 | *
|
---|
2061 | * If (pcszExt != NULL), the temp file receives
|
---|
2062 | * that extension, or no extension otherwise.
|
---|
2063 | * Do not specify the dot in pcszExt.
|
---|
2064 | *
|
---|
2065 | * pszTempFileName receives the fully qualified
|
---|
2066 | * file name of the temp file in that directory
|
---|
2067 | * and must point to a buffer CCHMAXPATH in size.
|
---|
2068 | * The file name is 8+3 compliant if pcszExt does
|
---|
2069 | * not exceed three characters.
|
---|
2070 | *
|
---|
2071 | * If (pcszPrefix != NULL), the temp file name
|
---|
2072 | * is prefixed with pcszPrefix. Since the temp
|
---|
2073 | * file name must not exceed 8+3 letters, we
|
---|
2074 | * can only use ( 8 - strlen(pcszPrefix ) digits
|
---|
2075 | * for a random number to make the temp file name
|
---|
2076 | * unique. You must therefore use a maximum of
|
---|
2077 | * four characters for the prefix. Otherwise
|
---|
2078 | * ERROR_INVALID_PARAMETER is returned.
|
---|
2079 | *
|
---|
2080 | * Example: Assuming TEMP is set to C:\TEMP,
|
---|
2081 | +
|
---|
2082 | + dosCreateTempFileName(szBuffer,
|
---|
2083 | + NULL, // use $(TEMP)
|
---|
2084 | + "pre", // prefix
|
---|
2085 | + "tmp") // extension
|
---|
2086 | +
|
---|
2087 | * would produce something like "C:\TEMP\pre07FG2.tmp".
|
---|
2088 | *
|
---|
2089 | *@@added V0.9.9 (2001-04-04) [umoeller]
|
---|
2090 | */
|
---|
2091 |
|
---|
2092 | APIRET doshCreateTempFileName(PSZ pszTempFileName, // out: fully q'fied temp file name
|
---|
2093 | PCSZ pcszDir, // in: dir or NULL for %TEMP%
|
---|
2094 | PCSZ pcszPrefix, // in: prefix for temp file or NULL
|
---|
2095 | PCSZ pcszExt) // in: extension (without dot) or NULL
|
---|
2096 | {
|
---|
2097 | APIRET arc = NO_ERROR;
|
---|
2098 |
|
---|
2099 | ULONG ulPrefixLen = (pcszPrefix)
|
---|
2100 | ? strlen(pcszPrefix)
|
---|
2101 | : 0;
|
---|
2102 |
|
---|
2103 | if ( (!pszTempFileName)
|
---|
2104 | || (ulPrefixLen > 4)
|
---|
2105 | )
|
---|
2106 | arc = ERROR_INVALID_PARAMETER;
|
---|
2107 | else
|
---|
2108 | {
|
---|
2109 | CHAR szDir[CCHMAXPATH] = "";
|
---|
2110 | FILESTATUS3 fs3;
|
---|
2111 |
|
---|
2112 | const char *pcszTemp = pcszDir;
|
---|
2113 |
|
---|
2114 | if (!pcszTemp)
|
---|
2115 | {
|
---|
2116 | pcszTemp = getenv("TEMP");
|
---|
2117 | if (!pcszTemp)
|
---|
2118 | pcszTemp = getenv("TMP");
|
---|
2119 | }
|
---|
2120 |
|
---|
2121 | if (pcszTemp) // either pcszDir or $(TEMP) or $(TMP) now
|
---|
2122 | if (DosQueryPathInfo((PSZ)pcszTemp,
|
---|
2123 | FIL_STANDARD,
|
---|
2124 | &fs3,
|
---|
2125 | sizeof(fs3)))
|
---|
2126 | // TEMP doesn't exist:
|
---|
2127 | pcszTemp = NULL;
|
---|
2128 |
|
---|
2129 | if (!pcszTemp)
|
---|
2130 | // not set, or doesn't exist:
|
---|
2131 | // use root directory on boot drive
|
---|
2132 | sprintf(szDir,
|
---|
2133 | "%c:\\",
|
---|
2134 | doshQueryBootDrive());
|
---|
2135 | else
|
---|
2136 | {
|
---|
2137 | strcpy(szDir, pcszTemp);
|
---|
2138 | if (szDir[strlen(szDir) - 1] != '\\')
|
---|
2139 | strcat(szDir, "\\");
|
---|
2140 | }
|
---|
2141 |
|
---|
2142 | if (!szDir[0])
|
---|
2143 | arc = ERROR_PATH_NOT_FOUND; // shouldn't happen
|
---|
2144 | else
|
---|
2145 | {
|
---|
2146 | ULONG ulRandom = 0;
|
---|
2147 | ULONG cAttempts = 0;
|
---|
2148 |
|
---|
2149 | // produce random number
|
---|
2150 | DosQuerySysInfo(QSV_MS_COUNT,
|
---|
2151 | QSV_MS_COUNT,
|
---|
2152 | &ulRandom,
|
---|
2153 | sizeof(ulRandom));
|
---|
2154 |
|
---|
2155 | do
|
---|
2156 | {
|
---|
2157 | CHAR szFile[20] = "",
|
---|
2158 | szFullTryThis[CCHMAXPATH];
|
---|
2159 |
|
---|
2160 | // use the lower eight hex digits of the
|
---|
2161 | // system uptime as the temp dir name
|
---|
2162 | sprintf(szFile,
|
---|
2163 | "%08lX",
|
---|
2164 | ulRandom & 0xFFFFFFFF);
|
---|
2165 |
|
---|
2166 | // if prefix is specified, overwrite the
|
---|
2167 | // first characters in the random number
|
---|
2168 | if (pcszPrefix)
|
---|
2169 | memcpy(szFile, pcszPrefix, ulPrefixLen);
|
---|
2170 |
|
---|
2171 | if (pcszExt)
|
---|
2172 | {
|
---|
2173 | szFile[8] = '.';
|
---|
2174 | strcpy(szFile + 9, pcszExt);
|
---|
2175 | }
|
---|
2176 |
|
---|
2177 | // now compose full temp file name
|
---|
2178 | strcpy(szFullTryThis, szDir);
|
---|
2179 | strcat(szFullTryThis, szFile);
|
---|
2180 | // now we have: "C:\temp\wpiXXXXX"
|
---|
2181 | if (DosQueryPathInfo(szFullTryThis,
|
---|
2182 | FIL_STANDARD,
|
---|
2183 | &fs3,
|
---|
2184 | sizeof(fs3))
|
---|
2185 | == ERROR_FILE_NOT_FOUND)
|
---|
2186 | {
|
---|
2187 | // file or dir doesn't exist:
|
---|
2188 | // cool, we're done
|
---|
2189 | strcpy(pszTempFileName, szFullTryThis);
|
---|
2190 | return (NO_ERROR);
|
---|
2191 | }
|
---|
2192 |
|
---|
2193 | // if this didn't work, raise ulRandom and try again
|
---|
2194 | ulRandom += 123;
|
---|
2195 |
|
---|
2196 | // try only 100 times, just to be sure
|
---|
2197 | cAttempts++;
|
---|
2198 | } while (cAttempts < 100);
|
---|
2199 |
|
---|
2200 | // 100 loops elapsed:
|
---|
2201 | arc = ERROR_BAD_FORMAT;
|
---|
2202 | }
|
---|
2203 | }
|
---|
2204 |
|
---|
2205 | return (arc);
|
---|
2206 | }
|
---|
2207 |
|
---|
2208 | /*
|
---|
2209 | *@@ doshWriteTextFile:
|
---|
2210 | * writes a text file to disk; pszFile must contain the
|
---|
2211 | * whole path and filename.
|
---|
2212 | *
|
---|
2213 | * An existing file will be backed up if (pszBackup != NULL),
|
---|
2214 | * using doshCreateBackupFileName. In that case, pszBackup
|
---|
2215 | * receives the name of the backup created, so that buffer
|
---|
2216 | * should be CCHMAXPATH in size.
|
---|
2217 | *
|
---|
2218 | * If (pszbackup == NULL), an existing file will be overwritten.
|
---|
2219 | *
|
---|
2220 | * On success (NO_ERROR returned), *pulWritten receives
|
---|
2221 | * the no. of bytes written.
|
---|
2222 | *
|
---|
2223 | *@@changed V0.9.3 (2000-05-01) [umoeller]: optimized DosOpen; added error checking; changed prototype
|
---|
2224 | *@@changed V0.9.3 (2000-05-12) [umoeller]: added pszBackup
|
---|
2225 | */
|
---|
2226 |
|
---|
2227 | APIRET doshWriteTextFile(const char* pszFile, // in: file name
|
---|
2228 | const char* pszContent, // in: text to write
|
---|
2229 | PULONG pulWritten, // out: bytes written (ptr can be NULL)
|
---|
2230 | PSZ pszBackup) // in/out: create-backup?
|
---|
2231 | {
|
---|
2232 | APIRET arc = NO_ERROR;
|
---|
2233 | ULONG ulWritten = 0;
|
---|
2234 |
|
---|
2235 | if ((!pszFile) || (!pszContent))
|
---|
2236 | arc = ERROR_INVALID_PARAMETER;
|
---|
2237 | else
|
---|
2238 | {
|
---|
2239 | ULONG ulAction = 0,
|
---|
2240 | ulLocal = 0;
|
---|
2241 | HFILE hFile = 0;
|
---|
2242 |
|
---|
2243 | ULONG ulSize = strlen(pszContent); // exclude 0 byte
|
---|
2244 |
|
---|
2245 | if (pszBackup)
|
---|
2246 | {
|
---|
2247 | PSZ pszBackup2 = doshCreateBackupFileName(pszFile);
|
---|
2248 | DosCopy((PSZ)pszFile,
|
---|
2249 | pszBackup2,
|
---|
2250 | DCPY_EXISTING); // delete existing
|
---|
2251 | strcpy(pszBackup, pszBackup2);
|
---|
2252 | free(pszBackup2);
|
---|
2253 | }
|
---|
2254 |
|
---|
2255 | if (!(arc = DosOpen((PSZ)pszFile,
|
---|
2256 | &hFile,
|
---|
2257 | &ulAction, // action taken
|
---|
2258 | ulSize, // primary allocation size
|
---|
2259 | FILE_ARCHIVED | FILE_NORMAL, // file attribute
|
---|
2260 | OPEN_ACTION_CREATE_IF_NEW
|
---|
2261 | | OPEN_ACTION_REPLACE_IF_EXISTS, // open flags
|
---|
2262 | OPEN_FLAGS_NOINHERIT
|
---|
2263 | | OPEN_FLAGS_SEQUENTIAL // sequential, not random access
|
---|
2264 | | OPEN_SHARE_DENYWRITE // deny write mode
|
---|
2265 | | OPEN_ACCESS_WRITEONLY, // write mode
|
---|
2266 | NULL))) // no EAs
|
---|
2267 | {
|
---|
2268 | if (!(arc = DosSetFilePtr(hFile,
|
---|
2269 | 0L,
|
---|
2270 | FILE_BEGIN,
|
---|
2271 | &ulLocal)))
|
---|
2272 | if (!(arc = DosWrite(hFile,
|
---|
2273 | (PVOID)pszContent,
|
---|
2274 | ulSize,
|
---|
2275 | &ulWritten)))
|
---|
2276 | arc = DosSetFileSize(hFile, ulSize);
|
---|
2277 |
|
---|
2278 | DosClose(hFile);
|
---|
2279 | }
|
---|
2280 | } // end if ((pszFile) && (pszContent))
|
---|
2281 |
|
---|
2282 | if (pulWritten)
|
---|
2283 | *pulWritten = ulWritten;
|
---|
2284 |
|
---|
2285 | return (arc);
|
---|
2286 | }
|
---|
2287 |
|
---|
2288 | /*
|
---|
2289 | *@@category: Helpers\Control program helpers\Directory management
|
---|
2290 | * directory helpers (querying, creating, deleting etc.).
|
---|
2291 | */
|
---|
2292 |
|
---|
2293 | /* ******************************************************************
|
---|
2294 | *
|
---|
2295 | * Directory helpers
|
---|
2296 | *
|
---|
2297 | ********************************************************************/
|
---|
2298 |
|
---|
2299 | /*
|
---|
2300 | *@@ doshQueryDirExist:
|
---|
2301 | * returns TRUE if the given directory
|
---|
2302 | * exists.
|
---|
2303 | */
|
---|
2304 |
|
---|
2305 | BOOL doshQueryDirExist(PCSZ pcszDir)
|
---|
2306 | {
|
---|
2307 | FILESTATUS3 fs3;
|
---|
2308 | APIRET arc = DosQueryPathInfo((PSZ)pcszDir,
|
---|
2309 | FIL_STANDARD,
|
---|
2310 | &fs3,
|
---|
2311 | sizeof(fs3));
|
---|
2312 | if (arc == NO_ERROR)
|
---|
2313 | // file found:
|
---|
2314 | return ((fs3.attrFile & FILE_DIRECTORY) != 0);
|
---|
2315 | else
|
---|
2316 | return FALSE;
|
---|
2317 | }
|
---|
2318 |
|
---|
2319 | /*
|
---|
2320 | *@@ doshCreatePath:
|
---|
2321 | * this creates the specified directory.
|
---|
2322 | * As opposed to DosCreateDir, this
|
---|
2323 | * function can create several directories
|
---|
2324 | * at the same time, if the parent
|
---|
2325 | * directories do not exist yet.
|
---|
2326 | */
|
---|
2327 |
|
---|
2328 | APIRET doshCreatePath(PCSZ pcszPath,
|
---|
2329 | BOOL fHidden) // in: if TRUE, the new directories will get FILE_HIDDEN
|
---|
2330 | {
|
---|
2331 | APIRET arc0 = NO_ERROR;
|
---|
2332 | CHAR path[CCHMAXPATH];
|
---|
2333 | CHAR *cp, c;
|
---|
2334 | ULONG cbPath;
|
---|
2335 |
|
---|
2336 | strcpy(path, pcszPath);
|
---|
2337 | cbPath = strlen(path);
|
---|
2338 |
|
---|
2339 | if (path[cbPath] != '\\')
|
---|
2340 | {
|
---|
2341 | path[cbPath] = '\\';
|
---|
2342 | path[cbPath+1] = 0;
|
---|
2343 | }
|
---|
2344 |
|
---|
2345 | cp = path;
|
---|
2346 | // advance past the drive letter only if we have one
|
---|
2347 | if (*(cp+1) == ':')
|
---|
2348 | cp += 3;
|
---|
2349 |
|
---|
2350 | // go!
|
---|
2351 | while (*cp != 0)
|
---|
2352 | {
|
---|
2353 | if (*cp == '\\')
|
---|
2354 | {
|
---|
2355 | c = *cp;
|
---|
2356 | *cp = 0;
|
---|
2357 | if (!doshQueryDirExist(path))
|
---|
2358 | {
|
---|
2359 | APIRET arc = DosCreateDir(path,
|
---|
2360 | 0); // no EAs
|
---|
2361 | if (arc != NO_ERROR)
|
---|
2362 | {
|
---|
2363 | arc0 = arc;
|
---|
2364 | break;
|
---|
2365 | }
|
---|
2366 | else
|
---|
2367 | if (fHidden)
|
---|
2368 | {
|
---|
2369 | // hide the directory we just created
|
---|
2370 | doshSetPathAttr(path, FILE_HIDDEN);
|
---|
2371 | }
|
---|
2372 | }
|
---|
2373 | *cp = c;
|
---|
2374 | }
|
---|
2375 | cp++;
|
---|
2376 | }
|
---|
2377 | return (arc0);
|
---|
2378 | }
|
---|
2379 |
|
---|
2380 | /*
|
---|
2381 | *@@ doshQueryCurrentDir:
|
---|
2382 | * writes the current directory into
|
---|
2383 | * the specified buffer, which should be
|
---|
2384 | * CCHMAXPATH in size.
|
---|
2385 | *
|
---|
2386 | * As opposed to DosQueryCurrentDir, this
|
---|
2387 | * includes the drive letter.
|
---|
2388 | *
|
---|
2389 | *@@added V0.9.4 (2000-07-22) [umoeller]
|
---|
2390 | */
|
---|
2391 |
|
---|
2392 | APIRET doshQueryCurrentDir(PSZ pszBuf)
|
---|
2393 | {
|
---|
2394 | APIRET arc = NO_ERROR;
|
---|
2395 | ULONG ulCurDisk = 0;
|
---|
2396 | ULONG ulMap = 0;
|
---|
2397 | if (!(arc = DosQueryCurrentDisk(&ulCurDisk, &ulMap)))
|
---|
2398 | {
|
---|
2399 | ULONG cbBuf = CCHMAXPATH - 3;
|
---|
2400 | *pszBuf = ulCurDisk + 'A' - 1;
|
---|
2401 | *(pszBuf + 1) = ':';
|
---|
2402 | *(pszBuf + 2) = '\\';
|
---|
2403 | arc = DosQueryCurrentDir(0, pszBuf + 3, &cbBuf);
|
---|
2404 | }
|
---|
2405 |
|
---|
2406 | return (arc);
|
---|
2407 | }
|
---|
2408 |
|
---|
2409 | /*
|
---|
2410 | *@@ doshDeleteDir:
|
---|
2411 | * deletes a directory. As opposed to DosDeleteDir,
|
---|
2412 | * this removes empty subdirectories and/or files
|
---|
2413 | * as well.
|
---|
2414 | *
|
---|
2415 | * flFlags can be any combination of the following:
|
---|
2416 | *
|
---|
2417 | * -- DOSHDELDIR_RECURSE: recurse into subdirectories.
|
---|
2418 | *
|
---|
2419 | * -- DOSHDELDIR_DELETEFILES: delete all regular files
|
---|
2420 | * which are found on the way.
|
---|
2421 | *
|
---|
2422 | * THIS IS NOT IMPLEMENTED YET.
|
---|
2423 | *
|
---|
2424 | * If 0 is specified, this effectively behaves just as
|
---|
2425 | * DosDeleteDir.
|
---|
2426 | *
|
---|
2427 | * If you specify DOSHDELDIR_RECURSE only, only empty
|
---|
2428 | * subdirectories are deleted as well.
|
---|
2429 | *
|
---|
2430 | * If you specify DOSHDELDIR_RECURSE | DOSHDELDIR_DELETEFILES,
|
---|
2431 | * this removes an entire directory tree, including all
|
---|
2432 | * subdirectories and files.
|
---|
2433 | *
|
---|
2434 | *@@added V0.9.4 (2000-07-01) [umoeller]
|
---|
2435 | */
|
---|
2436 |
|
---|
2437 | APIRET doshDeleteDir(PCSZ pcszDir,
|
---|
2438 | ULONG flFlags,
|
---|
2439 | PULONG pulDirs, // out: directories found
|
---|
2440 | PULONG pulFiles) // out: files found
|
---|
2441 | {
|
---|
2442 | APIRET arc = NO_ERROR,
|
---|
2443 | arcReturn = NO_ERROR;
|
---|
2444 |
|
---|
2445 | HDIR hdirFindHandle = HDIR_CREATE;
|
---|
2446 | FILEFINDBUF3 ffb3 = {0}; // returned from FindFirst/Next
|
---|
2447 | ULONG ulResultBufLen = sizeof(FILEFINDBUF3);
|
---|
2448 | ULONG ulFindCount = 1; // look for 1 file at a time
|
---|
2449 |
|
---|
2450 | CHAR szFileMask[2*CCHMAXPATH];
|
---|
2451 | sprintf(szFileMask, "%s\\*", pcszDir);
|
---|
2452 |
|
---|
2453 | // find files
|
---|
2454 | arc = DosFindFirst(szFileMask,
|
---|
2455 | &hdirFindHandle, // directory search handle
|
---|
2456 | FILE_ARCHIVED | FILE_DIRECTORY | FILE_SYSTEM
|
---|
2457 | | FILE_HIDDEN | FILE_READONLY,
|
---|
2458 | // search attributes; include all, even dirs
|
---|
2459 | &ffb3, // result buffer
|
---|
2460 | ulResultBufLen, // result buffer length
|
---|
2461 | &ulFindCount, // number of entries to find
|
---|
2462 | FIL_STANDARD); // return level 1 file info
|
---|
2463 |
|
---|
2464 | if (arc == NO_ERROR)
|
---|
2465 | {
|
---|
2466 | // keep finding the next file until there are no more files
|
---|
2467 | while (arc == NO_ERROR) // != ERROR_NO_MORE_FILES
|
---|
2468 | {
|
---|
2469 | if (ffb3.attrFile & FILE_DIRECTORY)
|
---|
2470 | {
|
---|
2471 | // we found a directory:
|
---|
2472 |
|
---|
2473 | // ignore the pseudo-directories
|
---|
2474 | if ( (strcmp(ffb3.achName, ".") != 0)
|
---|
2475 | && (strcmp(ffb3.achName, "..") != 0)
|
---|
2476 | )
|
---|
2477 | {
|
---|
2478 | // real directory:
|
---|
2479 | if (flFlags & DOSHDELDIR_RECURSE)
|
---|
2480 | {
|
---|
2481 | // recurse!
|
---|
2482 | CHAR szSubDir[2*CCHMAXPATH];
|
---|
2483 | sprintf(szSubDir, "%s\\%s", pcszDir, ffb3.achName);
|
---|
2484 | arcReturn = doshDeleteDir(szSubDir,
|
---|
2485 | flFlags,
|
---|
2486 | pulDirs,
|
---|
2487 | pulFiles);
|
---|
2488 | // this removes ffb3.achName as well
|
---|
2489 | }
|
---|
2490 | else
|
---|
2491 | {
|
---|
2492 | // directory, but no recursion:
|
---|
2493 | // report "access denied"
|
---|
2494 | // (this is what OS/2 reports with DosDeleteDir as well)
|
---|
2495 | arcReturn = ERROR_ACCESS_DENIED; // 5
|
---|
2496 | (*pulDirs)++;
|
---|
2497 | }
|
---|
2498 | }
|
---|
2499 | }
|
---|
2500 | else
|
---|
2501 | {
|
---|
2502 | // it's a file:
|
---|
2503 | arcReturn = ERROR_ACCESS_DENIED;
|
---|
2504 | (*pulFiles)++;
|
---|
2505 | }
|
---|
2506 |
|
---|
2507 | if (arc == NO_ERROR)
|
---|
2508 | {
|
---|
2509 | ulFindCount = 1; // reset find count
|
---|
2510 | arc = DosFindNext(hdirFindHandle, // directory handle
|
---|
2511 | &ffb3, // result buffer
|
---|
2512 | ulResultBufLen, // result buffer length
|
---|
2513 | &ulFindCount); // number of entries to find
|
---|
2514 | }
|
---|
2515 | } // endwhile
|
---|
2516 |
|
---|
2517 | DosFindClose(hdirFindHandle); // close our find handle
|
---|
2518 | }
|
---|
2519 |
|
---|
2520 | if (arcReturn == NO_ERROR)
|
---|
2521 | // success so far:
|
---|
2522 | // delete our directory now
|
---|
2523 | arc = DosDeleteDir((PSZ)pcszDir);
|
---|
2524 |
|
---|
2525 | return (arcReturn);
|
---|
2526 | }
|
---|
2527 |
|
---|
2528 | /*
|
---|
2529 | *@@category: Helpers\Control program helpers\Module handling
|
---|
2530 | * helpers for importing functions from a module (DLL).
|
---|
2531 | */
|
---|
2532 |
|
---|
2533 | /* ******************************************************************
|
---|
2534 | *
|
---|
2535 | * Module handling helpers
|
---|
2536 | *
|
---|
2537 | ********************************************************************/
|
---|
2538 |
|
---|
2539 | /*
|
---|
2540 | *@@ doshResolveImports:
|
---|
2541 | * this function loads the module called pszModuleName
|
---|
2542 | * and resolves imports dynamically using DosQueryProcAddress.
|
---|
2543 | *
|
---|
2544 | * To specify the functions to be imported, a RESOLVEFUNCTION
|
---|
2545 | * array is used. In each of the array items, specify the
|
---|
2546 | * name of the function and a pointer to a function pointer
|
---|
2547 | * where to store the resolved address.
|
---|
2548 | *
|
---|
2549 | *@@added V0.9.3 (2000-04-29) [umoeller]
|
---|
2550 | */
|
---|
2551 |
|
---|
2552 | APIRET doshResolveImports(PSZ pszModuleName, // in: DLL to load
|
---|
2553 | HMODULE *phmod, // out: module handle
|
---|
2554 | PRESOLVEFUNCTION paResolves, // in/out: function resolves
|
---|
2555 | ULONG cResolves) // in: array item count (not array size!)
|
---|
2556 | {
|
---|
2557 | CHAR szName[CCHMAXPATH];
|
---|
2558 | APIRET arc = DosLoadModule(szName,
|
---|
2559 | sizeof(szName),
|
---|
2560 | pszModuleName,
|
---|
2561 | phmod);
|
---|
2562 | if (arc == NO_ERROR)
|
---|
2563 | {
|
---|
2564 | ULONG ul;
|
---|
2565 | for (ul = 0;
|
---|
2566 | ul < cResolves;
|
---|
2567 | ul++)
|
---|
2568 | {
|
---|
2569 | arc = DosQueryProcAddr(*phmod,
|
---|
2570 | 0, // ordinal, ignored
|
---|
2571 | (PSZ)paResolves[ul].pcszFunctionName,
|
---|
2572 | paResolves[ul].ppFuncAddress);
|
---|
2573 |
|
---|
2574 | /* _Pmpf(("Resolved %s to 0x%lX, rc: %d",
|
---|
2575 | paResolves[ul].pcszFunctionName,
|
---|
2576 | *paResolves[ul].ppFuncAddress,
|
---|
2577 | arc)); */
|
---|
2578 | if (arc != NO_ERROR)
|
---|
2579 | break;
|
---|
2580 | }
|
---|
2581 | }
|
---|
2582 |
|
---|
2583 | return (arc);
|
---|
2584 | }
|
---|
2585 |
|
---|
2586 | /*
|
---|
2587 | *@@category: Helpers\Control program helpers\Performance (CPU load) helpers
|
---|
2588 | * helpers around DosPerfSysCall.
|
---|
2589 | */
|
---|
2590 |
|
---|
2591 | /* ******************************************************************
|
---|
2592 | *
|
---|
2593 | * Performance Counters (CPU Load)
|
---|
2594 | *
|
---|
2595 | ********************************************************************/
|
---|
2596 |
|
---|
2597 | /*
|
---|
2598 | *@@ doshPerfOpen:
|
---|
2599 | * initializes the OS/2 DosPerfSysCall API for
|
---|
2600 | * the calling thread.
|
---|
2601 | *
|
---|
2602 | * Note: This API is not supported on all OS/2
|
---|
2603 | * versions. I believe it came up with some Warp 4
|
---|
2604 | * fixpak. The API is resolved dynamically by
|
---|
2605 | * this function (using DosQueryProcAddr). Only
|
---|
2606 | * if NO_ERROR is returned, you may call doshPerfGet
|
---|
2607 | * afterwards.
|
---|
2608 | *
|
---|
2609 | * This properly initializes the internal counters
|
---|
2610 | * which the OS/2 kernel uses for this API. Apparently,
|
---|
2611 | * with newer kernels (FP13/14), IBM has chosen to no
|
---|
2612 | * longer do this automatically, which is the reason
|
---|
2613 | * why many "pulse" utilities display garbage with these
|
---|
2614 | * fixpaks.
|
---|
2615 | *
|
---|
2616 | * After NO_ERROR is returned, DOSHPERFSYS.cProcessors
|
---|
2617 | * contains the no. of processors found on the system.
|
---|
2618 | * All pointers in DOSHPERFSYS then point to arrays
|
---|
2619 | * which have exactly cProcessors array items.
|
---|
2620 | *
|
---|
2621 | * Call doshPerfClose to clean up resources allocated
|
---|
2622 | * by this function.
|
---|
2623 | *
|
---|
2624 | * Example code:
|
---|
2625 | *
|
---|
2626 | + PDOSHPERFSYS pPerf = NULL;
|
---|
2627 | + APIRET arc = doshPerfOpen(&pPerf);
|
---|
2628 | + if (arc == NO_ERROR)
|
---|
2629 | + {
|
---|
2630 | + // this should really be in a timer
|
---|
2631 | + ULONG ulCPU;
|
---|
2632 | + arc = doshPerfGet(&pPerf);
|
---|
2633 | + // go thru all CPUs
|
---|
2634 | + for (ulCPU = 0; ulCPU < pPerf->cProcessors; ulCPU++)
|
---|
2635 | + {
|
---|
2636 | + LONG lLoadThis = pPerf->palLoads[ulCPU];
|
---|
2637 | + ...
|
---|
2638 | + }
|
---|
2639 | +
|
---|
2640 | + ...
|
---|
2641 | +
|
---|
2642 | + // clean up
|
---|
2643 | + doshPerfClose(&pPerf);
|
---|
2644 | + }
|
---|
2645 | +
|
---|
2646 | *
|
---|
2647 | *@@added V0.9.7 (2000-12-02) [umoeller]
|
---|
2648 | *@@changed V0.9.9 (2001-03-14) [umoeller]: added interrupt loads; thanks phaller
|
---|
2649 | */
|
---|
2650 |
|
---|
2651 | APIRET doshPerfOpen(PDOSHPERFSYS *ppPerfSys) // out: new DOSHPERFSYS structure
|
---|
2652 | {
|
---|
2653 | APIRET arc = NO_ERROR;
|
---|
2654 |
|
---|
2655 | // allocate DOSHPERFSYS structure
|
---|
2656 | *ppPerfSys = (PDOSHPERFSYS)malloc(sizeof(DOSHPERFSYS));
|
---|
2657 | if (!*ppPerfSys)
|
---|
2658 | arc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
2659 | else
|
---|
2660 | {
|
---|
2661 | // initialize structure
|
---|
2662 | PDOSHPERFSYS pPerfSys = *ppPerfSys;
|
---|
2663 | memset(pPerfSys, 0, sizeof(*pPerfSys));
|
---|
2664 |
|
---|
2665 | // resolve DosPerfSysCall API entry
|
---|
2666 | if (!(arc = DosLoadModule(NULL, 0, "DOSCALLS", &pPerfSys->hmod)))
|
---|
2667 | {
|
---|
2668 | if (!(arc = DosQueryProcAddr(pPerfSys->hmod,
|
---|
2669 | 976,
|
---|
2670 | "DosPerfSysCall",
|
---|
2671 | (PFN*)(&pPerfSys->pDosPerfSysCall))))
|
---|
2672 | {
|
---|
2673 | // OK, we got the API: initialize!
|
---|
2674 | if (!(arc = pPerfSys->pDosPerfSysCall(CMD_KI_ENABLE, 0, 0, 0)))
|
---|
2675 | {
|
---|
2676 | pPerfSys->fInitialized = TRUE;
|
---|
2677 | // call CMD_KI_DISABLE later
|
---|
2678 |
|
---|
2679 | if (!(arc = pPerfSys->pDosPerfSysCall(CMD_PERF_INFO,
|
---|
2680 | 0,
|
---|
2681 | (ULONG)(&pPerfSys->cProcessors),
|
---|
2682 | 0)))
|
---|
2683 | {
|
---|
2684 | ULONG ul = 0,
|
---|
2685 | cProcs = pPerfSys->cProcessors,
|
---|
2686 | cbDouble = cProcs * sizeof(double),
|
---|
2687 | cbLong = cProcs * sizeof(LONG);
|
---|
2688 |
|
---|
2689 | // allocate arrays
|
---|
2690 | if ( (!(pPerfSys->paCPUUtils = (PCPUUTIL)calloc(cProcs,
|
---|
2691 | sizeof(CPUUTIL))))
|
---|
2692 | || (!(pPerfSys->padBusyPrev
|
---|
2693 | = (double*)malloc(cbDouble)))
|
---|
2694 | || (!(pPerfSys->padTimePrev
|
---|
2695 | = (double*)malloc(cbDouble)))
|
---|
2696 | || (!(pPerfSys->padIntrPrev
|
---|
2697 | = (double*)malloc(cbDouble)))
|
---|
2698 | || (!(pPerfSys->palLoads
|
---|
2699 | = (PLONG)malloc(cbLong)))
|
---|
2700 | || (!(pPerfSys->palIntrs
|
---|
2701 | = (PLONG)malloc(cbLong)))
|
---|
2702 | )
|
---|
2703 | arc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
2704 | else
|
---|
2705 | {
|
---|
2706 | for (ul = 0; ul < cProcs; ul++)
|
---|
2707 | {
|
---|
2708 | pPerfSys->padBusyPrev[ul] = 0.0;
|
---|
2709 | pPerfSys->padTimePrev[ul] = 0.0;
|
---|
2710 | pPerfSys->padIntrPrev[ul] = 0.0;
|
---|
2711 | pPerfSys->palLoads[ul] = 0;
|
---|
2712 | pPerfSys->palIntrs[ul] = 0;
|
---|
2713 | }
|
---|
2714 | }
|
---|
2715 | }
|
---|
2716 | }
|
---|
2717 | } // end if (arc == NO_ERROR)
|
---|
2718 | } // end if (arc == NO_ERROR)
|
---|
2719 |
|
---|
2720 | if (arc)
|
---|
2721 | // error: clean up
|
---|
2722 | doshPerfClose(ppPerfSys);
|
---|
2723 |
|
---|
2724 | } // end else if (!*ppPerfSys)
|
---|
2725 |
|
---|
2726 | return (arc);
|
---|
2727 | }
|
---|
2728 |
|
---|
2729 | /*
|
---|
2730 | *@@ doshPerfGet:
|
---|
2731 | * calculates a current snapshot of the system load,
|
---|
2732 | * compared with the load which was calculated on
|
---|
2733 | * the previous call.
|
---|
2734 | *
|
---|
2735 | * If you want to continually measure the system CPU
|
---|
2736 | * load, this is the function you will want to call
|
---|
2737 | * regularly -- e.g. with a timer once per second.
|
---|
2738 | *
|
---|
2739 | * Call this ONLY if doshPerfOpen returned NO_ERROR,
|
---|
2740 | * or you'll get crashes.
|
---|
2741 | *
|
---|
2742 | * If this call returns NO_ERROR, you get LONG load
|
---|
2743 | * values for each CPU in the system in the arrays
|
---|
2744 | * in DOSHPERFSYS (in per-mille, 0-1000).
|
---|
2745 | *
|
---|
2746 | * There are two arrays:
|
---|
2747 | *
|
---|
2748 | * -- DOSHPERFSYS.palLoads contains the "user" load
|
---|
2749 | * for each CPU.
|
---|
2750 | *
|
---|
2751 | * -- DOSHPERFSYS.palIntrs contains the "IRQ" load
|
---|
2752 | * for each CPU.
|
---|
2753 | *
|
---|
2754 | * Sum up the two values to get the total load for
|
---|
2755 | * each CPU.
|
---|
2756 | *
|
---|
2757 | * For example, if there are two CPUs, after this call,
|
---|
2758 | *
|
---|
2759 | * -- DOSHPERFSYS.palLoads[0] contains the "user" load
|
---|
2760 | * of the first CPU,
|
---|
2761 | *
|
---|
2762 | * -- DOSHPERFSYS.palLoads[0] contains the "user" load
|
---|
2763 | * of the second CPU.
|
---|
2764 | *
|
---|
2765 | * See doshPerfOpen for example code.
|
---|
2766 | *
|
---|
2767 | *@@added V0.9.7 (2000-12-02) [umoeller]
|
---|
2768 | *@@changed V0.9.9 (2001-03-14) [umoeller]: added interrupt loads; thanks phaller
|
---|
2769 | */
|
---|
2770 |
|
---|
2771 | APIRET doshPerfGet(PDOSHPERFSYS pPerfSys)
|
---|
2772 | {
|
---|
2773 | APIRET arc = NO_ERROR;
|
---|
2774 | if (!pPerfSys->pDosPerfSysCall)
|
---|
2775 | arc = ERROR_INVALID_PARAMETER;
|
---|
2776 | else
|
---|
2777 | {
|
---|
2778 | if (!(arc = pPerfSys->pDosPerfSysCall(CMD_KI_RDCNT,
|
---|
2779 | (ULONG)pPerfSys->paCPUUtils,
|
---|
2780 | 0, 0)))
|
---|
2781 | {
|
---|
2782 | // go thru all processors
|
---|
2783 | ULONG ul = 0;
|
---|
2784 | for (; ul < pPerfSys->cProcessors; ul++)
|
---|
2785 | {
|
---|
2786 | PCPUUTIL pCPUUtilThis = &pPerfSys->paCPUUtils[ul];
|
---|
2787 |
|
---|
2788 | double dTime = LL2F(pCPUUtilThis->ulTimeHigh,
|
---|
2789 | pCPUUtilThis->ulTimeLow);
|
---|
2790 | double dBusy = LL2F(pCPUUtilThis->ulBusyHigh,
|
---|
2791 | pCPUUtilThis->ulBusyLow);
|
---|
2792 | double dIntr = LL2F(pCPUUtilThis->ulIntrHigh,
|
---|
2793 | pCPUUtilThis->ulIntrLow);
|
---|
2794 |
|
---|
2795 | double *pdBusyPrevThis = &pPerfSys->padBusyPrev[ul];
|
---|
2796 | double *pdTimePrevThis = &pPerfSys->padTimePrev[ul];
|
---|
2797 | double *pdIntrPrevThis = &pPerfSys->padIntrPrev[ul];
|
---|
2798 |
|
---|
2799 | // avoid division by zero
|
---|
2800 | double dTimeDelta = (dTime - *pdTimePrevThis);
|
---|
2801 | if (dTimeDelta)
|
---|
2802 | {
|
---|
2803 | pPerfSys->palLoads[ul]
|
---|
2804 | = (LONG)( (double)( (dBusy - *pdBusyPrevThis)
|
---|
2805 | / dTimeDelta
|
---|
2806 | * 1000.0
|
---|
2807 | )
|
---|
2808 | );
|
---|
2809 | pPerfSys->palIntrs[ul]
|
---|
2810 | = (LONG)( (double)( (dIntr - *pdIntrPrevThis)
|
---|
2811 | / dTimeDelta
|
---|
2812 | * 1000.0
|
---|
2813 | )
|
---|
2814 | );
|
---|
2815 | }
|
---|
2816 | else
|
---|
2817 | {
|
---|
2818 | // no clear readings are available
|
---|
2819 | pPerfSys->palLoads[ul] = 0;
|
---|
2820 | pPerfSys->palIntrs[ul] = 0;
|
---|
2821 | }
|
---|
2822 |
|
---|
2823 | *pdTimePrevThis = dTime;
|
---|
2824 | *pdBusyPrevThis = dBusy;
|
---|
2825 | *pdIntrPrevThis = dIntr;
|
---|
2826 | }
|
---|
2827 | }
|
---|
2828 | }
|
---|
2829 |
|
---|
2830 | return (arc);
|
---|
2831 | }
|
---|
2832 |
|
---|
2833 | /*
|
---|
2834 | *@@ doshPerfClose:
|
---|
2835 | * frees all resources allocated by doshPerfOpen.
|
---|
2836 | *
|
---|
2837 | *@@added V0.9.7 (2000-12-02) [umoeller]
|
---|
2838 | *@@changed V0.9.9 (2001-02-06) [umoeller]: removed disable; this broke the WarpCenter
|
---|
2839 | *@@changed V0.9.9 (2001-03-14) [umoeller]: fixed memory leak
|
---|
2840 | *@@changed V0.9.9 (2001-03-14) [umoeller]: added interrupt loads; thanks phaller
|
---|
2841 | */
|
---|
2842 |
|
---|
2843 | APIRET doshPerfClose(PDOSHPERFSYS *ppPerfSys)
|
---|
2844 | {
|
---|
2845 | APIRET arc = NO_ERROR;
|
---|
2846 | PDOSHPERFSYS pPerfSys = *ppPerfSys;
|
---|
2847 | if (!pPerfSys)
|
---|
2848 | arc = ERROR_INVALID_PARAMETER;
|
---|
2849 | else
|
---|
2850 | {
|
---|
2851 | // do not call this, this messes up the WarpCenter V0.9.9 (2001-02-06) [umoeller]
|
---|
2852 | // if (pPerfSys->fInitialized) pPerfSys->pDosPerfSysCall(CMD_KI_DISABLE, 0, 0, 0);
|
---|
2853 |
|
---|
2854 | if (pPerfSys->paCPUUtils)
|
---|
2855 | free(pPerfSys->paCPUUtils);
|
---|
2856 | if (pPerfSys->padBusyPrev)
|
---|
2857 | free(pPerfSys->padBusyPrev);
|
---|
2858 | if (pPerfSys->padTimePrev)
|
---|
2859 | free(pPerfSys->padTimePrev);
|
---|
2860 | if (pPerfSys->padIntrPrev)
|
---|
2861 | free(pPerfSys->padIntrPrev);
|
---|
2862 | if (pPerfSys->palLoads) // was missing V0.9.9 (2001-03-14) [umoeller]
|
---|
2863 | free(pPerfSys->palLoads);
|
---|
2864 | if (pPerfSys->palIntrs)
|
---|
2865 | free(pPerfSys->palIntrs);
|
---|
2866 |
|
---|
2867 | if (pPerfSys->hmod)
|
---|
2868 | DosFreeModule(pPerfSys->hmod);
|
---|
2869 | free(pPerfSys);
|
---|
2870 | *ppPerfSys = NULL;
|
---|
2871 | }
|
---|
2872 |
|
---|
2873 | return (arc);
|
---|
2874 | }
|
---|
2875 |
|
---|
2876 | /*
|
---|
2877 | *@@category: Helpers\Control program helpers\Process management
|
---|
2878 | * helpers for starting subprocesses.
|
---|
2879 | */
|
---|
2880 |
|
---|
2881 | /* ******************************************************************
|
---|
2882 | *
|
---|
2883 | * Process helpers
|
---|
2884 | *
|
---|
2885 | ********************************************************************/
|
---|
2886 |
|
---|
2887 | static PVOID // G_pvGlobalInfoSeg = NULL,
|
---|
2888 | G_pvLocalInfoSeg = NULL;
|
---|
2889 |
|
---|
2890 | USHORT _Far16 _Pascal Dos16GetInfoSeg(PSEL pselGlobal,
|
---|
2891 | PSEL pselLocal);
|
---|
2892 |
|
---|
2893 | /*
|
---|
2894 | * GetInfoSegs:
|
---|
2895 | *
|
---|
2896 | */
|
---|
2897 |
|
---|
2898 | VOID GetInfoSegs(VOID)
|
---|
2899 | {
|
---|
2900 | SEL GlobalInfoSegSelector,
|
---|
2901 | LocalInfoSegSelector;
|
---|
2902 |
|
---|
2903 | // get selectors (old 16-bit API; this gets called only once)
|
---|
2904 | Dos16GetInfoSeg(&GlobalInfoSegSelector,
|
---|
2905 | &LocalInfoSegSelector);
|
---|
2906 | // thunk
|
---|
2907 | /* G_pvGlobalInfoSeg = (PVOID)( (GlobalInfoSegSelector << 0x000D)
|
---|
2908 | & 0x01fff0000); */
|
---|
2909 | G_pvLocalInfoSeg = (PVOID)( (LocalInfoSegSelector << 0x000D)
|
---|
2910 | & 0x01fff0000);
|
---|
2911 | }
|
---|
2912 |
|
---|
2913 | /*
|
---|
2914 | *@@ doshMyPID:
|
---|
2915 | * returns the PID of the current process.
|
---|
2916 | *
|
---|
2917 | * This uses an interesting hack which is way
|
---|
2918 | * faster than DosGetInfoBlocks.
|
---|
2919 | *
|
---|
2920 | *@@added V0.9.9 (2001-04-04) [umoeller]
|
---|
2921 | */
|
---|
2922 |
|
---|
2923 | ULONG doshMyPID(VOID)
|
---|
2924 | {
|
---|
2925 | if (!G_pvLocalInfoSeg)
|
---|
2926 | // first call:
|
---|
2927 | GetInfoSegs();
|
---|
2928 |
|
---|
2929 | // PID is at offset 0 in the local info seg
|
---|
2930 | return (*(PUSHORT)G_pvLocalInfoSeg);
|
---|
2931 | }
|
---|
2932 |
|
---|
2933 | /*
|
---|
2934 | *@@ doshMyTID:
|
---|
2935 | * returns the TID of the current thread.
|
---|
2936 | *
|
---|
2937 | * This uses an interesting hack which is way
|
---|
2938 | * faster than DosGetInfoBlocks.
|
---|
2939 | *
|
---|
2940 | *@@added V0.9.9 (2001-04-04) [umoeller]
|
---|
2941 | */
|
---|
2942 |
|
---|
2943 | ULONG doshMyTID(VOID)
|
---|
2944 | {
|
---|
2945 | if (!G_pvLocalInfoSeg)
|
---|
2946 | // first call:
|
---|
2947 | GetInfoSegs();
|
---|
2948 |
|
---|
2949 | // TID is at offset 6 in the local info seg
|
---|
2950 | return (*(PUSHORT)((PBYTE)G_pvLocalInfoSeg + 6));
|
---|
2951 | }
|
---|
2952 |
|
---|
2953 | /*
|
---|
2954 | *@@ doshExecVIO:
|
---|
2955 | * executes cmd.exe with the /c parameter
|
---|
2956 | * and pcszExecWithArgs. This is equivalent
|
---|
2957 | * to the C library system() function,
|
---|
2958 | * except that an OS/2 error code is returned
|
---|
2959 | * and that this works with the VAC subsystem
|
---|
2960 | * library.
|
---|
2961 | *
|
---|
2962 | * This uses DosExecPgm and handles the sick
|
---|
2963 | * argument passing.
|
---|
2964 | *
|
---|
2965 | * If NO_ERROR is returned, *plExitCode receives
|
---|
2966 | * the exit code of the process. If the process
|
---|
2967 | * was terminated abnormally, *plExitCode receives:
|
---|
2968 | *
|
---|
2969 | * -- -1: hard error halt
|
---|
2970 | * -- -2: 16-bit trap
|
---|
2971 | * -- -3: DosKillProcess
|
---|
2972 | * -- -4: 32-bit exception
|
---|
2973 | *
|
---|
2974 | *@@added V0.9.4 (2000-07-27) [umoeller]
|
---|
2975 | */
|
---|
2976 |
|
---|
2977 | APIRET doshExecVIO(PCSZ pcszExecWithArgs,
|
---|
2978 | PLONG plExitCode) // out: exit code (ptr can be NULL)
|
---|
2979 | {
|
---|
2980 | APIRET arc = NO_ERROR;
|
---|
2981 |
|
---|
2982 | if (strlen(pcszExecWithArgs) > 255)
|
---|
2983 | arc = ERROR_INSUFFICIENT_BUFFER;
|
---|
2984 | else
|
---|
2985 | {
|
---|
2986 | CHAR szObj[CCHMAXPATH];
|
---|
2987 | RESULTCODES res = {0};
|
---|
2988 | CHAR szBuffer[300];
|
---|
2989 |
|
---|
2990 | // DosExecPgm expects two args in szBuffer:
|
---|
2991 | // -- cmd.exe\0
|
---|
2992 | // -- then the actual argument, terminated by two 0 bytes.
|
---|
2993 | memset(szBuffer, 0, sizeof(szBuffer));
|
---|
2994 | strcpy(szBuffer, "cmd.exe\0");
|
---|
2995 | sprintf(szBuffer + 8, "/c %s",
|
---|
2996 | pcszExecWithArgs);
|
---|
2997 |
|
---|
2998 | arc = DosExecPgm(szObj, sizeof(szObj),
|
---|
2999 | EXEC_SYNC,
|
---|
3000 | szBuffer,
|
---|
3001 | 0,
|
---|
3002 | &res,
|
---|
3003 | "cmd.exe");
|
---|
3004 | if ((arc == NO_ERROR) && (plExitCode))
|
---|
3005 | {
|
---|
3006 | if (res.codeTerminate == 0)
|
---|
3007 | // normal exit:
|
---|
3008 | *plExitCode = res.codeResult;
|
---|
3009 | else
|
---|
3010 | *plExitCode = -((LONG)res.codeTerminate);
|
---|
3011 | }
|
---|
3012 | }
|
---|
3013 |
|
---|
3014 | return (arc);
|
---|
3015 | }
|
---|
3016 |
|
---|
3017 | /*
|
---|
3018 | *@@ doshQuickStartSession:
|
---|
3019 | * this is a shortcut to DosStartSession w/out having to
|
---|
3020 | * deal with all the messy parameters.
|
---|
3021 | *
|
---|
3022 | * This one starts pszPath as a child session and passes
|
---|
3023 | * pszParams to it.
|
---|
3024 | *
|
---|
3025 | * In usPgmCtl, OR any or none of the following (V0.9.0):
|
---|
3026 | * -- SSF_CONTROL_NOAUTOCLOSE (0x0008): do not close automatically.
|
---|
3027 | * This bit is used only for VIO Windowable apps and ignored
|
---|
3028 | * for all other applications.
|
---|
3029 | * -- SSF_CONTROL_MINIMIZE (0x0004)
|
---|
3030 | * -- SSF_CONTROL_MAXIMIZE (0x0002)
|
---|
3031 | * -- SSF_CONTROL_INVISIBLE (0x0001)
|
---|
3032 | * -- SSF_CONTROL_VISIBLE (0x0000)
|
---|
3033 | *
|
---|
3034 | * Specifying 0 will therefore auto-close the session and make it
|
---|
3035 | * visible.
|
---|
3036 | *
|
---|
3037 | * If (fWait), this function will create a termination queue
|
---|
3038 | * and not return until the child session has ended. Be warned,
|
---|
3039 | * this blocks the calling thread.
|
---|
3040 | *
|
---|
3041 | * Otherwise the function will return immediately.
|
---|
3042 | *
|
---|
3043 | * The session and process IDs of the child session will be
|
---|
3044 | * written to *pulSID and *ppid. Of course, in "wait" mode,
|
---|
3045 | * these are no longer valid after this function returns.
|
---|
3046 | *
|
---|
3047 | * Returns the error code of DosStartSession.
|
---|
3048 | *
|
---|
3049 | * Note: According to CPREF, calling DosStartSession calls
|
---|
3050 | * DosExecPgm in turn for all full-screen, VIO, and PM sessions.
|
---|
3051 | *
|
---|
3052 | *@@changed V0.9.0 [umoeller]: prototype changed to include usPgmCtl
|
---|
3053 | *@@changed V0.9.1 (99-12-30) [umoeller]: queue was sometimes not closed. Fixed.
|
---|
3054 | *@@changed V0.9.3 (2000-05-03) [umoeller]: added fForeground
|
---|
3055 | *@@changed V0.9.14 (2001-08-03) [umoeller]: fixed potential queue leak
|
---|
3056 | *@@changed V0.9.14 (2001-08-03) [umoeller]: fixed memory leak in wait mode; added pusReturn to prototype
|
---|
3057 | */
|
---|
3058 |
|
---|
3059 | APIRET doshQuickStartSession(PCSZ pcszPath, // in: program to start
|
---|
3060 | PCSZ pcszParams, // in: parameters for program
|
---|
3061 | BOOL fForeground, // in: if TRUE, session will be in foreground
|
---|
3062 | USHORT usPgmCtl, // in: STARTDATA.PgmControl
|
---|
3063 | BOOL fWait, // in: wait for termination?
|
---|
3064 | PULONG pulSID, // out: session ID (req.)
|
---|
3065 | PPID ppid, // out: process ID (req.)
|
---|
3066 | PUSHORT pusReturn) // out: in wait mode, session's return code (ptr can be NULL)
|
---|
3067 | {
|
---|
3068 | APIRET arc = NO_ERROR;
|
---|
3069 | // queue stuff
|
---|
3070 | const char *pcszQueueName = "\\queues\\xwphlpsw.que";
|
---|
3071 | HQUEUE hq = 0;
|
---|
3072 | PID qpid = 0;
|
---|
3073 |
|
---|
3074 | if (fWait)
|
---|
3075 | {
|
---|
3076 | if (!(arc = DosCreateQueue(&hq,
|
---|
3077 | QUE_FIFO | QUE_CONVERT_ADDRESS,
|
---|
3078 | (PSZ)pcszQueueName)))
|
---|
3079 | arc = DosOpenQueue(&qpid, &hq, (PSZ)pcszQueueName);
|
---|
3080 | }
|
---|
3081 |
|
---|
3082 | if (!arc) // V0.9.14 (2001-08-03) [umoeller]
|
---|
3083 | {
|
---|
3084 | STARTDATA SData;
|
---|
3085 | CHAR szObjBuf[CCHMAXPATH];
|
---|
3086 |
|
---|
3087 | SData.Length = sizeof(STARTDATA);
|
---|
3088 | SData.Related = SSF_RELATED_CHILD; //INDEPENDENT;
|
---|
3089 | SData.FgBg = (fForeground) ? SSF_FGBG_FORE : SSF_FGBG_BACK;
|
---|
3090 | // V0.9.3 (2000-05-03) [umoeller]
|
---|
3091 | SData.TraceOpt = SSF_TRACEOPT_NONE;
|
---|
3092 |
|
---|
3093 | SData.PgmTitle = (PSZ)pcszPath; // title for window
|
---|
3094 | SData.PgmName = (PSZ)pcszPath;
|
---|
3095 | SData.PgmInputs = (PSZ)pcszParams;
|
---|
3096 |
|
---|
3097 | SData.TermQ = (fWait) ? (PSZ)pcszQueueName : NULL;
|
---|
3098 | SData.Environment = 0;
|
---|
3099 | SData.InheritOpt = SSF_INHERTOPT_PARENT;
|
---|
3100 | SData.SessionType = SSF_TYPE_DEFAULT;
|
---|
3101 | SData.IconFile = 0;
|
---|
3102 | SData.PgmHandle = 0;
|
---|
3103 |
|
---|
3104 | SData.PgmControl = usPgmCtl;
|
---|
3105 |
|
---|
3106 | SData.InitXPos = 30;
|
---|
3107 | SData.InitYPos = 40;
|
---|
3108 | SData.InitXSize = 200;
|
---|
3109 | SData.InitYSize = 140;
|
---|
3110 | SData.Reserved = 0;
|
---|
3111 | SData.ObjectBuffer = szObjBuf;
|
---|
3112 | SData.ObjectBuffLen = (ULONG)sizeof(szObjBuf);
|
---|
3113 |
|
---|
3114 | if ( (!(arc = DosStartSession(&SData, pulSID, ppid)))
|
---|
3115 | && (fWait)
|
---|
3116 | )
|
---|
3117 | {
|
---|
3118 | // block on the termination queue, which is written
|
---|
3119 | // to when the subprocess ends
|
---|
3120 | REQUESTDATA rqdata;
|
---|
3121 | ULONG cbData = 0;
|
---|
3122 | PULONG pulData = NULL;
|
---|
3123 | BYTE elpri;
|
---|
3124 |
|
---|
3125 | rqdata.pid = qpid;
|
---|
3126 | if (!(arc = DosReadQueue(hq, // in: queue handle
|
---|
3127 | &rqdata, // out: pid and ulData
|
---|
3128 | &cbData, // out: size of data returned
|
---|
3129 | (PVOID*)&pulData, // out: data returned
|
---|
3130 | 0, // in: remove first element in queue
|
---|
3131 | 0, // in: wait for queue data (block thread)
|
---|
3132 | &elpri, // out: element's priority
|
---|
3133 | 0))) // in: event semaphore to be posted
|
---|
3134 | {
|
---|
3135 | if (!rqdata.ulData)
|
---|
3136 | {
|
---|
3137 | // child session ended:
|
---|
3138 | // V0.9.14 (2001-08-03) [umoeller]
|
---|
3139 |
|
---|
3140 | // *pulSID = (*pulData) & 0xffff;
|
---|
3141 | if (pusReturn)
|
---|
3142 | *pusReturn = ((*pulData) >> 16) & 0xffff;
|
---|
3143 |
|
---|
3144 | }
|
---|
3145 | // else: continue looping
|
---|
3146 |
|
---|
3147 | if (pulData)
|
---|
3148 | DosFreeMem(pulData);
|
---|
3149 | }
|
---|
3150 | }
|
---|
3151 | }
|
---|
3152 |
|
---|
3153 | if (hq)
|
---|
3154 | DosCloseQueue(hq);
|
---|
3155 |
|
---|
3156 | return (arc);
|
---|
3157 | }
|
---|
3158 |
|
---|
3159 |
|
---|