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

Last change on this file since 22 was 22, checked in by umoeller, 25 years ago

Misc. updates.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 65.1 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_DOSSESMGR
52#define INCL_DOSQUEUES
53#define INCL_DOSMISC
54#define INCL_DOSDEVICES
55#define INCL_DOSDEVIOCTL
56#define INCL_DOSERRORS
57
58#define INCL_KBD
59#include <os2.h>
60
61#include <stdlib.h>
62#include <string.h>
63#include <stdio.h>
64
65#include "setup.h" // code generation and debugging options
66
67#include "helpers\dosh.h"
68
69#pragma hdrstop
70
71const CHAR G_acDriveLetters[28] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
72
73/*
74 *@@category: Helpers\Control program helpers\Miscellaneous
75 * Miscellaneous helpers in dosh.c that didn't fit into any other
76 * category.
77 */
78
79/* ******************************************************************
80 *
81 * Miscellaneous
82 *
83 ********************************************************************/
84
85/*
86 *@@ doshGetChar:
87 * reads a single character from the keyboard.
88 * Useful for VIO sessions, since there's great
89 * confusion between the various C dialects about
90 * how to use getc(), and getc() doesn't work
91 * with the VAC subsystem library.
92 *
93 *@@added V0.9.4 (2000-07-27) [umoeller]
94 */
95
96CHAR doshGetChar(VOID)
97{
98 // CHAR c;
99 // ULONG ulRead = 0;
100
101 KBDKEYINFO kki;
102 KbdCharIn(&kki,
103 0, // wait
104 0);
105
106 return (kki.chChar);
107}
108
109/*
110 *@@ doshQueryShiftState:
111 * returns TRUE if any of the SHIFT keys are
112 * currently pressed. Useful for checks during
113 * WM_COMMAND messages from menus.
114 *
115 *@@changed V0.9.5 (2000-09-27) [umoeller]: added error checking
116 */
117
118BOOL doshQueryShiftState(VOID)
119{
120 BOOL brc = FALSE;
121 APIRET arc = NO_ERROR;
122 HFILE hfKbd;
123 ULONG ulAction;
124
125 arc = DosOpen("KBD$", &hfKbd, &ulAction, 0,
126 FILE_NORMAL, FILE_OPEN,
127 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYWRITE,
128 (PEAOP2)NULL);
129 if (arc == NO_ERROR)
130 {
131 SHIFTSTATE ShiftState;
132 ULONG cbDataLen = sizeof(ShiftState);
133
134 arc = DosDevIOCtl(hfKbd, IOCTL_KEYBOARD, KBD_GETSHIFTSTATE,
135 NULL, 0, NULL, // no parameters
136 &ShiftState, cbDataLen, &cbDataLen);
137 if (arc == NO_ERROR)
138 brc = ((ShiftState.fsState & 3) != 0);
139
140 DosClose(hfKbd);
141 }
142
143 return brc;
144}
145
146
147/*
148 *@@ doshIsWarp4:
149 * returns TRUE only if at least OS/2 Warp 4 is running.
150 *
151 *@@changed V0.9.2 (2000-03-05) [umoeller]: reported TRUE on Warp 3 also; fixed
152 *@@changed V0.9.6 (2000-10-16) [umoeller]: patched for speed
153 */
154
155BOOL doshIsWarp4(VOID)
156{
157 static BOOL s_brc = FALSE;
158 static BOOL s_fQueried = FALSE;
159 if (!s_fQueried)
160 {
161 // first call:
162 ULONG aulBuf[3];
163
164 DosQuerySysInfo(QSV_VERSION_MAJOR, // 11
165 QSV_VERSION_MINOR, // 12
166 &aulBuf, sizeof(aulBuf));
167 // Warp 3 is reported as 20.30
168 // Warp 4 is reported as 20.40
169 // Aurora is reported as 20.45
170
171 if ( (aulBuf[0] > 20) // major > 20; not the case with Warp 3, 4, 5
172 || ( (aulBuf[0] == 20) // major == 20 and minor >= 40
173 && (aulBuf[1] >= 40)
174 )
175 )
176 s_brc = TRUE;
177
178 s_fQueried = TRUE;
179 }
180
181 return (s_brc);
182}
183
184/*
185 *@@ doshQuerySysErrorMsg:
186 * this retrieves the error message for a system error
187 * (APIRET) from the system error message file (OSO001.MSG).
188 * This file better be on the DPATH (it normally is).
189 *
190 * This returns the string in the "SYSxxx: blahblah" style,
191 * which is normally displayed on the command line when
192 * errors occur.
193 *
194 * The error message is returned in a newly allocated
195 * buffer, which should be free()'d afterwards.
196 *
197 * Returns NULL upon errors.
198 */
199
200PSZ doshQuerySysErrorMsg(APIRET arc) // in: DOS error code
201{
202 PSZ pszReturn = 0;
203 CHAR szDosError[1000];
204 ULONG cbDosError = 0;
205 DosGetMessage(NULL, 0, // no string replacements
206 szDosError, sizeof(szDosError),
207 arc,
208 "OSO001.MSG", // default OS/2 message file
209 &cbDosError);
210 if (cbDosError > 2)
211 {
212 szDosError[cbDosError - 2] = 0;
213 pszReturn = strdup(szDosError);
214 }
215 return (pszReturn);
216}
217
218/*
219 *@@category: Helpers\Control program helpers\Shared memory management
220 * helpers for allocating and requesting shared memory.
221 */
222
223/* ******************************************************************
224 *
225 * Memory helpers
226 *
227 ********************************************************************/
228
229/*
230 *@@ doshAllocSharedMem:
231 * wrapper for DosAllocSharedMem which has
232 * a malloc()-like syntax. Just due to my
233 * lazyness.
234 *
235 * Note that ulSize is always rounded up to the
236 * next 4KB value, so don't use this hundreds of times.
237 *
238 * Returns NULL upon errors. Possible errors include
239 * that a memory block calle pcszName has already been
240 * allocated.
241 *
242 * Use DosFreeMem(pvrc) to free the memory. The memory
243 * will only be freed if no other process has requested
244 * access.
245 *
246 *@@added V0.9.3 (2000-04-18) [umoeller]
247 */
248
249PVOID doshAllocSharedMem(ULONG ulSize, // in: requested mem block size (rounded up to 4KB)
250 const char* pcszName) // in: name of block ("\\SHAREMEM\\xxx") or NULL
251{
252 PVOID pvrc = NULL;
253 APIRET arc = DosAllocSharedMem((PVOID*)(&pvrc),
254 (PSZ)pcszName,
255 ulSize,
256 PAG_COMMIT | PAG_READ | PAG_WRITE);
257 if (arc == NO_ERROR)
258 return (pvrc);
259
260 return (NULL);
261}
262
263/*
264 *@@ doshRequestSharedMem:
265 * requests access to a block of named shared memory
266 * allocated by doshAllocSharedMem.
267 *
268 * Returns NULL upon errors.
269 *
270 * Use DosFreeMem(pvrc) to free the memory. The memory
271 * will only be freed if no other process has requested
272 * access.
273 *
274 *@@added V0.9.3 (2000-04-19) [umoeller]
275 */
276
277PVOID doshRequestSharedMem(const char *pcszName)
278{
279 PVOID pvrc = NULL;
280 APIRET arc = DosGetNamedSharedMem((PVOID*)(pvrc),
281 (PSZ)pcszName,
282 PAG_READ | PAG_WRITE);
283 if (arc == NO_ERROR)
284 return (pvrc);
285
286 return (NULL);
287}
288
289/*
290 *@@category: Helpers\Control program helpers\Drive management
291 * functions for managing drives... enumerating, testing,
292 * querying etc.
293 */
294
295/* ******************************************************************
296 *
297 * Drive helpers
298 *
299 ********************************************************************/
300
301/*
302 *@@ doshEnumDrives:
303 * this function enumerates all valid drive letters on
304 * the system by composing a string of drive letters
305 * in the buffer pointed to by pszBuffer, which should
306 * be 27 characters in size to hold information for
307 * all drives. The buffer will be null-terminated.
308 *
309 * If (pcszFileSystem != NULL), only drives matching
310 * the specified file system type (e.g. "HPFS") will
311 * be enumerated. If (pcszFileSystem == NULL), all
312 * drives will be enumerated.
313 *
314 * If (fSkipRemovables == TRUE), removeable drives will
315 * be skipped. This applies to floppy, CD-ROM, and
316 * virtual floppy drives. This will start the search
317 * at drive letter C: so that drives A: and B: will
318 * never be checked (to avoid the hardware bumps).
319 *
320 * Otherwise, the search starts at drive A:. Still,
321 * removeable drives will only be added if valid media
322 * is inserted.
323 *
324 *@@changed V0.9.4 (2000-07-03) [umoeller]: this stopped at the first invalid drive letter; fixed
325 *@@changed V0.9.4 (2000-07-03) [umoeller]: added fSkipRemoveables
326 */
327
328VOID doshEnumDrives(PSZ pszBuffer, // out: drive letters
329 const char *pcszFileSystem, // in: FS's to match or NULL
330 BOOL fSkipRemoveables) // in: if TRUE, only non-removeable disks will be returned
331{
332 CHAR szName[5] = "";
333 ULONG ulLogicalDrive = 1, // start with drive A:
334 ulFound = 0; // found drives count
335 APIRET arc = NO_ERROR; // return code
336
337 if (fSkipRemoveables)
338 // start with drive C:
339 ulLogicalDrive = 3;
340
341 // go thru the drives, start with C: (== 3), stop after Z: (== 26)
342 while (ulLogicalDrive <= 26)
343 {
344 UCHAR nonRemovable=0;
345 ULONG parmSize=2;
346 ULONG dataLen=1;
347
348 #pragma pack(1)
349 struct
350 {
351 UCHAR dummy,drive;
352 } parms;
353 #pragma pack()
354
355 parms.drive=(UCHAR)(ulLogicalDrive-1);
356 arc = DosDevIOCtl((HFILE)-1,
357 IOCTL_DISK,
358 DSK_BLOCKREMOVABLE,
359 &parms,
360 parmSize,
361 &parmSize,
362 &nonRemovable,
363 1,
364 &dataLen);
365
366 /* _Pmpf((" ul = %d, Drive %c: arc = %d nonRemoveable = %d",
367 ulLogicalDrive,
368 G_acDriveLetters[ulLogicalDrive],
369 arc,
370 nonRemovable)); */
371
372 if ( // fixed disk and non-removeable
373 ((arc == NO_ERROR) && (nonRemovable))
374 // or network drive:
375 || (arc == ERROR_NOT_SUPPORTED)
376 )
377 {
378 ULONG ulOrdinal = 0; // ordinal of entry in name list
379 BYTE fsqBuffer[sizeof(FSQBUFFER2) + (3 * CCHMAXPATH)] = {0};
380 ULONG cbBuffer = sizeof(fsqBuffer); // Buffer length)
381 PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)fsqBuffer;
382
383 szName[0] = G_acDriveLetters[ulLogicalDrive];
384 szName[1] = ':';
385 szName[2] = '\0';
386
387 arc = DosQueryFSAttach(szName, // logical drive of attached FS
388 ulOrdinal, // ignored for FSAIL_QUERYNAME
389 FSAIL_QUERYNAME, // return data for a Drive or Device
390 pfsqBuffer, // returned data
391 &cbBuffer); // returned data length
392
393 if (arc == NO_ERROR)
394 {
395 // The data for the last three fields in the FSQBUFFER2
396 // structure are stored at the offset of fsqBuffer.szName.
397 // Each data field following fsqBuffer.szName begins
398 // immediately after the previous item.
399 CHAR* pszFSDName = (PSZ)&(pfsqBuffer->szName) + (pfsqBuffer->cbName) + 1;
400 if (pcszFileSystem == NULL)
401 {
402 // enum-all mode: always copy
403 pszBuffer[ulFound] = szName[0]; // drive letter
404 ulFound++;
405 }
406 else if (strcmp(pszFSDName, pcszFileSystem) == 0)
407 {
408 pszBuffer[ulFound] = szName[0]; // drive letter
409 ulFound++;
410 }
411 }
412 }
413
414 ulLogicalDrive++;
415 } // end while (G_acDriveLetters[ulLogicalDrive] <= 'Z')
416
417 pszBuffer[ulFound] = '\0';
418}
419
420/*
421 *@@ doshQueryBootDrive:
422 * returns the letter of the boot drive as a
423 * single (capital) character, which is useful for
424 * constructing file names using sprintf and such.
425 */
426
427CHAR doshQueryBootDrive(VOID)
428{
429 ULONG ulBootDrive;
430 DosQuerySysInfo(QSV_BOOT_DRIVE, QSV_BOOT_DRIVE,
431 &ulBootDrive,
432 sizeof(ulBootDrive));
433 return (G_acDriveLetters[ulBootDrive]);
434}
435
436/*
437 *@@ doshAssertDrive:
438 * this checks for whether the given drive
439 * is currently available without provoking
440 * those ugly white "Drive not ready" popups.
441 *
442 * This returns (from my testing):
443 * -- NO_ERROR: drive is available.
444 * -- ERROR_INVALID_DRIVE (15): drive letter does not exist.
445 * -- ERROR_NOT_READY (21): drive exists, but is not ready
446 * (e.g. CD-ROM drive without CD inserted).
447 * -- ERROR_NOT_SUPPORTED (50): this is returned by the RAMFS.IFS
448 * file system; apparently, the IFS doesn't support
449 * DASD opening.
450 *
451 *@@changed V0.9.1 (99-12-13) [umoeller]: rewritten, prototype changed. Now using DosOpen on the drive instead of DosError.
452 *@@changed V0.9.1 (2000-01-08) [umoeller]: DosClose was called even if DosOpen failed, which messed up OS/2 error handling.
453 *@@changed V0.9.1 (2000-02-09) [umoeller]: this didn't work for network drives, including RAMFS; fixed.
454 *@@changed V0.9.3 (2000-03-28) [umoeller]: added check for network drives, which weren't working
455 *@@changed V0.9.4 (2000-08-03) [umoeller]: more network fixes
456 */
457
458APIRET doshAssertDrive(ULONG ulLogicalDrive) // in: 1 for A:, 2 for B:, 3 for C:, ...
459{
460 CHAR szDrive[3] = "C:";
461 HFILE hfDrive = 0;
462 ULONG ulTemp = 0;
463 APIRET arc;
464
465 szDrive[0] = 'A' + ulLogicalDrive - 1;
466
467 arc = DosOpen(szDrive, // "C:", "D:", ...
468 &hfDrive,
469 &ulTemp,
470 0,
471 FILE_NORMAL,
472 OPEN_ACTION_FAIL_IF_NEW
473 | OPEN_ACTION_OPEN_IF_EXISTS,
474 OPEN_FLAGS_DASD
475 | OPEN_FLAGS_FAIL_ON_ERROR
476 | OPEN_FLAGS_NOINHERIT // V0.9.6 (2000-11-25) [pr]
477 | OPEN_ACCESS_READONLY
478 | OPEN_SHARE_DENYNONE,
479 NULL);
480
481 // _Pmpf((__FUNCTION__ ": DosOpen(OPEN_FLAGS_DASD) returned %d", arc));
482
483 switch (arc)
484 {
485 case ERROR_NETWORK_ACCESS_DENIED: // 65
486 // added V0.9.3 (2000-03-27) [umoeller];
487 // according to user reports, this is returned
488 // by all network drives, which apparently don't
489 // support DASD DosOpen
490 case ERROR_ACCESS_DENIED: // 5
491 // added V0.9.4 (2000-07-10) [umoeller]
492 // LAN drives still didn't work... apparently
493 // the above only works for NFS drives
494 case ERROR_PATH_NOT_FOUND: // 3
495 // added V0.9.4 (2000-08-03) [umoeller]:
496 // this is returned by some other network types...
497 // sigh...
498 case ERROR_NOT_SUPPORTED: // 50
499 {
500 // this is returned by file systems which don't
501 // support DASD DosOpen;
502 // use some other method then, this isn't likely
503 // to fail -- V0.9.1 (2000-02-09) [umoeller]
504 FSALLOCATE fsa;
505 arc = DosQueryFSInfo(ulLogicalDrive,
506 FSIL_ALLOC,
507 &fsa,
508 sizeof(fsa));
509 break; }
510
511 case NO_ERROR:
512 DosClose(hfDrive);
513 break;
514 }
515
516 return (arc);
517
518 /* FSALLOCATE fsa;
519 APIRET arc = NO_ERROR;
520
521 if (fSuppress)
522 {
523 DosError(FERR_DISABLEHARDERR | FERR_DISABLEEXCEPTION);
524 DosEnterCritSec();
525 }
526
527 arc = DosQueryFSInfo(ulLogicalDrive, FSIL_ALLOC, &fsa, sizeof(fsa));
528
529 if (fSuppress)
530 {
531 DosError(FERR_ENABLEHARDERR | FERR_ENABLEEXCEPTION);
532 DosExitCritSec();
533 }
534
535 return (arc); */
536}
537
538/*
539 *@@ doshSetLogicalMap:
540 * sets the mapping of logical floppy drives onto a single
541 * physical floppy drive.
542 * This means selecting either drive A: or drive B: to refer
543 * to the physical drive.
544 *
545 *@@added V0.9.6 (2000-11-24) [pr]
546 */
547
548APIRET doshSetLogicalMap(ULONG ulLogicalDrive)
549{
550 CHAR name[3] = "?:";
551 ULONG fd = 0,
552 action = 0,
553 paramsize = 0,
554 datasize = 0;
555 APIRET rc = NO_ERROR;
556 USHORT data,
557 param;
558
559 name[0] = doshQueryBootDrive();
560 rc = DosOpen(name,
561 &fd,
562 &action,
563 0,
564 0,
565 OPEN_ACTION_FAIL_IF_NEW
566 | OPEN_ACTION_OPEN_IF_EXISTS,
567 OPEN_FLAGS_DASD
568 | OPEN_FLAGS_FAIL_ON_ERROR
569 | OPEN_FLAGS_NOINHERIT
570 | OPEN_ACCESS_READONLY
571 | OPEN_SHARE_DENYNONE,
572 0);
573 if (rc == NO_ERROR)
574 {
575 param = 0;
576 data = (USHORT)ulLogicalDrive;
577 paramsize = sizeof(param);
578 datasize = sizeof(data);
579 rc = DosDevIOCtl(fd,
580 IOCTL_DISK, DSK_SETLOGICALMAP,
581 &param, paramsize, &paramsize,
582 &data, datasize, &datasize);
583 DosClose(fd);
584 }
585
586 return(rc);
587}
588
589/*
590 *@@ doshQueryDiskFree:
591 * returns the number of bytes remaining on the disk
592 * specified by the given logical drive.
593 *
594 * Note: This returns a "double" value, because a ULONG
595 * can only hold values of some 4 billion, which would
596 * lead to funny results for drives > 4 GB.
597 *
598 *@@changed V0.9.0 [umoeller]: fixed another > 4 GB bug (thanks to Rdiger Ihle)
599 *@@changed V0.9.7 (2000-12-01) [umoeller]: changed prototype
600 */
601
602APIRET doshQueryDiskFree(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
603 double *pdFree)
604{
605 APIRET arc = NO_ERROR;
606 FSALLOCATE fsa;
607 double dbl = -1;
608
609 arc = DosQueryFSInfo(ulLogicalDrive, FSIL_ALLOC, &fsa, sizeof(fsa));
610 if (arc == NO_ERROR)
611 *pdFree = ((double)fsa.cSectorUnit * fsa.cbSector * fsa.cUnitAvail);
612 // ^ fixed V0.9.0
613
614 return (arc);
615}
616
617/*
618 *@@ doshQueryDiskFSType:
619 * copies the file-system type of the given disk object
620 * (HPFS, FAT, CDFS etc.) to pszBuf.
621 * Returns the DOS error code.
622 *
623 *@@changed V0.9.1 (99-12-12) [umoeller]: added cbBuf to prototype
624 */
625
626APIRET doshQueryDiskFSType(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
627 PSZ pszBuf, // out: buffer for FS type
628 ULONG cbBuf) // in: size of that buffer
629{
630 APIRET arc = NO_ERROR;
631 CHAR szName[5];
632
633 BYTE fsqBuffer[sizeof(FSQBUFFER2) + (3 * CCHMAXPATH)] = {0};
634 ULONG cbBuffer = sizeof(fsqBuffer); // Buffer length)
635 PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)fsqBuffer;
636
637 // compose "D:"-type string from logical drive letter
638 szName[0] = G_acDriveLetters[ulLogicalDrive];
639 szName[1] = ':';
640 szName[2] = '\0';
641
642 arc = DosQueryFSAttach(szName, // logical drive of attached FS ("D:"-style)
643 0, // ulOrdinal, ignored for FSAIL_QUERYNAME
644 FSAIL_QUERYNAME, // return name for a drive or device
645 pfsqBuffer, // buffer for returned data
646 &cbBuffer); // sizeof(*pfsqBuffer)
647
648 if (arc == NO_ERROR)
649 {
650 if (pszBuf)
651 {
652 // The data for the last three fields in the FSQBUFFER2
653 // structure are stored at the offset of fsqBuffer.szName.
654 // Each data field following fsqBuffer.szName begins
655 // immediately after the previous item.
656 strcpy(pszBuf,
657 (CHAR*)(&pfsqBuffer->szName) + pfsqBuffer->cbName + 1);
658 }
659 }
660
661 return (arc);
662}
663
664/*
665 *@@ doshIsFixedDisk:
666 * checks whether a disk is fixed or removeable.
667 * ulLogicalDrive must be 1 for drive A:, 2 for B:, ...
668 * The result is stored in *pfFixed.
669 * Returns DOS error code.
670 *
671 * Warning: This uses DosDevIOCtl, which has proved
672 * to cause problems with some device drivers for
673 * removeable disks.
674 */
675
676APIRET doshIsFixedDisk(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
677 PBOOL pfFixed) // out: TRUE for fixed disks
678{
679 APIRET arc = ERROR_INVALID_DRIVE;
680
681 if (ulLogicalDrive)
682 {
683 // parameter packet
684 #pragma pack(1)
685 struct {
686 UCHAR command, drive;
687 } parms;
688 #pragma pack()
689
690 // data packet
691 UCHAR ucNonRemoveable;
692
693 ULONG ulParmSize = sizeof(parms);
694 ULONG ulDataSize = sizeof(ucNonRemoveable);
695
696 parms.drive = (UCHAR)(ulLogicalDrive-1);
697 arc = DosDevIOCtl((HFILE)-1,
698 IOCTL_DISK,
699 DSK_BLOCKREMOVABLE,
700 &parms,
701 ulParmSize,
702 &ulParmSize,
703 &ucNonRemoveable,
704 ulDataSize,
705 &ulDataSize);
706
707 if (arc == NO_ERROR)
708 *pfFixed = (BOOL)ucNonRemoveable;
709 }
710
711 return (arc);
712}
713
714/*
715 *@@ doshQueryDiskParams:
716 * this retrieves more information about a given drive,
717 * which is stored in the specified DRIVEPARAMS structure
718 * (dosh.h).
719 *
720 * Warning: This uses DosDevIOCtl, which has proved
721 * to cause problems with some device drivers for
722 * removeable disks.
723 *
724 * This returns the DOS error code of DosDevIOCtl.
725 *
726 *@@added V0.9.0 [umoeller]
727 */
728
729APIRET doshQueryDiskParams(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
730 PDRIVEPARAMS pdp) // out: drive parameters
731{
732 APIRET arc = ERROR_INVALID_DRIVE;
733
734 if (ulLogicalDrive)
735 {
736 #pragma pack(1)
737 // parameter packet
738 struct {
739 UCHAR command, drive;
740 } parms;
741 #pragma pack()
742
743 ULONG ulParmSize = sizeof(parms);
744 ULONG ulDataSize = sizeof(DRIVEPARAMS);
745
746 parms.command = 1; // read currently inserted media
747 parms.drive=(UCHAR)(ulLogicalDrive-1);
748
749 arc = DosDevIOCtl((HFILE)-1,
750 IOCTL_DISK,
751 DSK_GETDEVICEPARAMS,
752 // parameter packet:
753 &parms, ulParmSize, &ulParmSize,
754 // data packet: DRIVEPARAMS structure
755 pdp, ulDataSize, &ulDataSize);
756 }
757
758 return (arc);
759}
760
761/*
762 *@@ doshQueryDiskLabel:
763 * this returns the label of a disk into
764 * *pszVolumeLabel, which must be 12 bytes
765 * in size.
766 *
767 * This function was added because the Toolkit
768 * information for DosQueryFSInfo is only partly
769 * correct. On OS/2 2.x, that function does not
770 * take an FSINFO structure as input, but a VOLUMELABEL.
771 * On Warp, this does take an FSINFO.
772 *
773 * DosSetFSInfo is even worse. See doshSetDiskLabel.
774 *
775 * See http://zebra.asta.fh-weingarten.de/os2/Snippets/Bugi6787.HTML
776 * for details.
777 *
778 *@@added V0.9.0 [umoeller]
779 */
780
781APIRET doshQueryDiskLabel(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
782 PSZ pszVolumeLabel) // out: volume label (must be 12 chars in size)
783{
784 APIRET arc;
785
786 #ifdef __OS2V2X__
787 VOLUMELABEL FSInfoBuf;
788 #else
789 FSINFO FSInfoBuf;
790 #endif
791
792 arc = DosQueryFSInfo(ulLogicalDrive,
793 FSIL_VOLSER,
794 &FSInfoBuf,
795 sizeof(FSInfoBuf)); // depends
796
797 #ifdef __OS2V2X__
798 strcpy(pszVolumeLabel, FSInfoBuf.szVolLabel);
799 #else
800 strcpy(pszVolumeLabel, FSInfoBuf.vol.szVolLabel);
801 #endif
802
803 return (arc);
804}
805
806/*
807 *@@ doshSetDiskLabel:
808 * this sets the label of a disk.
809 *
810 * This function was added because the Toolkit
811 * information for DosSetFSInfo is flat out wrong.
812 * That function does not take an FSINFO structure
813 * as input, but a VOLUMELABEL. As a result, using
814 * that function with the Toolkit's calling specs
815 * results in ERROR_LABEL_TOO_LONG always.
816 *
817 * See http://zebra.asta.fh-weingarten.de/os2/Snippets/Bugi6787.HTML
818 * for details.
819 *
820 *@@added V0.9.0 [umoeller]
821 */
822
823APIRET doshSetDiskLabel(ULONG ulLogicalDrive, // in: 1 for A:, 2 for B:, 3 for C:, ...
824 PSZ pszNewLabel)
825{
826 VOLUMELABEL FSInfoBuf;
827
828 // check length; 11 chars plus null byte allowed
829 FSInfoBuf.cch = (BYTE)strlen(pszNewLabel);
830 if (FSInfoBuf.cch < sizeof(FSInfoBuf.szVolLabel))
831 {
832 strcpy(FSInfoBuf.szVolLabel, pszNewLabel);
833
834 return (DosSetFSInfo(ulLogicalDrive,
835 FSIL_VOLSER,
836 &FSInfoBuf,
837 sizeof(FSInfoBuf)));
838 }
839 else
840 return (ERROR_LABEL_TOO_LONG);
841}
842
843/*
844 *@@category: Helpers\Control program helpers\File management
845 */
846
847/* ******************************************************************
848 *
849 * File helpers
850 *
851 ********************************************************************/
852
853/*
854 *@@ doshGetExtension:
855 * finds the file name extension of pszFilename,
856 * which can be a file name only or a fully
857 * qualified filename.
858 *
859 * This returns a pointer into pszFilename to
860 * the character after the last dot.
861 *
862 * Returns NULL if not found (e.g. if the filename
863 * has no dot in it).
864 *
865 *@@added V0.9.6 (2000-10-16) [umoeller]
866 *@@changed V0.9.7 (2000-12-10) [umoeller]: fixed "F:filename.ext" case
867 */
868
869PSZ doshGetExtension(const char *pcszFilename)
870{
871 PSZ pReturn = NULL;
872
873 if (pcszFilename)
874 {
875 // find filename
876 const char *p2 = strrchr(pcszFilename + 2, '\\'),
877 // works on "C:\blah" or "\\unc\blah"
878 *pStartOfName = NULL,
879 *pExtension = NULL;
880
881 if (p2)
882 pStartOfName = p2 + 1;
883 else
884 {
885 // no backslash found:
886 // maybe only a drive letter was specified:
887 if (*(pcszFilename + 1) == ':')
888 // yes:
889 pStartOfName = pcszFilename + 2;
890 else
891 // then this is not qualified at all...
892 // use start of filename
893 pStartOfName = (PSZ)pcszFilename;
894 }
895
896 // find last dot in filename
897 pExtension = strrchr(pStartOfName, '.');
898 if (pExtension)
899 pReturn = (PSZ)pExtension + 1;
900 }
901
902 return (pReturn);
903}
904
905/*
906 *@@ doshIsFileOnFAT:
907 * returns TRUE if pszFileName resides on
908 * a FAT drive. Note that pszFileName must
909 * be fully qualified (i.e. the drive letter
910 * must be the first character), or this will
911 * return garbage.
912 */
913
914BOOL doshIsFileOnFAT(const char* pcszFileName)
915{
916 BOOL brc = FALSE;
917 CHAR szName[5];
918
919 APIRET rc = NO_ERROR; // return code
920 BYTE fsqBuffer[sizeof(FSQBUFFER2) + (3 * CCHMAXPATH)] = {0};
921 ULONG cbBuffer = sizeof(fsqBuffer); // Buffer length)
922 PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)fsqBuffer;
923
924 szName[0] = pcszFileName[0]; // copy drive letter
925 szName[1] = ':';
926 szName[2] = '\0';
927
928 rc = DosQueryFSAttach(szName, // logical drive of attached FS
929 0, // ulOrdinal, ignored for FSAIL_QUERYNAME
930 FSAIL_QUERYNAME, // return data for a Drive or Device
931 pfsqBuffer, // returned data
932 &cbBuffer); // returned data length
933
934 if (rc == NO_ERROR)
935 {
936 // The data for the last three fields in the FSQBUFFER2
937 // structure are stored at the offset of fsqBuffer.szName.
938 // Each data field following fsqBuffer.szName begins
939 // immediately after the previous item.
940 if (strncmp((PSZ)&(pfsqBuffer->szName) + pfsqBuffer->cbName + 1,
941 "FAT",
942 3)
943 == 0)
944 brc = TRUE;
945 }
946
947 return (brc);
948}
949
950/*
951 *@@ doshQueryFileSize:
952 * returns the size of an already opened file
953 * or 0 upon errors.
954 * Use doshQueryPathSize to query the size of
955 * any file.
956 */
957
958ULONG doshQueryFileSize(HFILE hFile)
959{
960 FILESTATUS3 fs3;
961 if (DosQueryFileInfo(hFile, FIL_STANDARD, &fs3, sizeof(fs3)))
962 return (0);
963 else
964 return (fs3.cbFile);
965}
966
967/*
968 *@@ doshQueryPathSize:
969 * returns the size of any file,
970 * or 0 if the file could not be
971 * found.
972 * Use doshQueryFileSize instead to query the
973 * size if you have a HFILE.
974 */
975
976ULONG doshQueryPathSize(PSZ pszFile)
977{
978 FILESTATUS3 fs3;
979 if (DosQueryPathInfo(pszFile, FIL_STANDARD, &fs3, sizeof(fs3)))
980 return (0);
981 else
982 return (fs3.cbFile);
983}
984
985/*
986 *@@ doshQueryPathAttr:
987 * returns the file attributes of pszFile,
988 * which can be fully qualified. The
989 * attributes will be stored in *pulAttr.
990 * pszFile can also specify a directory,
991 * although not all attributes make sense
992 * for directories.
993 *
994 * fAttr can be:
995 * -- FILE_ARCHIVED
996 * -- FILE_READONLY
997 * -- FILE_SYSTEM
998 * -- FILE_HIDDEN
999 *
1000 * This returns the APIRET of DosQueryPathAttr.
1001 * *pulAttr is only valid if NO_ERROR is
1002 * returned.
1003 *
1004 *@@added V0.9.0 [umoeller]
1005 */
1006
1007APIRET doshQueryPathAttr(const char* pcszFile, // in: file or directory name
1008 PULONG pulAttr) // out: attributes
1009{
1010 FILESTATUS3 fs3;
1011 APIRET arc = DosQueryPathInfo((PSZ)pcszFile,
1012 FIL_STANDARD,
1013 &fs3,
1014 sizeof(fs3));
1015 if (arc == NO_ERROR)
1016 {
1017 if (pulAttr)
1018 *pulAttr = fs3.attrFile;
1019 }
1020
1021 return (arc);
1022}
1023
1024/*
1025 *@@ doshSetPathAttr:
1026 * sets the file attributes of pszFile,
1027 * which can be fully qualified.
1028 * pszFile can also specify a directory,
1029 * although not all attributes make sense
1030 * for directories.
1031 *
1032 * fAttr can be:
1033 * -- FILE_ARCHIVED
1034 * -- FILE_READONLY
1035 * -- FILE_SYSTEM
1036 * -- FILE_HIDDEN
1037 *
1038 * Note that this simply sets all the given
1039 * attributes; the existing attributes
1040 * are lost.
1041 *
1042 * This returns the APIRET of DosQueryPathInfo.
1043 */
1044
1045APIRET doshSetPathAttr(const char* pcszFile, // in: file or directory name
1046 ULONG ulAttr) // in: new attributes
1047{
1048 FILESTATUS3 fs3;
1049 APIRET rc = DosQueryPathInfo((PSZ)pcszFile,
1050 FIL_STANDARD,
1051 &fs3,
1052 sizeof(fs3));
1053
1054 if (rc == NO_ERROR)
1055 {
1056 fs3.attrFile = ulAttr;
1057 rc = DosSetPathInfo((PSZ)pcszFile,
1058 FIL_STANDARD,
1059 &fs3,
1060 sizeof(fs3),
1061 DSPI_WRTTHRU);
1062 }
1063 return (rc);
1064}
1065
1066/*
1067 *@@ doshReadTextFile:
1068 * reads a text file from disk, allocates memory
1069 * via malloc() and sets a pointer to this
1070 * buffer (or NULL upon errors).
1071 *
1072 * This returns the APIRET of DosOpen and DosRead.
1073 * If any error occured, no buffer was allocated.
1074 * Otherwise, you should free() the buffer when
1075 * no longer needed.
1076 */
1077
1078APIRET doshReadTextFile(PSZ pszFile, // in: file name to read
1079 PSZ* ppszContent) // out: newly allocated buffer with file's content
1080{
1081 ULONG ulSize,
1082 ulBytesRead = 0,
1083 ulAction, ulLocal;
1084 HFILE hFile;
1085 PSZ pszContent = NULL;
1086
1087 APIRET arc = DosOpen(pszFile,
1088 &hFile,
1089 &ulAction, // action taken
1090 5000L, // primary allocation size
1091 FILE_ARCHIVED | FILE_NORMAL, // file attribute
1092 OPEN_ACTION_OPEN_IF_EXISTS, // open flags
1093 OPEN_FLAGS_NOINHERIT
1094 | OPEN_SHARE_DENYNONE
1095 | OPEN_ACCESS_READONLY, // read-only mode
1096 NULL); // no EAs
1097
1098 if (arc == NO_ERROR)
1099 {
1100 ulSize = doshQueryFileSize(hFile);
1101 pszContent = (PSZ)malloc(ulSize+1);
1102 arc = DosSetFilePtr(hFile,
1103 0L,
1104 FILE_BEGIN,
1105 &ulLocal);
1106 arc = DosRead(hFile,
1107 pszContent,
1108 ulSize,
1109 &ulBytesRead);
1110 DosClose(hFile);
1111 *(pszContent+ulBytesRead) = 0;
1112
1113 // set output buffer pointer
1114 *ppszContent = pszContent;
1115 }
1116 else
1117 *ppszContent = 0;
1118
1119 return (arc);
1120}
1121
1122/*
1123 *@@ doshCreateBackupFileName:
1124 * creates a valid backup filename of pszExisting
1125 * with a numerical file name extension which does
1126 * not exist in the directory where pszExisting
1127 * resides.
1128 * Returns a PSZ to a new buffer which was allocated
1129 * using malloc().
1130 *
1131 * <B>Example:</B> returns "C:\CONFIG.002" for input
1132 * "C:\CONFIG.SYS" if "C:\CONFIG.001" already exists.
1133 *
1134 *@@changed V0.9.1 (99-12-13) [umoeller]: this crashed if pszExisting had no file extension
1135 */
1136
1137PSZ doshCreateBackupFileName(const char* pszExisting)
1138{
1139 CHAR szFilename[CCHMAXPATH];
1140 PSZ pszLastDot;
1141 ULONG ulCount = 1;
1142 CHAR szCount[5];
1143
1144 strcpy(szFilename, pszExisting);
1145 pszLastDot = strrchr(szFilename, '.');
1146 if (!pszLastDot)
1147 // no dot in filename:
1148 pszLastDot = szFilename + strlen(szFilename);
1149 do
1150 {
1151 sprintf(szCount, ".%03lu", ulCount);
1152 strcpy(pszLastDot, szCount);
1153 ulCount++;
1154 } while (doshQueryPathSize(szFilename) != 0);
1155
1156 return (strdup(szFilename));
1157}
1158
1159/*
1160 *@@ doshWriteTextFile:
1161 * writes a text file to disk; pszFile must contain the
1162 * whole path and filename.
1163 *
1164 * An existing file will be backed up if (pszBackup != NULL),
1165 * using doshCreateBackupFileName. In that case, pszBackup
1166 * receives the name of the backup created, so that buffer
1167 * should be CCHMAXPATH in size.
1168 *
1169 * If (pszbackup == NULL), an existing file will be overwritten.
1170 *
1171 * On success (NO_ERROR returned), *pulWritten receives
1172 * the no. of bytes written.
1173 *
1174 *@@changed V0.9.3 (2000-05-01) [umoeller]: optimized DosOpen; added error checking; changed prototype
1175 *@@changed V0.9.3 (2000-05-12) [umoeller]: added pszBackup
1176 */
1177
1178APIRET doshWriteTextFile(const char* pszFile, // in: file name
1179 const char* pszContent, // in: text to write
1180 PULONG pulWritten, // out: bytes written (ptr can be NULL)
1181 PSZ pszBackup) // in/out: create-backup?
1182{
1183 APIRET arc = NO_ERROR;
1184 ULONG ulWritten = 0;
1185
1186 if ((!pszFile) || (!pszContent))
1187 arc = ERROR_INVALID_PARAMETER;
1188 else
1189 {
1190 ULONG ulAction = 0,
1191 ulLocal = 0;
1192 HFILE hFile = 0;
1193
1194 ULONG ulSize = strlen(pszContent); // exclude 0 byte
1195
1196 if (pszBackup)
1197 {
1198 PSZ pszBackup2 = doshCreateBackupFileName(pszFile);
1199 DosCopy((PSZ)pszFile,
1200 pszBackup2,
1201 DCPY_EXISTING); // delete existing
1202 strcpy(pszBackup, pszBackup2);
1203 free(pszBackup2);
1204 }
1205
1206 arc = DosOpen((PSZ)pszFile,
1207 &hFile,
1208 &ulAction, // action taken
1209 ulSize, // primary allocation size
1210 FILE_ARCHIVED | FILE_NORMAL, // file attribute
1211 OPEN_ACTION_CREATE_IF_NEW
1212 | OPEN_ACTION_REPLACE_IF_EXISTS, // open flags
1213 OPEN_FLAGS_NOINHERIT
1214 | OPEN_FLAGS_SEQUENTIAL // sequential, not random access
1215 | OPEN_SHARE_DENYWRITE // deny write mode
1216 | OPEN_ACCESS_WRITEONLY, // write mode
1217 NULL); // no EAs
1218
1219 if (arc == NO_ERROR)
1220 {
1221 arc = DosSetFilePtr(hFile,
1222 0L,
1223 FILE_BEGIN,
1224 &ulLocal);
1225 if (arc == NO_ERROR)
1226 {
1227 arc = DosWrite(hFile,
1228 (PVOID)pszContent,
1229 ulSize,
1230 &ulWritten);
1231 if (arc == NO_ERROR)
1232 arc = DosSetFileSize(hFile, ulSize);
1233 }
1234
1235 DosClose(hFile);
1236 }
1237 } // end if ((pszFile) && (pszContent))
1238
1239 if (pulWritten)
1240 *pulWritten = ulWritten;
1241
1242 return (arc);
1243}
1244
1245/*
1246 *@@ doshOpenLogFile:
1247 * this opens a log file in the root directory of
1248 * the boot drive; it is titled pszFilename, and
1249 * the file handle is returned.
1250 */
1251
1252HFILE doshOpenLogFile(const char* pcszFilename)
1253{
1254 APIRET rc;
1255 CHAR szFileName[CCHMAXPATH];
1256 HFILE hfLog;
1257 ULONG ulAction;
1258 ULONG ibActual;
1259
1260 sprintf(szFileName, "%c:\\%s", doshQueryBootDrive(), pcszFilename);
1261 rc = DosOpen(szFileName,
1262 &hfLog,
1263 &ulAction,
1264 0, // file size
1265 FILE_NORMAL,
1266 OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
1267 OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYWRITE,
1268 (PEAOP2)NULL);
1269 if (rc == NO_ERROR)
1270 {
1271 DosSetFilePtr(hfLog, 0, FILE_END, &ibActual);
1272 return (hfLog);
1273 }
1274 else
1275 return (0);
1276}
1277
1278/*
1279 * doshWriteToLogFile
1280 * writes a string to a log file, adding a
1281 * leading timestamp.
1282 */
1283
1284APIRET doshWriteToLogFile(HFILE hfLog, const char* pcsz)
1285{
1286 if (hfLog)
1287 {
1288 DATETIME dt;
1289 CHAR szTemp[2000];
1290 ULONG cbWritten;
1291 DosGetDateTime(&dt);
1292 sprintf(szTemp, "Time: %02d:%02d:%02d %s",
1293 dt.hours, dt.minutes, dt.seconds,
1294 pcsz);
1295 return (DosWrite(hfLog, (PVOID)szTemp, strlen(szTemp), &cbWritten));
1296 }
1297 else return (ERROR_INVALID_HANDLE);
1298}
1299
1300/*
1301 *@@category: Helpers\Control program helpers\Directory management
1302 * directory helpers (querying, creating, deleting etc.).
1303 */
1304
1305/* ******************************************************************
1306 *
1307 * Directory helpers
1308 *
1309 ********************************************************************/
1310
1311/*
1312 *@@ doshQueryDirExist:
1313 * returns TRUE if the given directory
1314 * exists.
1315 */
1316
1317BOOL doshQueryDirExist(PSZ pszDir)
1318{
1319 FILESTATUS3 fs3;
1320 APIRET arc = DosQueryPathInfo(pszDir, FIL_STANDARD, &fs3, sizeof(fs3));
1321 if (arc == NO_ERROR)
1322 // file found:
1323 return ((fs3.attrFile & FILE_DIRECTORY) != 0);
1324 else
1325 return FALSE;
1326}
1327
1328/*
1329 *@@ doshCreatePath:
1330 * this creates the specified directory.
1331 * As opposed to DosCreateDir, this
1332 * function can create several directories
1333 * at the same time, if the parent
1334 * directories do not exist yet.
1335 */
1336
1337APIRET doshCreatePath(PSZ pszPath,
1338 BOOL fHidden) // in: if TRUE, the new directories will get FILE_HIDDEN
1339{
1340 APIRET arc0 = NO_ERROR;
1341 CHAR path[CCHMAXPATH];
1342 CHAR *cp, c;
1343 ULONG cbPath;
1344
1345 strcpy(path, pszPath);
1346 cbPath = strlen(path);
1347
1348 if (path[cbPath] != '\\')
1349 {
1350 path[cbPath] = '\\';
1351 path[cbPath+1] = 0;
1352 }
1353
1354 cp = path;
1355 // advance past the drive letter only if we have one
1356 if (*(cp+1) == ':')
1357 cp += 3;
1358
1359 // go!
1360 while (*cp != 0)
1361 {
1362 if (*cp == '\\')
1363 {
1364 c = *cp;
1365 *cp = 0;
1366 if (!doshQueryDirExist(path))
1367 {
1368 APIRET arc = DosCreateDir(path,
1369 0); // no EAs
1370 if (arc != NO_ERROR)
1371 {
1372 arc0 = arc;
1373 break;
1374 }
1375 else
1376 if (fHidden)
1377 {
1378 // hide the directory we just created
1379 doshSetPathAttr(path, FILE_HIDDEN);
1380 }
1381 }
1382 *cp = c;
1383 }
1384 cp++;
1385 }
1386 return (arc0);
1387}
1388
1389/*
1390 *@@ doshQueryCurrentDir:
1391 * writes the current directory into
1392 * the specified buffer, which should be
1393 * CCHMAXPATH in size.
1394 *
1395 * As opposed to DosQueryCurrentDir, this
1396 * includes the drive letter.
1397 *
1398 *@@added V0.9.4 (2000-07-22) [umoeller]
1399 */
1400
1401APIRET doshQueryCurrentDir(PSZ pszBuf)
1402{
1403 APIRET arc = NO_ERROR;
1404 ULONG ulCurDisk = 0;
1405 ULONG ulMap = 0;
1406 arc = DosQueryCurrentDisk(&ulCurDisk, &ulMap);
1407 if (arc == NO_ERROR)
1408 {
1409 ULONG cbBuf = CCHMAXPATH - 3;
1410 *pszBuf = G_acDriveLetters[ulCurDisk];
1411 *(pszBuf + 1) = ':';
1412 *(pszBuf + 2) = '\\';
1413 arc = DosQueryCurrentDir(0, pszBuf + 3, &cbBuf);
1414 }
1415
1416 return (arc);
1417}
1418
1419/*
1420 *@@ doshDeleteDir:
1421 * deletes a directory. As opposed to DosDeleteDir,
1422 * this removes empty subdirectories and/or files
1423 * as well.
1424 *
1425 * flFlags can be any combination of the following:
1426 *
1427 * -- DOSHDELDIR_RECURSE: recurse into subdirectories.
1428 *
1429 * -- DOSHDELDIR_DELETEFILES: delete all regular files
1430 * which are found on the way.
1431 *
1432 * THIS IS NOT IMPLEMENTED YET.
1433 *
1434 * If 0 is specified, this effectively behaves just as
1435 * DosDeleteDir.
1436 *
1437 * If you specify DOSHDELDIR_RECURSE only, only empty
1438 * subdirectories are deleted as well.
1439 *
1440 * If you specify DOSHDELDIR_RECURSE | DOSHDELDIR_DELETEFILES,
1441 * this removes an entire directory tree, including all
1442 * subdirectories and files.
1443 *
1444 *@@added V0.9.4 (2000-07-01) [umoeller]
1445 */
1446
1447APIRET doshDeleteDir(const char *pcszDir,
1448 ULONG flFlags,
1449 PULONG pulDirs, // out: directories found
1450 PULONG pulFiles) // out: files found
1451{
1452 APIRET arc = NO_ERROR,
1453 arcReturn = NO_ERROR;
1454
1455 HDIR hdirFindHandle = HDIR_CREATE;
1456 FILEFINDBUF3 ffb3 = {0}; // returned from FindFirst/Next
1457 ULONG ulResultBufLen = sizeof(FILEFINDBUF3);
1458 ULONG ulFindCount = 1; // look for 1 file at a time
1459
1460 CHAR szFileMask[2*CCHMAXPATH];
1461 sprintf(szFileMask, "%s\\*", pcszDir);
1462
1463 // find files
1464 arc = DosFindFirst(szFileMask,
1465 &hdirFindHandle, // directory search handle
1466 FILE_ARCHIVED | FILE_DIRECTORY | FILE_SYSTEM
1467 | FILE_HIDDEN | FILE_READONLY,
1468 // search attributes; include all, even dirs
1469 &ffb3, // result buffer
1470 ulResultBufLen, // result buffer length
1471 &ulFindCount, // number of entries to find
1472 FIL_STANDARD); // return level 1 file info
1473
1474 if (arc == NO_ERROR)
1475 {
1476 // keep finding the next file until there are no more files
1477 while (arc == NO_ERROR) // != ERROR_NO_MORE_FILES
1478 {
1479 if (ffb3.attrFile & FILE_DIRECTORY)
1480 {
1481 // we found a directory:
1482
1483 // ignore the pseudo-directories
1484 if ( (strcmp(ffb3.achName, ".") != 0)
1485 && (strcmp(ffb3.achName, "..") != 0)
1486 )
1487 {
1488 // real directory:
1489 if (flFlags & DOSHDELDIR_RECURSE)
1490 {
1491 // recurse!
1492 CHAR szSubDir[2*CCHMAXPATH];
1493 sprintf(szSubDir, "%s\\%s", pcszDir, ffb3.achName);
1494 arcReturn = doshDeleteDir(szSubDir,
1495 flFlags,
1496 pulDirs,
1497 pulFiles);
1498 // this removes ffb3.achName as well
1499 }
1500 else
1501 {
1502 // directory, but no recursion:
1503 // report "access denied"
1504 // (this is what OS/2 reports with DosDeleteDir as well)
1505 arcReturn = ERROR_ACCESS_DENIED; // 5
1506 (*pulDirs)++;
1507 }
1508 }
1509 }
1510 else
1511 {
1512 // it's a file:
1513 arcReturn = ERROR_ACCESS_DENIED;
1514 (*pulFiles)++;
1515 }
1516
1517 if (arc == NO_ERROR)
1518 {
1519 ulFindCount = 1; // reset find count
1520 arc = DosFindNext(hdirFindHandle, // directory handle
1521 &ffb3, // result buffer
1522 ulResultBufLen, // result buffer length
1523 &ulFindCount); // number of entries to find
1524 }
1525 } // endwhile
1526
1527 DosFindClose(hdirFindHandle); // close our find handle
1528 }
1529
1530 if (arcReturn == NO_ERROR)
1531 // success so far:
1532 // delete our directory now
1533 arc = DosDeleteDir((PSZ)pcszDir);
1534
1535 return (arcReturn);
1536}
1537
1538/*
1539 *@@category: Helpers\Control program helpers\Module handling
1540 * helpers for importing functions from a module (DLL).
1541 */
1542
1543/* ******************************************************************
1544 *
1545 * Module handling helpers
1546 *
1547 ********************************************************************/
1548
1549/*
1550 *@@ doshResolveImports:
1551 * this function loads the module called pszModuleName
1552 * and resolves imports dynamically using DosQueryProcAddress.
1553 *
1554 * To specify the functions to be imported, a RESOLVEFUNCTION
1555 * array is used. In each of the array items, specify the
1556 * name of the function and a pointer to a function pointer
1557 * where to store the resolved address.
1558 *
1559 *@@added V0.9.3 (2000-04-29) [umoeller]
1560 */
1561
1562APIRET doshResolveImports(PSZ pszModuleName, // in: DLL to load
1563 HMODULE *phmod, // out: module handle
1564 PRESOLVEFUNCTION paResolves, // in/out: function resolves
1565 ULONG cResolves) // in: array item count (not array size!)
1566{
1567 CHAR szName[CCHMAXPATH];
1568 APIRET arc = DosLoadModule(szName,
1569 sizeof(szName),
1570 pszModuleName,
1571 phmod);
1572 if (arc == NO_ERROR)
1573 {
1574 ULONG ul;
1575 for (ul = 0;
1576 ul < cResolves;
1577 ul++)
1578 {
1579 arc = DosQueryProcAddr(*phmod,
1580 0, // ordinal, ignored
1581 (PSZ)paResolves[ul].pcszFunctionName,
1582 paResolves[ul].ppFuncAddress);
1583
1584 /* _Pmpf(("Resolved %s to 0x%lX, rc: %d",
1585 paResolves[ul].pcszFunctionName,
1586 *paResolves[ul].ppFuncAddress,
1587 arc)); */
1588 if (arc != NO_ERROR)
1589 break;
1590 }
1591 }
1592
1593 return (arc);
1594}
1595
1596/*
1597 *@@category: Helpers\Control program helpers\Performance (CPU load) helpers
1598 * helpers around DosPerfSysCall.
1599 */
1600
1601/* ******************************************************************
1602 *
1603 * Performance Counters (CPU Load)
1604 *
1605 ********************************************************************/
1606
1607/*
1608 *@@ doshPerfOpen:
1609 * initializes the OS/2 DosPerfSysCall API for
1610 * the calling thread.
1611 *
1612 * Note: This API is not supported on all OS/2
1613 * versions. I believe it came up with some Warp 4
1614 * fixpak. The API is resolved dynamically by
1615 * this function (using DosQueryProcAddr). Only
1616 * if NO_ERROR is returned, you may call doshPerfGet
1617 * afterwards.
1618 *
1619 * This properly initializes the internal counters
1620 * which the OS/2 kernel uses for this API. Apparently,
1621 * with newer kernels (FP13/14), IBM has chosen to no
1622 * longer do this automatically, which is the reason
1623 * why many "pulse" utilities display garbage with these
1624 * fixpaks.
1625 *
1626 * After NO_ERROR is returned, DOSHPERFSYS.cProcessors
1627 * contains the no. of processors found on the system.
1628 * All pointers in DOSHPERFSYS then point to arrays
1629 * which have exactly cProcessors array items.
1630 *
1631 * Call doshPerfClose to clean up resources allocated
1632 * by this function.
1633 *
1634 * Example code:
1635 *
1636 + PDOSHPERFSYS pPerf = NULL;
1637 + APIRET arc = doshPerfOpen(&pPerf);
1638 + if (arc == NO_ERROR)
1639 + {
1640 + // this should really be in a timer
1641 + ULONG ulCPU;
1642 + arc = doshPerfGet(&pPerf);
1643 + // go thru all CPUs
1644 + for (ulCPU = 0; ulCPU < pPerf->cProcessors; ulCPU++)
1645 + {
1646 + LONG lLoadThis = pPerf->palLoads[ulCPU];
1647 + ...
1648 + }
1649 +
1650 + ...
1651 +
1652 + // clean up
1653 + doshPerfClose(&pPerf);
1654 + }
1655 +
1656 *
1657 *@@added V0.9.7 (2000-12-02) [umoeller]
1658 */
1659
1660APIRET doshPerfOpen(PDOSHPERFSYS *ppPerfSys) // out: new DOSHPERFSYS structure
1661{
1662 APIRET arc = NO_ERROR;
1663
1664 // allocate DOSHPERFSYS structure
1665 *ppPerfSys = (PDOSHPERFSYS)malloc(sizeof(DOSHPERFSYS));
1666 if (!*ppPerfSys)
1667 arc = ERROR_NOT_ENOUGH_MEMORY;
1668 else
1669 {
1670 // initialize structure
1671 PDOSHPERFSYS pPerfSys = *ppPerfSys;
1672 memset(pPerfSys, 0, sizeof(*pPerfSys));
1673
1674 // resolve DosPerfSysCall API entry
1675 arc = DosLoadModule(NULL, 0, "DOSCALLS", &pPerfSys->hmod);
1676 if (arc == NO_ERROR)
1677 {
1678 arc = DosQueryProcAddr(pPerfSys->hmod,
1679 976,
1680 "DosPerfSysCall",
1681 (PFN*)(&pPerfSys->pDosPerfSysCall));
1682 if (arc == NO_ERROR)
1683 {
1684 // OK, we got the API: initialize!
1685 arc = pPerfSys->pDosPerfSysCall(CMD_KI_ENABLE, 0, 0, 0);
1686 if (arc == NO_ERROR)
1687 {
1688 pPerfSys->fInitialized = TRUE;
1689 // call CMD_KI_DISABLE later
1690
1691 arc = pPerfSys->pDosPerfSysCall(CMD_PERF_INFO,
1692 0,
1693 (ULONG)(&pPerfSys->cProcessors),
1694 0);
1695 if (arc == NO_ERROR)
1696 {
1697 ULONG ul = 0;
1698
1699 // allocate arrays
1700 pPerfSys->paCPUUtils = (PCPUUTIL)calloc(pPerfSys->cProcessors,
1701 sizeof(CPUUTIL));
1702 if (!pPerfSys->paCPUUtils)
1703 arc = ERROR_NOT_ENOUGH_MEMORY;
1704 else
1705 {
1706 pPerfSys->padBusyPrev = (double*)malloc(pPerfSys->cProcessors * sizeof(double));
1707 if (!pPerfSys->padBusyPrev)
1708 arc = ERROR_NOT_ENOUGH_MEMORY;
1709 else
1710 {
1711 pPerfSys->padTimePrev
1712 = (double*)malloc(pPerfSys->cProcessors * sizeof(double));
1713 if (!pPerfSys->padTimePrev)
1714 arc = ERROR_NOT_ENOUGH_MEMORY;
1715 else
1716 {
1717 pPerfSys->palLoads = (PLONG)malloc(pPerfSys->cProcessors * sizeof(LONG));
1718 if (!pPerfSys->palLoads)
1719 arc = ERROR_NOT_ENOUGH_MEMORY;
1720 else
1721 {
1722 for (ul = 0; ul < pPerfSys->cProcessors; ul++)
1723 {
1724 pPerfSys->padBusyPrev[ul] = 0.0;
1725 pPerfSys->padTimePrev[ul] = 0.0;
1726 pPerfSys->palLoads[ul] = 0;
1727 }
1728 }
1729 }
1730 }
1731 }
1732 }
1733 }
1734 } // end if (arc == NO_ERROR)
1735 } // end if (arc == NO_ERROR)
1736
1737 if (arc != NO_ERROR)
1738 {
1739 doshPerfClose(ppPerfSys);
1740 }
1741 } // end else if (!*ppPerfSys)
1742
1743 return (arc);
1744}
1745
1746/*
1747 *@@ doshPerfGet:
1748 * calculates a current snapshot of the system load,
1749 * compared with the load which was calculated on
1750 * the previous call.
1751 *
1752 * If you want to continually measure the system CPU
1753 * load, this is the function you will want to call
1754 * regularly -- e.g. with a timer once per second.
1755 *
1756 * Call this ONLY if doshPerfOpen returned NO_ERROR,
1757 * or you'll get crashes.
1758 *
1759 * If this call returns NO_ERROR, you get a LONG
1760 * CPU load for each CPU in the system in the
1761 * DOSHPERFSYS.palLoads array (in per-mille, 0-1000).
1762 *
1763 * For example, if there are two CPUs, after this call,
1764 *
1765 * -- DOSHPERFSYS.palLoads[0] contains the load of
1766 * the first CPU,
1767 *
1768 * -- DOSHPERFSYS.palLoads[1] contains the load of
1769 * the second CPU.
1770 *
1771 * See doshPerfOpen for example code.
1772 *
1773 *@@added V0.9.7 (2000-12-02) [umoeller]
1774 */
1775
1776APIRET doshPerfGet(PDOSHPERFSYS pPerfSys)
1777{
1778 APIRET arc = NO_ERROR;
1779 if (!pPerfSys->pDosPerfSysCall)
1780 arc = ERROR_INVALID_PARAMETER;
1781 else
1782 {
1783 arc = pPerfSys->pDosPerfSysCall(CMD_KI_RDCNT,
1784 (ULONG)pPerfSys->paCPUUtils,
1785 0, 0);
1786 if (arc == NO_ERROR)
1787 {
1788 // go thru all processors
1789 ULONG ul = 0;
1790 for (; ul < pPerfSys->cProcessors; ul++)
1791 {
1792 PCPUUTIL pCPUUtilThis = &pPerfSys->paCPUUtils[ul];
1793
1794 double dTime = LL2F(pCPUUtilThis->ulTimeHigh,
1795 pCPUUtilThis->ulTimeLow);
1796 double dBusy = LL2F(pCPUUtilThis->ulBusyHigh,
1797 pCPUUtilThis->ulBusyLow);
1798
1799 double *pdBusyPrevThis = &pPerfSys->padBusyPrev[ul];
1800 double *pdTimePrevThis = &pPerfSys->padTimePrev[ul];
1801
1802 // avoid division by zero
1803 double dTimeDelta = (dTime - *pdTimePrevThis);
1804 if (dTimeDelta)
1805 pPerfSys->palLoads[ul]
1806 = (LONG)( (double)( (dBusy - *pdBusyPrevThis)
1807 / dTimeDelta
1808 * 1000.0
1809 )
1810 );
1811 else
1812 pPerfSys->palLoads[ul] = 0;
1813
1814 *pdTimePrevThis = dTime;
1815 *pdBusyPrevThis = dBusy;
1816 }
1817 }
1818 }
1819
1820 return (arc);
1821}
1822
1823/*
1824 *@@ doshPerfClose:
1825 * frees all resources allocated by doshPerfOpen.
1826 *
1827 *@@added V0.9.7 (2000-12-02) [umoeller]
1828 */
1829
1830APIRET doshPerfClose(PDOSHPERFSYS *ppPerfSys)
1831{
1832 APIRET arc = NO_ERROR;
1833 PDOSHPERFSYS pPerfSys = *ppPerfSys;
1834 if (!pPerfSys)
1835 arc = ERROR_INVALID_PARAMETER;
1836 else
1837 {
1838 if (pPerfSys->fInitialized)
1839 pPerfSys->pDosPerfSysCall(CMD_KI_DISABLE,
1840 0, 0, 0);
1841
1842 if (pPerfSys->paCPUUtils)
1843 free(pPerfSys->paCPUUtils);
1844 if (pPerfSys->padBusyPrev)
1845 free(pPerfSys->padBusyPrev);
1846 if (pPerfSys->padTimePrev)
1847 free(pPerfSys->padTimePrev);
1848
1849 if (pPerfSys->hmod)
1850 DosFreeModule(pPerfSys->hmod);
1851 free(pPerfSys);
1852 *ppPerfSys = NULL;
1853 }
1854
1855 return (arc);
1856}
1857
1858/*
1859 *@@category: Helpers\Control program helpers\Process management
1860 * helpers for starting subprocesses.
1861 */
1862
1863/* ******************************************************************
1864 *
1865 * Process helpers
1866 *
1867 ********************************************************************/
1868
1869/*
1870 *@@ doshExecVIO:
1871 * executes cmd.exe with the /c parameter
1872 * and pcszExecWithArgs. This is equivalent
1873 * to the C library system() function,
1874 * except that an OS/2 error code is returned
1875 * and that this works with the VAC subsystem
1876 * library.
1877 *
1878 * This uses DosExecPgm and handles the sick
1879 * argument passing.
1880 *
1881 * If NO_ERROR is returned, *plExitCode receives
1882 * the exit code of the process. If the process
1883 * was terminated abnormally, *plExitCode receives:
1884 *
1885 * -- -1: hard error halt
1886 * -- -2: 16-bit trap
1887 * -- -3: DosKillProcess
1888 * -- -4: 32-bit exception
1889 *
1890 *@@added V0.9.4 (2000-07-27) [umoeller]
1891 */
1892
1893APIRET doshExecVIO(const char *pcszExecWithArgs,
1894 PLONG plExitCode) // out: exit code (ptr can be NULL)
1895{
1896 APIRET arc = NO_ERROR;
1897
1898 if (strlen(pcszExecWithArgs) > 255)
1899 arc = ERROR_INSUFFICIENT_BUFFER;
1900 {
1901 CHAR szObj[CCHMAXPATH];
1902 RESULTCODES res = {0};
1903 CHAR szBuffer[300];
1904
1905 // DosExecPgm expects two args in szBuffer:
1906 // -- cmd.exe\0
1907 // -- then the actual argument, terminated by two 0 bytes.
1908 memset(szBuffer, 0, sizeof(szBuffer));
1909 strcpy(szBuffer, "cmd.exe\0");
1910 sprintf(szBuffer + 8, "/c %s",
1911 pcszExecWithArgs);
1912
1913 arc = DosExecPgm(szObj, sizeof(szObj),
1914 EXEC_SYNC,
1915 szBuffer,
1916 0,
1917 &res,
1918 "cmd.exe");
1919 if ((arc == NO_ERROR) && (plExitCode))
1920 {
1921 if (res.codeTerminate == 0)
1922 // normal exit:
1923 *plExitCode = res.codeResult;
1924 else
1925 *plExitCode = -((LONG)res.codeTerminate);
1926 }
1927 }
1928
1929 return (arc);
1930}
1931
1932/*
1933 *@@ doshQuickStartSession:
1934 * this is a shortcut to DosStartSession w/out having to
1935 * deal with all the messy parameters.
1936 *
1937 * This one starts pszPath as a child session and passes
1938 * pszParams to it.
1939 *
1940 * In usPgmCtl, OR any or none of the following (V0.9.0):
1941 * -- SSF_CONTROL_NOAUTOCLOSE (0x0008): do not close automatically.
1942 * This bit is used only for VIO Windowable apps and ignored
1943 * for all other applications.
1944 * -- SSF_CONTROL_MINIMIZE (0x0004)
1945 * -- SSF_CONTROL_MAXIMIZE (0x0002)
1946 * -- SSF_CONTROL_INVISIBLE (0x0001)
1947 * -- SSF_CONTROL_VISIBLE (0x0000)
1948 *
1949 * Specifying 0 will therefore auto-close the session and make it
1950 * visible.
1951 *
1952 * If (fWait), this function will create a termination queue
1953 * and not return until the child session has ended. Otherwise
1954 * the function will return immediately, and the SID/PID of
1955 * the child session can be found in *pulSID and *ppid.
1956 *
1957 * Returns the error code of DosStartSession.
1958 *
1959 * Note: According to CPREF, calling DosStartSession calls
1960 * DosExecPgm in turn for all full-screen, VIO, and PM sessions.
1961 *
1962 *@@changed V0.9.0 [umoeller]: prototype changed to include usPgmCtl
1963 *@@changed V0.9.1 (99-12-30) [umoeller]: queue was sometimes not closed. Fixed.
1964 *@@changed V0.9.3 (2000-05-03) [umoeller]: added fForeground
1965 */
1966
1967APIRET doshQuickStartSession(const char *pcszPath, // in: program to start
1968 const char *pcszParams, // in: parameters for program
1969 BOOL fForeground, // in: if TRUE, session will be in foreground
1970 USHORT usPgmCtl, // in: STARTDATA.PgmControl
1971 BOOL fWait, // in: wait for termination?
1972 PULONG pulSID, // out: session ID (req.)
1973 PPID ppid) // out: process ID (req.)
1974{
1975 APIRET arc;
1976 // queue stuff
1977 const char *pcszQueueName = "\\queues\\kfgstart.que";
1978 HQUEUE hq = 0;
1979 PID qpid = 0;
1980 STARTDATA SData;
1981 CHAR szObjBuf[CCHMAXPATH];
1982
1983 if (fWait)
1984 {
1985 if ((arc = DosCreateQueue(&hq,
1986 QUE_FIFO | QUE_CONVERT_ADDRESS,
1987 (PSZ)pcszQueueName))
1988 != NO_ERROR)
1989 return (arc);
1990
1991 if ((arc = DosOpenQueue(&qpid, &hq, (PSZ)pcszQueueName)) != NO_ERROR)
1992 return (arc);
1993 }
1994
1995 SData.Length = sizeof(STARTDATA);
1996 SData.Related = SSF_RELATED_CHILD; //INDEPENDENT;
1997 SData.FgBg = (fForeground) ? SSF_FGBG_FORE : SSF_FGBG_BACK;
1998 // V0.9.3 (2000-05-03) [umoeller]
1999 SData.TraceOpt = SSF_TRACEOPT_NONE;
2000
2001 SData.PgmTitle = (PSZ)pcszPath; // title for window
2002 SData.PgmName = (PSZ)pcszPath;
2003 SData.PgmInputs = (PSZ)pcszParams;
2004
2005 SData.TermQ = (fWait) ? "\\queues\\kfgstart.que" : NULL;
2006 SData.Environment = 0;
2007 SData.InheritOpt = SSF_INHERTOPT_PARENT;
2008 SData.SessionType = SSF_TYPE_DEFAULT;
2009 SData.IconFile = 0;
2010 SData.PgmHandle = 0;
2011
2012 SData.PgmControl = usPgmCtl;
2013
2014 SData.InitXPos = 30;
2015 SData.InitYPos = 40;
2016 SData.InitXSize = 200;
2017 SData.InitYSize = 140;
2018 SData.Reserved = 0;
2019 SData.ObjectBuffer = (CHAR*)&szObjBuf;
2020 SData.ObjectBuffLen = (ULONG)sizeof(szObjBuf);
2021
2022 arc = DosStartSession(&SData, pulSID, ppid);
2023
2024 if (arc == NO_ERROR)
2025 {
2026 if (fWait)
2027 {
2028 REQUESTDATA rqdata;
2029 ULONG DataLength = 0;
2030 PULONG DataAddress;
2031 BYTE elpri;
2032
2033 rqdata.pid = qpid;
2034 DosReadQueue(hq, // in: queue handle
2035 &rqdata, // out: pid and ulData
2036 &DataLength, // out: size of data returned
2037 (PVOID*)&DataAddress, // out: data returned
2038 0, // in: remove first element in queue
2039 0, // in: wait for queue data (block thread)
2040 &elpri, // out: element's priority
2041 0); // in: event semaphore to be posted
2042 }
2043 }
2044
2045 if (hq)
2046 DosCloseQueue(hq);
2047
2048 return (arc);
2049}
2050
2051
Note: See TracBrowser for help on using the repository browser.