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-2012 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_DOSEXCEPTIONS
|
---|
52 | #define INCL_DOSSESMGR
|
---|
53 | #define INCL_DOSQUEUES
|
---|
54 | #define INCL_DOSSEMAPHORES
|
---|
55 | #define INCL_DOSMISC
|
---|
56 | #define INCL_DOSDEVICES
|
---|
57 | #define INCL_DOSDEVIOCTL
|
---|
58 | #define INCL_DOSERRORS
|
---|
59 |
|
---|
60 | #define INCL_KBD
|
---|
61 | #include <os2.h>
|
---|
62 |
|
---|
63 | #include <stdlib.h>
|
---|
64 | #include <string.h>
|
---|
65 | #include <stdio.h>
|
---|
66 | #include <stdarg.h>
|
---|
67 | #include <ctype.h>
|
---|
68 |
|
---|
69 | #include "setup.h" // code generation and debugging options
|
---|
70 |
|
---|
71 | #include "helpers\dosh.h"
|
---|
72 | #include "helpers\standards.h"
|
---|
73 |
|
---|
74 | #pragma hdrstop
|
---|
75 |
|
---|
76 | // static const CHAR G_acDriveLetters[28] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
---|
77 |
|
---|
78 | /*
|
---|
79 | *@@category: Helpers\Control program helpers\Wrappers
|
---|
80 | */
|
---|
81 |
|
---|
82 | /* ******************************************************************
|
---|
83 | *
|
---|
84 | * Wrappers
|
---|
85 | *
|
---|
86 | ********************************************************************/
|
---|
87 |
|
---|
88 | #ifdef DOSH_STANDARDWRAPPERS
|
---|
89 |
|
---|
90 | /*
|
---|
91 | *@@ doshSleep:
|
---|
92 | *
|
---|
93 | *@@added V0.9.16 (2002-01-26) [umoeller]
|
---|
94 | */
|
---|
95 |
|
---|
96 | APIRET doshSleep(ULONG msec)
|
---|
97 | {
|
---|
98 | // put the call in brackets so the macro won't apply here
|
---|
99 | return (DosSleep)(msec);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /*
|
---|
103 | *@@ doshCreateMutexSem:
|
---|
104 | *
|
---|
105 | *@@added V0.9.16 (2002-01-26) [umoeller]
|
---|
106 | */
|
---|
107 |
|
---|
108 | APIRET doshCreateMutexSem(PSZ pszName,
|
---|
109 | PHMTX phmtx,
|
---|
110 | ULONG flAttr,
|
---|
111 | BOOL32 fState)
|
---|
112 | {
|
---|
113 | // put the call in brackets so the macro won't apply here
|
---|
114 | return (DosCreateMutexSem)(pszName, phmtx, flAttr, fState);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /*
|
---|
118 | *@@ doshRequestMutexSem:
|
---|
119 | *
|
---|
120 | *@@added V0.9.16 (2002-01-26) [umoeller]
|
---|
121 | */
|
---|
122 |
|
---|
123 | APIRET doshRequestMutexSem(HMTX hmtx, ULONG ulTimeout)
|
---|
124 | {
|
---|
125 | return (DosRequestMutexSem)(hmtx, ulTimeout);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /*
|
---|
129 | *@@ doshReleaseMutexSem:
|
---|
130 | *
|
---|
131 | *@@added V0.9.16 (2002-01-26) [umoeller]
|
---|
132 | */
|
---|
133 |
|
---|
134 | APIRET doshReleaseMutexSem(HMTX hmtx)
|
---|
135 | {
|
---|
136 | return (DosReleaseMutexSem)(hmtx);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*
|
---|
140 | *@@ doshSetExceptionHandler:
|
---|
141 | *
|
---|
142 | *@@added V0.9.16 (2002-01-26) [umoeller]
|
---|
143 | */
|
---|
144 |
|
---|
145 | APIRET doshSetExceptionHandler(PEXCEPTIONREGISTRATIONRECORD pERegRec)
|
---|
146 | {
|
---|
147 | // put the call in brackets so the macro won't apply here
|
---|
148 | return (DosSetExceptionHandler)(pERegRec);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /*
|
---|
152 | *@@ doshUnsetExceptionHandler:
|
---|
153 | *
|
---|
154 | *@@added V0.9.16 (2002-01-26) [umoeller]
|
---|
155 | */
|
---|
156 |
|
---|
157 | APIRET doshUnsetExceptionHandler(PEXCEPTIONREGISTRATIONRECORD pERegRec)
|
---|
158 | {
|
---|
159 | // put the call in brackets so the macro won't apply here
|
---|
160 | return (DosUnsetExceptionHandler)(pERegRec);
|
---|
161 | }
|
---|
162 |
|
---|
163 | #endif
|
---|
164 |
|
---|
165 | /*
|
---|
166 | *@@category: Helpers\Control program helpers\Miscellaneous
|
---|
167 | * Miscellaneous helpers in dosh.c that didn't fit into any other
|
---|
168 | * category.
|
---|
169 | */
|
---|
170 |
|
---|
171 | /* ******************************************************************
|
---|
172 | *
|
---|
173 | * Miscellaneous
|
---|
174 | *
|
---|
175 | ********************************************************************/
|
---|
176 |
|
---|
177 | /*
|
---|
178 | *@@ doshGetChar:
|
---|
179 | * reads a single character from the keyboard.
|
---|
180 | * Useful for VIO sessions, since there's great
|
---|
181 | * confusion between the various C dialects about
|
---|
182 | * how to use getc(), and getc() doesn't work
|
---|
183 | * with the VAC subsystem library.
|
---|
184 | *
|
---|
185 | *@@added V0.9.4 (2000-07-27) [umoeller]
|
---|
186 | */
|
---|
187 |
|
---|
188 | CHAR doshGetChar(VOID)
|
---|
189 | {
|
---|
190 | // CHAR c;
|
---|
191 | // ULONG ulRead = 0;
|
---|
192 |
|
---|
193 | KBDKEYINFO kki;
|
---|
194 | KbdCharIn(&kki,
|
---|
195 | 0, // wait
|
---|
196 | 0);
|
---|
197 |
|
---|
198 | return (kki.chChar);
|
---|
199 | }
|
---|
200 |
|
---|
201 | /*
|
---|
202 | *@@ doshQueryShiftState:
|
---|
203 | * returns TRUE if any of the SHIFT keys are
|
---|
204 | * currently pressed. Useful for checks during
|
---|
205 | * WM_COMMAND messages from menus.
|
---|
206 | *
|
---|
207 | *@@changed V0.9.5 (2000-09-27) [umoeller]: added error checking
|
---|
208 | */
|
---|
209 |
|
---|
210 | BOOL doshQueryShiftState(VOID)
|
---|
211 | {
|
---|
212 | BOOL brc = FALSE;
|
---|
213 | APIRET arc = NO_ERROR;
|
---|
214 | HFILE hfKbd;
|
---|
215 | ULONG ulAction;
|
---|
216 |
|
---|
217 | if (!(arc = DosOpen("KBD$", &hfKbd, &ulAction, 0,
|
---|
218 | FILE_NORMAL,
|
---|
219 | FILE_OPEN,
|
---|
220 | OPEN_ACCESS_READONLY | OPEN_SHARE_DENYWRITE,
|
---|
221 | (PEAOP2)NULL)))
|
---|
222 | {
|
---|
223 | SHIFTSTATE ShiftState;
|
---|
224 | ULONG cbDataLen = sizeof(ShiftState);
|
---|
225 |
|
---|
226 | if (!(arc = DosDevIOCtl(hfKbd, IOCTL_KEYBOARD, KBD_GETSHIFTSTATE,
|
---|
227 | NULL, 0, NULL, // no parameters
|
---|
228 | &ShiftState, cbDataLen, &cbDataLen)))
|
---|
229 | brc = ((ShiftState.fsState & 3) != 0);
|
---|
230 |
|
---|
231 | DosClose(hfKbd);
|
---|
232 | }
|
---|
233 |
|
---|
234 | return brc;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /*
|
---|
238 | *@@ doshQuerySysErrorMsg:
|
---|
239 | * this retrieves the error message for a system error
|
---|
240 | * (APIRET) from the system error message file (OSO001.MSG).
|
---|
241 | * This file better be on the DPATH (it normally is).
|
---|
242 | *
|
---|
243 | * This returns the string in the "SYSxxx: blahblah" style,
|
---|
244 | * which is normally displayed on the command line when
|
---|
245 | * errors occur.
|
---|
246 | *
|
---|
247 | * The error message is returned in a newly allocated
|
---|
248 | * buffer, which should be free()'d afterwards.
|
---|
249 | *
|
---|
250 | * The returned string is properly null-terminated but
|
---|
251 | * should not end with a line break (\r or \n) if I see
|
---|
252 | * this correctly.
|
---|
253 | *
|
---|
254 | * Returns NULL upon errors.
|
---|
255 | *
|
---|
256 | *@@changed V1.0.0 (2002-09-24) [umoeller]: now supporting NET messages as well
|
---|
257 | */
|
---|
258 |
|
---|
259 | PSZ doshQuerySysErrorMsg(APIRET arc) // in: DOS error code
|
---|
260 | {
|
---|
261 | PSZ pszReturn = NULL;
|
---|
262 | CHAR szDosError[1000];
|
---|
263 | ULONG cbDosError = 0;
|
---|
264 | PCSZ pcszMsgFile;
|
---|
265 |
|
---|
266 | if ( (arc >= 2100)
|
---|
267 | && (arc <= 8000)
|
---|
268 | )
|
---|
269 | pcszMsgFile = "NET.MSG";
|
---|
270 | else
|
---|
271 | pcszMsgFile = "OSO001.MSG"; // default OS/2 message file
|
---|
272 |
|
---|
273 | if (!DosGetMessage(NULL, 0, // no string replacements
|
---|
274 | szDosError, sizeof(szDosError),
|
---|
275 | arc,
|
---|
276 | (PSZ)pcszMsgFile,
|
---|
277 | &cbDosError))
|
---|
278 | {
|
---|
279 | szDosError[cbDosError - 2] = 0;
|
---|
280 | pszReturn = strdup(szDosError);
|
---|
281 | }
|
---|
282 |
|
---|
283 | return pszReturn;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /*
|
---|
287 | *@@ doshQuerySysUptime:
|
---|
288 | * returns the system uptime in milliseconds.
|
---|
289 | * This can be used for time comparisons.
|
---|
290 | *
|
---|
291 | *@@added V0.9.12 (2001-05-18) [umoeller]
|
---|
292 | */
|
---|
293 |
|
---|
294 | ULONG doshQuerySysUptime(VOID)
|
---|
295 | {
|
---|
296 | ULONG ulms;
|
---|
297 | DosQuerySysInfo(QSV_MS_COUNT,
|
---|
298 | QSV_MS_COUNT,
|
---|
299 | &ulms,
|
---|
300 | sizeof(ulms));
|
---|
301 | return (ulms);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /*
|
---|
305 | *@@ doshDevIOCtl:
|
---|
306 | *
|
---|
307 | * Works with those IOCtls where the buffer
|
---|
308 | * size parameters are always the same anyway,
|
---|
309 | * which applies to all IOCtls I have seen
|
---|
310 | * so far.
|
---|
311 | *
|
---|
312 | *@@added V0.9.13 (2001-06-14) [umoeller]
|
---|
313 | */
|
---|
314 |
|
---|
315 | APIRET doshDevIOCtl(HFILE hf,
|
---|
316 | ULONG ulCategory,
|
---|
317 | ULONG ulFunction,
|
---|
318 | PVOID pvParams,
|
---|
319 | ULONG cbParams,
|
---|
320 | PVOID pvData,
|
---|
321 | ULONG cbData)
|
---|
322 | {
|
---|
323 | return (DosDevIOCtl(hf,
|
---|
324 | ulCategory,
|
---|
325 | ulFunction,
|
---|
326 | pvParams, cbParams, &cbParams,
|
---|
327 | pvData, cbData, &cbData));
|
---|
328 | }
|
---|
329 |
|
---|
330 | /*
|
---|
331 | *@@category: Helpers\Control program helpers\Shared memory management
|
---|
332 | * helpers for allocating and requesting shared memory.
|
---|
333 | */
|
---|
334 |
|
---|
335 | /* ******************************************************************
|
---|
336 | *
|
---|
337 | * Memory helpers
|
---|
338 | *
|
---|
339 | ********************************************************************/
|
---|
340 |
|
---|
341 | /*
|
---|
342 | *@@ doshMalloc:
|
---|
343 | * wrapper around malloc() which automatically
|
---|
344 | * sets ERROR_NOT_ENOUGH_MEMORY.
|
---|
345 | *
|
---|
346 | *@@added V0.9.16 (2001-10-19) [umoeller]
|
---|
347 | */
|
---|
348 |
|
---|
349 | PVOID doshMalloc(ULONG cb,
|
---|
350 | APIRET *parc)
|
---|
351 | {
|
---|
352 | PVOID pv;
|
---|
353 | *parc = NO_ERROR;
|
---|
354 | if (!(pv = malloc(cb)))
|
---|
355 | *parc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
356 |
|
---|
357 | return (pv);
|
---|
358 | }
|
---|
359 |
|
---|
360 | /*
|
---|
361 | *@@ doshAllocArray:
|
---|
362 | * allocates c * cbArrayItem bytes.
|
---|
363 | * Similar to calloc(), but returns
|
---|
364 | * error codes:
|
---|
365 | *
|
---|
366 | * -- NO_ERROR: *ppv and *pcbAllocated were set.
|
---|
367 | *
|
---|
368 | * -- ERROR_NO_DATA: either c or cbArrayItem are
|
---|
369 | * zero.
|
---|
370 | *
|
---|
371 | * -- ERROR_NOT_ENOUGH_MEMORY: malloc() failed.
|
---|
372 | *
|
---|
373 | *@@added V0.9.16 (2001-12-08) [umoeller]
|
---|
374 | */
|
---|
375 |
|
---|
376 | APIRET doshAllocArray(ULONG c, // in: array item count
|
---|
377 | ULONG cbArrayItem, // in: size of one array item
|
---|
378 | PBYTE *ppv, // out: memory ptr if NO_ERROR is returned
|
---|
379 | PULONG pcbAllocated) // out: # of bytes allocated
|
---|
380 | {
|
---|
381 | if (!c || !cbArrayItem)
|
---|
382 | return ERROR_NO_DATA;
|
---|
383 |
|
---|
384 | *pcbAllocated = c * cbArrayItem;
|
---|
385 | if (!(*ppv = (PBYTE)malloc(*pcbAllocated)))
|
---|
386 | return ERROR_NOT_ENOUGH_MEMORY;
|
---|
387 |
|
---|
388 | return NO_ERROR;
|
---|
389 | }
|
---|
390 |
|
---|
391 | /*
|
---|
392 | *@@ doshAllocSharedMem:
|
---|
393 | * wrapper for DosAllocSharedMem which has
|
---|
394 | * a malloc()-like syntax. Just due to my
|
---|
395 | * lazyness.
|
---|
396 | *
|
---|
397 | * Note that ulSize is always rounded up to the
|
---|
398 | * next 4KB value, so don't use this hundreds of times.
|
---|
399 | *
|
---|
400 | * Returns NULL upon errors. Possible errors include
|
---|
401 | * that a memory block calle pcszName has already been
|
---|
402 | * allocated.
|
---|
403 | *
|
---|
404 | * Use DosFreeMem(pvrc) to free the memory. The memory
|
---|
405 | * will only be freed if no other process has requested
|
---|
406 | * access.
|
---|
407 | *
|
---|
408 | *@@added V0.9.3 (2000-04-18) [umoeller]
|
---|
409 | */
|
---|
410 |
|
---|
411 | PVOID doshAllocSharedMem(ULONG ulSize, // in: requested mem block size (rounded up to 4KB)
|
---|
412 | const char* pcszName) // in: name of block ("\\SHAREMEM\\xxx") or NULL
|
---|
413 | {
|
---|
414 | PVOID pvrc = NULL;
|
---|
415 | APIRET arc = DosAllocSharedMem((PVOID*)&pvrc,
|
---|
416 | (PSZ)pcszName,
|
---|
417 | ulSize,
|
---|
418 | PAG_COMMIT | PAG_READ | PAG_WRITE);
|
---|
419 | if (arc == NO_ERROR)
|
---|
420 | return (pvrc);
|
---|
421 |
|
---|
422 | return NULL;
|
---|
423 | }
|
---|
424 |
|
---|
425 | /*
|
---|
426 | *@@ doshRequestSharedMem:
|
---|
427 | * requests access to a block of named shared memory
|
---|
428 | * allocated by doshAllocSharedMem.
|
---|
429 | *
|
---|
430 | * Returns NULL upon errors.
|
---|
431 | *
|
---|
432 | * Use DosFreeMem(pvrc) to free the memory. The memory
|
---|
433 | * will only be freed if no other process has requested
|
---|
434 | * access.
|
---|
435 | *
|
---|
436 | *@@added V0.9.3 (2000-04-19) [umoeller]
|
---|
437 | */
|
---|
438 |
|
---|
439 | PVOID doshRequestSharedMem(PCSZ pcszName)
|
---|
440 | {
|
---|
441 | PVOID pvrc = NULL;
|
---|
442 | APIRET arc = DosGetNamedSharedMem((PVOID*)pvrc,
|
---|
443 | (PSZ)pcszName,
|
---|
444 | PAG_READ | PAG_WRITE);
|
---|
445 | if (arc == NO_ERROR)
|
---|
446 | return (pvrc);
|
---|
447 |
|
---|
448 | return NULL;
|
---|
449 | }
|
---|
450 |
|
---|
451 | /*
|
---|
452 | *@@category: Helpers\Control program helpers\Drive management
|
---|
453 | * functions for managing drives... enumerating, testing,
|
---|
454 | * querying etc.
|
---|
455 | */
|
---|
456 |
|
---|
457 | /* ******************************************************************
|
---|
458 | *
|
---|
459 | * Drive helpers
|
---|
460 | *
|
---|
461 | ********************************************************************/
|
---|
462 |
|
---|
463 | /*
|
---|
464 | *@@ doshIsFixedDisk:
|
---|
465 | * checks whether a disk is fixed or removeable.
|
---|
466 | * ulLogicalDrive must be 1 for drive A:, 2 for B:, ...
|
---|
467 | * The result is stored in *pfFixed.
|
---|
468 | * Returns DOS error code.
|
---|
469 | *
|
---|
470 | * From my testing, this function does _not_ provoke
|
---|
471 | * "drive not ready" popups, even if the disk is not
|
---|
472 | * ready.
|
---|
473 | *
|
---|
474 | * Warning: This uses DosDevIOCtl, which has proved
|
---|
475 | * to cause problems with some device drivers for
|
---|
476 | * removeable disks.
|
---|
477 | *
|
---|
478 | * Returns:
|
---|
479 | *
|
---|
480 | * -- NO_ERROR: *pfFixed was set.
|
---|
481 | *
|
---|
482 | * -- ERROR_INVALID_DRIVE: drive letter invalid
|
---|
483 | *
|
---|
484 | * -- ERROR_NOT_SUPPORTED (50): for network drives.
|
---|
485 | *
|
---|
486 | *@@changed V0.9.14 (2001-08-03) [umoeller]: added extra fix for A: and B:
|
---|
487 | */
|
---|
488 |
|
---|
489 | APIRET doshIsFixedDisk(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
490 | PBOOL pfFixed) // out: TRUE for fixed disks
|
---|
491 | {
|
---|
492 | APIRET arc = ERROR_INVALID_DRIVE;
|
---|
493 |
|
---|
494 | if ( (ulLogicalDrive == 1)
|
---|
495 | || (ulLogicalDrive == 2)
|
---|
496 | )
|
---|
497 | {
|
---|
498 | // drive A: and B: can never be fixed V0.9.14 (2001-08-03) [umoeller]
|
---|
499 | *pfFixed = FALSE;
|
---|
500 | return NO_ERROR;
|
---|
501 | }
|
---|
502 |
|
---|
503 | if (ulLogicalDrive)
|
---|
504 | {
|
---|
505 | // parameter packet
|
---|
506 | #pragma pack(1)
|
---|
507 | struct {
|
---|
508 | UCHAR command,
|
---|
509 | drive;
|
---|
510 | } parms;
|
---|
511 | #pragma pack()
|
---|
512 |
|
---|
513 | // data packet
|
---|
514 | UCHAR ucNonRemoveable;
|
---|
515 |
|
---|
516 | parms.drive = (UCHAR)(ulLogicalDrive - 1);
|
---|
517 | if (!(arc = doshDevIOCtl((HFILE)-1,
|
---|
518 | IOCTL_DISK, // 0x08
|
---|
519 | DSK_BLOCKREMOVABLE, // 0x20
|
---|
520 | &parms, sizeof(parms),
|
---|
521 | &ucNonRemoveable, sizeof(ucNonRemoveable))))
|
---|
522 | *pfFixed = (BOOL)ucNonRemoveable;
|
---|
523 | }
|
---|
524 |
|
---|
525 | return arc;
|
---|
526 | }
|
---|
527 |
|
---|
528 | /*
|
---|
529 | *@@ doshQueryDiskParams:
|
---|
530 | * this retrieves more information about a given drive,
|
---|
531 | * which is stored in the specified BIOSPARAMETERBLOCK
|
---|
532 | * structure.
|
---|
533 | *
|
---|
534 | * BIOSPARAMETERBLOCK is defined in the Toolkit headers,
|
---|
535 | * and from my testing, it's the same with the Toolkits
|
---|
536 | * 3 and 4.5.
|
---|
537 | *
|
---|
538 | * If NO_ERROR is returned, the bDeviceType field can
|
---|
539 | * be one of the following (according to CPREF):
|
---|
540 | *
|
---|
541 | * -- 0: 48 TPI low-density diskette drive
|
---|
542 | * -- 1: 96 TPI high-density diskette drive
|
---|
543 | * -- 2: 3.5-inch 720KB diskette drive
|
---|
544 | * -- 3: 8-Inch single-density diskette drive
|
---|
545 | * -- 4: 8-Inch double-density diskette drive
|
---|
546 | * -- 5: Fixed disk
|
---|
547 | * -- 6: Tape drive
|
---|
548 | * -- 7: Other (includes 1.44MB 3.5-inch diskette drive)
|
---|
549 | * -- 8: R/W optical disk
|
---|
550 | * -- 9: 3.5-inch 4.0MB diskette drive (2.88MB formatted)
|
---|
551 | *
|
---|
552 | * From my testing, this function does _not_ provoke
|
---|
553 | * "drive not ready" popups, even if the disk is not
|
---|
554 | * ready.
|
---|
555 | *
|
---|
556 | * Warning: This uses DosDevIOCtl, which has proved
|
---|
557 | * to cause problems with some device drivers for
|
---|
558 | * removeable disks.
|
---|
559 | *
|
---|
560 | * This returns the DOS error code of DosDevIOCtl.
|
---|
561 | * This will be:
|
---|
562 | *
|
---|
563 | * -- NO_ERROR for all local disks;
|
---|
564 | *
|
---|
565 | * -- ERROR_NOT_SUPPORTED (50) for network drives.
|
---|
566 | *
|
---|
567 | *@@added V0.9.0 [umoeller]
|
---|
568 | *@@changed V0.9.13 (2001-06-14) [umoeller]: changed prototype to use BIOSPARAMETERBLOCK directly
|
---|
569 | *@@changed V0.9.13 (2001-06-14) [umoeller]: now querying standard media, no redetermine
|
---|
570 | */
|
---|
571 |
|
---|
572 | APIRET doshQueryDiskParams(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
573 | PBIOSPARAMETERBLOCK pdp) // out: drive parameters
|
---|
574 | {
|
---|
575 | APIRET arc = ERROR_INVALID_DRIVE;
|
---|
576 |
|
---|
577 | if (ulLogicalDrive)
|
---|
578 | {
|
---|
579 | #pragma pack(1)
|
---|
580 | // parameter packet
|
---|
581 | struct {
|
---|
582 | UCHAR ucCommand,
|
---|
583 | ucDrive;
|
---|
584 | } parms;
|
---|
585 | #pragma pack()
|
---|
586 |
|
---|
587 | parms.ucCommand = 0; // 0 = return standard media,
|
---|
588 | // 1 = read currently inserted media
|
---|
589 | // (1 doesn't work any more, returns arc 87
|
---|
590 | // V0.9.13 (2001-06-14) [umoeller])
|
---|
591 | parms.ucDrive=(UCHAR)(ulLogicalDrive-1);
|
---|
592 |
|
---|
593 | // zero the structure V0.9.13 (2001-06-14) [umoeller]
|
---|
594 | memset(pdp, 0, sizeof(BIOSPARAMETERBLOCK));
|
---|
595 |
|
---|
596 | arc = doshDevIOCtl((HFILE)-1,
|
---|
597 | IOCTL_DISK, // 0x08
|
---|
598 | DSK_GETDEVICEPARAMS, // 0x63
|
---|
599 | &parms, sizeof(parms),
|
---|
600 | pdp, sizeof(BIOSPARAMETERBLOCK));
|
---|
601 |
|
---|
602 | /* if (!arc)
|
---|
603 | {
|
---|
604 | _Pmpf((" bDeviceType: %d", pdp->bDeviceType));
|
---|
605 | _Pmpf((" bytes per sector: %d", pdp->usBytesPerSector));
|
---|
606 | _Pmpf((" sectors per track: %d", pdp->usSectorsPerTrack));
|
---|
607 | } */
|
---|
608 | }
|
---|
609 |
|
---|
610 | return arc;
|
---|
611 | }
|
---|
612 |
|
---|
613 | /*
|
---|
614 | *@@ doshQueryDriveType:
|
---|
615 | * tests the specified BIOSPARAMETERBLOCK
|
---|
616 | * for whether it represents a CD-ROM or
|
---|
617 | * some other removeable drive type.
|
---|
618 | *
|
---|
619 | * Returns one of:
|
---|
620 | *
|
---|
621 | * -- DRVTYPE_HARDDISK (0)
|
---|
622 | *
|
---|
623 | * -- DRVTYPE_PARTITIONABLEREMOVEABLE
|
---|
624 | *
|
---|
625 | * -- DRVTYPE_CDROM
|
---|
626 | *
|
---|
627 | * -- DRVTYPE_TAPE
|
---|
628 | *
|
---|
629 | * -- DRVTYPE_VDISK
|
---|
630 | *
|
---|
631 | * -- DRVTYPE_FLOPPY
|
---|
632 | *
|
---|
633 | * -- DRVTYPE_UNKNOWN (255)
|
---|
634 | *
|
---|
635 | * The BIOSPARAMETERBLOCK must be filled
|
---|
636 | * first using doshQueryDiskParams.
|
---|
637 | *
|
---|
638 | *@@added V0.9.16 (2002-01-13) [umoeller]
|
---|
639 | */
|
---|
640 |
|
---|
641 | BYTE doshQueryDriveType(ULONG ulLogicalDrive,
|
---|
642 | PBIOSPARAMETERBLOCK pdp,
|
---|
643 | BOOL fFixed)
|
---|
644 | {
|
---|
645 | if (pdp)
|
---|
646 | {
|
---|
647 | if (pdp->fsDeviceAttr & DEVATTR_PARTITIONALREMOVEABLE) // 0x08
|
---|
648 | return DRVTYPE_PRT;
|
---|
649 |
|
---|
650 | if (fFixed)
|
---|
651 | return DRVTYPE_HARDDISK;
|
---|
652 |
|
---|
653 | if ( (pdp->bDeviceType == 7) // "other"
|
---|
654 | && (pdp->usBytesPerSector == 2048)
|
---|
655 | && (pdp->usSectorsPerTrack == (USHORT)-1)
|
---|
656 | )
|
---|
657 | return DRVTYPE_CDROM;
|
---|
658 |
|
---|
659 | switch (pdp->bDeviceType)
|
---|
660 | {
|
---|
661 | case DEVTYPE_TAPE: // 6
|
---|
662 | return DRVTYPE_TAPE;
|
---|
663 |
|
---|
664 | case DEVTYPE_48TPI: // 0, 360k 5.25" floppy
|
---|
665 | case DEVTYPE_96TPI: // 1, 1.2M 5.25" floppy
|
---|
666 | case DEVTYPE_35: // 2, 720k 3.5" floppy
|
---|
667 | case DEVTYPE_OTHER: // 7, 1.44 3.5" floppy
|
---|
668 | // 1.84M 3.5" floppy
|
---|
669 | case DEVTYPE_35_288MB:
|
---|
670 | if ( (ulLogicalDrive == 1)
|
---|
671 | || (ulLogicalDrive == 2)
|
---|
672 | )
|
---|
673 | return DRVTYPE_FLOPPY;
|
---|
674 |
|
---|
675 | return DRVTYPE_VDISK;
|
---|
676 |
|
---|
677 | case DEVTYPE_RWOPTICAL: // 8, what is this?!?
|
---|
678 | return DRVTYPE_FLOPPY;
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | return DRVTYPE_UNKNOWN;
|
---|
683 | }
|
---|
684 |
|
---|
685 | /*
|
---|
686 | *@@ doshQueryCDDrives:
|
---|
687 | * returns the no. of CD-ROM drives on the system
|
---|
688 | * as well as the drive letter of the first
|
---|
689 | * CD-ROM drive.
|
---|
690 | *
|
---|
691 | *@@added V1.0.0 (2002-08-31) [umoeller]
|
---|
692 | */
|
---|
693 |
|
---|
694 | APIRET doshQueryCDDrives(PBYTE pcCDs, // out: CD-ROM drives count
|
---|
695 | PCHAR pcFirstCD) // out: drive letter of first CD
|
---|
696 | {
|
---|
697 | APIRET arc;
|
---|
698 | HFILE hfCDROM;
|
---|
699 | ULONG ulAction;
|
---|
700 |
|
---|
701 | if (!(arc = DosOpen("\\DEV\\CD-ROM2$",
|
---|
702 | &hfCDROM,
|
---|
703 | &ulAction,
|
---|
704 | 0,
|
---|
705 | FILE_NORMAL,
|
---|
706 | OPEN_ACTION_OPEN_IF_EXISTS,
|
---|
707 | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY,
|
---|
708 | NULL)))
|
---|
709 | {
|
---|
710 | struct
|
---|
711 | {
|
---|
712 | USHORT cCDs;
|
---|
713 | USHORT usFirstCD; // 0 == A:, 1 == B:, ...
|
---|
714 | } cdinfo;
|
---|
715 |
|
---|
716 | ULONG cb = sizeof(cdinfo);
|
---|
717 |
|
---|
718 | if (!(arc = DosDevIOCtl(hfCDROM,
|
---|
719 | 0x82,
|
---|
720 | 0x60,
|
---|
721 | NULL,
|
---|
722 | 0,
|
---|
723 | NULL,
|
---|
724 | &cdinfo,
|
---|
725 | cb,
|
---|
726 | &cb)))
|
---|
727 | {
|
---|
728 | *pcCDs = cdinfo.cCDs;
|
---|
729 | *pcFirstCD = cdinfo.usFirstCD + 'A';
|
---|
730 | }
|
---|
731 |
|
---|
732 | DosClose(hfCDROM);
|
---|
733 | }
|
---|
734 |
|
---|
735 | return arc;
|
---|
736 | }
|
---|
737 |
|
---|
738 | /*
|
---|
739 | *@@ doshOpenDrive:
|
---|
740 | * opens the given logical drive using
|
---|
741 | * DosOpen with OPEN_FLAGS_DASD. Use
|
---|
742 | * the file handle returned from here
|
---|
743 | * for doshHasAudioCD and doshQueryCDStatus.
|
---|
744 | *
|
---|
745 | * If NO_ERROR is returned, use DosClose
|
---|
746 | * to close the device again.
|
---|
747 | *
|
---|
748 | *@@added V1.0.0 (2002-08-31) [umoeller]
|
---|
749 | */
|
---|
750 |
|
---|
751 | APIRET doshOpenDrive(ULONG ulLogicalDrive,
|
---|
752 | HFILE *phf) // out: open drive's file handle
|
---|
753 | {
|
---|
754 | ULONG dummy;
|
---|
755 |
|
---|
756 | CHAR szDrive[] = "C:";
|
---|
757 | szDrive[0] = 'A' + ulLogicalDrive - 1;
|
---|
758 |
|
---|
759 | return DosOpen(szDrive, // "C:", "D:", ...
|
---|
760 | phf,
|
---|
761 | &dummy,
|
---|
762 | 0,
|
---|
763 | FILE_NORMAL,
|
---|
764 | // OPEN_ACTION_FAIL_IF_NEW
|
---|
765 | OPEN_ACTION_OPEN_IF_EXISTS,
|
---|
766 | OPEN_FLAGS_DASD
|
---|
767 | | OPEN_FLAGS_FAIL_ON_ERROR
|
---|
768 | // ^^^ if this flag is not set, we get the white
|
---|
769 | // hard-error box
|
---|
770 | | OPEN_FLAGS_NOINHERIT // V0.9.6 (2000-11-25) [pr]
|
---|
771 | // | OPEN_ACCESS_READONLY // V0.9.13 (2001-06-14) [umoeller]
|
---|
772 | | OPEN_SHARE_DENYNONE,
|
---|
773 | NULL);
|
---|
774 | }
|
---|
775 |
|
---|
776 | /*
|
---|
777 | *@@ doshHasAudioCD:
|
---|
778 | * sets *pfAudio to whether ulLogicalDrive
|
---|
779 | * currently has an audio CD inserted.
|
---|
780 | *
|
---|
781 | * Better call this only if you're sure that
|
---|
782 | * ulLogicalDrive is a CD-ROM drive. Use
|
---|
783 | * doshQueryRemoveableType to check.
|
---|
784 | *
|
---|
785 | *@@added V0.9.14 (2001-08-01) [umoeller]
|
---|
786 | *@@changed V1.0.0 (2002-08-31) [umoeller]: removed ulLogicalDrive which was not needed
|
---|
787 | */
|
---|
788 |
|
---|
789 | APIRET doshHasAudioCD(HFILE hfDrive, // in: DASD open
|
---|
790 | BOOL fMixedModeCD,
|
---|
791 | PBOOL pfAudio)
|
---|
792 | {
|
---|
793 | APIRET arc = NO_ERROR;
|
---|
794 |
|
---|
795 | ULONG ulAudioTracks = 0,
|
---|
796 | ulDataTracks = 0;
|
---|
797 |
|
---|
798 | CHAR cds1[4] = { 'C', 'D', '0', '1' };
|
---|
799 | CHAR cds2[4];
|
---|
800 |
|
---|
801 | *pfAudio = FALSE;
|
---|
802 |
|
---|
803 | // check for proper driver signature
|
---|
804 | if (!(arc = doshDevIOCtl(hfDrive,
|
---|
805 | IOCTL_CDROMDISK,
|
---|
806 | CDROMDISK_GETDRIVER,
|
---|
807 | &cds1, sizeof(cds1),
|
---|
808 | &cds2, sizeof(cds2))))
|
---|
809 | {
|
---|
810 | if (memcmp(&cds1, &cds2, 4))
|
---|
811 | // this is not a CD-ROM then:
|
---|
812 | arc = NO_ERROR;
|
---|
813 | else
|
---|
814 | {
|
---|
815 | #pragma pack(1) // V1.0.0 (2002-08-31) [umoeller]
|
---|
816 |
|
---|
817 | struct {
|
---|
818 | UCHAR ucFirstTrack,
|
---|
819 | ucLastTrack;
|
---|
820 | ULONG ulLeadOut;
|
---|
821 | } cdat;
|
---|
822 |
|
---|
823 | struct {
|
---|
824 | ULONG ulTrackAddress;
|
---|
825 | BYTE bFlags;
|
---|
826 | } trackdata;
|
---|
827 |
|
---|
828 | #pragma pack()
|
---|
829 |
|
---|
830 | // get track count
|
---|
831 | if (!(arc = doshDevIOCtl(hfDrive,
|
---|
832 | IOCTL_CDROMAUDIO,
|
---|
833 | CDROMAUDIO_GETAUDIODISK,
|
---|
834 | &cds1, sizeof(cds1),
|
---|
835 | &cdat, sizeof(cdat))))
|
---|
836 | {
|
---|
837 | // still no error: build the audio TOC
|
---|
838 | ULONG i;
|
---|
839 | for (i = cdat.ucFirstTrack;
|
---|
840 | i <= cdat.ucLastTrack;
|
---|
841 | i++)
|
---|
842 | {
|
---|
843 | BYTE cdtp[5] =
|
---|
844 | { 'C', 'D', '0', '1', (UCHAR)i };
|
---|
845 |
|
---|
846 | if (!(arc = doshDevIOCtl(hfDrive,
|
---|
847 | IOCTL_CDROMAUDIO,
|
---|
848 | CDROMAUDIO_GETAUDIOTRACK,
|
---|
849 | &cdtp, sizeof(cdtp),
|
---|
850 | &trackdata, sizeof(trackdata))))
|
---|
851 | {
|
---|
852 | if (trackdata.bFlags & 64)
|
---|
853 | ulDataTracks++;
|
---|
854 | else
|
---|
855 | {
|
---|
856 | ulAudioTracks++;
|
---|
857 |
|
---|
858 | if (!fMixedModeCD)
|
---|
859 | {
|
---|
860 | // caller doesn't want mixed mode:
|
---|
861 | // stop here
|
---|
862 | ulDataTracks = 0;
|
---|
863 | break;
|
---|
864 | }
|
---|
865 | }
|
---|
866 | }
|
---|
867 | }
|
---|
868 |
|
---|
869 | // _Pmpf((" got %d audio, %d data tracks",
|
---|
870 | // ulAudioTracks, ulDataTracks));
|
---|
871 |
|
---|
872 | if (!ulDataTracks)
|
---|
873 | *pfAudio = TRUE;
|
---|
874 | }
|
---|
875 | else
|
---|
876 | {
|
---|
877 | // not audio disk:
|
---|
878 | // go on then
|
---|
879 | // _Pmpf((" CDROMAUDIO_GETAUDIODISK returned %d", arc));
|
---|
880 | arc = NO_ERROR;
|
---|
881 | }
|
---|
882 | }
|
---|
883 | }
|
---|
884 | else
|
---|
885 | {
|
---|
886 | // not CD-ROM: go on then
|
---|
887 | // _Pmpf((" CDROMDISK_GETDRIVER returned %d", arc));
|
---|
888 | arc = NO_ERROR;
|
---|
889 | }
|
---|
890 |
|
---|
891 | return arc;
|
---|
892 | }
|
---|
893 |
|
---|
894 | /*
|
---|
895 | *@@ doshQueryCDStatus:
|
---|
896 | * returns the status bits of a CD-ROM drive.
|
---|
897 | * This calls the CDROMDISK_DEVICESTATUS
|
---|
898 | * ioctl.
|
---|
899 | *
|
---|
900 | * If NO_ERROR is returned, *pflStatus has
|
---|
901 | * received the following flags:
|
---|
902 | *
|
---|
903 | * -- CDFL_DOOROPEN (bit 0)
|
---|
904 | *
|
---|
905 | * -- CDFL_DOORLOCKED (bit 1)
|
---|
906 | *
|
---|
907 | * and many more (see dosh.h).
|
---|
908 | *
|
---|
909 | * Actually I wrote this function to have a way to
|
---|
910 | * find out whether the drive door is already open.
|
---|
911 | * But thanks to IBM's thoughtful design, this ioctl
|
---|
912 | * is 99% useless for that purpose since it requires
|
---|
913 | * a DASD disk handle to be passed in, which cannot
|
---|
914 | * be obtained if there's no media in the drive.
|
---|
915 | *
|
---|
916 | * In other words, it is absolutely impossible to
|
---|
917 | * ever get the CDFL_DOOROPEN flag, because if the
|
---|
918 | * door is open, DosOpen already fails on the drive.
|
---|
919 | * As a consequence, it is seems to be impossible
|
---|
920 | * to find out if the door is open with OS/2.
|
---|
921 | *
|
---|
922 | *@@added V1.0.0 (2002-08-31) [umoeller]
|
---|
923 | */
|
---|
924 |
|
---|
925 | APIRET doshQueryCDStatus(HFILE hfDrive, // in: DASD open
|
---|
926 | PULONG pflStatus) // out: CD-ROM status bits
|
---|
927 | {
|
---|
928 | APIRET arc;
|
---|
929 |
|
---|
930 | CHAR cds1[4] = { 'C', 'D', '0', '1' };
|
---|
931 | ULONG fl;
|
---|
932 |
|
---|
933 | *pflStatus = 0;
|
---|
934 |
|
---|
935 | if (!(arc = doshDevIOCtl(hfDrive,
|
---|
936 | IOCTL_CDROMDISK,
|
---|
937 | CDROMDISK_DEVICESTATUS,
|
---|
938 | &cds1, sizeof(cds1),
|
---|
939 | &fl, sizeof(fl))))
|
---|
940 | {
|
---|
941 | *pflStatus = fl;
|
---|
942 | }
|
---|
943 |
|
---|
944 | return arc;
|
---|
945 | }
|
---|
946 |
|
---|
947 | /*
|
---|
948 | *@@ doshEnumDrives:
|
---|
949 | * this function enumerates all valid drive letters on
|
---|
950 | * the system by composing a string of drive letters
|
---|
951 | * in the buffer pointed to by pszBuffer, which should
|
---|
952 | * be 27 characters in size to hold information for
|
---|
953 | * all drives. The buffer will be null-terminated.
|
---|
954 | *
|
---|
955 | * If (pcszFileSystem != NULL), only drives matching
|
---|
956 | * the specified file system type (e.g. "HPFS") will
|
---|
957 | * be enumerated. If (pcszFileSystem == NULL), all
|
---|
958 | * drives will be enumerated.
|
---|
959 | *
|
---|
960 | * If (fSkipRemovables == TRUE), removeable drives will
|
---|
961 | * be skipped. This applies to floppy, CD-ROM, and
|
---|
962 | * virtual floppy drives. This will start the search
|
---|
963 | * at drive letter C: so that drives A: and B: will
|
---|
964 | * never be checked (to avoid the hardware bumps).
|
---|
965 | *
|
---|
966 | * Otherwise, the search starts at drive A:. Still,
|
---|
967 | * removeable drives will only be added if valid media
|
---|
968 | * is inserted.
|
---|
969 | *
|
---|
970 | *@@changed V0.9.4 (2000-07-03) [umoeller]: this stopped at the first invalid drive letter; fixed
|
---|
971 | *@@changed V0.9.4 (2000-07-03) [umoeller]: added fSkipRemoveables
|
---|
972 | */
|
---|
973 |
|
---|
974 | VOID doshEnumDrives(PSZ pszBuffer, // out: drive letters
|
---|
975 | PCSZ pcszFileSystem, // in: FS's to match or NULL
|
---|
976 | BOOL fSkipRemoveables) // in: if TRUE, only non-removeable disks will be returned
|
---|
977 | {
|
---|
978 | CHAR szName[5] = "";
|
---|
979 | ULONG ulLogicalDrive = 1, // start with drive A:
|
---|
980 | ulFound = 0; // found drives count
|
---|
981 | APIRET arc = NO_ERROR; // return code
|
---|
982 |
|
---|
983 | if (fSkipRemoveables)
|
---|
984 | // start with drive C:
|
---|
985 | ulLogicalDrive = 3;
|
---|
986 |
|
---|
987 | // go thru the drives, start with C: (== 3), stop after Z: (== 26)
|
---|
988 | while (ulLogicalDrive <= 26)
|
---|
989 | {
|
---|
990 | #pragma pack(1)
|
---|
991 | struct
|
---|
992 | {
|
---|
993 | UCHAR dummy,drive;
|
---|
994 | } parms;
|
---|
995 | #pragma pack()
|
---|
996 |
|
---|
997 | // data packet
|
---|
998 | UCHAR nonRemovable=0;
|
---|
999 |
|
---|
1000 | parms.drive=(UCHAR)(ulLogicalDrive-1);
|
---|
1001 | arc = doshDevIOCtl((HFILE)-1,
|
---|
1002 | IOCTL_DISK,
|
---|
1003 | DSK_BLOCKREMOVABLE,
|
---|
1004 | &parms, sizeof(parms),
|
---|
1005 | &nonRemovable, sizeof(nonRemovable));
|
---|
1006 |
|
---|
1007 | if ( // fixed disk and non-removeable
|
---|
1008 | ((arc == NO_ERROR) && (nonRemovable))
|
---|
1009 | // or network drive:
|
---|
1010 | || (arc == ERROR_NOT_SUPPORTED)
|
---|
1011 | )
|
---|
1012 | {
|
---|
1013 | ULONG ulOrdinal = 0; // ordinal of entry in name list
|
---|
1014 | BYTE fsqBuffer[sizeof(FSQBUFFER2) + (3 * CCHMAXPATH)] = {0};
|
---|
1015 | ULONG cbBuffer = sizeof(fsqBuffer); // Buffer length)
|
---|
1016 | PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)fsqBuffer;
|
---|
1017 |
|
---|
1018 | szName[0] = ulLogicalDrive + 'A' - 1;
|
---|
1019 | szName[1] = ':';
|
---|
1020 | szName[2] = '\0';
|
---|
1021 |
|
---|
1022 | arc = DosQueryFSAttach(szName, // logical drive of attached FS
|
---|
1023 | ulOrdinal, // ignored for FSAIL_QUERYNAME
|
---|
1024 | FSAIL_QUERYNAME, // return data for a Drive or Device
|
---|
1025 | pfsqBuffer, // returned data
|
---|
1026 | &cbBuffer); // returned data length
|
---|
1027 |
|
---|
1028 | if (arc == NO_ERROR)
|
---|
1029 | {
|
---|
1030 | // The data for the last three fields in the FSQBUFFER2
|
---|
1031 | // structure are stored at the offset of fsqBuffer.szName.
|
---|
1032 | // Each data field following fsqBuffer.szName begins
|
---|
1033 | // immediately after the previous item.
|
---|
1034 | CHAR* pszFSDName = (PSZ)&(pfsqBuffer->szName) + (pfsqBuffer->cbName) + 1;
|
---|
1035 | if (pcszFileSystem == NULL)
|
---|
1036 | {
|
---|
1037 | // enum-all mode: always copy
|
---|
1038 | pszBuffer[ulFound] = szName[0]; // drive letter
|
---|
1039 | ulFound++;
|
---|
1040 | }
|
---|
1041 | else if (strcmp(pszFSDName, pcszFileSystem) == 0)
|
---|
1042 | {
|
---|
1043 | pszBuffer[ulFound] = szName[0]; // drive letter
|
---|
1044 | ulFound++;
|
---|
1045 | }
|
---|
1046 | }
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | ulLogicalDrive++;
|
---|
1050 | } // end while (G_acDriveLetters[ulLogicalDrive] <= 'Z')
|
---|
1051 |
|
---|
1052 | pszBuffer[ulFound] = '\0';
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | /*
|
---|
1056 | *@@ doshQueryBootDrive:
|
---|
1057 | * returns the letter of the boot drive as a
|
---|
1058 | * single (capital) character, which is useful for
|
---|
1059 | * constructing file names using sprintf and such.
|
---|
1060 | *
|
---|
1061 | *@@changed V0.9.16 (2002-01-13) [umoeller]: optimized
|
---|
1062 | */
|
---|
1063 |
|
---|
1064 | CHAR doshQueryBootDrive(VOID)
|
---|
1065 | {
|
---|
1066 | // this can never change, so query this only once
|
---|
1067 | // V0.9.16 (2002-01-13) [umoeller]
|
---|
1068 | static CHAR cBootDrive = '\0';
|
---|
1069 |
|
---|
1070 | if (!cBootDrive)
|
---|
1071 | {
|
---|
1072 | ULONG ulBootDrive;
|
---|
1073 | DosQuerySysInfo(QSV_BOOT_DRIVE, QSV_BOOT_DRIVE,
|
---|
1074 | &ulBootDrive,
|
---|
1075 | sizeof(ulBootDrive));
|
---|
1076 | cBootDrive = (CHAR)ulBootDrive + 'A' - 1;
|
---|
1077 | }
|
---|
1078 |
|
---|
1079 | return cBootDrive;
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 | /*
|
---|
1083 | *@@ doshQueryMedia:
|
---|
1084 | * determines whether the given drive currently
|
---|
1085 | * has media inserted.
|
---|
1086 | *
|
---|
1087 | * Call this only for non-fixed (removable) disks.
|
---|
1088 | * Use doshIsFixedDisk to find out.
|
---|
1089 | *
|
---|
1090 | * Returns:
|
---|
1091 | *
|
---|
1092 | * -- NO_ERROR: media is present.
|
---|
1093 | *
|
---|
1094 | * -- ERROR_AUDIO_CD_ROM (10000): audio CD-ROM is present.
|
---|
1095 | *
|
---|
1096 | * -- ERROR_NOT_READY (21) or other: drive has no media.
|
---|
1097 | *
|
---|
1098 | *@@added V0.9.16 (2002-01-13) [umoeller]
|
---|
1099 | */
|
---|
1100 |
|
---|
1101 | APIRET doshQueryMedia(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
1102 | BOOL fCDROM, // in: is drive CD-ROM?
|
---|
1103 | ULONG fl) // in: DRVFL_* flags
|
---|
1104 | {
|
---|
1105 | APIRET arc;
|
---|
1106 |
|
---|
1107 | HFILE hf;
|
---|
1108 |
|
---|
1109 | // exported this code to doshOpenDrive V1.0.0 (2002-08-31) [umoeller]
|
---|
1110 | arc = doshOpenDrive(ulLogicalDrive,
|
---|
1111 | &hf);
|
---|
1112 |
|
---|
1113 | // this still returns NO_ERROR for audio CDs in a
|
---|
1114 | // CD-ROM drive...
|
---|
1115 | // however, the WPS then attempts to read in the
|
---|
1116 | // root directory for audio CDs, which produces
|
---|
1117 | // a "sector not found" error box...
|
---|
1118 |
|
---|
1119 | if ( (!arc)
|
---|
1120 | && (hf)
|
---|
1121 | && (fCDROM)
|
---|
1122 | )
|
---|
1123 | {
|
---|
1124 | BOOL fAudio;
|
---|
1125 | if ( (!(arc = doshHasAudioCD(hf,
|
---|
1126 | ((fl & DRVFL_MIXEDMODECD) != 0),
|
---|
1127 | &fAudio)))
|
---|
1128 | && (fAudio)
|
---|
1129 | )
|
---|
1130 | arc = ERROR_AUDIO_CD_ROM; // special private error code (10000)
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | if (hf)
|
---|
1134 | DosClose(hf);
|
---|
1135 |
|
---|
1136 | return arc;
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | /*
|
---|
1140 | *@@ doshAssertDrive:
|
---|
1141 | * this checks for whether the given drive
|
---|
1142 | * is currently available without provoking
|
---|
1143 | * those ugly white "Drive not ready" popups.
|
---|
1144 | *
|
---|
1145 | * "fl" can specify additional flags for testing
|
---|
1146 | * and can be any combination of:
|
---|
1147 | *
|
---|
1148 | * -- DRVFL_MIXEDMODECD: whether to allow
|
---|
1149 | * mixed-mode CD-ROMs. See error codes below.
|
---|
1150 | *
|
---|
1151 | * This returns (from my testing):
|
---|
1152 | *
|
---|
1153 | * -- NO_ERROR: drive is available.
|
---|
1154 | *
|
---|
1155 | * -- ERROR_INVALID_DRIVE (15): drive letter does not exist.
|
---|
1156 | *
|
---|
1157 | * -- ERROR_NOT_READY (21): drive exists, but is not ready.
|
---|
1158 | * This is produced by floppies and CD-ROM drives
|
---|
1159 | * which do not have valid media inserted.
|
---|
1160 | *
|
---|
1161 | * -- ERROR_AUDIO_CD_ROM (10000): special error code returned
|
---|
1162 | * only by this function if a CD-ROM drive has audio
|
---|
1163 | * media inserted.
|
---|
1164 | *
|
---|
1165 | * If DRVFL_MIXEDMODECD was specified, ERROR_AUDIO_CD_ROM
|
---|
1166 | * is returned _only_ if _no_ data tracks are
|
---|
1167 | * present on a CD-ROM. Since OS/2 is not very
|
---|
1168 | * good at handling mixed-mode CDs, this might not
|
---|
1169 | * be desireable.
|
---|
1170 | *
|
---|
1171 | * If DRVFL_MIXEDMODECD was not set, ERROR_AUDIO_CD_ROM
|
---|
1172 | * will be returned already if _one_ audio track is present.
|
---|
1173 | *
|
---|
1174 | *@@changed V0.9.1 (99-12-13) [umoeller]: rewritten, prototype changed. Now using DosOpen on the drive instead of DosError.
|
---|
1175 | *@@changed V0.9.1 (2000-01-08) [umoeller]: DosClose was called even if DosOpen failed, which messed up OS/2 error handling.
|
---|
1176 | *@@changed V0.9.1 (2000-02-09) [umoeller]: this didn't work for network drives, including RAMFS; fixed.
|
---|
1177 | *@@changed V0.9.3 (2000-03-28) [umoeller]: added check for network drives, which weren't working
|
---|
1178 | *@@changed V0.9.4 (2000-08-03) [umoeller]: more network fixes
|
---|
1179 | *@@changed V0.9.9 (2001-03-19) [pr]: validate drive number
|
---|
1180 | *@@changed V0.9.11 (2001-04-23) [umoeller]: added an extra check for floppies
|
---|
1181 | *@@changed V0.9.13 (2001-06-14) [umoeller]: added "fl" parameter and lots of CD-ROM checks
|
---|
1182 | */
|
---|
1183 |
|
---|
1184 | APIRET doshAssertDrive(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
1185 | ULONG fl) // in: DRVFL_* flags
|
---|
1186 | {
|
---|
1187 | APIRET arc = NO_ERROR;
|
---|
1188 | BOOL fFixed = FALSE,
|
---|
1189 | fCDROM = FALSE;
|
---|
1190 |
|
---|
1191 | if ((ulLogicalDrive < 1) || (ulLogicalDrive > 26))
|
---|
1192 | return(ERROR_PATH_NOT_FOUND);
|
---|
1193 |
|
---|
1194 | arc = doshIsFixedDisk(ulLogicalDrive,
|
---|
1195 | &fFixed); // V0.9.13 (2001-06-14) [umoeller]
|
---|
1196 |
|
---|
1197 | // _Pmpf((__FUNCTION__ ": doshIsFixedDisk returned %d for disk %d", arc, ulLogicalDrive));
|
---|
1198 | // _Pmpf((" fFixed is %d", fFixed));
|
---|
1199 |
|
---|
1200 | if (!arc)
|
---|
1201 | if (!fFixed)
|
---|
1202 | {
|
---|
1203 | // removeable disk:
|
---|
1204 | // check if it's a CD-ROM
|
---|
1205 | BIOSPARAMETERBLOCK bpb;
|
---|
1206 | arc = doshQueryDiskParams(ulLogicalDrive,
|
---|
1207 | &bpb);
|
---|
1208 | // _Pmpf((" doshQueryDiskParams returned %d", arc));
|
---|
1209 |
|
---|
1210 | if ( (!arc)
|
---|
1211 | && (DRVTYPE_CDROM == doshQueryDriveType(ulLogicalDrive,
|
---|
1212 | &bpb,
|
---|
1213 | fFixed))
|
---|
1214 | )
|
---|
1215 | {
|
---|
1216 | // _Pmpf((" --> is CD-ROM"));
|
---|
1217 | fCDROM = TRUE;
|
---|
1218 | }
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | if (!arc)
|
---|
1222 | arc = doshQueryMedia(ulLogicalDrive,
|
---|
1223 | fCDROM,
|
---|
1224 | fl);
|
---|
1225 |
|
---|
1226 | switch (arc)
|
---|
1227 | {
|
---|
1228 | case ERROR_NETWORK_ACCESS_DENIED: // 65
|
---|
1229 | // added V0.9.3 (2000-03-27) [umoeller];
|
---|
1230 | // according to user reports, this is returned
|
---|
1231 | // by all network drives, which apparently don't
|
---|
1232 | // support DASD DosOpen
|
---|
1233 | case ERROR_ACCESS_DENIED: // 5
|
---|
1234 | // added V0.9.4 (2000-07-10) [umoeller]
|
---|
1235 | // LAN drives still didn't work... apparently
|
---|
1236 | // the above only works for NFS drives
|
---|
1237 | case ERROR_PATH_NOT_FOUND: // 3
|
---|
1238 | // added V0.9.4 (2000-08-03) [umoeller]:
|
---|
1239 | // this is returned by some other network types...
|
---|
1240 | // sigh...
|
---|
1241 | case ERROR_NOT_SUPPORTED: // 50
|
---|
1242 | // this is returned by file systems which don't
|
---|
1243 | // support DASD DosOpen;
|
---|
1244 | // use some other method then, this isn't likely
|
---|
1245 | // to fail -- V0.9.1 (2000-02-09) [umoeller]
|
---|
1246 |
|
---|
1247 | // but don't do this for floppies
|
---|
1248 | // V0.9.11 (2001-04-23) [umoeller]
|
---|
1249 | if (ulLogicalDrive > 2)
|
---|
1250 | {
|
---|
1251 | FSALLOCATE fsa;
|
---|
1252 | arc = DosQueryFSInfo(ulLogicalDrive,
|
---|
1253 | FSIL_ALLOC,
|
---|
1254 | &fsa,
|
---|
1255 | sizeof(fsa));
|
---|
1256 | // _Pmpf((" re-checked, DosQueryFSInfo returned %d", arc));
|
---|
1257 | }
|
---|
1258 | break;
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | return arc;
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 | /*
|
---|
1265 | *@@ doshGetDriveInfo:
|
---|
1266 | * fills the given XDISKINFO buffer with
|
---|
1267 | * information about the given logical drive.
|
---|
1268 | *
|
---|
1269 | * This function will not provoke "Drive not
|
---|
1270 | * ready" popups, hopefully.
|
---|
1271 | *
|
---|
1272 | * fl can be any combination of the following:
|
---|
1273 | *
|
---|
1274 | * -- DRVFL_MIXEDMODECD: see doshAssertDrive.
|
---|
1275 | *
|
---|
1276 | * -- DRVFL_TOUCHFLOPPIES: drive A: and B: should
|
---|
1277 | * be touched for media checks (click, click);
|
---|
1278 | * otherwise they will be left alone and
|
---|
1279 | * default values will be returned.
|
---|
1280 | *
|
---|
1281 | * -- DRVFL_CHECKEAS: drive should always be
|
---|
1282 | * checked for EA support. If this is set,
|
---|
1283 | * we will call DosFSCtl for the non-well-known
|
---|
1284 | * file systems so we will always have a
|
---|
1285 | * value for the DFL_SUPPORTS_EAS flags.
|
---|
1286 | * The EA support returned by DosFSCtl
|
---|
1287 | * might not be correct for remote file
|
---|
1288 | * systems since not all of them support
|
---|
1289 | * that query.
|
---|
1290 | *
|
---|
1291 | * If not set, we set DFL_SUPPORTS_EAS only
|
---|
1292 | * for file systems such as HPFS and JFS
|
---|
1293 | * that are known to support EAs.
|
---|
1294 | *
|
---|
1295 | * -- DRVFL_CHECKLONGNAMES: drive should be
|
---|
1296 | * tested for longname support. If this is
|
---|
1297 | * set, we will try a DosOpen("\\long.name.file")
|
---|
1298 | * on the drive to see if it supports long
|
---|
1299 | * filenames (unless it's a "well-known"
|
---|
1300 | * file-system and we know it does). If enabled,
|
---|
1301 | * the DFL_SUPPORTS_LONGNAMES flag is reliable.
|
---|
1302 | * Note that this does not check for what special
|
---|
1303 | * characters are supported in file names.
|
---|
1304 | *
|
---|
1305 | * This should return only one of the following:
|
---|
1306 | *
|
---|
1307 | * -- NO_ERROR: disk info was filled, but not
|
---|
1308 | * necessarily all info was available (e.g.
|
---|
1309 | * if no media was present in CD-ROM drive).
|
---|
1310 | * See remarks below.
|
---|
1311 | *
|
---|
1312 | * -- ERROR_INVALID_DRIVE 15): ulLogicalDrive
|
---|
1313 | * is not used at all (invalid drive letter)
|
---|
1314 | *
|
---|
1315 | * -- ERROR_BAD_UNIT (20): if drive was renamed for
|
---|
1316 | * some reason (according to user reports
|
---|
1317 | *
|
---|
1318 | * -- ERROR_NOT_READY (21): for ZIP disks where
|
---|
1319 | * no media is inserted, depending on the
|
---|
1320 | * driver apparently... normally ZIP drive
|
---|
1321 | * letters should disappear when no media
|
---|
1322 | * is present
|
---|
1323 | *
|
---|
1324 | * -- ERROR_DRIVE_LOCKED (108)
|
---|
1325 | *
|
---|
1326 | * So in order to check whether a drive is present
|
---|
1327 | * and available, use this function as follows:
|
---|
1328 | *
|
---|
1329 | * 1) Call this function and check whether it
|
---|
1330 | * returns NO_ERROR for the given drive.
|
---|
1331 | * This will rule out invalid drive letters
|
---|
1332 | * and drives that are presently locked by
|
---|
1333 | * CHKDSK or something.
|
---|
1334 | *
|
---|
1335 | * 2) If so, check whether XDISKINFO.flDevice
|
---|
1336 | * has the DFL_MEDIA_PRESENT flag set.
|
---|
1337 | * This will rule out removeable drives without
|
---|
1338 | * media and unformatted hard disks.
|
---|
1339 | *
|
---|
1340 | * 3) If so, you can test the other fields if
|
---|
1341 | * you need more information. For example,
|
---|
1342 | * it would not be a good idea to create
|
---|
1343 | * a new file if the bType field is
|
---|
1344 | * DRVTYPE_CDROM.
|
---|
1345 | *
|
---|
1346 | * If you want to exclude removeable disks,
|
---|
1347 | * instead of checking bType, you should
|
---|
1348 | * rather check flDevice for the DFL_FIXED
|
---|
1349 | * flag, which will be set for ZIP drives also.
|
---|
1350 | *
|
---|
1351 | * Remarks for special drive types:
|
---|
1352 | *
|
---|
1353 | * -- Hard disks always have bType == DRVTYPE_HARDDISK.
|
---|
1354 | * For them, we always check the file system.
|
---|
1355 | * If this is reported as "UNKNOWN", this means
|
---|
1356 | * that the drive is unformatted or formatted
|
---|
1357 | * with a file system that OS/2 does not understand
|
---|
1358 | * (e.g. NTFS). Only in that case, flDevice
|
---|
1359 | * has the DFL_MEDIA_PRESENT bit clear.
|
---|
1360 | *
|
---|
1361 | * DFL_FIXED is always set.
|
---|
1362 | *
|
---|
1363 | * -- Remote (LAN) drives always have bType == DRVTYPE_LAN.
|
---|
1364 | * flDevice will always have the DFL_REMOTE and
|
---|
1365 | * DFL_MEDIA_PRESENT bits set.
|
---|
1366 | *
|
---|
1367 | * -- ZIP disks will have bType == DRVTYPE_PARTITIONABLEREMOVEABLE.
|
---|
1368 | * For them, flDevice will have both the
|
---|
1369 | * and DFL_PARTITIONABLEREMOVEABLE and DFL_FIXED
|
---|
1370 | * bits set.
|
---|
1371 | *
|
---|
1372 | * ZIP disks are a bit special because they are
|
---|
1373 | * dynamically mounted and unmounted when media
|
---|
1374 | * is inserted and removed. In other words, if
|
---|
1375 | * no media is present, the drive letter becomes
|
---|
1376 | * invalid.
|
---|
1377 | *
|
---|
1378 | * -- CD-ROM and DVD drives and CD writers will always
|
---|
1379 | * be reported as DRVTYPE_CDROM. The DFL_FIXED bit
|
---|
1380 | * will be clear always. For them, always check the
|
---|
1381 | * DFL_MEDIA_PRESENT present bit to avoid "Drive not
|
---|
1382 | * ready" popups.
|
---|
1383 | *
|
---|
1384 | * As a special goody, we can also determine if the
|
---|
1385 | * drive currently has audio media inserted (which
|
---|
1386 | * would provoke errors also), by setting the
|
---|
1387 | * DFL_AUDIO_CD bit.
|
---|
1388 | *
|
---|
1389 | *@@added V0.9.16 (2002-01-13) [umoeller]
|
---|
1390 | *@@changed V0.9.19 (2002-04-25) [umoeller]: added CDWFS (RSJ CD-Writer)
|
---|
1391 | */
|
---|
1392 |
|
---|
1393 | APIRET doshGetDriveInfo(ULONG ulLogicalDrive,
|
---|
1394 | ULONG fl, // in: DRVFL_* flags
|
---|
1395 | PXDISKINFO pdi)
|
---|
1396 | {
|
---|
1397 | APIRET arc = NO_ERROR;
|
---|
1398 |
|
---|
1399 | HFILE hf;
|
---|
1400 | ULONG dummy;
|
---|
1401 | BOOL fCheck = TRUE,
|
---|
1402 | fCheckFS = FALSE,
|
---|
1403 | fCheckLongnames = FALSE,
|
---|
1404 | fCheckEAs = FALSE;
|
---|
1405 |
|
---|
1406 | memset(pdi, 0, sizeof(XDISKINFO));
|
---|
1407 |
|
---|
1408 | pdi->cDriveLetter = 'A' + ulLogicalDrive - 1;
|
---|
1409 | pdi->cLogicalDrive = ulLogicalDrive;
|
---|
1410 |
|
---|
1411 | pdi->bType = DRVTYPE_UNKNOWN;
|
---|
1412 | pdi->fPresent = TRUE; // for now
|
---|
1413 |
|
---|
1414 | if ( (ulLogicalDrive == 1)
|
---|
1415 | || (ulLogicalDrive == 2)
|
---|
1416 | )
|
---|
1417 | {
|
---|
1418 | // drive A: and B: are special cases,
|
---|
1419 | // we don't even want to touch them (click, click)
|
---|
1420 | pdi->bType = DRVTYPE_FLOPPY;
|
---|
1421 |
|
---|
1422 | if (0 == (fl & DRVFL_TOUCHFLOPPIES))
|
---|
1423 | {
|
---|
1424 | fCheck = FALSE;
|
---|
1425 | // these support EAs too
|
---|
1426 | pdi->flDevice = DFL_MEDIA_PRESENT | DFL_SUPPORTS_EAS;
|
---|
1427 | strcpy(pdi->szFileSystem, "FAT");
|
---|
1428 | pdi->lFileSystem = FSYS_FAT;
|
---|
1429 | }
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 | if (fCheck)
|
---|
1433 | {
|
---|
1434 | // any other drive:
|
---|
1435 | // check if it's removeable first
|
---|
1436 | BOOL fFixed = FALSE;
|
---|
1437 | arc = doshIsFixedDisk(ulLogicalDrive,
|
---|
1438 | &fFixed);
|
---|
1439 |
|
---|
1440 | switch (arc)
|
---|
1441 | {
|
---|
1442 | case ERROR_INVALID_DRIVE:
|
---|
1443 | // drive letter doesn't exist at all:
|
---|
1444 | pdi->fPresent = FALSE;
|
---|
1445 | // return this APIRET
|
---|
1446 | break;
|
---|
1447 |
|
---|
1448 | case ERROR_NOT_SUPPORTED: // 50 for network drives
|
---|
1449 | // we get this for remote drives added
|
---|
1450 | // via "net use", so set these flags
|
---|
1451 | pdi->bType = DRVTYPE_LAN;
|
---|
1452 | pdi->lFileSystem = FSYS_REMOTE;
|
---|
1453 | pdi->flDevice |= DFL_REMOTE | DFL_MEDIA_PRESENT;
|
---|
1454 | // but still check what file-system we
|
---|
1455 | // have and whether longnames are supported
|
---|
1456 | fCheckFS = TRUE;
|
---|
1457 | fCheckLongnames = TRUE;
|
---|
1458 | fCheckEAs = TRUE;
|
---|
1459 | break;
|
---|
1460 |
|
---|
1461 | case NO_ERROR:
|
---|
1462 | {
|
---|
1463 | if (fFixed)
|
---|
1464 | {
|
---|
1465 | // fixed drive:
|
---|
1466 | pdi->flDevice |= DFL_FIXED | DFL_MEDIA_PRESENT;
|
---|
1467 |
|
---|
1468 | fCheckFS = TRUE;
|
---|
1469 | fCheckLongnames = TRUE;
|
---|
1470 | fCheckEAs = TRUE;
|
---|
1471 | }
|
---|
1472 |
|
---|
1473 | if (!(arc = doshQueryDiskParams(ulLogicalDrive,
|
---|
1474 | &pdi->bpb)))
|
---|
1475 | {
|
---|
1476 | BYTE bTemp = doshQueryDriveType(ulLogicalDrive,
|
---|
1477 | &pdi->bpb,
|
---|
1478 | fFixed);
|
---|
1479 | if (bTemp != DRVTYPE_UNKNOWN)
|
---|
1480 | {
|
---|
1481 | // recognized: store it then
|
---|
1482 | pdi->bType = bTemp;
|
---|
1483 |
|
---|
1484 | if (bTemp == DRVTYPE_PRT)
|
---|
1485 | pdi->flDevice |= DFL_FIXED
|
---|
1486 | | DFL_PARTITIONABLEREMOVEABLE;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | if (!fFixed)
|
---|
1490 | {
|
---|
1491 | // removeable:
|
---|
1492 |
|
---|
1493 | // before checking the drive, try if we have media
|
---|
1494 | if (!(arc = doshQueryMedia(ulLogicalDrive,
|
---|
1495 | (pdi->bType == DRVTYPE_CDROM),
|
---|
1496 | fl)))
|
---|
1497 | {
|
---|
1498 | pdi->flDevice |= DFL_MEDIA_PRESENT;
|
---|
1499 | fCheckFS = TRUE;
|
---|
1500 | fCheckLongnames = TRUE;
|
---|
1501 | // but never EAs
|
---|
1502 | }
|
---|
1503 | else if (arc == ERROR_AUDIO_CD_ROM)
|
---|
1504 | {
|
---|
1505 | pdi->flDevice |= DFL_AUDIO_CD;
|
---|
1506 | // do not check longnames and file-system
|
---|
1507 | }
|
---|
1508 | else
|
---|
1509 | pdi->arcQueryMedia = arc;
|
---|
1510 |
|
---|
1511 | arc = NO_ERROR;
|
---|
1512 | }
|
---|
1513 | }
|
---|
1514 | else
|
---|
1515 | pdi->arcQueryDiskParams = arc;
|
---|
1516 | }
|
---|
1517 | break;
|
---|
1518 |
|
---|
1519 | default:
|
---|
1520 | pdi->arcIsFixedDisk = arc;
|
---|
1521 | // and return this
|
---|
1522 | break;
|
---|
1523 |
|
---|
1524 | } // end swich arc = doshIsFixedDisk(ulLogicalDrive, &fFixed);
|
---|
1525 | }
|
---|
1526 |
|
---|
1527 | if (fCheckFS)
|
---|
1528 | {
|
---|
1529 | // TRUE only for local fixed disks or
|
---|
1530 | // remote drives or if media was present above
|
---|
1531 | if (!(arc = doshQueryDiskFSType(ulLogicalDrive,
|
---|
1532 | pdi->szFileSystem,
|
---|
1533 | sizeof(pdi->szFileSystem))))
|
---|
1534 | {
|
---|
1535 | if (!stricmp(pdi->szFileSystem, "UNKNOWN"))
|
---|
1536 | {
|
---|
1537 | // this is returned by the stupid DosQueryFSAttach
|
---|
1538 | // if the file system is not recognized by OS/2,
|
---|
1539 | // or if the drive is unformatted
|
---|
1540 | pdi->lFileSystem = FSYS_UNKNOWN;
|
---|
1541 | pdi->flDevice &= ~DFL_MEDIA_PRESENT;
|
---|
1542 | fCheckLongnames = FALSE;
|
---|
1543 | fCheckEAs = FALSE;
|
---|
1544 | // should we return ERROR_NOT_DOS_DISK (26)
|
---|
1545 | // in this case?
|
---|
1546 | }
|
---|
1547 | else if (!stricmp(pdi->szFileSystem, "FAT"))
|
---|
1548 | {
|
---|
1549 | pdi->lFileSystem = FSYS_FAT;
|
---|
1550 | pdi->flDevice |= DFL_SUPPORTS_EAS;
|
---|
1551 | fCheckLongnames = FALSE;
|
---|
1552 | fCheckEAs = FALSE;
|
---|
1553 | }
|
---|
1554 | else if ( (!stricmp(pdi->szFileSystem, "HPFS"))
|
---|
1555 | || (!stricmp(pdi->szFileSystem, "JFS"))
|
---|
1556 | )
|
---|
1557 | {
|
---|
1558 | pdi->lFileSystem = FSYS_HPFS_JFS;
|
---|
1559 | pdi->flDevice |= DFL_SUPPORTS_EAS | DFL_SUPPORTS_LONGNAMES;
|
---|
1560 | fCheckLongnames = FALSE;
|
---|
1561 | fCheckEAs = FALSE;
|
---|
1562 | }
|
---|
1563 | else if (!stricmp(pdi->szFileSystem, "CDFS"))
|
---|
1564 | pdi->lFileSystem = FSYS_CDFS;
|
---|
1565 | else if ( (!stricmp(pdi->szFileSystem, "FAT32"))
|
---|
1566 | || (!stricmp(pdi->szFileSystem, "ext2"))
|
---|
1567 | )
|
---|
1568 | {
|
---|
1569 | pdi->lFileSystem = FSYS_FAT32_EXT2;
|
---|
1570 | fCheckLongnames = TRUE;
|
---|
1571 | fCheckEAs = TRUE;
|
---|
1572 | }
|
---|
1573 | else if (!stricmp(pdi->szFileSystem, "RAMFS"))
|
---|
1574 | {
|
---|
1575 | pdi->lFileSystem = FSYS_RAMFS;
|
---|
1576 | pdi->flDevice |= DFL_SUPPORTS_EAS | DFL_SUPPORTS_LONGNAMES;
|
---|
1577 | fCheckLongnames = FALSE;
|
---|
1578 | fCheckEAs = FALSE;
|
---|
1579 | }
|
---|
1580 | else if (!stricmp(pdi->szFileSystem, "TVFS"))
|
---|
1581 | {
|
---|
1582 | pdi->lFileSystem = FSYS_TVFS;
|
---|
1583 | fCheckLongnames = TRUE;
|
---|
1584 | fCheckEAs = TRUE;
|
---|
1585 | }
|
---|
1586 | else if (!stricmp(pdi->szFileSystem, "CDWFS"))
|
---|
1587 | // V0.9.19 (2002-04-25) [umoeller]
|
---|
1588 | {
|
---|
1589 | pdi->lFileSystem = FSYS_CDWFS;
|
---|
1590 | pdi->flDevice |= DFL_SUPPORTS_LONGNAMES;
|
---|
1591 | fCheckLongnames = FALSE;
|
---|
1592 | fCheckEAs = FALSE;
|
---|
1593 | }
|
---|
1594 | }
|
---|
1595 | else
|
---|
1596 | // store negative error code
|
---|
1597 | pdi->lFileSystem = -(LONG)arc;
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 | if ( (!arc)
|
---|
1601 | && (fCheckLongnames)
|
---|
1602 | && (fl & DRVFL_CHECKLONGNAMES)
|
---|
1603 | )
|
---|
1604 | {
|
---|
1605 | CHAR szTemp[30] = "?:\\long.name.file";
|
---|
1606 | szTemp[0] = ulLogicalDrive + 'A' - 1;
|
---|
1607 | if (!(arc = DosOpen(szTemp,
|
---|
1608 | &hf,
|
---|
1609 | &dummy,
|
---|
1610 | 0,
|
---|
1611 | 0,
|
---|
1612 | FILE_READONLY,
|
---|
1613 | OPEN_SHARE_DENYNONE | OPEN_FLAGS_NOINHERIT,
|
---|
1614 | 0)))
|
---|
1615 | {
|
---|
1616 | DosClose(hf);
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 | switch (arc)
|
---|
1620 | {
|
---|
1621 | case NO_ERROR:
|
---|
1622 | case ERROR_OPEN_FAILED:
|
---|
1623 | case ERROR_FILE_NOT_FOUND: // returned by TVFS
|
---|
1624 | pdi->flDevice |= DFL_SUPPORTS_LONGNAMES;
|
---|
1625 | break;
|
---|
1626 |
|
---|
1627 | // if longnames are not supported,
|
---|
1628 | // we get ERROR_INVALID_NAME
|
---|
1629 | default:
|
---|
1630 | pdi->arcOpenLongnames = arc;
|
---|
1631 | break;
|
---|
1632 |
|
---|
1633 | // default:
|
---|
1634 | // printf(" drive %d returned %d\n", ulLogicalDrive, arc);
|
---|
1635 | }
|
---|
1636 |
|
---|
1637 | arc = NO_ERROR;
|
---|
1638 | }
|
---|
1639 |
|
---|
1640 | if ( (!arc)
|
---|
1641 | && (fCheckEAs)
|
---|
1642 | && (fl & DRVFL_CHECKEAS)
|
---|
1643 | )
|
---|
1644 | {
|
---|
1645 | EASIZEBUF easb = {0};
|
---|
1646 | ULONG cbData = sizeof(easb),
|
---|
1647 | cbParams = 0;
|
---|
1648 | CHAR szDrive[] = "?:\\";
|
---|
1649 | szDrive[0] = pdi->cDriveLetter;
|
---|
1650 | if (!(arc = DosFSCtl(&easb,
|
---|
1651 | cbData,
|
---|
1652 | &cbData,
|
---|
1653 | NULL, // params,
|
---|
1654 | cbParams,
|
---|
1655 | &cbParams,
|
---|
1656 | FSCTL_MAX_EASIZE,
|
---|
1657 | szDrive,
|
---|
1658 | -1, // HFILE
|
---|
1659 | FSCTL_PATHNAME)))
|
---|
1660 | if (easb.cbMaxEASize != 0)
|
---|
1661 | // the other field (cbMaxEAListSize) is 0 always, I think
|
---|
1662 | pdi->flDevice |= DFL_SUPPORTS_EAS;
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | if (doshQueryBootDrive() == pdi->cDriveLetter)
|
---|
1666 | pdi->flDevice |= DFL_BOOTDRIVE;
|
---|
1667 |
|
---|
1668 | return arc;
|
---|
1669 | }
|
---|
1670 |
|
---|
1671 | /*
|
---|
1672 | *@@ doshSetLogicalMap:
|
---|
1673 | * sets the mapping of logical floppy drives onto a single
|
---|
1674 | * physical floppy drive.
|
---|
1675 | * This means selecting either drive A: or drive B: to refer
|
---|
1676 | * to the physical drive.
|
---|
1677 | *
|
---|
1678 | * Paul explained this to me as follows:
|
---|
1679 | *
|
---|
1680 | * "It is really very simple - in a single physical floppy
|
---|
1681 | * drive system, you still have 2 logical floppy drives
|
---|
1682 | * A: and B:. This was primarily to support disk copying
|
---|
1683 | * e.g. diskcopy a: b: on a single floppy system without
|
---|
1684 | * having to rewrite applications. Whenever the application
|
---|
1685 | * accessed the other logical drive, the user would get a
|
---|
1686 | * prompt from the OS to swap disks.
|
---|
1687 | *
|
---|
1688 | * "These calls allow applications to bypass the prompt by
|
---|
1689 | * doing the mapping themselves. They just get/set which
|
---|
1690 | * logical drive is currently mapped to the physical drive.
|
---|
1691 | * This concept existed in DOS as well although the specifics
|
---|
1692 | * of how it was done escape me now.... actually I just
|
---|
1693 | * looked it up - the byte at 0:504h in low memory on
|
---|
1694 | * DOS controlled this (it doesn't work in VDMs)."
|
---|
1695 | *
|
---|
1696 | *@@added V0.9.6 (2000-11-24) [pr]
|
---|
1697 | */
|
---|
1698 |
|
---|
1699 | APIRET doshSetLogicalMap(ULONG ulLogicalDrive)
|
---|
1700 | {
|
---|
1701 | CHAR name[3] = "?:";
|
---|
1702 | ULONG fd = 0,
|
---|
1703 | action = 0;
|
---|
1704 | // paramsize = 0;
|
---|
1705 | // datasize = 0;
|
---|
1706 | APIRET rc = NO_ERROR;
|
---|
1707 | USHORT data,
|
---|
1708 | param;
|
---|
1709 |
|
---|
1710 | name[0] = doshQueryBootDrive();
|
---|
1711 | rc = DosOpen(name,
|
---|
1712 | &fd,
|
---|
1713 | &action,
|
---|
1714 | 0,
|
---|
1715 | 0,
|
---|
1716 | OPEN_ACTION_FAIL_IF_NEW
|
---|
1717 | | OPEN_ACTION_OPEN_IF_EXISTS,
|
---|
1718 | OPEN_FLAGS_DASD
|
---|
1719 | | OPEN_FLAGS_FAIL_ON_ERROR
|
---|
1720 | | OPEN_FLAGS_NOINHERIT
|
---|
1721 | | OPEN_ACCESS_READONLY
|
---|
1722 | | OPEN_SHARE_DENYNONE,
|
---|
1723 | 0);
|
---|
1724 |
|
---|
1725 | if (rc == NO_ERROR)
|
---|
1726 | {
|
---|
1727 | param = 0;
|
---|
1728 | data = (USHORT)ulLogicalDrive;
|
---|
1729 | // paramsize = sizeof(param);
|
---|
1730 | // datasize = sizeof(data);
|
---|
1731 | rc = doshDevIOCtl(fd,
|
---|
1732 | IOCTL_DISK, DSK_SETLOGICALMAP,
|
---|
1733 | ¶m, sizeof(param),
|
---|
1734 | &data, sizeof(data));
|
---|
1735 | DosClose(fd);
|
---|
1736 | }
|
---|
1737 |
|
---|
1738 | return(rc);
|
---|
1739 | }
|
---|
1740 |
|
---|
1741 | /*
|
---|
1742 | *@@ doshQueryDiskSize:
|
---|
1743 | * returns the size of the specified disk in bytes.
|
---|
1744 | *
|
---|
1745 | * Note: This returns a "double" value, because a ULONG
|
---|
1746 | * can only hold values of some 4 billion, which would
|
---|
1747 | * lead to funny results for drives > 4 GB.
|
---|
1748 | *
|
---|
1749 | *@@added V0.9.11 (2001-04-18) [umoeller]
|
---|
1750 | */
|
---|
1751 |
|
---|
1752 | APIRET doshQueryDiskSize(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
1753 | double *pdSize)
|
---|
1754 | {
|
---|
1755 | APIRET arc = NO_ERROR;
|
---|
1756 | FSALLOCATE fsa;
|
---|
1757 | // double dbl = -1;
|
---|
1758 |
|
---|
1759 | if (!(arc = DosQueryFSInfo(ulLogicalDrive, FSIL_ALLOC, &fsa, sizeof(fsa))))
|
---|
1760 | *pdSize = ((double)fsa.cSectorUnit * fsa.cbSector * fsa.cUnit);
|
---|
1761 |
|
---|
1762 | return arc;
|
---|
1763 | }
|
---|
1764 |
|
---|
1765 | /*
|
---|
1766 | *@@ doshQueryDiskFree:
|
---|
1767 | * returns the number of bytes remaining on the disk
|
---|
1768 | * specified by the given logical drive.
|
---|
1769 | *
|
---|
1770 | * Note: This returns a "double" value, because a ULONG
|
---|
1771 | * can only hold values of some 4 billion, which would
|
---|
1772 | * lead to funny results for drives > 4 GB.
|
---|
1773 | *
|
---|
1774 | *@@changed V0.9.0 [umoeller]: fixed another > 4 GB bug (thanks to Rdiger Ihle)
|
---|
1775 | *@@changed V0.9.7 (2000-12-01) [umoeller]: changed prototype
|
---|
1776 | */
|
---|
1777 |
|
---|
1778 | APIRET doshQueryDiskFree(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
1779 | double *pdFree)
|
---|
1780 | {
|
---|
1781 | APIRET arc = NO_ERROR;
|
---|
1782 | FSALLOCATE fsa;
|
---|
1783 | // double dbl = -1;
|
---|
1784 |
|
---|
1785 | if (!(arc = DosQueryFSInfo(ulLogicalDrive, FSIL_ALLOC, &fsa, sizeof(fsa))))
|
---|
1786 | *pdFree = ((double)fsa.cSectorUnit * fsa.cbSector * fsa.cUnitAvail);
|
---|
1787 | // ^ fixed V0.9.0
|
---|
1788 |
|
---|
1789 | return arc;
|
---|
1790 | }
|
---|
1791 |
|
---|
1792 | /*
|
---|
1793 | *@@ doshQueryDiskFSType:
|
---|
1794 | * copies the file-system type of the given disk object
|
---|
1795 | * (HPFS, FAT, CDFS etc.) to pszBuf.
|
---|
1796 | * Returns the DOS error code.
|
---|
1797 | *
|
---|
1798 | *@@changed V0.9.1 (99-12-12) [umoeller]: added cbBuf to prototype
|
---|
1799 | *@@changed V0.9.14 (2001-08-01) [umoeller]: fixed, this never respected cbBuf
|
---|
1800 | *@@changed V0.9.16 (2001-10-02) [umoeller]: added check for valid logical disk no
|
---|
1801 | */
|
---|
1802 |
|
---|
1803 | APIRET doshQueryDiskFSType(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
1804 | PSZ pszBuf, // out: buffer for FS type
|
---|
1805 | ULONG cbBuf) // in: size of that buffer
|
---|
1806 | {
|
---|
1807 | APIRET arc = NO_ERROR;
|
---|
1808 | CHAR szName[5];
|
---|
1809 |
|
---|
1810 | BYTE fsqBuffer[sizeof(FSQBUFFER2) + (3 * CCHMAXPATH)] = {0};
|
---|
1811 | ULONG cbBuffer = sizeof(fsqBuffer); // Buffer length)
|
---|
1812 | PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)fsqBuffer;
|
---|
1813 |
|
---|
1814 | // compose "D:"-type string from logical drive letter
|
---|
1815 | if (ulLogicalDrive > 0 && ulLogicalDrive < 27)
|
---|
1816 | {
|
---|
1817 | szName[0] = ulLogicalDrive + 'A' - 1;
|
---|
1818 | szName[1] = ':';
|
---|
1819 | szName[2] = '\0';
|
---|
1820 |
|
---|
1821 | arc = DosQueryFSAttach(szName, // logical drive of attached FS ("D:"-style)
|
---|
1822 | 0, // ulOrdinal, ignored for FSAIL_QUERYNAME
|
---|
1823 | FSAIL_QUERYNAME, // return name for a drive or device
|
---|
1824 | pfsqBuffer, // buffer for returned data
|
---|
1825 | &cbBuffer); // sizeof(*pfsqBuffer)
|
---|
1826 |
|
---|
1827 | if (arc == NO_ERROR)
|
---|
1828 | {
|
---|
1829 | if (pszBuf)
|
---|
1830 | {
|
---|
1831 | // The data for the last three fields in the FSQBUFFER2
|
---|
1832 | // structure are stored at the offset of fsqBuffer.szName.
|
---|
1833 | // Each data field following fsqBuffer.szName begins
|
---|
1834 | // immediately after the previous item.
|
---|
1835 | strncpy(pszBuf,
|
---|
1836 | (CHAR*)(&pfsqBuffer->szName) + pfsqBuffer->cbName + 1,
|
---|
1837 | cbBuf); // V0.9.14 (2001-08-01) [umoeller]
|
---|
1838 | *(pszBuf + cbBuf) = '\0';
|
---|
1839 | }
|
---|
1840 | }
|
---|
1841 | }
|
---|
1842 | else
|
---|
1843 | arc = ERROR_INVALID_PARAMETER; // V0.9.16 (2001-10-02) [umoeller]
|
---|
1844 |
|
---|
1845 | return arc;
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | /*
|
---|
1849 | *@@ doshQueryDiskLabel:
|
---|
1850 | * this returns the label of a disk into
|
---|
1851 | * *pszVolumeLabel, which must be 12 bytes
|
---|
1852 | * in size.
|
---|
1853 | *
|
---|
1854 | * This function was added because the Toolkit
|
---|
1855 | * information for DosQueryFSInfo is only partly
|
---|
1856 | * correct. On OS/2 2.x, that function does not
|
---|
1857 | * take an FSINFO structure as input, but a VOLUMELABEL.
|
---|
1858 | * On Warp, this does take an FSINFO.
|
---|
1859 | *
|
---|
1860 | * DosSetFSInfo is even worse. See doshSetDiskLabel.
|
---|
1861 | *
|
---|
1862 | * See http://zebra.asta.fh-weingarten.de/os2/Snippets/Bugi6787.HTML
|
---|
1863 | * for details.
|
---|
1864 | *
|
---|
1865 | *@@added V0.9.0 [umoeller]
|
---|
1866 | *@@changed V0.9.11 (2001-04-22) [umoeller]: this copied even with errors, fixed
|
---|
1867 | */
|
---|
1868 |
|
---|
1869 | APIRET doshQueryDiskLabel(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
1870 | PSZ pszVolumeLabel) // out: volume label (must be 12 chars in size)
|
---|
1871 | {
|
---|
1872 | APIRET arc;
|
---|
1873 |
|
---|
1874 | #ifdef __OS2V2X__
|
---|
1875 | VOLUMELABEL FSInfoBuf;
|
---|
1876 | #else
|
---|
1877 | FSINFO FSInfoBuf;
|
---|
1878 | #endif
|
---|
1879 |
|
---|
1880 | arc = DosQueryFSInfo(ulLogicalDrive,
|
---|
1881 | FSIL_VOLSER,
|
---|
1882 | &FSInfoBuf,
|
---|
1883 | sizeof(FSInfoBuf)); // depends
|
---|
1884 |
|
---|
1885 | if (!arc) // V0.9.11 (2001-04-22) [umoeller]
|
---|
1886 | {
|
---|
1887 | #ifdef __OS2V2X__
|
---|
1888 | strcpy(pszVolumeLabel, FSInfoBuf.szVolLabel);
|
---|
1889 | #else
|
---|
1890 | strcpy(pszVolumeLabel, FSInfoBuf.vol.szVolLabel);
|
---|
1891 | #endif
|
---|
1892 | }
|
---|
1893 |
|
---|
1894 | return arc;
|
---|
1895 | }
|
---|
1896 |
|
---|
1897 | /*
|
---|
1898 | *@@ doshSetDiskLabel:
|
---|
1899 | * this sets the label of a disk.
|
---|
1900 | *
|
---|
1901 | * This function was added because the Toolkit
|
---|
1902 | * information for DosSetFSInfo is flat out wrong.
|
---|
1903 | * That function does not take an FSINFO structure
|
---|
1904 | * as input, but a VOLUMELABEL. As a result, using
|
---|
1905 | * that function with the Toolkit's calling specs
|
---|
1906 | * results in ERROR_LABEL_TOO_LONG always.
|
---|
1907 | *
|
---|
1908 | * See http://zebra.asta.fh-weingarten.de/os2/Snippets/Bugi6787.HTML
|
---|
1909 | * for details.
|
---|
1910 | *
|
---|
1911 | *@@added V0.9.0 [umoeller]
|
---|
1912 | */
|
---|
1913 |
|
---|
1914 | APIRET doshSetDiskLabel(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
|
---|
1915 | PSZ pszNewLabel)
|
---|
1916 | {
|
---|
1917 | VOLUMELABEL FSInfoBuf;
|
---|
1918 |
|
---|
1919 | // check length; 11 chars plus null byte allowed
|
---|
1920 | FSInfoBuf.cch = (BYTE)strlen(pszNewLabel);
|
---|
1921 | if (FSInfoBuf.cch < sizeof(FSInfoBuf.szVolLabel))
|
---|
1922 | {
|
---|
1923 | strcpy(FSInfoBuf.szVolLabel, pszNewLabel);
|
---|
1924 |
|
---|
1925 | return (DosSetFSInfo(ulLogicalDrive,
|
---|
1926 | FSIL_VOLSER,
|
---|
1927 | &FSInfoBuf,
|
---|
1928 | sizeof(FSInfoBuf)));
|
---|
1929 | }
|
---|
1930 | else
|
---|
1931 | return (ERROR_LABEL_TOO_LONG);
|
---|
1932 | }
|
---|
1933 |
|
---|
1934 | /*
|
---|
1935 | *@@category: Helpers\Control program helpers\File name parsing
|
---|
1936 | */
|
---|
1937 |
|
---|
1938 | /* ******************************************************************
|
---|
1939 | *
|
---|
1940 | * File name parsing
|
---|
1941 | *
|
---|
1942 | ********************************************************************/
|
---|
1943 |
|
---|
1944 | /*
|
---|
1945 | *@@ doshGetDriveSpec:
|
---|
1946 | * returns the drive specification in pcszFullFile,
|
---|
1947 | * if any is present. This is useful for UNC support.
|
---|
1948 | *
|
---|
1949 | * This returns:
|
---|
1950 | *
|
---|
1951 | * -- NO_ERROR: drive spec was given, and the output
|
---|
1952 | * fields have been set.
|
---|
1953 | *
|
---|
1954 | * -- ERROR_INVALID_NAME: incorrect UNC syntax.
|
---|
1955 | *
|
---|
1956 | * -- ERROR_INVALID_DRIVE: second char is ':', but
|
---|
1957 | * drive letter is not in the range [A-Z].
|
---|
1958 | *
|
---|
1959 | * -- ERROR_INVALID_PARAMETER: no drive spec given
|
---|
1960 | * at all; apparently pcszFullFile is not fully
|
---|
1961 | * qualified in the first place, or it is NULL,
|
---|
1962 | * or its length is <= 2.
|
---|
1963 | *
|
---|
1964 | *@@added V0.9.16 (2001-10-25) [umoeller]
|
---|
1965 | */
|
---|
1966 |
|
---|
1967 | APIRET doshGetDriveSpec(PCSZ pcszFullFile, // in: fully q'fied file spec
|
---|
1968 | PSZ pszDrive, // out: drive spec ("C:" or "\\SERVER\RESOURCE"; ptr can be NULL)
|
---|
1969 | PULONG pulDriveLen, // out: length of drive spec (2 if local drive; ptr can be NULL)
|
---|
1970 | PBOOL pfIsUNC) // out: set to TRUE if UNC name, FALSE otherwise (ptr can be NULL)
|
---|
1971 | {
|
---|
1972 | APIRET arc = NO_ERROR;
|
---|
1973 | ULONG ulFileSpecLength;
|
---|
1974 |
|
---|
1975 | if ( (pcszFullFile)
|
---|
1976 | && (ulFileSpecLength = strlen(pcszFullFile))
|
---|
1977 | && (ulFileSpecLength >= 2)
|
---|
1978 | )
|
---|
1979 | {
|
---|
1980 | // upper-case the drive letter
|
---|
1981 | if (pcszFullFile[1] == ':')
|
---|
1982 | {
|
---|
1983 | CHAR cDrive = toupper(*pcszFullFile);
|
---|
1984 | // local drive specified:
|
---|
1985 | if ( (cDrive >= 'A')
|
---|
1986 | && (cDrive <= 'Z')
|
---|
1987 | )
|
---|
1988 | {
|
---|
1989 | if (pszDrive)
|
---|
1990 | {
|
---|
1991 | pszDrive[0] = cDrive;
|
---|
1992 | pszDrive[1] = ':';
|
---|
1993 | pszDrive[2] = '\0';
|
---|
1994 | }
|
---|
1995 |
|
---|
1996 | if (pulDriveLen)
|
---|
1997 | *pulDriveLen = 2;
|
---|
1998 | if (pfIsUNC)
|
---|
1999 | *pfIsUNC = FALSE;
|
---|
2000 | }
|
---|
2001 | else
|
---|
2002 | // this is not a valid drive:
|
---|
2003 | arc = ERROR_INVALID_DRIVE;
|
---|
2004 | }
|
---|
2005 | else if ( (pcszFullFile[0] == '\\')
|
---|
2006 | && (pcszFullFile[1] == '\\')
|
---|
2007 | )
|
---|
2008 | {
|
---|
2009 | // UNC drive specified:
|
---|
2010 | // this better be a full \\SERVER\RESOURCE string
|
---|
2011 | PCSZ pResource;
|
---|
2012 | if (pResource = strchr(pcszFullFile + 3, '\\'))
|
---|
2013 | {
|
---|
2014 | // we got at least \\SERVER\:
|
---|
2015 | ULONG ulLength;
|
---|
2016 | PCSZ p;
|
---|
2017 |
|
---|
2018 | // check if more stuff is coming
|
---|
2019 | if (p = strchr(pResource + 1, '\\'))
|
---|
2020 | {
|
---|
2021 | // yes: copy server and resource excluding that backslash
|
---|
2022 | if (p == pResource + 1)
|
---|
2023 | // "\\SERVER\\" is invalid
|
---|
2024 | arc = ERROR_INVALID_NAME;
|
---|
2025 | else
|
---|
2026 | // we got "\\SERVER\something\":
|
---|
2027 | // drop the last backslash
|
---|
2028 | ulLength = p - pcszFullFile;
|
---|
2029 | }
|
---|
2030 | else
|
---|
2031 | // "\\SERVER\something" only:
|
---|
2032 | ulLength = ulFileSpecLength;
|
---|
2033 |
|
---|
2034 | if (!arc)
|
---|
2035 | {
|
---|
2036 | if (pszDrive)
|
---|
2037 | {
|
---|
2038 | memcpy(pszDrive,
|
---|
2039 | pcszFullFile,
|
---|
2040 | ulLength);
|
---|
2041 | pszDrive[ulLength] = '\0';
|
---|
2042 | }
|
---|
2043 |
|
---|
2044 | if (pulDriveLen)
|
---|
2045 | *pulDriveLen = ulLength;
|
---|
2046 | if (pfIsUNC)
|
---|
2047 | *pfIsUNC = TRUE;
|
---|
2048 | }
|
---|
2049 | }
|
---|
2050 | else
|
---|
2051 | // invalid UNC name:
|
---|
2052 | arc = ERROR_INVALID_NAME;
|
---|
2053 | }
|
---|
2054 | else
|
---|
2055 | // neither local, nor UNC:
|
---|
2056 | arc = ERROR_INVALID_PARAMETER;
|
---|
2057 | }
|
---|
2058 | else
|
---|
2059 | arc = ERROR_INVALID_PARAMETER;
|
---|
2060 |
|
---|
2061 | return arc;
|
---|
2062 | }
|
---|
2063 |
|
---|
2064 | /*
|
---|
2065 | *@@ doshGetExtension:
|
---|
2066 | * finds the file name extension of pszFilename,
|
---|
2067 | * which can be a file name only or a fully
|
---|
2068 | * qualified filename.
|
---|
2069 | *
|
---|
2070 | * This returns a pointer into pszFilename to
|
---|
2071 | * the character after the last dot.
|
---|
2072 | *
|
---|
2073 | * Returns NULL if not found (e.g. if the filename
|
---|
2074 | * has no dot in it).
|
---|
2075 | *
|
---|
2076 | * In the pathological case of a dot in the path
|
---|
2077 | * but not in the filename itself (e.g.
|
---|
2078 | * "C:\files.new\readme"), this correctly returns
|
---|
2079 | * NULL.
|
---|
2080 | *
|
---|
2081 | *@@added V0.9.6 (2000-10-16) [umoeller]
|
---|
2082 | *@@changed V0.9.7 (2000-12-10) [umoeller]: fixed "F:filename.ext" case
|
---|
2083 | */
|
---|
2084 |
|
---|
2085 | PSZ doshGetExtension(PCSZ pcszFilename)
|
---|
2086 | {
|
---|
2087 | PSZ pReturn = NULL;
|
---|
2088 |
|
---|
2089 | if (pcszFilename)
|
---|
2090 | {
|
---|
2091 | // find filename
|
---|
2092 | PCSZ p2,
|
---|
2093 | pStartOfName = NULL,
|
---|
2094 | pExtension = NULL;
|
---|
2095 |
|
---|
2096 | if (p2 = strrchr(pcszFilename + 2, '\\'))
|
---|
2097 | // works on "C:\blah" or "\\unc\blah"
|
---|
2098 | pStartOfName = p2 + 1;
|
---|
2099 | else
|
---|
2100 | {
|
---|
2101 | // no backslash found:
|
---|
2102 | // maybe only a drive letter was specified:
|
---|
2103 | if (pcszFilename[1] == ':')
|
---|
2104 | // yes:
|
---|
2105 | pStartOfName = pcszFilename + 2;
|
---|
2106 | else
|
---|
2107 | // then this is not qualified at all...
|
---|
2108 | // use start of filename
|
---|
2109 | pStartOfName = (PSZ)pcszFilename;
|
---|
2110 | }
|
---|
2111 |
|
---|
2112 | // find last dot in filename
|
---|
2113 | if (pExtension = strrchr(pStartOfName, '.'))
|
---|
2114 | pReturn = (PSZ)pExtension + 1;
|
---|
2115 | }
|
---|
2116 |
|
---|
2117 | return (pReturn);
|
---|
2118 | }
|
---|
2119 |
|
---|
2120 | /*
|
---|
2121 | *@@category: Helpers\Control program helpers\File management
|
---|
2122 | */
|
---|
2123 |
|
---|
2124 | /* ******************************************************************
|
---|
2125 | *
|
---|
2126 | * File helpers
|
---|
2127 | *
|
---|
2128 | ********************************************************************/
|
---|
2129 |
|
---|
2130 | /*
|
---|
2131 | *@@ doshIsFileOnFAT:
|
---|
2132 | * returns TRUE if pszFileName resides on
|
---|
2133 | * a FAT drive. Note that pszFileName must
|
---|
2134 | * be fully qualified (i.e. the drive letter
|
---|
2135 | * must be the first character), or this will
|
---|
2136 | * return garbage.
|
---|
2137 | *
|
---|
2138 | *@@changed WarpIN V1.0.20 (2010-06-05) [pr]: now compares FSD name correctly
|
---|
2139 | */
|
---|
2140 |
|
---|
2141 | BOOL doshIsFileOnFAT(const char* pcszFileName)
|
---|
2142 | {
|
---|
2143 | BOOL brc = FALSE;
|
---|
2144 | CHAR szName[5];
|
---|
2145 |
|
---|
2146 | APIRET arc;
|
---|
2147 | BYTE fsqBuffer[sizeof(FSQBUFFER2) + (3 * CCHMAXPATH)] = {0};
|
---|
2148 | ULONG cbBuffer = sizeof(fsqBuffer); // Buffer length)
|
---|
2149 | PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)fsqBuffer;
|
---|
2150 |
|
---|
2151 | szName[0] = pcszFileName[0]; // copy drive letter
|
---|
2152 | szName[1] = ':';
|
---|
2153 | szName[2] = '\0';
|
---|
2154 |
|
---|
2155 | if (!(arc = DosQueryFSAttach(szName, // logical drive of attached FS
|
---|
2156 | 0, // ulOrdinal, ignored for FSAIL_QUERYNAME
|
---|
2157 | FSAIL_QUERYNAME, // return data for a Drive or Device
|
---|
2158 | pfsqBuffer, // returned data
|
---|
2159 | &cbBuffer))) // returned data length
|
---|
2160 | {
|
---|
2161 | // The data for the last three fields in the FSQBUFFER2
|
---|
2162 | // structure are stored at the offset of fsqBuffer.szName.
|
---|
2163 | // Each data field following fsqBuffer.szName begins
|
---|
2164 | // immediately after the previous item.
|
---|
2165 | if (!strncmp(pfsqBuffer->szName + pfsqBuffer->cbName + 1,
|
---|
2166 | "FAT",
|
---|
2167 | pfsqBuffer->cbFSDName))
|
---|
2168 | brc = TRUE;
|
---|
2169 | }
|
---|
2170 |
|
---|
2171 | return brc;
|
---|
2172 | }
|
---|
2173 |
|
---|
2174 | /*
|
---|
2175 | *@@ doshQueryFileSize:
|
---|
2176 | * returns the size of an already opened file
|
---|
2177 | * or 0 upon errors.
|
---|
2178 | * Use doshQueryPathSize to query the size of
|
---|
2179 | * any file.
|
---|
2180 | *
|
---|
2181 | *@@changed V0.9.16 (2001-10-19) [umoeller]: now returning APIRET
|
---|
2182 | */
|
---|
2183 |
|
---|
2184 | APIRET doshQueryFileSize(HFILE hFile, // in: file handle
|
---|
2185 | PULONG pulSize) // out: file size (ptr can be NULL)
|
---|
2186 | {
|
---|
2187 | APIRET arc;
|
---|
2188 | FILESTATUS3 fs3;
|
---|
2189 | if (!(arc = DosQueryFileInfo(hFile, FIL_STANDARD, &fs3, sizeof(fs3))))
|
---|
2190 | if (pulSize)
|
---|
2191 | *pulSize = fs3.cbFile;
|
---|
2192 | return arc;
|
---|
2193 | }
|
---|
2194 |
|
---|
2195 | /*
|
---|
2196 | *@@ doshQueryPathSize:
|
---|
2197 | * returns the size of any file,
|
---|
2198 | * or 0 if the file could not be
|
---|
2199 | * found.
|
---|
2200 | *
|
---|
2201 | * Use doshQueryFileSize instead to query the
|
---|
2202 | * size if you have a HFILE.
|
---|
2203 | *
|
---|
2204 | * Otherwise this returns:
|
---|
2205 | *
|
---|
2206 | * -- ERROR_FILE_NOT_FOUND
|
---|
2207 | * -- ERROR_PATH_NOT_FOUND
|
---|
2208 | * -- ERROR_SHARING_VIOLATION
|
---|
2209 | * -- ERROR_FILENAME_EXCED_RANGE
|
---|
2210 | * -- ERROR_INVALID_PARAMETER
|
---|
2211 | *
|
---|
2212 | *@@changed V0.9.16 (2001-10-19) [umoeller]: now returning APIRET
|
---|
2213 | */
|
---|
2214 |
|
---|
2215 | APIRET doshQueryPathSize(PCSZ pcszFile, // in: filename
|
---|
2216 | PULONG pulSize) // out: file size (ptr can be NULL)
|
---|
2217 | {
|
---|
2218 | APIRET arc;
|
---|
2219 |
|
---|
2220 | if (pcszFile) // V0.9.16 (2001-12-08) [umoeller]
|
---|
2221 | {
|
---|
2222 | FILESTATUS3 fs3;
|
---|
2223 | if (!(arc = DosQueryPathInfo((PSZ)pcszFile, FIL_STANDARD, &fs3, sizeof(fs3))))
|
---|
2224 | if (pulSize)
|
---|
2225 | *pulSize = fs3.cbFile;
|
---|
2226 | }
|
---|
2227 | else
|
---|
2228 | arc = ERROR_INVALID_PARAMETER;
|
---|
2229 |
|
---|
2230 | return arc;
|
---|
2231 | }
|
---|
2232 |
|
---|
2233 | /*
|
---|
2234 | *@@ doshQueryPathAttr:
|
---|
2235 | * returns the file attributes of pszFile,
|
---|
2236 | * which can be fully qualified. The
|
---|
2237 | * attributes will be stored in *pulAttr.
|
---|
2238 | * pszFile can also specify a directory,
|
---|
2239 | * although not all attributes make sense
|
---|
2240 | * for directories.
|
---|
2241 | *
|
---|
2242 | * fAttr can be:
|
---|
2243 | * -- FILE_ARCHIVED
|
---|
2244 | * -- FILE_READONLY
|
---|
2245 | * -- FILE_SYSTEM
|
---|
2246 | * -- FILE_HIDDEN
|
---|
2247 | *
|
---|
2248 | * This returns the APIRET of DosQueryPathInfo.
|
---|
2249 | * *pulAttr is only valid if NO_ERROR is
|
---|
2250 | * returned.
|
---|
2251 | *
|
---|
2252 | * Otherwise this returns:
|
---|
2253 | *
|
---|
2254 | * -- ERROR_FILE_NOT_FOUND
|
---|
2255 | * -- ERROR_PATH_NOT_FOUND
|
---|
2256 | * -- ERROR_SHARING_VIOLATION
|
---|
2257 | * -- ERROR_FILENAME_EXCED_RANGE
|
---|
2258 | *
|
---|
2259 | *@@added V0.9.0 [umoeller]
|
---|
2260 | */
|
---|
2261 |
|
---|
2262 | APIRET doshQueryPathAttr(const char* pcszFile, // in: file or directory name
|
---|
2263 | PULONG pulAttr) // out: attributes (ptr can be NULL)
|
---|
2264 | {
|
---|
2265 | FILESTATUS3 fs3;
|
---|
2266 | APIRET arc;
|
---|
2267 |
|
---|
2268 | if (!(arc = DosQueryPathInfo((PSZ)pcszFile,
|
---|
2269 | FIL_STANDARD,
|
---|
2270 | &fs3,
|
---|
2271 | sizeof(fs3))))
|
---|
2272 | {
|
---|
2273 | if (pulAttr)
|
---|
2274 | *pulAttr = fs3.attrFile;
|
---|
2275 | }
|
---|
2276 |
|
---|
2277 | return arc;
|
---|
2278 | }
|
---|
2279 |
|
---|
2280 | /*
|
---|
2281 | *@@ doshSetPathAttr:
|
---|
2282 | * sets the file attributes of pszFile,
|
---|
2283 | * which can be fully qualified.
|
---|
2284 | * pszFile can also specify a directory,
|
---|
2285 | * although not all attributes make sense
|
---|
2286 | * for directories.
|
---|
2287 | *
|
---|
2288 | * fAttr can be:
|
---|
2289 | * -- FILE_ARCHIVED
|
---|
2290 | * -- FILE_READONLY
|
---|
2291 | * -- FILE_SYSTEM
|
---|
2292 | * -- FILE_HIDDEN
|
---|
2293 | *
|
---|
2294 | * Note that this simply sets all the given
|
---|
2295 | * attributes; the existing attributes
|
---|
2296 | * are lost.
|
---|
2297 | *
|
---|
2298 | * This returns the APIRET of DosQueryPathInfo.
|
---|
2299 | */
|
---|
2300 |
|
---|
2301 | APIRET doshSetPathAttr(const char* pcszFile, // in: file or directory name
|
---|
2302 | ULONG ulAttr) // in: new attributes
|
---|
2303 | {
|
---|
2304 | APIRET arc;
|
---|
2305 |
|
---|
2306 | if (pcszFile)
|
---|
2307 | {
|
---|
2308 | FILESTATUS3 fs3;
|
---|
2309 | if (!(arc = DosQueryPathInfo((PSZ)pcszFile,
|
---|
2310 | FIL_STANDARD,
|
---|
2311 | &fs3,
|
---|
2312 | sizeof(fs3))))
|
---|
2313 | {
|
---|
2314 | fs3.attrFile = ulAttr;
|
---|
2315 | arc = DosSetPathInfo((PSZ)pcszFile,
|
---|
2316 | FIL_STANDARD,
|
---|
2317 | &fs3,
|
---|
2318 | sizeof(fs3),
|
---|
2319 | DSPI_WRTTHRU);
|
---|
2320 | }
|
---|
2321 | }
|
---|
2322 | else
|
---|
2323 | arc = ERROR_INVALID_PARAMETER;
|
---|
2324 |
|
---|
2325 | return arc;
|
---|
2326 | }
|
---|
2327 |
|
---|
2328 | /*
|
---|
2329 | *@@category: Helpers\Control program helpers\File management\XFILEs
|
---|
2330 | */
|
---|
2331 |
|
---|
2332 | /* ******************************************************************
|
---|
2333 | *
|
---|
2334 | * XFILEs
|
---|
2335 | *
|
---|
2336 | ********************************************************************/
|
---|
2337 |
|
---|
2338 | /*
|
---|
2339 | * doshOpen:
|
---|
2340 | * wrapper around DosOpen for simpler opening
|
---|
2341 | * of files.
|
---|
2342 | *
|
---|
2343 | * ulOpenMode determines the mode to open the
|
---|
2344 | * file in (fptr specifies the position after
|
---|
2345 | * the open):
|
---|
2346 | *
|
---|
2347 | + +-------------------------+------+------------+-----------+
|
---|
2348 | + | ulOpenMode | mode | if exists | if new |
|
---|
2349 | + +-------------------------+------+------------+-----------+
|
---|
2350 | + | XOPEN_READ_EXISTING | read | opens | fails |
|
---|
2351 | + | | | fptr = 0 | |
|
---|
2352 | + +-------------------------+------+------------+-----------+
|
---|
2353 | + | XOPEN_READWRITE_EXISTING r/w | opens | fails |
|
---|
2354 | + | | | fptr = 0 | |
|
---|
2355 | + +-------------------------+------+------------+-----------+
|
---|
2356 | + | XOPEN_READWRITE_APPEND | r/w | opens, | creates |
|
---|
2357 | + | | | appends | |
|
---|
2358 | + | | | fptr = end | fptr = 0 |
|
---|
2359 | + +-------------------------+------+------------+-----------+
|
---|
2360 | + | XOPEN_READWRITE_NEW | r/w | replaces | creates |
|
---|
2361 | + | | | fptr = 0 | fptr = 0 |
|
---|
2362 | + +-------------------------+------+------------+-----------+
|
---|
2363 | *
|
---|
2364 | * In addition, you can OR one of the above values with
|
---|
2365 | * the XOPEN_BINARY flag:
|
---|
2366 | *
|
---|
2367 | * -- If XOPEN_BINARY is set, no conversion is performed
|
---|
2368 | * on read and write.
|
---|
2369 | *
|
---|
2370 | * -- If XOPEN_BINARY is _not_ set, all \n chars are
|
---|
2371 | * converted to \r\n on write.
|
---|
2372 | *
|
---|
2373 | * *ppFile receives a new XFILE structure describing
|
---|
2374 | * the open file, if NO_ERROR is returned.
|
---|
2375 | *
|
---|
2376 | * The file pointer is then set to the beginning of the
|
---|
2377 | * file _unless_ XOPEN_READWRITE_APPEND was specified;
|
---|
2378 | * in that case only, the file pointer is set to the
|
---|
2379 | * end of the file so data can be appended (see above).
|
---|
2380 | *
|
---|
2381 | * Otherwise this returns:
|
---|
2382 | *
|
---|
2383 | * -- ERROR_FILE_NOT_FOUND
|
---|
2384 | * -- ERROR_PATH_NOT_FOUND
|
---|
2385 | * -- ERROR_SHARING_VIOLATION
|
---|
2386 | * -- ERROR_FILENAME_EXCED_RANGE
|
---|
2387 | *
|
---|
2388 | * -- ERROR_NOT_ENOUGH_MEMORY
|
---|
2389 | * -- ERROR_INVALID_PARAMETER
|
---|
2390 | *
|
---|
2391 | *@@added V0.9.16 (2001-10-19) [umoeller]
|
---|
2392 | *@@changed V0.9.16 (2001-12-18) [umoeller]: fixed error codes
|
---|
2393 | */
|
---|
2394 |
|
---|
2395 | APIRET doshOpen(PCSZ pcszFilename, // in: filename to open
|
---|
2396 | ULONG flOpenMode, // in: XOPEN_* mode
|
---|
2397 | PULONG pcbFile, // in: new file size (if new file is created)
|
---|
2398 | // out: file size
|
---|
2399 | PXFILE *ppFile)
|
---|
2400 | {
|
---|
2401 | APIRET arc = NO_ERROR;
|
---|
2402 |
|
---|
2403 | ULONG fsOpenFlags = 0,
|
---|
2404 | fsOpenMode = OPEN_FLAGS_FAIL_ON_ERROR
|
---|
2405 | | OPEN_FLAGS_NO_LOCALITY
|
---|
2406 | | OPEN_FLAGS_NOINHERIT;
|
---|
2407 |
|
---|
2408 | switch (flOpenMode & XOPEN_ACCESS_MASK)
|
---|
2409 | {
|
---|
2410 | case XOPEN_READ_EXISTING:
|
---|
2411 | fsOpenFlags = OPEN_ACTION_FAIL_IF_NEW
|
---|
2412 | | OPEN_ACTION_OPEN_IF_EXISTS;
|
---|
2413 | fsOpenMode |= OPEN_SHARE_DENYWRITE
|
---|
2414 | | OPEN_ACCESS_READONLY;
|
---|
2415 |
|
---|
2416 | // run this first, because if the file doesn't
|
---|
2417 | // exists, DosOpen only returns ERROR_OPEN_FAILED,
|
---|
2418 | // which isn't that meaningful
|
---|
2419 | // V0.9.16 (2001-12-08) [umoeller]
|
---|
2420 | arc = doshQueryPathSize(pcszFilename,
|
---|
2421 | pcbFile);
|
---|
2422 | break;
|
---|
2423 |
|
---|
2424 | case XOPEN_READWRITE_EXISTING:
|
---|
2425 | fsOpenFlags = OPEN_ACTION_FAIL_IF_NEW
|
---|
2426 | | OPEN_ACTION_OPEN_IF_EXISTS;
|
---|
2427 | fsOpenMode |= OPEN_SHARE_DENYWRITE
|
---|
2428 | | OPEN_ACCESS_READWRITE;
|
---|
2429 |
|
---|
2430 | arc = doshQueryPathSize(pcszFilename,
|
---|
2431 | pcbFile);
|
---|
2432 | break;
|
---|
2433 |
|
---|
2434 | case XOPEN_READWRITE_APPEND:
|
---|
2435 | fsOpenFlags = OPEN_ACTION_CREATE_IF_NEW
|
---|
2436 | | OPEN_ACTION_OPEN_IF_EXISTS;
|
---|
2437 | fsOpenMode |= OPEN_SHARE_DENYREADWRITE
|
---|
2438 | | OPEN_ACCESS_READWRITE;
|
---|
2439 | // _Pmpf((__FUNCTION__ ": opening XOPEN_READWRITE_APPEND"));
|
---|
2440 | break;
|
---|
2441 |
|
---|
2442 | case XOPEN_READWRITE_NEW:
|
---|
2443 | fsOpenFlags = OPEN_ACTION_CREATE_IF_NEW
|
---|
2444 | | OPEN_ACTION_REPLACE_IF_EXISTS;
|
---|
2445 | fsOpenMode |= OPEN_SHARE_DENYREADWRITE
|
---|
2446 | | OPEN_ACCESS_READWRITE;
|
---|
2447 | // _Pmpf((__FUNCTION__ ": opening XOPEN_READWRITE_NEW"));
|
---|
2448 | break;
|
---|
2449 | }
|
---|
2450 |
|
---|
2451 | if ((!arc) && fsOpenFlags && pcbFile && ppFile)
|
---|
2452 | {
|
---|
2453 | PXFILE pFile;
|
---|
2454 | if (pFile = NEW(XFILE))
|
---|
2455 | {
|
---|
2456 | ULONG ulAction;
|
---|
2457 |
|
---|
2458 | ZERO(pFile);
|
---|
2459 |
|
---|
2460 | // copy open flags
|
---|
2461 | pFile->flOpenMode = flOpenMode;
|
---|
2462 |
|
---|
2463 | if (!(arc = DosOpen((PSZ)pcszFilename,
|
---|
2464 | &pFile->hf,
|
---|
2465 | &ulAction,
|
---|
2466 | *pcbFile,
|
---|
2467 | FILE_ARCHIVED,
|
---|
2468 | fsOpenFlags,
|
---|
2469 | fsOpenMode,
|
---|
2470 | NULL))) // EAs
|
---|
2471 | {
|
---|
2472 | // alright, got the file:
|
---|
2473 |
|
---|
2474 | if ( (ulAction == FILE_EXISTED)
|
---|
2475 | && ((flOpenMode & XOPEN_ACCESS_MASK) == XOPEN_READWRITE_APPEND)
|
---|
2476 | )
|
---|
2477 | // get its size and set ptr to end for append
|
---|
2478 | arc = DosSetFilePtr(pFile->hf,
|
---|
2479 | 0,
|
---|
2480 | FILE_END,
|
---|
2481 | pcbFile);
|
---|
2482 | else
|
---|
2483 | arc = doshQueryFileSize(pFile->hf,
|
---|
2484 | pcbFile);
|
---|
2485 | // file ptr is at beginning
|
---|
2486 |
|
---|
2487 | #ifdef DEBUG_DOSOPEN
|
---|
2488 | if (arc)
|
---|
2489 | _Pmpf((__FUNCTION__ ": DosSetFilePtr/queryfilesize returned %d for %s",
|
---|
2490 | arc, pcszFilename));
|
---|
2491 | #endif
|
---|
2492 |
|
---|
2493 | // store file size
|
---|
2494 | pFile->cbInitial
|
---|
2495 | = pFile->cbCurrent
|
---|
2496 | = *pcbFile;
|
---|
2497 |
|
---|
2498 | pFile->pszFilename = strdup(pcszFilename);
|
---|
2499 | }
|
---|
2500 | #ifdef DEBUG_DOSOPEN
|
---|
2501 | else
|
---|
2502 | _Pmpf((__FUNCTION__ ": DosOpen returned %d for %s",
|
---|
2503 | arc, pcszFilename));
|
---|
2504 | #endif
|
---|
2505 |
|
---|
2506 | if (arc)
|
---|
2507 | doshClose(&pFile);
|
---|
2508 | else
|
---|
2509 | *ppFile = pFile;
|
---|
2510 | }
|
---|
2511 | else
|
---|
2512 | arc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
2513 | }
|
---|
2514 | else
|
---|
2515 | if (!arc) // V0.9.19 (2002-04-02) [umoeller]
|
---|
2516 | arc = ERROR_INVALID_PARAMETER;
|
---|
2517 |
|
---|
2518 | return arc;
|
---|
2519 | }
|
---|
2520 |
|
---|
2521 | /*
|
---|
2522 | *@@ doshReadAt:
|
---|
2523 | * reads cb bytes from the position specified by
|
---|
2524 | * lOffset into the buffer pointed to by pbData,
|
---|
2525 | * which should be cb bytes in size.
|
---|
2526 | *
|
---|
2527 | * Note that lOffset is always considered to
|
---|
2528 | * be from the beginning of the file (FILE_BEGIN
|
---|
2529 | * method).
|
---|
2530 | *
|
---|
2531 | * This implements a small cache so that several
|
---|
2532 | * calls with a near offset will not touch the
|
---|
2533 | * disk always. The cache has been optimized for
|
---|
2534 | * the exeh* functions and works quite nicely
|
---|
2535 | * there.
|
---|
2536 | *
|
---|
2537 | * Note that the position of the file pointer is
|
---|
2538 | * undefined after calling this function because
|
---|
2539 | * the data might have been returned from the
|
---|
2540 | * cache.
|
---|
2541 | *
|
---|
2542 | * fl may be any combination of the following:
|
---|
2543 | *
|
---|
2544 | * -- DRFL_NOCACHE: do not fill the cache with
|
---|
2545 | * new data if the data is not in the cache
|
---|
2546 | * currently.
|
---|
2547 | *
|
---|
2548 | * -- DRFL_FAILIFLESS: return ERROR_NO_DATA
|
---|
2549 | * if the data returned by DosRead is less
|
---|
2550 | * than what was specified. This might
|
---|
2551 | * simplify error handling.
|
---|
2552 | *
|
---|
2553 | *@@added V0.9.13 (2001-06-14) [umoeller]
|
---|
2554 | *@@changed V0.9.16 (2001-12-18) [umoeller]: now with XFILE, and always using FILE_BEGIN
|
---|
2555 | *@@chaanged V0.9.19 (2002-04-02) [umoeller]: added params checking
|
---|
2556 | */
|
---|
2557 |
|
---|
2558 | APIRET doshReadAt(PXFILE pFile,
|
---|
2559 | ULONG ulOffset, // in: offset to read from (from beginning of file)
|
---|
2560 | PULONG pcb, // in: bytes to read, out: bytes read (req.)
|
---|
2561 | PBYTE pbData, // out: read buffer (must be cb bytes)
|
---|
2562 | ULONG fl) // in: DRFL_* flags
|
---|
2563 | {
|
---|
2564 | APIRET arc = NO_ERROR;
|
---|
2565 | ULONG cb;
|
---|
2566 | ULONG ulDummy;
|
---|
2567 |
|
---|
2568 | if (!pFile || !pcb)
|
---|
2569 | // V0.9.19 (2002-04-02) [umoeller]
|
---|
2570 | return ERROR_INVALID_PARAMETER;
|
---|
2571 |
|
---|
2572 | cb = *pcb;
|
---|
2573 | *pcb = 0;
|
---|
2574 |
|
---|
2575 | // check if we have the data in the cache already;
|
---|
2576 |
|
---|
2577 | if ( (pFile->pbCache)
|
---|
2578 | // first byte must be in cache
|
---|
2579 | && (ulOffset >= pFile->ulReadFrom)
|
---|
2580 | // last byte must be in cache
|
---|
2581 | && ( ulOffset + cb
|
---|
2582 | <= pFile->ulReadFrom + pFile->cbCache
|
---|
2583 | )
|
---|
2584 | )
|
---|
2585 | {
|
---|
2586 | // alright, return data from cache simply
|
---|
2587 | ULONG ulOfsInCache = ulOffset - pFile->ulReadFrom;
|
---|
2588 |
|
---|
2589 | memcpy(pbData,
|
---|
2590 | pFile->pbCache + ulOfsInCache,
|
---|
2591 | cb);
|
---|
2592 | *pcb = cb;
|
---|
2593 |
|
---|
2594 | #ifdef DEBUG_DOSOPEN
|
---|
2595 | _Pmpf((__FUNCTION__ " %s: data is fully in cache",
|
---|
2596 | pFile->pszFilename));
|
---|
2597 | _Pmpf((" caller wants %d bytes from %d",
|
---|
2598 | cb, ulOffset));
|
---|
2599 | _Pmpf((" we got %d bytes from %d",
|
---|
2600 | pFile->cbCache, pFile->ulReadFrom));
|
---|
2601 | _Pmpf((" so copied %d bytes from cache ofs %d",
|
---|
2602 | cb, ulOfsInCache));
|
---|
2603 | #endif
|
---|
2604 | }
|
---|
2605 | else
|
---|
2606 | {
|
---|
2607 | // data is not in cache:
|
---|
2608 | // check how much it is... for small amounts,
|
---|
2609 | // we load the cache first
|
---|
2610 | if ( (cb <= 4096 - 512)
|
---|
2611 | && (!(fl & DRFL_NOCACHE))
|
---|
2612 | )
|
---|
2613 | {
|
---|
2614 | #ifdef DEBUG_DOSOPEN
|
---|
2615 | _Pmpf((__FUNCTION__ " %s: filling cache anew",
|
---|
2616 | pFile->pszFilename));
|
---|
2617 | _Pmpf((" caller wants %d bytes from %d",
|
---|
2618 | cb, ulOffset));
|
---|
2619 | #endif
|
---|
2620 |
|
---|
2621 | // OK, then fix the offset to read from
|
---|
2622 | // to a multiple of 512 to get a full sector
|
---|
2623 | pFile->ulReadFrom = ulOffset / 512L * 512L;
|
---|
2624 | // and read 4096 bytes always plus the
|
---|
2625 | // value we cut off above
|
---|
2626 | pFile->cbCache = 4096;
|
---|
2627 |
|
---|
2628 | #ifdef DEBUG_DOSOPEN
|
---|
2629 | _Pmpf((" getting %d bytes from %d",
|
---|
2630 | pFile->cbCache, pFile->ulReadFrom));
|
---|
2631 | #endif
|
---|
2632 |
|
---|
2633 | // free old cache
|
---|
2634 | if (pFile->pbCache)
|
---|
2635 | free(pFile->pbCache);
|
---|
2636 |
|
---|
2637 | // allocate new cache
|
---|
2638 | if (!(pFile->pbCache = (PBYTE)malloc(pFile->cbCache)))
|
---|
2639 | arc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
2640 | else
|
---|
2641 | {
|
---|
2642 | ULONG ulOfsInCache = 0;
|
---|
2643 |
|
---|
2644 | if (!(arc = DosSetFilePtr(pFile->hf,
|
---|
2645 | (LONG)pFile->ulReadFrom,
|
---|
2646 | FILE_BEGIN,
|
---|
2647 | &ulDummy)))
|
---|
2648 | {
|
---|
2649 | if (!(arc = DosRead(pFile->hf,
|
---|
2650 | pFile->pbCache,
|
---|
2651 | pFile->cbCache,
|
---|
2652 | &ulDummy)))
|
---|
2653 | {
|
---|
2654 | // got data:
|
---|
2655 | #ifdef DEBUG_DOSOPEN
|
---|
2656 | _Pmpf((" %d bytes read", ulDummy));
|
---|
2657 | #endif
|
---|
2658 |
|
---|
2659 | pFile->cbCache = ulDummy;
|
---|
2660 |
|
---|
2661 | // check bounds
|
---|
2662 | ulOfsInCache = ulOffset - pFile->ulReadFrom;
|
---|
2663 |
|
---|
2664 | /*
|
---|
2665 | if (ulOfsInCache + cb > pFile->cbCache)
|
---|
2666 | {
|
---|
2667 | cb = pFile->cbCache - ulOfsInCache;
|
---|
2668 | if (fl & DRFL_FAILIFLESS)
|
---|
2669 | arc = ERROR_NO_DATA;
|
---|
2670 | }
|
---|
2671 | */
|
---|
2672 | }
|
---|
2673 | }
|
---|
2674 |
|
---|
2675 | if (!arc)
|
---|
2676 | {
|
---|
2677 | // copy to caller
|
---|
2678 | memcpy(pbData,
|
---|
2679 | pFile->pbCache + ulOfsInCache,
|
---|
2680 | cb);
|
---|
2681 | *pcb = cb;
|
---|
2682 |
|
---|
2683 | #ifdef DEBUG_DOSOPEN
|
---|
2684 | _Pmpf((" so copied %d bytes from cache ofs %d",
|
---|
2685 | cb, ulOfsInCache));
|
---|
2686 | #endif
|
---|
2687 | }
|
---|
2688 | else
|
---|
2689 | {
|
---|
2690 | free(pFile->pbCache);
|
---|
2691 | pFile->pbCache = NULL;
|
---|
2692 | }
|
---|
2693 | } // end else if (!(pFile->pbCache = (PBYTE)malloc(pFile->cbCache)))
|
---|
2694 | }
|
---|
2695 | else
|
---|
2696 | {
|
---|
2697 | // read uncached:
|
---|
2698 | #ifdef DEBUG_DOSOPEN
|
---|
2699 | _Pmpf((" " __FUNCTION__ " %s: reading uncached",
|
---|
2700 | pFile->pszFilename));
|
---|
2701 | _Pmpf((" caller wants %d bytes from %d",
|
---|
2702 | cb, ulOffset));
|
---|
2703 | #endif
|
---|
2704 |
|
---|
2705 | if (!(arc = DosSetFilePtr(pFile->hf,
|
---|
2706 | (LONG)ulOffset,
|
---|
2707 | FILE_BEGIN,
|
---|
2708 | &ulDummy)))
|
---|
2709 | {
|
---|
2710 | if (!(arc = DosRead(pFile->hf,
|
---|
2711 | pbData,
|
---|
2712 | cb,
|
---|
2713 | &ulDummy)))
|
---|
2714 | {
|
---|
2715 | if ( (fl & DRFL_FAILIFLESS)
|
---|
2716 | && (ulDummy != cb)
|
---|
2717 | )
|
---|
2718 | arc = ERROR_NO_DATA;
|
---|
2719 | else
|
---|
2720 | *pcb = ulDummy; // bytes read
|
---|
2721 | }
|
---|
2722 | }
|
---|
2723 | }
|
---|
2724 | }
|
---|
2725 |
|
---|
2726 | return arc;
|
---|
2727 | }
|
---|
2728 |
|
---|
2729 | /*
|
---|
2730 | *@@ doshWrite:
|
---|
2731 | * writes the specified data to the file.
|
---|
2732 | * If (cb == 0), this runs strlen on pcsz
|
---|
2733 | * to find out the length.
|
---|
2734 | *
|
---|
2735 | * If the file is not in binary mode, all
|
---|
2736 | * \n chars are converted to \r\n before
|
---|
2737 | * writing.
|
---|
2738 | *
|
---|
2739 | * Note that this expects that the file
|
---|
2740 | * pointer is at the end of the file, or
|
---|
2741 | * you will get garbage.
|
---|
2742 | *
|
---|
2743 | *@@added V0.9.16 (2001-10-19) [umoeller]
|
---|
2744 | *@@changed V0.9.16 (2001-12-02) [umoeller]: added XOPEN_BINARY \r\n support
|
---|
2745 | *@@changed V0.9.16 (2001-12-06) [umoeller]: added check for pFile != NULL
|
---|
2746 | */
|
---|
2747 |
|
---|
2748 | APIRET doshWrite(PXFILE pFile,
|
---|
2749 | ULONG cb,
|
---|
2750 | PCSZ pbData)
|
---|
2751 | {
|
---|
2752 | APIRET arc = NO_ERROR;
|
---|
2753 | if ((!pFile) || (!pbData))
|
---|
2754 | arc = ERROR_INVALID_PARAMETER;
|
---|
2755 | else
|
---|
2756 | {
|
---|
2757 | if (!cb)
|
---|
2758 | cb = strlen(pbData);
|
---|
2759 |
|
---|
2760 | if (!cb)
|
---|
2761 | arc = ERROR_INVALID_PARAMETER;
|
---|
2762 | else
|
---|
2763 | {
|
---|
2764 | PSZ pszNew = NULL;
|
---|
2765 |
|
---|
2766 | if (!(pFile->flOpenMode & XOPEN_BINARY))
|
---|
2767 | {
|
---|
2768 | // convert all \n to \r\n:
|
---|
2769 | // V0.9.16 (2001-12-02) [umoeller]
|
---|
2770 |
|
---|
2771 | // count all \n first
|
---|
2772 | ULONG cNewLines = 0;
|
---|
2773 | PCSZ pSource = pbData;
|
---|
2774 | ULONG ul;
|
---|
2775 | for (ul = 0;
|
---|
2776 | ul < cb;
|
---|
2777 | ul++)
|
---|
2778 | {
|
---|
2779 | if (*pSource++ == '\n')
|
---|
2780 | cNewLines++;
|
---|
2781 | }
|
---|
2782 |
|
---|
2783 | if (cNewLines)
|
---|
2784 | {
|
---|
2785 | // we have '\n' chars:
|
---|
2786 | // then we need just as many \r chars inserted
|
---|
2787 | ULONG cbNew = cb + cNewLines;
|
---|
2788 | if (!(pszNew = (PSZ)malloc(cbNew)))
|
---|
2789 | arc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
2790 | else
|
---|
2791 | {
|
---|
2792 | PSZ pTarget = pszNew;
|
---|
2793 | pSource = pbData;
|
---|
2794 | for (ul = 0;
|
---|
2795 | ul < cb;
|
---|
2796 | ul++)
|
---|
2797 | {
|
---|
2798 | CHAR c = *pSource++;
|
---|
2799 | if (c == '\n')
|
---|
2800 | *pTarget++ = '\r';
|
---|
2801 | *pTarget++ = c;
|
---|
2802 | }
|
---|
2803 |
|
---|
2804 | cb = cbNew;
|
---|
2805 | }
|
---|
2806 | }
|
---|
2807 | }
|
---|
2808 |
|
---|
2809 | if (!arc)
|
---|
2810 | {
|
---|
2811 | ULONG cbWritten;
|
---|
2812 | if (!(arc = DosWrite(pFile->hf,
|
---|
2813 | (pszNew)
|
---|
2814 | ? pszNew
|
---|
2815 | : (PSZ)pbData,
|
---|
2816 | cb,
|
---|
2817 | &cbWritten)))
|
---|
2818 | {
|
---|
2819 | pFile->cbCurrent += cbWritten;
|
---|
2820 | // invalidate the cache
|
---|
2821 | FREE(pFile->pbCache);
|
---|
2822 | }
|
---|
2823 | }
|
---|
2824 |
|
---|
2825 | if (pszNew)
|
---|
2826 | free(pszNew);
|
---|
2827 | }
|
---|
2828 | }
|
---|
2829 |
|
---|
2830 | return arc;
|
---|
2831 | }
|
---|
2832 |
|
---|
2833 | /*
|
---|
2834 | *@@ doshWriteAt:
|
---|
2835 | * writes cb bytes (pointed to by pbData) to the
|
---|
2836 | * specified file at the position lOffset (from
|
---|
2837 | * the beginning of the file).
|
---|
2838 | *
|
---|
2839 | *@@added V0.9.13 (2001-06-14) [umoeller]
|
---|
2840 | */
|
---|
2841 |
|
---|
2842 | APIRET doshWriteAt(PXFILE pFile,
|
---|
2843 | ULONG ulOffset, // in: offset to write at
|
---|
2844 | ULONG cb, // in: bytes to write
|
---|
2845 | PCSZ pbData) // in: ptr to bytes to write (must be cb bytes)
|
---|
2846 | {
|
---|
2847 | APIRET arc = NO_ERROR;
|
---|
2848 | ULONG cbWritten;
|
---|
2849 | if (!(arc = DosSetFilePtr(pFile->hf,
|
---|
2850 | (LONG)ulOffset,
|
---|
2851 | FILE_BEGIN,
|
---|
2852 | &cbWritten)))
|
---|
2853 | {
|
---|
2854 | if (!(arc = DosWrite(pFile->hf,
|
---|
2855 | (PSZ)pbData,
|
---|
2856 | cb,
|
---|
2857 | &cbWritten)))
|
---|
2858 | {
|
---|
2859 | if (ulOffset + cbWritten > pFile->cbCurrent)
|
---|
2860 | pFile->cbCurrent = ulOffset + cbWritten;
|
---|
2861 | // invalidate the cache V0.9.19 (2002-04-02) [umoeller]
|
---|
2862 | FREE(pFile->pbCache);
|
---|
2863 | }
|
---|
2864 | }
|
---|
2865 |
|
---|
2866 | return arc;
|
---|
2867 | }
|
---|
2868 |
|
---|
2869 | /*
|
---|
2870 | *@@ doshWriteLogEntry
|
---|
2871 | * writes a log string to an XFILE, adding a
|
---|
2872 | * leading timestamp before the line.
|
---|
2873 | *
|
---|
2874 | * The internal string buffer is limited to 2000
|
---|
2875 | * characters. Length checking is _not_ performed.
|
---|
2876 | *
|
---|
2877 | *@@added V0.9.16 (2001-10-19) [umoeller]
|
---|
2878 | *@@changed V0.9.16 (2001-12-06) [umoeller]: added check for pFile != NULL
|
---|
2879 | */
|
---|
2880 |
|
---|
2881 | APIRET doshWriteLogEntry(PXFILE pFile,
|
---|
2882 | const char* pcszFormat,
|
---|
2883 | ...)
|
---|
2884 | {
|
---|
2885 | APIRET arc = NO_ERROR;
|
---|
2886 |
|
---|
2887 | if ((!pFile) || (!pcszFormat))
|
---|
2888 | arc = ERROR_INVALID_PARAMETER;
|
---|
2889 | else
|
---|
2890 | {
|
---|
2891 | DATETIME dt;
|
---|
2892 | CHAR szTemp[2000];
|
---|
2893 | ULONG ulLength;
|
---|
2894 |
|
---|
2895 | DosGetDateTime(&dt);
|
---|
2896 | if (ulLength = sprintf(szTemp,
|
---|
2897 | "%04d-%02d-%02d %02d:%02d:%02d:%02d ",
|
---|
2898 | dt.year, dt.month, dt.day,
|
---|
2899 | dt.hours, dt.minutes, dt.seconds, dt.hundredths))
|
---|
2900 | {
|
---|
2901 | if (!(arc = doshWrite(pFile,
|
---|
2902 | ulLength,
|
---|
2903 | szTemp)))
|
---|
2904 | {
|
---|
2905 | va_list arg_ptr;
|
---|
2906 | va_start(arg_ptr, pcszFormat);
|
---|
2907 | ulLength = vsprintf(szTemp, pcszFormat, arg_ptr);
|
---|
2908 | va_end(arg_ptr);
|
---|
2909 |
|
---|
2910 | if (pFile->flOpenMode & XOPEN_BINARY)
|
---|
2911 | // if we're in binary mode, we need to add \r too
|
---|
2912 | szTemp[ulLength++] = '\r';
|
---|
2913 | szTemp[ulLength++] = '\n';
|
---|
2914 |
|
---|
2915 | arc = doshWrite(pFile,
|
---|
2916 | ulLength,
|
---|
2917 | szTemp);
|
---|
2918 | }
|
---|
2919 | }
|
---|
2920 | }
|
---|
2921 |
|
---|
2922 | return arc;
|
---|
2923 | }
|
---|
2924 |
|
---|
2925 | /*
|
---|
2926 | * doshClose:
|
---|
2927 | * closes an XFILE opened by doshOpen and
|
---|
2928 | * sets *ppFile to NULL.
|
---|
2929 | *
|
---|
2930 | *@@added V0.9.16 (2001-10-19) [umoeller]
|
---|
2931 | */
|
---|
2932 |
|
---|
2933 | APIRET doshClose(PXFILE *ppFile)
|
---|
2934 | {
|
---|
2935 | APIRET arc = NO_ERROR;
|
---|
2936 | PXFILE pFile;
|
---|
2937 |
|
---|
2938 | if ( (ppFile)
|
---|
2939 | && (pFile = *ppFile)
|
---|
2940 | )
|
---|
2941 | {
|
---|
2942 | // set the ptr to NULL
|
---|
2943 | *ppFile = NULL;
|
---|
2944 |
|
---|
2945 | FREE(pFile->pbCache);
|
---|
2946 | FREE(pFile->pszFilename);
|
---|
2947 |
|
---|
2948 | if (pFile->hf)
|
---|
2949 | {
|
---|
2950 | DosSetFileSize(pFile->hf, pFile->cbCurrent);
|
---|
2951 | DosClose(pFile->hf);
|
---|
2952 | pFile->hf = NULLHANDLE;
|
---|
2953 | }
|
---|
2954 |
|
---|
2955 | free(pFile);
|
---|
2956 | }
|
---|
2957 | else
|
---|
2958 | arc = ERROR_INVALID_PARAMETER;
|
---|
2959 |
|
---|
2960 | return arc;
|
---|
2961 | }
|
---|
2962 |
|
---|
2963 | /*
|
---|
2964 | *@@ doshReadText:
|
---|
2965 | * reads all the contents of the given XFILE into
|
---|
2966 | * a newly allocated buffer. Handles Ctrl-Z properly.
|
---|
2967 | *
|
---|
2968 | * Implementation for doshLoadTextFile, but can
|
---|
2969 | * be called separately now.
|
---|
2970 | *
|
---|
2971 | *@@added V0.9.20 (2002-07-19) [umoeller]
|
---|
2972 | */
|
---|
2973 |
|
---|
2974 | APIRET doshReadText(PXFILE pFile,
|
---|
2975 | PSZ* ppszContent, // out: newly allocated buffer with file's content
|
---|
2976 | PULONG pcbRead) // out: size of that buffer including null byte (ptr can be NULL)
|
---|
2977 | {
|
---|
2978 | APIRET arc;
|
---|
2979 | PSZ pszContent;
|
---|
2980 |
|
---|
2981 | if (!(pszContent = (PSZ)malloc(pFile->cbCurrent + 1)))
|
---|
2982 | arc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
2983 | else
|
---|
2984 | {
|
---|
2985 | ULONG cbRead = 0;
|
---|
2986 | if (!(arc = DosRead(pFile->hf,
|
---|
2987 | pszContent,
|
---|
2988 | pFile->cbCurrent,
|
---|
2989 | &cbRead)))
|
---|
2990 | {
|
---|
2991 | if (cbRead != pFile->cbCurrent)
|
---|
2992 | arc = ERROR_NO_DATA;
|
---|
2993 | else
|
---|
2994 | {
|
---|
2995 | PSZ p;
|
---|
2996 | pszContent[cbRead] = '\0';
|
---|
2997 |
|
---|
2998 | // check if we have a ctrl-z (EOF) marker
|
---|
2999 | // this is present, for example, in config.sys
|
---|
3000 | // after install, and stupid E.EXE writes this
|
---|
3001 | // all the time when saving a file
|
---|
3002 | // V0.9.18 (2002-03-08) [umoeller]
|
---|
3003 | // if (p = strchr(pszContent, '\26')) What the hell is this??? Octal 26 = Decimal 22 != Ctrl-Z
|
---|
3004 | if (p = strchr(pszContent, '\x1A')) // V1.0.4 (2004-12-18) [pr]
|
---|
3005 | {
|
---|
3006 | *p = '\0';
|
---|
3007 | cbRead = p - pszContent;
|
---|
3008 | }
|
---|
3009 |
|
---|
3010 | *ppszContent = pszContent;
|
---|
3011 | if (pcbRead)
|
---|
3012 | *pcbRead = cbRead + 1;
|
---|
3013 | }
|
---|
3014 | }
|
---|
3015 |
|
---|
3016 | if (arc)
|
---|
3017 | free(pszContent);
|
---|
3018 | }
|
---|
3019 |
|
---|
3020 | return arc;
|
---|
3021 | }
|
---|
3022 |
|
---|
3023 | /*
|
---|
3024 | *@@ doshLoadTextFile:
|
---|
3025 | * reads a text file from disk, allocates memory
|
---|
3026 | * via malloc() and sets a pointer to this
|
---|
3027 | * buffer (or NULL upon errors).
|
---|
3028 | *
|
---|
3029 | * This allocates one extra byte to make the
|
---|
3030 | * buffer null-terminated always. The buffer
|
---|
3031 | * is _not_ converted WRT the line format.
|
---|
3032 | *
|
---|
3033 | * If CTRL-Z (ASCII 26) is encountered in the
|
---|
3034 | * content, it is set to the null character
|
---|
3035 | * instead (V0.9.18).
|
---|
3036 | *
|
---|
3037 | * This returns the APIRET of DosOpen and DosRead.
|
---|
3038 | * If any error occurred, no buffer was allocated.
|
---|
3039 | * Otherwise, you should free() the buffer when
|
---|
3040 | * no longer needed.
|
---|
3041 | *
|
---|
3042 | *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from doshReadTextFile
|
---|
3043 | *@@changed V0.9.16 (2002-01-05) [umoeller]: added pcbRead
|
---|
3044 | *@@changed V0.9.16 (2002-01-05) [umoeller]: rewritten using doshOpen
|
---|
3045 | *@@changed V0.9.18 (2002-03-08) [umoeller]: fixed ctrl-z (EOF) bug
|
---|
3046 | */
|
---|
3047 |
|
---|
3048 | APIRET doshLoadTextFile(PCSZ pcszFile, // in: file name to read
|
---|
3049 | PSZ* ppszContent, // out: newly allocated buffer with file's content
|
---|
3050 | PULONG pcbRead) // out: size of that buffer including null byte (ptr can be NULL)
|
---|
3051 | {
|
---|
3052 | APIRET arc;
|
---|
3053 |
|
---|
3054 | ULONG cbFile = 0;
|
---|
3055 | PXFILE pFile = NULL;
|
---|
3056 |
|
---|
3057 | if (!(arc = doshOpen(pcszFile,
|
---|
3058 | XOPEN_READ_EXISTING,
|
---|
3059 | &cbFile,
|
---|
3060 | &pFile)))
|
---|
3061 | {
|
---|
3062 | doshReadText(pFile,
|
---|
3063 | ppszContent,
|
---|
3064 | pcbRead);
|
---|
3065 | doshClose(&pFile);
|
---|
3066 | }
|
---|
3067 |
|
---|
3068 | return arc;
|
---|
3069 | }
|
---|
3070 |
|
---|
3071 | /*
|
---|
3072 | *@@ doshCreateBackupFileName:
|
---|
3073 | * creates a valid backup filename of pszExisting
|
---|
3074 | * with a numerical file name extension which does
|
---|
3075 | * not exist in the directory where pszExisting
|
---|
3076 | * resides.
|
---|
3077 | * Returns a PSZ to a new buffer which was allocated
|
---|
3078 | * using malloc().
|
---|
3079 | *
|
---|
3080 | * <B>Example:</B> returns "C:\CONFIG.002" for input
|
---|
3081 | * "C:\CONFIG.SYS" if "C:\CONFIG.001" already exists.
|
---|
3082 | *
|
---|
3083 | *@@changed V0.9.1 (99-12-13) [umoeller]: this crashed if pszExisting had no file extension
|
---|
3084 | */
|
---|
3085 |
|
---|
3086 | PSZ doshCreateBackupFileName(const char* pszExisting)
|
---|
3087 | {
|
---|
3088 | CHAR szFilename[CCHMAXPATH];
|
---|
3089 | PSZ pszLastDot;
|
---|
3090 | ULONG ulCount = 1;
|
---|
3091 | CHAR szCount[5];
|
---|
3092 | ULONG ulDummy;
|
---|
3093 |
|
---|
3094 | strcpy(szFilename, pszExisting);
|
---|
3095 |
|
---|
3096 | if (!(pszLastDot = strrchr(szFilename, '.')))
|
---|
3097 | // no dot in filename:
|
---|
3098 | pszLastDot = szFilename + strlen(szFilename);
|
---|
3099 |
|
---|
3100 | do
|
---|
3101 | {
|
---|
3102 | sprintf(szCount, ".%03lu", ulCount);
|
---|
3103 | strcpy(pszLastDot, szCount);
|
---|
3104 | ulCount++;
|
---|
3105 | } while (!doshQueryPathSize(szFilename, &ulDummy));
|
---|
3106 |
|
---|
3107 | return (strdup(szFilename));
|
---|
3108 | }
|
---|
3109 |
|
---|
3110 | /*
|
---|
3111 | *@@ doshCreateTempFileName:
|
---|
3112 | * produces a file name in the the specified directory
|
---|
3113 | * or $(TEMP) which presently doesn't exist. This
|
---|
3114 | * checks the directory for existing files, but does
|
---|
3115 | * not open the temp file.
|
---|
3116 | *
|
---|
3117 | * If (pcszDir != NULL), we look into that directory.
|
---|
3118 | * Otherwise we look into the directory specified
|
---|
3119 | * by the $(TEMP) environment variable.
|
---|
3120 | * If $(TEMP) is not set, $(TMP) is tried next.
|
---|
3121 | *
|
---|
3122 | * If the directory thus specified does not exist, the
|
---|
3123 | * root directory of the boot drive is used instead.
|
---|
3124 | * As a result, this function should be fairly bomb-proof.
|
---|
3125 | *
|
---|
3126 | * If (pcszExt != NULL), the temp file receives
|
---|
3127 | * that extension, or no extension otherwise.
|
---|
3128 | * Do not specify the dot in pcszExt.
|
---|
3129 | *
|
---|
3130 | * pszTempFileName receives the fully qualified
|
---|
3131 | * file name of the temp file in that directory
|
---|
3132 | * and must point to a buffer CCHMAXPATH in size.
|
---|
3133 | * The file name is 8+3 compliant if pcszExt does
|
---|
3134 | * not exceed three characters.
|
---|
3135 | *
|
---|
3136 | * If (pcszPrefix != NULL), the temp file name
|
---|
3137 | * is prefixed with pcszPrefix. Since the temp
|
---|
3138 | * file name must not exceed 8+3 letters, we
|
---|
3139 | * can only use ( 8 - strlen(pcszPrefix ) digits
|
---|
3140 | * for a random number to make the temp file name
|
---|
3141 | * unique. You must therefore use a maximum of
|
---|
3142 | * four characters for the prefix. Otherwise
|
---|
3143 | * ERROR_INVALID_PARAMETER is returned.
|
---|
3144 | *
|
---|
3145 | * Example: Assuming TEMP is set to C:\TEMP,
|
---|
3146 | +
|
---|
3147 | + doshCreateTempFileName(szBuffer,
|
---|
3148 | + NULL, // use $(TEMP)
|
---|
3149 | + "pre", // prefix
|
---|
3150 | + "tmp") // extension
|
---|
3151 | +
|
---|
3152 | * would produce something like "C:\TEMP\pre07FG2.tmp".
|
---|
3153 | *
|
---|
3154 | *@@added V0.9.9 (2001-04-04) [umoeller]
|
---|
3155 | */
|
---|
3156 |
|
---|
3157 | APIRET doshCreateTempFileName(PSZ pszTempFileName, // out: fully q'fied temp file name
|
---|
3158 | PCSZ pcszDir, // in: dir or NULL for %TEMP%
|
---|
3159 | PCSZ pcszPrefix, // in: prefix for temp file or NULL
|
---|
3160 | PCSZ pcszExt) // in: extension (without dot) or NULL
|
---|
3161 | {
|
---|
3162 | APIRET arc = NO_ERROR;
|
---|
3163 |
|
---|
3164 | ULONG ulPrefixLen = (pcszPrefix)
|
---|
3165 | ? strlen(pcszPrefix)
|
---|
3166 | : 0;
|
---|
3167 |
|
---|
3168 | if ( (!pszTempFileName)
|
---|
3169 | || (ulPrefixLen > 4)
|
---|
3170 | )
|
---|
3171 | arc = ERROR_INVALID_PARAMETER;
|
---|
3172 | else
|
---|
3173 | {
|
---|
3174 | CHAR szDir[CCHMAXPATH] = "";
|
---|
3175 | FILESTATUS3 fs3;
|
---|
3176 |
|
---|
3177 | const char *pcszTemp = pcszDir;
|
---|
3178 |
|
---|
3179 | if (!pcszTemp)
|
---|
3180 | {
|
---|
3181 | if (!(pcszTemp = getenv("TEMP")))
|
---|
3182 | pcszTemp = getenv("TMP");
|
---|
3183 | }
|
---|
3184 |
|
---|
3185 | if (pcszTemp) // either pcszDir or $(TEMP) or $(TMP) now
|
---|
3186 | if (DosQueryPathInfo((PSZ)pcszTemp,
|
---|
3187 | FIL_STANDARD,
|
---|
3188 | &fs3,
|
---|
3189 | sizeof(fs3)))
|
---|
3190 | // TEMP doesn't exist:
|
---|
3191 | pcszTemp = NULL;
|
---|
3192 |
|
---|
3193 | if (!pcszTemp)
|
---|
3194 | // not set, or doesn't exist:
|
---|
3195 | // use root directory on boot drive
|
---|
3196 | sprintf(szDir,
|
---|
3197 | "%c:\\",
|
---|
3198 | doshQueryBootDrive());
|
---|
3199 | else
|
---|
3200 | {
|
---|
3201 | strcpy(szDir, pcszTemp);
|
---|
3202 | if (szDir[strlen(szDir) - 1] != '\\')
|
---|
3203 | strcat(szDir, "\\");
|
---|
3204 | }
|
---|
3205 |
|
---|
3206 | if (!szDir[0])
|
---|
3207 | arc = ERROR_PATH_NOT_FOUND; // shouldn't happen
|
---|
3208 | else
|
---|
3209 | {
|
---|
3210 | ULONG ulRandom = 0;
|
---|
3211 | ULONG cAttempts = 0;
|
---|
3212 |
|
---|
3213 | // produce random number
|
---|
3214 | DosQuerySysInfo(QSV_MS_COUNT,
|
---|
3215 | QSV_MS_COUNT,
|
---|
3216 | &ulRandom,
|
---|
3217 | sizeof(ulRandom));
|
---|
3218 |
|
---|
3219 | do
|
---|
3220 | {
|
---|
3221 | CHAR szFile[20] = "",
|
---|
3222 | szFullTryThis[CCHMAXPATH];
|
---|
3223 |
|
---|
3224 | // use the lower eight hex digits of the
|
---|
3225 | // system uptime as the temp dir name
|
---|
3226 | sprintf(szFile,
|
---|
3227 | "%08lX",
|
---|
3228 | ulRandom & 0xFFFFFFFF);
|
---|
3229 |
|
---|
3230 | // if prefix is specified, overwrite the
|
---|
3231 | // first characters in the random number
|
---|
3232 | if (pcszPrefix)
|
---|
3233 | memcpy(szFile, pcszPrefix, ulPrefixLen);
|
---|
3234 |
|
---|
3235 | if (pcszExt)
|
---|
3236 | {
|
---|
3237 | szFile[8] = '.';
|
---|
3238 | strcpy(szFile + 9, pcszExt);
|
---|
3239 | }
|
---|
3240 |
|
---|
3241 | // now compose full temp file name
|
---|
3242 | strcpy(szFullTryThis, szDir);
|
---|
3243 | strcat(szFullTryThis, szFile);
|
---|
3244 | // now we have: "C:\temp\wpiXXXXX"
|
---|
3245 | if (DosQueryPathInfo(szFullTryThis,
|
---|
3246 | FIL_STANDARD,
|
---|
3247 | &fs3,
|
---|
3248 | sizeof(fs3))
|
---|
3249 | == ERROR_FILE_NOT_FOUND)
|
---|
3250 | {
|
---|
3251 | // file or dir doesn't exist:
|
---|
3252 | // cool, we're done
|
---|
3253 | strcpy(pszTempFileName, szFullTryThis);
|
---|
3254 | return NO_ERROR;
|
---|
3255 | }
|
---|
3256 |
|
---|
3257 | // if this didn't work, raise ulRandom and try again
|
---|
3258 | ulRandom += 123;
|
---|
3259 |
|
---|
3260 | // try only 100 times, just to be sure
|
---|
3261 | cAttempts++;
|
---|
3262 | } while (cAttempts < 100);
|
---|
3263 |
|
---|
3264 | // 100 loops elapsed:
|
---|
3265 | arc = ERROR_BAD_FORMAT;
|
---|
3266 | }
|
---|
3267 | }
|
---|
3268 |
|
---|
3269 | return arc;
|
---|
3270 | }
|
---|
3271 |
|
---|
3272 | /*
|
---|
3273 | *@@ doshWriteTextFile:
|
---|
3274 | * writes a text file to disk; pszFile must contain the
|
---|
3275 | * whole path and filename.
|
---|
3276 | *
|
---|
3277 | * An existing file will be backed up if (pszBackup != NULL),
|
---|
3278 | * using doshCreateBackupFileName. In that case, pszBackup
|
---|
3279 | * receives the name of the backup created, so that buffer
|
---|
3280 | * should be CCHMAXPATH in size.
|
---|
3281 | *
|
---|
3282 | * If (pszbackup == NULL), an existing file will be overwritten.
|
---|
3283 | *
|
---|
3284 | * On success (NO_ERROR returned), *pulWritten receives
|
---|
3285 | * the no. of bytes written.
|
---|
3286 | *
|
---|
3287 | *@@changed V0.9.3 (2000-05-01) [umoeller]: optimized DosOpen; added error checking; changed prototype
|
---|
3288 | *@@changed V0.9.3 (2000-05-12) [umoeller]: added pszBackup
|
---|
3289 | */
|
---|
3290 |
|
---|
3291 | APIRET doshWriteTextFile(const char* pszFile, // in: file name
|
---|
3292 | const char* pszContent, // in: text to write
|
---|
3293 | PULONG pulWritten, // out: bytes written (ptr can be NULL)
|
---|
3294 | PSZ pszBackup) // in/out: create-backup?
|
---|
3295 | {
|
---|
3296 | APIRET arc = NO_ERROR;
|
---|
3297 | ULONG ulWritten = 0;
|
---|
3298 |
|
---|
3299 | if ((!pszFile) || (!pszContent))
|
---|
3300 | arc = ERROR_INVALID_PARAMETER;
|
---|
3301 | else
|
---|
3302 | {
|
---|
3303 | ULONG ulAction = 0,
|
---|
3304 | ulLocal = 0;
|
---|
3305 | HFILE hFile = 0;
|
---|
3306 |
|
---|
3307 | ULONG ulSize = strlen(pszContent); // exclude 0 byte
|
---|
3308 |
|
---|
3309 | if (pszBackup)
|
---|
3310 | {
|
---|
3311 | PSZ pszBackup2 = doshCreateBackupFileName(pszFile);
|
---|
3312 | DosCopy((PSZ)pszFile,
|
---|
3313 | pszBackup2,
|
---|
3314 | DCPY_EXISTING); // delete existing
|
---|
3315 | strcpy(pszBackup, pszBackup2);
|
---|
3316 | free(pszBackup2);
|
---|
3317 | }
|
---|
3318 |
|
---|
3319 | if (!(arc = DosOpen((PSZ)pszFile,
|
---|
3320 | &hFile,
|
---|
3321 | &ulAction, // action taken
|
---|
3322 | ulSize, // primary allocation size
|
---|
3323 | FILE_ARCHIVED | FILE_NORMAL, // file attribute
|
---|
3324 | OPEN_ACTION_CREATE_IF_NEW
|
---|
3325 | | OPEN_ACTION_REPLACE_IF_EXISTS, // open flags
|
---|
3326 | OPEN_FLAGS_NOINHERIT
|
---|
3327 | | OPEN_FLAGS_SEQUENTIAL // sequential, not random access
|
---|
3328 | | OPEN_SHARE_DENYWRITE // deny write mode
|
---|
3329 | | OPEN_ACCESS_WRITEONLY, // write mode
|
---|
3330 | NULL))) // no EAs
|
---|
3331 | {
|
---|
3332 | if (!(arc = DosSetFilePtr(hFile,
|
---|
3333 | 0L,
|
---|
3334 | FILE_BEGIN,
|
---|
3335 | &ulLocal)))
|
---|
3336 | if (!(arc = DosWrite(hFile,
|
---|
3337 | (PVOID)pszContent,
|
---|
3338 | ulSize,
|
---|
3339 | &ulWritten)))
|
---|
3340 | arc = DosSetFileSize(hFile, ulSize);
|
---|
3341 |
|
---|
3342 | DosClose(hFile);
|
---|
3343 | }
|
---|
3344 | } // end if ((pszFile) && (pszContent))
|
---|
3345 |
|
---|
3346 | if (pulWritten)
|
---|
3347 | *pulWritten = ulWritten;
|
---|
3348 |
|
---|
3349 | return arc;
|
---|
3350 | }
|
---|
3351 |
|
---|
3352 | /*
|
---|
3353 | *@@category: Helpers\Control program helpers\Directory management
|
---|
3354 | * directory helpers (querying, creating, deleting etc.).
|
---|
3355 | */
|
---|
3356 |
|
---|
3357 | /* ******************************************************************
|
---|
3358 | *
|
---|
3359 | * Directory helpers
|
---|
3360 | *
|
---|
3361 | ********************************************************************/
|
---|
3362 |
|
---|
3363 | /*
|
---|
3364 | *@@ doshQueryDirExist:
|
---|
3365 | * returns TRUE if the given directory
|
---|
3366 | * exists and really is a directory.
|
---|
3367 | */
|
---|
3368 |
|
---|
3369 | BOOL doshQueryDirExist(PCSZ pcszDir)
|
---|
3370 | {
|
---|
3371 | FILESTATUS3 fs3;
|
---|
3372 | APIRET arc;
|
---|
3373 |
|
---|
3374 | if (!(arc = DosQueryPathInfo((PSZ)pcszDir,
|
---|
3375 | FIL_STANDARD,
|
---|
3376 | &fs3,
|
---|
3377 | sizeof(fs3))))
|
---|
3378 | // file found:
|
---|
3379 | return ((fs3.attrFile & FILE_DIRECTORY) != 0);
|
---|
3380 | else
|
---|
3381 | return FALSE;
|
---|
3382 | }
|
---|
3383 |
|
---|
3384 | /*
|
---|
3385 | *@@ doshCreatePath:
|
---|
3386 | * this creates the specified directory.
|
---|
3387 | * As opposed to DosCreateDir, this
|
---|
3388 | * function can create several directories
|
---|
3389 | * at the same time, if the parent
|
---|
3390 | * directories do not exist yet.
|
---|
3391 | */
|
---|
3392 |
|
---|
3393 | APIRET doshCreatePath(PCSZ pcszPath,
|
---|
3394 | BOOL fHidden) // in: if TRUE, the new directories will get FILE_HIDDEN
|
---|
3395 | {
|
---|
3396 | APIRET arc0 = NO_ERROR;
|
---|
3397 | CHAR path[CCHMAXPATH];
|
---|
3398 | CHAR *cp, c;
|
---|
3399 | ULONG cbPath;
|
---|
3400 |
|
---|
3401 | strcpy(path, pcszPath);
|
---|
3402 | cbPath = strlen(path);
|
---|
3403 |
|
---|
3404 | if (path[cbPath] != '\\')
|
---|
3405 | {
|
---|
3406 | path[cbPath] = '\\';
|
---|
3407 | path[cbPath + 1] = 0;
|
---|
3408 | }
|
---|
3409 |
|
---|
3410 | cp = path;
|
---|
3411 | // advance past the drive letter only if we have one
|
---|
3412 | if (*(cp+1) == ':')
|
---|
3413 | cp += 3;
|
---|
3414 |
|
---|
3415 | // go!
|
---|
3416 | while (*cp != 0)
|
---|
3417 | {
|
---|
3418 | if (*cp == '\\')
|
---|
3419 | {
|
---|
3420 | c = *cp;
|
---|
3421 | *cp = 0;
|
---|
3422 | if (!doshQueryDirExist(path))
|
---|
3423 | {
|
---|
3424 | APIRET arc = DosCreateDir(path,
|
---|
3425 | 0); // no EAs
|
---|
3426 | if (arc != NO_ERROR)
|
---|
3427 | {
|
---|
3428 | arc0 = arc;
|
---|
3429 | break;
|
---|
3430 | }
|
---|
3431 | else
|
---|
3432 | if (fHidden)
|
---|
3433 | {
|
---|
3434 | // hide the directory we just created
|
---|
3435 | doshSetPathAttr(path, FILE_HIDDEN);
|
---|
3436 | }
|
---|
3437 | }
|
---|
3438 | *cp = c;
|
---|
3439 | }
|
---|
3440 | cp++;
|
---|
3441 | }
|
---|
3442 |
|
---|
3443 | return (arc0);
|
---|
3444 | }
|
---|
3445 |
|
---|
3446 | /*
|
---|
3447 | *@@ doshQueryCurrentDir:
|
---|
3448 | * writes the current directory into
|
---|
3449 | * the specified buffer, which should be
|
---|
3450 | * CCHMAXPATH in size.
|
---|
3451 | *
|
---|
3452 | * As opposed to DosQueryCurrentDir, this
|
---|
3453 | * includes the drive letter.
|
---|
3454 | *
|
---|
3455 | *@@added V0.9.4 (2000-07-22) [umoeller]
|
---|
3456 | */
|
---|
3457 |
|
---|
3458 | APIRET doshQueryCurrentDir(PSZ pszBuf)
|
---|
3459 | {
|
---|
3460 | APIRET arc = NO_ERROR;
|
---|
3461 | ULONG ulCurDisk = 0;
|
---|
3462 | ULONG ulMap = 0;
|
---|
3463 | if (!(arc = DosQueryCurrentDisk(&ulCurDisk, &ulMap)))
|
---|
3464 | {
|
---|
3465 | ULONG cbBuf = CCHMAXPATH - 3;
|
---|
3466 | pszBuf[0] = ulCurDisk + 'A' - 1;
|
---|
3467 | pszBuf[1] = ':';
|
---|
3468 | pszBuf[2] = '\\';
|
---|
3469 | pszBuf[3] = '\0';
|
---|
3470 | arc = DosQueryCurrentDir(0, pszBuf + 3, &cbBuf);
|
---|
3471 | }
|
---|
3472 |
|
---|
3473 | return arc;
|
---|
3474 | }
|
---|
3475 |
|
---|
3476 | /*
|
---|
3477 | *@@ doshDeleteDir:
|
---|
3478 | * deletes a directory. As opposed to DosDeleteDir,
|
---|
3479 | * this removes empty subdirectories and/or files
|
---|
3480 | * as well.
|
---|
3481 | *
|
---|
3482 | * flFlags can be any combination of the following:
|
---|
3483 | *
|
---|
3484 | * -- DOSHDELDIR_RECURSE: recurse into subdirectories.
|
---|
3485 | *
|
---|
3486 | * -- DOSHDELDIR_DELETEFILES: delete all regular files
|
---|
3487 | * which are found on the way.
|
---|
3488 | *
|
---|
3489 | * THIS IS NOT IMPLEMENTED YET.
|
---|
3490 | *
|
---|
3491 | * If 0 is specified, this effectively behaves just as
|
---|
3492 | * DosDeleteDir.
|
---|
3493 | *
|
---|
3494 | * If you specify DOSHDELDIR_RECURSE only, only empty
|
---|
3495 | * subdirectories are deleted as well.
|
---|
3496 | *
|
---|
3497 | * If you specify DOSHDELDIR_RECURSE | DOSHDELDIR_DELETEFILES,
|
---|
3498 | * this removes an entire directory tree, including all
|
---|
3499 | * subdirectories and files.
|
---|
3500 | *
|
---|
3501 | *@@added V0.9.4 (2000-07-01) [umoeller]
|
---|
3502 | *@@changed WarpIN V1.0.15 (2007-03-27) [pr]: allow NULL pulDirs and pulFiles
|
---|
3503 | *@@changed WarpIN V1.0.16 (2007-07-28) [pr]: bump directory count on recursive delete failures @@fixes 1001
|
---|
3504 | *@@changed WarpIN V1.0.20 (2012-03-29) [pr]: path not found no longer an error
|
---|
3505 | */
|
---|
3506 |
|
---|
3507 | APIRET doshDeleteDir(PCSZ pcszDir,
|
---|
3508 | ULONG flFlags,
|
---|
3509 | PULONG pulDirs, // out: directories found
|
---|
3510 | PULONG pulFiles) // out: files found
|
---|
3511 | {
|
---|
3512 | APIRET arc = NO_ERROR,
|
---|
3513 | arcReturn = NO_ERROR;
|
---|
3514 |
|
---|
3515 | HDIR hdirFindHandle = HDIR_CREATE;
|
---|
3516 | FILEFINDBUF3 ffb3 = {0}; // returned from FindFirst/Next
|
---|
3517 | ULONG ulResultBufLen = sizeof(FILEFINDBUF3);
|
---|
3518 | ULONG ulFindCount = 1; // look for 1 file at a time
|
---|
3519 |
|
---|
3520 | CHAR szFileMask[2*CCHMAXPATH];
|
---|
3521 | sprintf(szFileMask, "%s\\*", pcszDir);
|
---|
3522 |
|
---|
3523 | // find files
|
---|
3524 | arc = DosFindFirst(szFileMask,
|
---|
3525 | &hdirFindHandle, // directory search handle
|
---|
3526 | FILE_ARCHIVED | FILE_DIRECTORY | FILE_SYSTEM
|
---|
3527 | | FILE_HIDDEN | FILE_READONLY,
|
---|
3528 | // search attributes; include all, even dirs
|
---|
3529 | &ffb3, // result buffer
|
---|
3530 | ulResultBufLen, // result buffer length
|
---|
3531 | &ulFindCount, // number of entries to find
|
---|
3532 | FIL_STANDARD); // return level 1 file info
|
---|
3533 |
|
---|
3534 | if (arc == NO_ERROR)
|
---|
3535 | {
|
---|
3536 | // keep finding the next file until there are no more files
|
---|
3537 | while (arc == NO_ERROR) // != ERROR_NO_MORE_FILES
|
---|
3538 | {
|
---|
3539 | if (ffb3.attrFile & FILE_DIRECTORY)
|
---|
3540 | {
|
---|
3541 | // we found a directory:
|
---|
3542 |
|
---|
3543 | // ignore the pseudo-directories
|
---|
3544 | if ( (strcmp(ffb3.achName, ".") != 0)
|
---|
3545 | && (strcmp(ffb3.achName, "..") != 0)
|
---|
3546 | )
|
---|
3547 | {
|
---|
3548 | // real directory:
|
---|
3549 | if (flFlags & DOSHDELDIR_RECURSE)
|
---|
3550 | {
|
---|
3551 | // recurse!
|
---|
3552 | CHAR szSubDir[2*CCHMAXPATH];
|
---|
3553 | sprintf(szSubDir, "%s\\%s", pcszDir, ffb3.achName);
|
---|
3554 | arcReturn = doshDeleteDir(szSubDir,
|
---|
3555 | flFlags,
|
---|
3556 | pulDirs,
|
---|
3557 | pulFiles);
|
---|
3558 | // this removes ffb3.achName as well
|
---|
3559 | }
|
---|
3560 | else
|
---|
3561 | {
|
---|
3562 | // directory, but no recursion:
|
---|
3563 | // report "access denied"
|
---|
3564 | // (this is what OS/2 reports with DosDeleteDir as well)
|
---|
3565 | arcReturn = ERROR_ACCESS_DENIED; // 5
|
---|
3566 | }
|
---|
3567 |
|
---|
3568 | if (arcReturn != NO_ERROR) // WarpIN V1.0.16 (2007-07-28) [pr]
|
---|
3569 | if (pulDirs) // WarpIN V1.0.15 (2007-03-27) [pr]
|
---|
3570 | (*pulDirs)++;
|
---|
3571 | }
|
---|
3572 | }
|
---|
3573 | else
|
---|
3574 | {
|
---|
3575 | // it's a file:
|
---|
3576 | arcReturn = ERROR_ACCESS_DENIED;
|
---|
3577 | if (pulFiles) // WarpIN V1.0.15 (2007-03-27) [pr]
|
---|
3578 | (*pulFiles)++;
|
---|
3579 | }
|
---|
3580 |
|
---|
3581 | if (arc == NO_ERROR)
|
---|
3582 | {
|
---|
3583 | ulFindCount = 1; // reset find count
|
---|
3584 | arc = DosFindNext(hdirFindHandle, // directory handle
|
---|
3585 | &ffb3, // result buffer
|
---|
3586 | ulResultBufLen, // result buffer length
|
---|
3587 | &ulFindCount); // number of entries to find
|
---|
3588 | }
|
---|
3589 | } // endwhile
|
---|
3590 |
|
---|
3591 | DosFindClose(hdirFindHandle); // close our find handle
|
---|
3592 | }
|
---|
3593 |
|
---|
3594 | if (arcReturn == NO_ERROR)
|
---|
3595 | {
|
---|
3596 | // success so far:
|
---|
3597 | // delete our directory now
|
---|
3598 | arcReturn = DosDeleteDir((PSZ)pcszDir);
|
---|
3599 | if (arcReturn == ERROR_PATH_NOT_FOUND) // WarpIN V1.0.20 (2012-03-29) [pr]
|
---|
3600 | arcReturn = NO_ERROR;
|
---|
3601 | }
|
---|
3602 |
|
---|
3603 | return (arcReturn);
|
---|
3604 | }
|
---|
3605 |
|
---|
3606 | /*
|
---|
3607 | *@@ doshCanonicalize:
|
---|
3608 | * simplifies path specifications to remove '.'
|
---|
3609 | * and '..' entries and generates a fully
|
---|
3610 | * qualified path name where possible.
|
---|
3611 | * File specifications are left unchanged.
|
---|
3612 | *
|
---|
3613 | * This returns:
|
---|
3614 | *
|
---|
3615 | * -- NO_ERROR: the buffers were valid.
|
---|
3616 | *
|
---|
3617 | * -- ERROR_INVALID_PARAMETER: the buffers
|
---|
3618 | * were invalid.
|
---|
3619 | *
|
---|
3620 | *@@added V0.9.19 (2002-04-22) [pr]
|
---|
3621 | */
|
---|
3622 |
|
---|
3623 | APIRET doshCanonicalize(PCSZ pcszFileIn, // in: path to canonicalize
|
---|
3624 | PSZ pszFileOut, // out: canonicalized path if NO_ERROR
|
---|
3625 | ULONG cbFileOut) // in: size of pszFileOut buffer
|
---|
3626 | {
|
---|
3627 | APIRET ulrc = NO_ERROR;
|
---|
3628 | CHAR szFileTemp[CCHMAXPATH];
|
---|
3629 |
|
---|
3630 | if (pcszFileIn && pszFileOut && cbFileOut)
|
---|
3631 | {
|
---|
3632 | strncpy(szFileTemp, pcszFileIn, sizeof(szFileTemp) - 1);
|
---|
3633 | szFileTemp[sizeof(szFileTemp) - 1] = 0;
|
---|
3634 | if ( strchr(szFileTemp, '\\')
|
---|
3635 | || strchr(szFileTemp, ':')
|
---|
3636 | )
|
---|
3637 | {
|
---|
3638 | ULONG cbFileTemp = strlen(szFileTemp);
|
---|
3639 |
|
---|
3640 | if ( (cbFileTemp > 3)
|
---|
3641 | && (szFileTemp[cbFileTemp - 1] == '\\')
|
---|
3642 | )
|
---|
3643 | {
|
---|
3644 | szFileTemp[cbFileTemp - 1] = 0;
|
---|
3645 | }
|
---|
3646 |
|
---|
3647 | if (DosQueryPathInfo(szFileTemp,
|
---|
3648 | FIL_QUERYFULLNAME,
|
---|
3649 | pszFileOut,
|
---|
3650 | cbFileOut))
|
---|
3651 | {
|
---|
3652 | pszFileOut[0] = 0;
|
---|
3653 | }
|
---|
3654 | }
|
---|
3655 | else
|
---|
3656 | {
|
---|
3657 | strncpy(pszFileOut, pcszFileIn, cbFileOut - 1);
|
---|
3658 | pszFileOut[cbFileOut - 1] = 0;
|
---|
3659 | }
|
---|
3660 | }
|
---|
3661 | else
|
---|
3662 | ulrc = ERROR_INVALID_PARAMETER;
|
---|
3663 |
|
---|
3664 | return(ulrc);
|
---|
3665 | }
|
---|
3666 |
|
---|
3667 | /*
|
---|
3668 | *@@category: Helpers\Control program helpers\Module handling
|
---|
3669 | * helpers for importing functions from a module (DLL).
|
---|
3670 | */
|
---|
3671 |
|
---|
3672 | /* ******************************************************************
|
---|
3673 | *
|
---|
3674 | * Module handling helpers
|
---|
3675 | *
|
---|
3676 | ********************************************************************/
|
---|
3677 |
|
---|
3678 | /*
|
---|
3679 | *@@ doshQueryProcAddr:
|
---|
3680 | * attempts to resolve the given procedure from
|
---|
3681 | * the given module name. Saves you from querying
|
---|
3682 | * the module handle and all that.
|
---|
3683 | *
|
---|
3684 | * This is intended for resolving undocumented
|
---|
3685 | * APIs from OS/2 system DLls such as PMMERGE
|
---|
3686 | * and DOSCALLS. It is assumed that the specified
|
---|
3687 | * module is already loaded.
|
---|
3688 | *
|
---|
3689 | * Returns:
|
---|
3690 | *
|
---|
3691 | * -- NO_ERROR
|
---|
3692 | *
|
---|
3693 | * -- ERROR_INVALID_NAME
|
---|
3694 | *
|
---|
3695 | * -- ERROR_INVALID_ORDINAL
|
---|
3696 | *
|
---|
3697 | * plus the error codes of DosLoadModule.
|
---|
3698 | *
|
---|
3699 | *@@added V0.9.16 (2002-01-13) [umoeller]
|
---|
3700 | */
|
---|
3701 |
|
---|
3702 | APIRET doshQueryProcAddr(PCSZ pcszModuleName, // in: module name (e.g. "PMMERGE")
|
---|
3703 | ULONG ulOrdinal, // in: proc ordinal
|
---|
3704 | PFN *ppfn) // out: proc address
|
---|
3705 | {
|
---|
3706 | HMODULE hmod;
|
---|
3707 | APIRET arc;
|
---|
3708 | if (!(arc = DosQueryModuleHandle((PSZ)pcszModuleName,
|
---|
3709 | &hmod)))
|
---|
3710 | {
|
---|
3711 | if ((arc = DosQueryProcAddr(hmod,
|
---|
3712 | ulOrdinal,
|
---|
3713 | NULL,
|
---|
3714 | ppfn)))
|
---|
3715 | {
|
---|
3716 | // the CP programming guide and reference says use
|
---|
3717 | // DosLoadModule if DosQueryProcAddr fails with this error
|
---|
3718 | if (arc == ERROR_INVALID_HANDLE)
|
---|
3719 | {
|
---|
3720 | if (!(arc = DosLoadModule(NULL,
|
---|
3721 | 0,
|
---|
3722 | (PSZ)pcszModuleName,
|
---|
3723 | &hmod)))
|
---|
3724 | {
|
---|
3725 | arc = DosQueryProcAddr(hmod,
|
---|
3726 | ulOrdinal,
|
---|
3727 | NULL,
|
---|
3728 | ppfn);
|
---|
3729 | }
|
---|
3730 | }
|
---|
3731 | }
|
---|
3732 | }
|
---|
3733 |
|
---|
3734 | return arc;
|
---|
3735 | }
|
---|
3736 |
|
---|
3737 | /*
|
---|
3738 | *@@ doshResolveImports:
|
---|
3739 | * this function loads the module called pszModuleName
|
---|
3740 | * and resolves imports dynamically using DosQueryProcAddress.
|
---|
3741 | *
|
---|
3742 | * To specify the functions to be imported, a RESOLVEFUNCTION
|
---|
3743 | * array is used. In each of the array items, specify the
|
---|
3744 | * name of the function and a pointer to a function pointer
|
---|
3745 | * where to store the resolved address.
|
---|
3746 | *
|
---|
3747 | *@@added V0.9.3 (2000-04-29) [umoeller]
|
---|
3748 | */
|
---|
3749 |
|
---|
3750 | APIRET doshResolveImports(PCSZ pcszModuleName, // in: DLL to load
|
---|
3751 | HMODULE *phmod, // out: module handle
|
---|
3752 | PCRESOLVEFUNCTION paResolves, // in/out: function resolves
|
---|
3753 | ULONG cResolves) // in: array item count (not array size!)
|
---|
3754 | {
|
---|
3755 | CHAR szName[CCHMAXPATH];
|
---|
3756 | APIRET arc;
|
---|
3757 |
|
---|
3758 | if (!(arc = DosLoadModule(szName,
|
---|
3759 | sizeof(szName),
|
---|
3760 | (PSZ)pcszModuleName,
|
---|
3761 | phmod)))
|
---|
3762 | {
|
---|
3763 | ULONG ul;
|
---|
3764 | for (ul = 0;
|
---|
3765 | ul < cResolves;
|
---|
3766 | ul++)
|
---|
3767 | {
|
---|
3768 | if (arc = DosQueryProcAddr(*phmod,
|
---|
3769 | 0, // ordinal, ignored
|
---|
3770 | (PSZ)paResolves[ul].pcszFunctionName,
|
---|
3771 | paResolves[ul].ppFuncAddress))
|
---|
3772 | // error:
|
---|
3773 | break;
|
---|
3774 | }
|
---|
3775 |
|
---|
3776 | if (arc)
|
---|
3777 | // V0.9.16 (2001-12-08) [umoeller]
|
---|
3778 | DosFreeModule(*phmod);
|
---|
3779 | }
|
---|
3780 |
|
---|
3781 | return arc;
|
---|
3782 | }
|
---|
3783 |
|
---|
3784 | /*
|
---|
3785 | *@@category: Helpers\Control program helpers\Performance (CPU load) helpers
|
---|
3786 | * helpers around DosPerfSysCall.
|
---|
3787 | */
|
---|
3788 |
|
---|
3789 | /* ******************************************************************
|
---|
3790 | *
|
---|
3791 | * Performance Counters (CPU Load)
|
---|
3792 | *
|
---|
3793 | ********************************************************************/
|
---|
3794 |
|
---|
3795 | /*
|
---|
3796 | *@@ doshPerfOpen:
|
---|
3797 | * initializes the OS/2 DosPerfSysCall API for
|
---|
3798 | * the calling thread.
|
---|
3799 | *
|
---|
3800 | * Note: This API is not supported on all OS/2
|
---|
3801 | * versions. I believe it came up with some Warp 4
|
---|
3802 | * fixpak. The API is resolved dynamically by
|
---|
3803 | * this function (using DosQueryProcAddr).
|
---|
3804 | *
|
---|
3805 | * This properly initializes the internal counters
|
---|
3806 | * which the OS/2 kernel uses for this API. Apparently,
|
---|
3807 | * with newer kernels (FP13/14), IBM has chosen to no
|
---|
3808 | * longer do this automatically, which is the reason
|
---|
3809 | * why many "pulse" utilities display garbage with these
|
---|
3810 | * fixpaks.
|
---|
3811 | *
|
---|
3812 | * After NO_ERROR is returned, DOSHPERFSYS.cProcessors
|
---|
3813 | * contains the no. of processors found on the system.
|
---|
3814 | * All pointers in DOSHPERFSYS then point to arrays
|
---|
3815 | * which have exactly cProcessors array items.
|
---|
3816 | *
|
---|
3817 | * So after NO_ERROR was returned here, you can keep
|
---|
3818 | * calling doshPerfGet to get a current snapshot of the
|
---|
3819 | * IRQ and user loads for each CPU. Note that interrupts
|
---|
3820 | * are only processed on CPU 0 on SMP systems.
|
---|
3821 | *
|
---|
3822 | * Call doshPerfClose to clean up resources allocated
|
---|
3823 | * by this function.
|
---|
3824 | *
|
---|
3825 | * For more sample code, take a look at the "Pulse" widget
|
---|
3826 | * in the XWorkplace sources (src\xcenter\w_pulse.c).
|
---|
3827 | *
|
---|
3828 | * Example code:
|
---|
3829 | *
|
---|
3830 | + PDOSHPERFSYS pPerf = NULL;
|
---|
3831 | + APIRET arc;
|
---|
3832 | + if (!(arc = arc = doshPerfOpen(&pPerf)))
|
---|
3833 | + {
|
---|
3834 | + // this should really be in a timer,
|
---|
3835 | + // e.g. once per second
|
---|
3836 | + ULONG ulCPU;
|
---|
3837 | + if (!(arc = doshPerfGet(&pPerf)))
|
---|
3838 | + {
|
---|
3839 | + // go thru all CPUs
|
---|
3840 | + for (ulCPU = 0; ulCPU < pPerf->cProcessors; ulCPU++)
|
---|
3841 | + {
|
---|
3842 | + LONG lUserLoadThis = pPerf->palLoads[ulCPU];
|
---|
3843 | + ...
|
---|
3844 | + }
|
---|
3845 | + }
|
---|
3846 | +
|
---|
3847 | + // clean up
|
---|
3848 | + doshPerfClose(&pPerf);
|
---|
3849 | + }
|
---|
3850 | +
|
---|
3851 | *
|
---|
3852 | *@@added V0.9.7 (2000-12-02) [umoeller]
|
---|
3853 | *@@changed V0.9.9 (2001-03-14) [umoeller]: added interrupt loads; thanks phaller
|
---|
3854 | */
|
---|
3855 |
|
---|
3856 | APIRET doshPerfOpen(PDOSHPERFSYS *ppPerfSys) // out: new DOSHPERFSYS structure
|
---|
3857 | {
|
---|
3858 | APIRET arc = NO_ERROR;
|
---|
3859 |
|
---|
3860 | // allocate DOSHPERFSYS structure
|
---|
3861 | *ppPerfSys = (PDOSHPERFSYS)malloc(sizeof(DOSHPERFSYS));
|
---|
3862 | if (!*ppPerfSys)
|
---|
3863 | arc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
3864 | else
|
---|
3865 | {
|
---|
3866 | // initialize structure
|
---|
3867 | PDOSHPERFSYS pPerfSys = *ppPerfSys;
|
---|
3868 | memset(pPerfSys, 0, sizeof(*pPerfSys));
|
---|
3869 |
|
---|
3870 | // resolve DosPerfSysCall API entry
|
---|
3871 | if (!(arc = DosLoadModule(NULL,
|
---|
3872 | 0,
|
---|
3873 | "DOSCALLS",
|
---|
3874 | &pPerfSys->hmod)))
|
---|
3875 | {
|
---|
3876 | if (!(arc = DosQueryProcAddr(pPerfSys->hmod,
|
---|
3877 | 976,
|
---|
3878 | "DosPerfSysCall",
|
---|
3879 | (PFN*)&pPerfSys->pDosPerfSysCall)))
|
---|
3880 | {
|
---|
3881 | // OK, we got the API: initialize!
|
---|
3882 | if (!(arc = pPerfSys->pDosPerfSysCall(CMD_KI_ENABLE,
|
---|
3883 | 0,
|
---|
3884 | 0,
|
---|
3885 | 0)))
|
---|
3886 | {
|
---|
3887 | pPerfSys->fInitialized = TRUE;
|
---|
3888 | // call CMD_KI_DISABLE later
|
---|
3889 |
|
---|
3890 | if (!(arc = pPerfSys->pDosPerfSysCall(CMD_PERF_INFO,
|
---|
3891 | 0,
|
---|
3892 | (ULONG)&pPerfSys->cProcessors,
|
---|
3893 | 0)))
|
---|
3894 | {
|
---|
3895 | ULONG ul = 0,
|
---|
3896 | cProcs = pPerfSys->cProcessors,
|
---|
3897 | cbDouble = cProcs * sizeof(double),
|
---|
3898 | cbLong = cProcs * sizeof(LONG);
|
---|
3899 |
|
---|
3900 | // allocate arrays
|
---|
3901 | if ( (!(pPerfSys->paCPUUtils = (PCPUUTIL)calloc(cProcs,
|
---|
3902 | sizeof(CPUUTIL))))
|
---|
3903 | || (!(pPerfSys->padBusyPrev
|
---|
3904 | = (double*)malloc(cbDouble)))
|
---|
3905 | || (!(pPerfSys->padTimePrev
|
---|
3906 | = (double*)malloc(cbDouble)))
|
---|
3907 | || (!(pPerfSys->padIntrPrev
|
---|
3908 | = (double*)malloc(cbDouble)))
|
---|
3909 | || (!(pPerfSys->palLoads
|
---|
3910 | = (PLONG)malloc(cbLong)))
|
---|
3911 | || (!(pPerfSys->palIntrs
|
---|
3912 | = (PLONG)malloc(cbLong)))
|
---|
3913 | )
|
---|
3914 | arc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
3915 | else
|
---|
3916 | {
|
---|
3917 | for (ul = 0; ul < cProcs; ul++)
|
---|
3918 | {
|
---|
3919 | pPerfSys->padBusyPrev[ul] = 0.0;
|
---|
3920 | pPerfSys->padTimePrev[ul] = 0.0;
|
---|
3921 | pPerfSys->padIntrPrev[ul] = 0.0;
|
---|
3922 | pPerfSys->palLoads[ul] = 0;
|
---|
3923 | pPerfSys->palIntrs[ul] = 0;
|
---|
3924 | }
|
---|
3925 | }
|
---|
3926 | }
|
---|
3927 | }
|
---|
3928 | } // end if (arc == NO_ERROR)
|
---|
3929 | } // end if (arc == NO_ERROR)
|
---|
3930 |
|
---|
3931 | if (arc)
|
---|
3932 | // error: clean up
|
---|
3933 | doshPerfClose(ppPerfSys);
|
---|
3934 |
|
---|
3935 | } // end else if (!*ppPerfSys)
|
---|
3936 |
|
---|
3937 | return arc;
|
---|
3938 | }
|
---|
3939 |
|
---|
3940 | /*
|
---|
3941 | *@@ doshPerfGet:
|
---|
3942 | * calculates a current snapshot of the system load,
|
---|
3943 | * compared with the load which was calculated on
|
---|
3944 | * the previous call.
|
---|
3945 | *
|
---|
3946 | * If you want to continually measure the system CPU
|
---|
3947 | * load, this is the function you will want to call
|
---|
3948 | * regularly -- e.g. with a timer once per second.
|
---|
3949 | *
|
---|
3950 | * Call this ONLY if doshPerfOpen returned NO_ERROR,
|
---|
3951 | * or you'll get crashes.
|
---|
3952 | *
|
---|
3953 | * If this call returns NO_ERROR, you get LONG load
|
---|
3954 | * values for each CPU in the system in the arrays
|
---|
3955 | * in DOSHPERFSYS (in per-mille, 0-1000).
|
---|
3956 | *
|
---|
3957 | * There are two arrays:
|
---|
3958 | *
|
---|
3959 | * -- DOSHPERFSYS.palLoads contains the "user" load
|
---|
3960 | * for each CPU.
|
---|
3961 | *
|
---|
3962 | * -- DOSHPERFSYS.palIntrs contains the "IRQ" load
|
---|
3963 | * for each CPU.
|
---|
3964 | *
|
---|
3965 | * Sum up the two values to get the total load for
|
---|
3966 | * each CPU.
|
---|
3967 | *
|
---|
3968 | * For example, if there are two CPUs, after this call,
|
---|
3969 | *
|
---|
3970 | * -- DOSHPERFSYS.palLoads[0] contains the "user" load
|
---|
3971 | * of the first CPU,
|
---|
3972 | *
|
---|
3973 | * -- DOSHPERFSYS.palLoads[0] contains the "user" load
|
---|
3974 | * of the second CPU.
|
---|
3975 | *
|
---|
3976 | * See doshPerfOpen for example code.
|
---|
3977 | *
|
---|
3978 | *@@added V0.9.7 (2000-12-02) [umoeller]
|
---|
3979 | *@@changed V0.9.9 (2001-03-14) [umoeller]: added interrupt loads; thanks phaller
|
---|
3980 | */
|
---|
3981 |
|
---|
3982 | APIRET doshPerfGet(PDOSHPERFSYS pPerfSys)
|
---|
3983 | {
|
---|
3984 | APIRET arc = NO_ERROR;
|
---|
3985 | if (!pPerfSys->pDosPerfSysCall)
|
---|
3986 | arc = ERROR_INVALID_PARAMETER;
|
---|
3987 | else
|
---|
3988 | {
|
---|
3989 | if (!(arc = pPerfSys->pDosPerfSysCall(CMD_KI_RDCNT,
|
---|
3990 | (ULONG)pPerfSys->paCPUUtils,
|
---|
3991 | 0, 0)))
|
---|
3992 | {
|
---|
3993 | // go thru all processors
|
---|
3994 | ULONG ul = 0;
|
---|
3995 | for (; ul < pPerfSys->cProcessors; ul++)
|
---|
3996 | {
|
---|
3997 | PCPUUTIL pCPUUtilThis = &pPerfSys->paCPUUtils[ul];
|
---|
3998 |
|
---|
3999 | double dTime = LL2F(pCPUUtilThis->ulTimeHigh,
|
---|
4000 | pCPUUtilThis->ulTimeLow);
|
---|
4001 | double dBusy = LL2F(pCPUUtilThis->ulBusyHigh,
|
---|
4002 | pCPUUtilThis->ulBusyLow);
|
---|
4003 | double dIntr = LL2F(pCPUUtilThis->ulIntrHigh,
|
---|
4004 | pCPUUtilThis->ulIntrLow);
|
---|
4005 |
|
---|
4006 | double *pdBusyPrevThis = &pPerfSys->padBusyPrev[ul];
|
---|
4007 | double *pdTimePrevThis = &pPerfSys->padTimePrev[ul];
|
---|
4008 | double *pdIntrPrevThis = &pPerfSys->padIntrPrev[ul];
|
---|
4009 |
|
---|
4010 | // avoid division by zero
|
---|
4011 | double dTimeDelta;
|
---|
4012 | if (dTimeDelta = (dTime - *pdTimePrevThis))
|
---|
4013 | {
|
---|
4014 | pPerfSys->palLoads[ul]
|
---|
4015 | = (LONG)( (double)( (dBusy - *pdBusyPrevThis)
|
---|
4016 | / dTimeDelta
|
---|
4017 | * 1000.0
|
---|
4018 | )
|
---|
4019 | );
|
---|
4020 | pPerfSys->palIntrs[ul]
|
---|
4021 | = (LONG)( (double)( (dIntr - *pdIntrPrevThis)
|
---|
4022 | / dTimeDelta
|
---|
4023 | * 1000.0
|
---|
4024 | )
|
---|
4025 | );
|
---|
4026 | }
|
---|
4027 | else
|
---|
4028 | {
|
---|
4029 | // no clear readings are available
|
---|
4030 | pPerfSys->palLoads[ul] = 0;
|
---|
4031 | pPerfSys->palIntrs[ul] = 0;
|
---|
4032 | }
|
---|
4033 |
|
---|
4034 | *pdTimePrevThis = dTime;
|
---|
4035 | *pdBusyPrevThis = dBusy;
|
---|
4036 | *pdIntrPrevThis = dIntr;
|
---|
4037 | }
|
---|
4038 | }
|
---|
4039 | }
|
---|
4040 |
|
---|
4041 | return arc;
|
---|
4042 | }
|
---|
4043 |
|
---|
4044 | /*
|
---|
4045 | *@@ doshPerfClose:
|
---|
4046 | * frees all resources allocated by doshPerfOpen.
|
---|
4047 | *
|
---|
4048 | *@@added V0.9.7 (2000-12-02) [umoeller]
|
---|
4049 | *@@changed V0.9.9 (2001-02-06) [umoeller]: removed disable; this broke the WarpCenter
|
---|
4050 | *@@changed V0.9.9 (2001-03-14) [umoeller]: fixed memory leak
|
---|
4051 | *@@changed V0.9.9 (2001-03-14) [umoeller]: added interrupt loads; thanks phaller
|
---|
4052 | */
|
---|
4053 |
|
---|
4054 | APIRET doshPerfClose(PDOSHPERFSYS *ppPerfSys)
|
---|
4055 | {
|
---|
4056 | APIRET arc = NO_ERROR;
|
---|
4057 | PDOSHPERFSYS pPerfSys;
|
---|
4058 | if ( (!ppPerfSys)
|
---|
4059 | || (!(pPerfSys = *ppPerfSys))
|
---|
4060 | )
|
---|
4061 | arc = ERROR_INVALID_PARAMETER;
|
---|
4062 | else
|
---|
4063 | {
|
---|
4064 | // do not call this, this messes up the WarpCenter V0.9.9 (2001-02-06) [umoeller]
|
---|
4065 | // if (pPerfSys->fInitialized) pPerfSys->pDosPerfSysCall(CMD_KI_DISABLE, 0, 0, 0);
|
---|
4066 |
|
---|
4067 | if (pPerfSys->paCPUUtils)
|
---|
4068 | free(pPerfSys->paCPUUtils);
|
---|
4069 | if (pPerfSys->padBusyPrev)
|
---|
4070 | free(pPerfSys->padBusyPrev);
|
---|
4071 | if (pPerfSys->padTimePrev)
|
---|
4072 | free(pPerfSys->padTimePrev);
|
---|
4073 | if (pPerfSys->padIntrPrev)
|
---|
4074 | free(pPerfSys->padIntrPrev);
|
---|
4075 | if (pPerfSys->palLoads) // was missing V0.9.9 (2001-03-14) [umoeller]
|
---|
4076 | free(pPerfSys->palLoads);
|
---|
4077 | if (pPerfSys->palIntrs)
|
---|
4078 | free(pPerfSys->palIntrs);
|
---|
4079 |
|
---|
4080 | if (pPerfSys->hmod)
|
---|
4081 | DosFreeModule(pPerfSys->hmod);
|
---|
4082 | free(pPerfSys);
|
---|
4083 | *ppPerfSys = NULL;
|
---|
4084 | }
|
---|
4085 |
|
---|
4086 | return arc;
|
---|
4087 | }
|
---|
4088 |
|
---|
4089 | /*
|
---|
4090 | *@@category: Helpers\Control program helpers\Process management
|
---|
4091 | * helpers for starting subprocesses.
|
---|
4092 | */
|
---|
4093 |
|
---|
4094 | /* ******************************************************************
|
---|
4095 | *
|
---|
4096 | * Process helpers
|
---|
4097 | *
|
---|
4098 | ********************************************************************/
|
---|
4099 |
|
---|
4100 | STATIC PVOID G_pvLocalInfoSeg = NULL;
|
---|
4101 |
|
---|
4102 | USHORT _Far16 _Pascal Dos16GetInfoSeg(PSEL pselGlobal,
|
---|
4103 | PSEL pselLocal);
|
---|
4104 |
|
---|
4105 | /*
|
---|
4106 | * GetInfoSegs:
|
---|
4107 | *
|
---|
4108 | */
|
---|
4109 |
|
---|
4110 | STATIC VOID GetInfoSegs(VOID)
|
---|
4111 | {
|
---|
4112 | SEL GlobalInfoSegSelector,
|
---|
4113 | LocalInfoSegSelector;
|
---|
4114 |
|
---|
4115 | // get selectors (old 16-bit API; this gets called only once)
|
---|
4116 | Dos16GetInfoSeg(&GlobalInfoSegSelector,
|
---|
4117 | &LocalInfoSegSelector);
|
---|
4118 | // thunk
|
---|
4119 | /* G_pvGlobalInfoSeg = (PVOID)( (GlobalInfoSegSelector << 0x000D)
|
---|
4120 | & 0x01fff0000); */
|
---|
4121 | G_pvLocalInfoSeg = (PVOID)( (LocalInfoSegSelector << 0x000D)
|
---|
4122 | & 0x01fff0000);
|
---|
4123 | }
|
---|
4124 |
|
---|
4125 | /*
|
---|
4126 | *@@ doshMyPID:
|
---|
4127 | * returns the PID of the current process.
|
---|
4128 | *
|
---|
4129 | * This uses an interesting hack which is way
|
---|
4130 | * faster than DosGetInfoBlocks.
|
---|
4131 | *
|
---|
4132 | *@@added V0.9.9 (2001-04-04) [umoeller]
|
---|
4133 | */
|
---|
4134 |
|
---|
4135 | ULONG doshMyPID(VOID)
|
---|
4136 | {
|
---|
4137 | if (!G_pvLocalInfoSeg)
|
---|
4138 | // first call:
|
---|
4139 | GetInfoSegs();
|
---|
4140 |
|
---|
4141 | // PID is at offset 0 in the local info seg
|
---|
4142 | return (*(PUSHORT)G_pvLocalInfoSeg);
|
---|
4143 | }
|
---|
4144 |
|
---|
4145 | /*
|
---|
4146 | *@@ doshMyTID:
|
---|
4147 | * returns the TID of the current thread.
|
---|
4148 | *
|
---|
4149 | * This uses an interesting hack which is way
|
---|
4150 | * faster than DosGetInfoBlocks.
|
---|
4151 | *
|
---|
4152 | *@@added V0.9.9 (2001-04-04) [umoeller]
|
---|
4153 | */
|
---|
4154 |
|
---|
4155 | ULONG doshMyTID(VOID)
|
---|
4156 | {
|
---|
4157 | if (!G_pvLocalInfoSeg)
|
---|
4158 | // first call:
|
---|
4159 | GetInfoSegs();
|
---|
4160 |
|
---|
4161 | // TID is at offset 6 in the local info seg
|
---|
4162 | return (*(PUSHORT)((PBYTE)G_pvLocalInfoSeg + 6));
|
---|
4163 | }
|
---|
4164 |
|
---|
4165 | /*
|
---|
4166 | *@@ doshExecVIO:
|
---|
4167 | * executes cmd.exe with the /c parameter
|
---|
4168 | * and pcszExecWithArgs. This is equivalent
|
---|
4169 | * to the C library system() function,
|
---|
4170 | * except that an OS/2 error code is returned
|
---|
4171 | * and that this works with the VAC subsystem
|
---|
4172 | * library.
|
---|
4173 | *
|
---|
4174 | * This uses DosExecPgm and handles the sick
|
---|
4175 | * argument passing.
|
---|
4176 | *
|
---|
4177 | * If NO_ERROR is returned, *plExitCode receives
|
---|
4178 | * the exit code of the process. If the process
|
---|
4179 | * was terminated abnormally, *plExitCode receives:
|
---|
4180 | *
|
---|
4181 | * -- -1: hard error halt
|
---|
4182 | * -- -2: 16-bit trap
|
---|
4183 | * -- -3: DosKillProcess
|
---|
4184 | * -- -4: 32-bit exception
|
---|
4185 | *
|
---|
4186 | *@@added V0.9.4 (2000-07-27) [umoeller]
|
---|
4187 | */
|
---|
4188 |
|
---|
4189 | APIRET doshExecVIO(PCSZ pcszExecWithArgs,
|
---|
4190 | PLONG plExitCode) // out: exit code (ptr can be NULL)
|
---|
4191 | {
|
---|
4192 | APIRET arc = NO_ERROR;
|
---|
4193 |
|
---|
4194 | if (strlen(pcszExecWithArgs) > 255)
|
---|
4195 | arc = ERROR_INSUFFICIENT_BUFFER;
|
---|
4196 | else
|
---|
4197 | {
|
---|
4198 | CHAR szObj[CCHMAXPATH];
|
---|
4199 | RESULTCODES res = {0};
|
---|
4200 | CHAR szBuffer[300];
|
---|
4201 |
|
---|
4202 | // DosExecPgm expects two args in szBuffer:
|
---|
4203 | // -- cmd.exe\0
|
---|
4204 | // -- then the actual argument, terminated by two 0 bytes.
|
---|
4205 | memset(szBuffer, 0, sizeof(szBuffer));
|
---|
4206 | strcpy(szBuffer, "cmd.exe\0");
|
---|
4207 | sprintf(szBuffer + 8, "/c %s",
|
---|
4208 | pcszExecWithArgs);
|
---|
4209 |
|
---|
4210 | arc = DosExecPgm(szObj, sizeof(szObj),
|
---|
4211 | EXEC_SYNC,
|
---|
4212 | szBuffer,
|
---|
4213 | 0,
|
---|
4214 | &res,
|
---|
4215 | "cmd.exe");
|
---|
4216 | if ((arc == NO_ERROR) && (plExitCode))
|
---|
4217 | {
|
---|
4218 | if (res.codeTerminate == 0)
|
---|
4219 | // normal exit:
|
---|
4220 | *plExitCode = res.codeResult;
|
---|
4221 | else
|
---|
4222 | *plExitCode = -((LONG)res.codeTerminate);
|
---|
4223 | }
|
---|
4224 | }
|
---|
4225 |
|
---|
4226 | return arc;
|
---|
4227 | }
|
---|
4228 |
|
---|
4229 | /*
|
---|
4230 | *@@ doshQuickStartSession:
|
---|
4231 | * this is a shortcut to DosStartSession w/out having to
|
---|
4232 | * deal with all the messy parameters.
|
---|
4233 | *
|
---|
4234 | * This one starts pszPath as a child session and passes
|
---|
4235 | * pszParams to it.
|
---|
4236 | *
|
---|
4237 | * In usPgmCtl, OR any or none of the following (V0.9.0):
|
---|
4238 | * -- SSF_CONTROL_NOAUTOCLOSE (0x0008): do not close automatically.
|
---|
4239 | * This bit is used only for VIO Windowable apps and ignored
|
---|
4240 | * for all other applications.
|
---|
4241 | * -- SSF_CONTROL_MINIMIZE (0x0004)
|
---|
4242 | * -- SSF_CONTROL_MAXIMIZE (0x0002)
|
---|
4243 | * -- SSF_CONTROL_INVISIBLE (0x0001)
|
---|
4244 | * -- SSF_CONTROL_VISIBLE (0x0000)
|
---|
4245 | *
|
---|
4246 | * Specifying 0 will therefore auto-close the session and make it
|
---|
4247 | * visible.
|
---|
4248 | *
|
---|
4249 | * If (fWait), this function will create a termination queue
|
---|
4250 | * and not return until the child session has ended. Be warned,
|
---|
4251 | * this blocks the calling thread.
|
---|
4252 | *
|
---|
4253 | * Otherwise the function will return immediately.
|
---|
4254 | *
|
---|
4255 | * The session and process IDs of the child session will be
|
---|
4256 | * written to *pulSID and *ppid. Of course, in "wait" mode,
|
---|
4257 | * these are no longer valid after this function returns.
|
---|
4258 | *
|
---|
4259 | * Returns the error code of DosStartSession.
|
---|
4260 | *
|
---|
4261 | * Note: According to CPREF, calling DosStartSession calls
|
---|
4262 | * DosExecPgm in turn for all full-screen, VIO, and PM sessions.
|
---|
4263 | *
|
---|
4264 | *@@changed V0.9.0 [umoeller]: prototype changed to include usPgmCtl
|
---|
4265 | *@@changed V0.9.1 (99-12-30) [umoeller]: queue was sometimes not closed. Fixed.
|
---|
4266 | *@@changed V0.9.3 (2000-05-03) [umoeller]: added fForeground
|
---|
4267 | *@@changed V0.9.14 (2001-08-03) [umoeller]: fixed potential queue leak
|
---|
4268 | *@@changed V0.9.14 (2001-08-03) [umoeller]: fixed memory leak in wait mode; added pusReturn to prototype
|
---|
4269 | *@@changed V1.0.4 (2005-10-16) [jsmall ]: inserted new 3rd parameter for session type
|
---|
4270 | *@@changed V1.0.4 (2005-10-16) [jsmall ]: changed code so when fWait = FALSE then session is started as independent, not as a child
|
---|
4271 | *@@changed V1.0.5 (2006-04-01) [pr]: Added pcszTitle parameter
|
---|
4272 | */
|
---|
4273 |
|
---|
4274 | APIRET doshQuickStartSession(PCSZ pcszPath, // in: program to start
|
---|
4275 | PCSZ pcszParams, // in: parameters for program
|
---|
4276 | PCSZ pcszTitle, // in: window title
|
---|
4277 | USHORT usSessionType, // in: session type
|
---|
4278 | BOOL fForeground, // in: if TRUE, session will be in foreground
|
---|
4279 | USHORT usPgmCtl, // in: STARTDATA.PgmControl
|
---|
4280 | BOOL fWait, // in: wait for termination?
|
---|
4281 | PULONG pulSID, // out: session ID (req.)
|
---|
4282 | PPID ppid, // out: process ID (req.)
|
---|
4283 | PUSHORT pusReturn) // out: in wait mode, session's return code (ptr can be NULL)
|
---|
4284 | {
|
---|
4285 | APIRET arc = NO_ERROR;
|
---|
4286 | // queue stuff
|
---|
4287 | const char *pcszQueueName = "\\queues\\xwphlpsw.que";
|
---|
4288 | HQUEUE hq = 0;
|
---|
4289 | PID qpid = 0;
|
---|
4290 |
|
---|
4291 | if (fWait)
|
---|
4292 | {
|
---|
4293 | if (!(arc = DosCreateQueue(&hq,
|
---|
4294 | QUE_FIFO | QUE_CONVERT_ADDRESS,
|
---|
4295 | (PSZ)pcszQueueName)))
|
---|
4296 | arc = DosOpenQueue(&qpid, &hq, (PSZ)pcszQueueName);
|
---|
4297 | }
|
---|
4298 |
|
---|
4299 | if (!arc) // V0.9.14 (2001-08-03) [umoeller]
|
---|
4300 | {
|
---|
4301 | STARTDATA SData;
|
---|
4302 | CHAR szObjBuf[CCHMAXPATH];
|
---|
4303 |
|
---|
4304 | SData.Length = sizeof(STARTDATA);
|
---|
4305 | SData.Related = (fWait) ? SSF_RELATED_CHILD : SSF_RELATED_INDEPENDENT;
|
---|
4306 | // V1.0.4 (2005-10-16) [jsmall]
|
---|
4307 | SData.FgBg = (fForeground) ? SSF_FGBG_FORE : SSF_FGBG_BACK;
|
---|
4308 | // V0.9.3 (2000-05-03) [umoeller]
|
---|
4309 | SData.TraceOpt = SSF_TRACEOPT_NONE;
|
---|
4310 |
|
---|
4311 | // V1.0.5 (2006-04-01) [pr]
|
---|
4312 | if (pcszTitle && *pcszTitle)
|
---|
4313 | SData.PgmTitle = (PSZ)pcszTitle; // title for window
|
---|
4314 | else
|
---|
4315 | SData.PgmTitle = (PSZ)pcszPath;
|
---|
4316 |
|
---|
4317 | SData.PgmName = (PSZ)pcszPath;
|
---|
4318 | SData.PgmInputs = (PSZ)pcszParams;
|
---|
4319 |
|
---|
4320 | SData.TermQ = (fWait) ? (PSZ)pcszQueueName : NULL;
|
---|
4321 | SData.Environment = 0;
|
---|
4322 | SData.InheritOpt = SSF_INHERTOPT_PARENT;
|
---|
4323 | SData.SessionType = usSessionType; // V1.0.4 (2005-10-16) [jsmall]
|
---|
4324 | SData.IconFile = 0;
|
---|
4325 | SData.PgmHandle = 0;
|
---|
4326 |
|
---|
4327 | SData.PgmControl = usPgmCtl;
|
---|
4328 |
|
---|
4329 | SData.InitXPos = 30;
|
---|
4330 | SData.InitYPos = 40;
|
---|
4331 | SData.InitXSize = 200;
|
---|
4332 | SData.InitYSize = 140;
|
---|
4333 | SData.Reserved = 0;
|
---|
4334 | SData.ObjectBuffer = szObjBuf;
|
---|
4335 | SData.ObjectBuffLen = (ULONG)sizeof(szObjBuf);
|
---|
4336 |
|
---|
4337 | if ( (!(arc = DosStartSession(&SData, pulSID, ppid)))
|
---|
4338 | && (fWait)
|
---|
4339 | )
|
---|
4340 | {
|
---|
4341 | // block on the termination queue, which is written
|
---|
4342 | // to when the subprocess ends
|
---|
4343 | REQUESTDATA rqdata;
|
---|
4344 | ULONG cbData = 0;
|
---|
4345 | PULONG pulData = NULL;
|
---|
4346 | BYTE elpri;
|
---|
4347 |
|
---|
4348 | rqdata.pid = qpid;
|
---|
4349 | if (!(arc = DosReadQueue(hq, // in: queue handle
|
---|
4350 | &rqdata, // out: pid and ulData
|
---|
4351 | &cbData, // out: size of data returned
|
---|
4352 | (PVOID*)&pulData, // out: data returned
|
---|
4353 | 0, // in: remove first element in queue
|
---|
4354 | 0, // in: wait for queue data (block thread)
|
---|
4355 | &elpri, // out: element's priority
|
---|
4356 | 0))) // in: event semaphore to be posted
|
---|
4357 | {
|
---|
4358 | if (!rqdata.ulData)
|
---|
4359 | {
|
---|
4360 | // child session ended:
|
---|
4361 | // V0.9.14 (2001-08-03) [umoeller]
|
---|
4362 |
|
---|
4363 | // *pulSID = (*pulData) & 0xffff;
|
---|
4364 | if (pusReturn)
|
---|
4365 | *pusReturn = ((*pulData) >> 16) & 0xffff;
|
---|
4366 |
|
---|
4367 | }
|
---|
4368 | // else: continue looping
|
---|
4369 |
|
---|
4370 | if (pulData)
|
---|
4371 | DosFreeMem(pulData);
|
---|
4372 | }
|
---|
4373 | }
|
---|
4374 | }
|
---|
4375 |
|
---|
4376 | if (hq)
|
---|
4377 | DosCloseQueue(hq);
|
---|
4378 |
|
---|
4379 | return arc;
|
---|
4380 | }
|
---|
4381 |
|
---|
4382 |
|
---|