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