source: trunk/src/helpers/dosh.c@ 159

Last change on this file since 159 was 159, checked in by umoeller, 23 years ago

Lots of dialog rework, plus other fixes.

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