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

Last change on this file since 121 was 121, checked in by umoeller, 24 years ago

Misc changes.

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