source: trunk/common_functions/sys_funcs.c@ 2

Last change on this file since 2 was 2, checked in by stevenhl, 8 years ago

Import sources from cwmm-full.zip dated 2005-03-21

File size: 17.6 KB
Line 
1#define INCL_WIN
2#define INCL_GPI
3#define INCL_DOS
4#define INCL_DOSPROCESS
5#define INCL_DOSERRORS
6#define INCL_DOSDEVIOCTL
7#define INCL_DOSDEVICES // DosDevIOCtl
8
9#include <os2.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <stdio.h>
13#include <string.h> /* For strlcpy() */
14#include <stdarg.h>
15#include <stdlib.h> /* For getenv() */
16
17#include "sys_funcs.h"
18#include "excptLogName.h"
19
20#if __cplusplus
21extern "C" {
22#endif
23
24 /**!**************************************************/
25 /* */
26 /* @@DESC */
27 /* */
28 /* This funktion returns the drive letter of the */
29 /* boot partition. */
30 /* */
31 /* @@RETURNS */
32 /* */
33 /* char chrDrive */
34 /* */
35 /* Drive from which the system was booted. */
36 /* */
37 /**!!*************************************************/
38char SysQueryBootDriveLetter(void)
39{
40 ULONG ulSysValue;
41
42 if(!DosQuerySysInfo(QSV_BOOT_DRIVE, QSV_BOOT_DRIVE,&ulSysValue,sizeof(ulSysValue)))
43 return 'a'+ulSysValue-1;
44
45 return 'c';
46}
47
48/*!**************************************************/
49/* */
50/* @@DESC */
51/* */
52/* This funktion returns the running OS version. */
53/* */
54/* @@RETURNS */
55/* */
56/* ULONG ulVersion */
57/* */
58/* */
59/* 30: Warp 3:p. */
60/* 40: Warp 4 */
61/* */
62/*!!*************************************************/
63ULONG SysQueryOSRelease(void)
64{
65 static ULONG ulVersionMinor=0;
66
67 if(!ulVersionMinor)
68 if(DosQuerySysInfo(QSV_VERSION_MINOR, QSV_VERSION_MINOR, &ulVersionMinor, sizeof(ulVersionMinor)))
69 ulVersionMinor=30;/* Default Warp 3 */
70
71 return ulVersionMinor;
72
73}
74
75/**!**************************************************/
76/* */
77/* @@DESC */
78/* */
79/* This function may be used to write data into the */
80/* traplog file. The syntax is similar to printf(). */
81/* */
82/* @@REMARKS */
83/* */
84/* The filename of the traplog is defined in the */
85/* include file :hp2.excptLogName.h:ehp2. */
86/* */
87/* !parsing is broken at the moment because of const and (...) */
88/* */
89/**!!*************************************************/
90void SysWriteToTrapLog(const char* chrFormat, ...)
91{
92 char logNameLocal[CCHMAXPATH];
93 FILE *fHandle;
94
95 sprintf(logNameLocal,"%c:\\%s", SysQueryBootDriveLetter(), EXCEPTION_LOGFILE_NAME);
96 fHandle=fopen(logNameLocal,"a");
97 if(fHandle) {
98 va_list arg_ptr;
99
100 va_start (arg_ptr, chrFormat);
101 vfprintf(fHandle, chrFormat, arg_ptr);
102 va_end (arg_ptr);
103 fclose(fHandle);
104 }
105}
106
107/*!**************************************************/
108/* */
109/* @@DESC */
110/* */
111/* This function checks if the given file exists. */
112/* thread. */
113/* */
114/* @@RETURNS */
115/* */
116/* BOOL fExists */
117/* */
118/* TRUE: File exists, FALSE otherwise. */
119/* */
120/*!!*************************************************/
121BOOL SysCheckFileExists(char* chrFileName)
122{
123 struct stat statBuf;
124
125 /* Check file path */
126 if(stat(chrFileName , &statBuf)==-1)
127 return FALSE;
128 else
129 return TRUE;
130}
131
132
133/*!**************************************************/
134/* */
135/* @@DESC */
136/* */
137/* This function queries the size of a file. */
138/* */
139/* @@RETURNS */
140/* */
141/* ULONG ulSize */
142/* */
143/* Size of the file. */
144/* */
145/*!!*************************************************/
146ULONG SysQueryFileSize(char* chrFileName)
147{
148 struct stat statBuf;
149
150 /* Check file path */
151 if(stat(chrFileName , &statBuf)==-1)
152 return 0;
153
154 return statBuf.st_size;
155}
156
157/*!**************************************************/
158/* */
159/* @@DESC */
160/* */
161/* This function returns the ID of the calling */
162/* thread. */
163/* */
164/* @@RETURNS */
165/* */
166/* ULONG ulTID */
167/* */
168/* ID of the current thread. */
169/* */
170/*!!*************************************************/
171ULONG SysQueryCurrentTID(void)
172{
173 PPIB ppib;
174 PTIB ptib;
175
176 DosGetInfoBlocks( &ptib, &ppib);
177
178 return ptib->tib_ptib2->tib2_ultid;
179}
180
181ULONG SysQueryDriveType(ULONG ulDriveNum)
182{
183 ULONG ulDisk=0;
184 ULONG ulParamLen;
185 ULONG ulDataLen;
186 char chrParam[2]={0};
187 char chrData;
188 ULONG rc;
189
190 if(ulDriveNum>26 || ulDriveNum<1)
191 return QDT_ERROR_DISK;
192
193 // SysWriteToTrapLog("Drive nr.: %d\n", ulDriveNum);
194
195 /*
196 Query fixed, removable and remote drives in the system.
197 */
198 ulDataLen=sizeof(chrData);
199 ulParamLen=sizeof(chrParam);
200 chrParam[1]=(BYTE)ulDriveNum-1;
201 chrParam[0]=0;
202 /* Check if drive is fixed or removable */
203 rc = DosDevIOCtl(-1, IOCTL_DISK, DSK_BLOCKREMOVABLE,
204 chrParam, sizeof(chrParam), &ulParamLen, &chrData,
205 sizeof(chrData), &ulDataLen);
206
207 if(rc) {
208 // SysWriteToTrapLog("Drive nr.: %d, rc for DSK_BLOCKREMOVABLE", ulDriveNum);
209 if(rc==ERROR_NOT_SUPPORTED) {
210 /* This may be a remote drive */
211 char chrDev[8];
212 ULONG cbBuf;
213 BYTE buf[sizeof(FSQBUFFER2)+3*CCHMAXPATH];
214 PFSQBUFFER2 pBuf=(PFSQBUFFER2)buf;
215
216 // SysWriteToTrapLog("Drive nr.: %d, rc for DSK_BLOCKREMOVABLE-> ERROR_NOT_SUPPORTED\n", ulDriveNum);
217 sprintf(chrDev, "%c:", 'a'+ulDriveNum-1);
218 cbBuf=sizeof(buf);
219
220 /* Check if local or remote (or anything else) */
221 rc= DosQueryFSAttach(chrDev, 0, FSAIL_QUERYNAME,(PFSQBUFFER2) &buf, &cbBuf);
222 if(!rc) {
223 if(pBuf->iType==FSAT_REMOTEDRV)
224 ulDisk=QDT_REMOTE_DRIVE;
225 }
226 }/* ERROR_NOT_SUPPPORTED */
227 else
228 ulDisk=QDT_NO_DRIVE; /* Everything we don't understand is no disk */
229 }/* rc != NO_ERROR */
230 else {
231 /* We end here for HDs, floppies and CD-ROMS */
232 // SysWriteToTrapLog("Drive nr.: %d, local ones\n", ulDriveNum);
233 if(chrData ) /* chrData is 1 if Fixed disk */
234 return QDT_FIXED_DISK;
235 else {
236 /* Check for floppy drives */
237 BYTE abParm[2] = { 0, (BYTE)ulDriveNum-1 };
238 ULONG ulParmLen = 2;
239 ULONG ulDataLen = 0;
240 BIOSPARAMETERBLOCK bpb;
241
242 // SysWriteToTrapLog("Drive nr.: %d, not a fixed disk\n", ulDriveNum);
243
244 /* The disk is local. Look for floppy */
245 if (DosDevIOCtl ((HFILE)-1, IOCTL_DISK, DSK_GETDEVICEPARAMS,
246 (PVOID)&abParm[0], sizeof (abParm), &ulParmLen, (PVOID)&bpb, sizeof (bpb), &ulDataLen) == NO_ERROR) {
247 {
248 ULONG ulDevType;
249 BOOL fRemovable;
250
251 if (ulDataLen < 36)
252 return (QDT_NO_DRIVE); /* Error */
253
254 ulDevType = bpb.bDeviceType;
255 fRemovable = (BOOL)((bpb.fsDeviceAttr & 0x01) ? FALSE : TRUE);
256 // SysWriteToTrapLog("Drive nr.: %d, ulDevType: %d\n", ulDriveNum, ulDevType);
257
258 if ((ulDevType == 7) && fRemovable) {
259 // Check if DEVTYPE_OTHER (7) could be a 1.44MB diskette (Just for the sake, check for a 720kB and 2.88MB while we're at it).
260 switch (bpb.usBytesPerSector * bpb.cSectors) {
261 case 512*1440:
262 return QDT_DISKETTE_DRIVE; //DEVTYPE_35INCH_1MB_DISKETTE;
263 case 512*2880:
264 return QDT_DISKETTE_DRIVE;//DEVTYPE_35INCH_2MB_DISKETTE;
265 case 512*5760:
266 return QDT_DISKETTE_DRIVE;//DEVTYPE_35INCH_2MB_DISKETTE;
267 }/* switch()*/
268 }else {
269 switch (ulDevType) {
270 case 0: /* 48TPI diskette */
271 case 1: /* 96TPI diskette */
272 case 2: /* 3.5 inch diskette */
273 case 3: /* 8inch single diskette */
274 case 4: /* 8inch double diskette */
275 case 9: /* 3.5 4MB (2.88 formatted) diskette */
276 return QDT_DISKETTE_DRIVE;
277 }/* switch() */
278 }/* else */
279 // SysWriteToTrapLog("Drive nr.: %d, ????\n", ulDriveNum);
280 /* Assume CD-ROM here */
281 return (QDT_CDROM_DRIVE); /* For example tape */
282 return (QDT_NO_DRIVE); /* For example tape */
283 }
284 }/* DSK_GETDEVICEPARAMS */
285 }/* else */
286 ulDisk=QDT_REMOVABLE_DISK;
287 }
288
289 return ulDisk;
290}
291
292#if 0
293ULONG SysQueryDriveType_(ULONG ulDriveNum)
294{
295 static char chrDisks[26]={0};
296 static BOOL bInit=FALSE;
297 int a;
298
299 if(ulDriveNum>26 || ulDriveNum<1)
300 return QDT_ERROR_DISK;
301
302 if(!bInit) {
303 /*
304 Query fixed, removable and remote drives in the system.
305 */
306 for(a=0; a<26; a++) {
307 ULONG ulParamLen;
308 ULONG ulDataLen;
309 char chrParam[2]={0};
310 char chrData;
311 ULONG rc;
312
313 ulDataLen=sizeof(chrData);
314 ulParamLen=sizeof(chrParam);
315 chrParam[1]=a;
316 chrParam[0]=0;
317 /* Check if drive is fixed or removable */
318 rc = DosDevIOCtl(-1, IOCTL_DISK, DSK_BLOCKREMOVABLE,
319 chrParam, sizeof(chrParam), &ulParamLen, &chrData,
320 sizeof(chrData), &ulDataLen);
321
322 if(rc) {
323 if(rc==ERROR_NOT_SUPPORTED) {
324 /* This may be a remote drive */
325 char chrDev[8];
326 ULONG cbBuf;
327 BYTE buf[sizeof(FSQBUFFER2)+3*CCHMAXPATH];
328 PFSQBUFFER2 pBuf=(PFSQBUFFER2)buf;
329
330 sprintf(chrDev, "%c:", 'a'+a);
331 cbBuf=sizeof(buf);
332
333 /* Check if local or remote (or anything else) */
334 rc= DosQueryFSAttach(chrDev, 0, FSAIL_QUERYNAME,(PFSQBUFFER2) &buf, &cbBuf);
335 if(!rc) {
336 if(pBuf->iType==FSAT_REMOTEDRV)
337 chrDisks[a]=QDT_REMOTE_DRIVE;
338 }
339 }/* ERROR_NOT_SUPPPORTED */
340 else
341 chrDisks[a]=QDT_NO_DRIVE; /* Everything we don't understand is no disk */
342 }/* rc */
343 else
344 chrDisks[a]=(chrData & QDT_FIXED_DISK); /* chrData is 1 if Fixed disk */
345 }/* for */
346 bInit=TRUE;
347 }/* bInit */
348 return chrDisks[ulDriveNum-1];
349}
350#endif
351
352ULONG SysQueryFreeDriveSpace(ULONG ulDriveNum, ULONG *ulTotal, ULONG * ulFree, ULONG* ulBytesUnit)
353{
354 FSALLOCATE fsa;
355 ULONG rc;
356
357 if(!ulTotal || !ulFree || !ulBytesUnit)
358 return 1;
359
360 if( ulDriveNum<1 || ulDriveNum > 26)
361 return ERROR_INVALID_DRIVE;
362
363 if((rc=DosQueryFSInfo(ulDriveNum, FSIL_ALLOC, &fsa, sizeof(FSALLOCATE)))!=NO_ERROR)
364 return rc;
365
366 *ulTotal=fsa.cUnit;
367 *ulFree=fsa.cUnitAvail;
368 *ulBytesUnit=fsa.cbSector*fsa.cSectorUnit;
369
370 return(NO_ERROR);
371}
372
373/*
374 The returned volname is always terminated by zero.
375 ulDriveNum: 1...26
376 */
377ULONG SysQueryDiskName(ULONG ulDriveNum, ULONG ulSize, char *chrBuffer)
378{
379 FSINFO fsIBuf;
380 ULONG rc;
381
382 if(!chrBuffer || !ulSize)
383 return 1;
384
385 if( ulDriveNum<1 || ulDriveNum > 26)
386 return ERROR_INVALID_DRIVE;
387
388 if((rc=DosQueryFSInfo(ulDriveNum, FSIL_VOLSER, &fsIBuf, sizeof(FSINFO)))!=NO_ERROR)
389 return rc;
390 strncpy(chrBuffer, fsIBuf.vol.szVolLabel, ulSize);
391 chrBuffer[ulSize-1]=0;
392
393 return(NO_ERROR);
394}
395
396BOOL SysQueryHomeDir(char* chrBuf, ULONG ulSize)
397{
398 char *chrHome=getenv("HOME");
399
400 if(!chrHome || !chrBuf)
401 return FALSE;
402
403#if 0
404 SysWriteToTrapLog("r:\\temp: %d , r:\\bla: %d \n",
405 SysCheckFileExists("r:\\temp"), SysCheckFileExists("r:\\bla"));
406#endif
407
408 if(strlcpy(chrBuf, chrHome, ulSize)>=ulSize) {
409 chrBuf[0]=0;
410 return FALSE;
411 }
412 if(SysCheckFileExists(chrHome))
413 return TRUE;
414
415 /* HOME dir specified but does not exist */
416 SysWriteToTrapLog("%s, %s: the in config.sys specified HOME directory '%s' does not exist.\n",
417 __DATE__, __TIME__, chrHome);
418 return FALSE;
419}
420
421char* my_strlcpy(char* string1, char* string2, size_t count)
422{
423
424 if((strlen(string2))<count)
425 strcpy(string1, string2);
426 else {
427 strncpy(string1, string2, count);
428 string1[count-1]=0;
429 }
430 return string1;
431}
432
433/********************************************************************/
434/* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */
435
436/*
437 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
438 *
439 * Permission to use, copy, modify, and distribute this software for any
440 * purpose with or without fee is hereby granted, provided that the above
441 * copyright notice and this permission notice appear in all copies.
442 *
443 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
444 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
445 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
446 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
447 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
448 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
449 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
450 */
451
452
453/*
454 * Copy src to string dst of size siz. At most siz-1 characters
455 * will be copied. Always NUL terminates (unless siz == 0).
456 * Returns strlen(src); if retval >= siz, truncation occurred.
457 */
458size_t
459strlcpy(char *dst, const char *src, size_t siz)
460{
461 register char *d = dst;
462 register const char *s = src;
463 register size_t n = siz;
464
465 /* Copy as many bytes as will fit */
466 if (n != 0 && --n != 0) {
467 do {
468 if ((*d++ = *s++) == 0)
469 break;
470 } while (--n != 0);
471 }
472
473 /* Not enough room in dst, add NUL and traverse rest of src */
474 if (n == 0) {
475 if (siz != 0)
476 *d = '\0'; /* NUL-terminate dst */
477 while (*s++)
478 ;
479 }
480
481 return(s - src - 1); /* count does not include NUL */
482}
483
484
485/* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */
486
487/*
488 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
489 *
490 * Permission to use, copy, modify, and distribute this software for any
491 * purpose with or without fee is hereby granted, provided that the above
492 * copyright notice and this permission notice appear in all copies.
493 *
494 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
495 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
496 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
497 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
498 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
499 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
500 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
501 */
502
503/*
504 * Appends src to string dst of size siz (unlike strncat, siz is the
505 * full size of dst, not space left). At most siz-1 characters
506 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
507 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
508 * If retval >= siz, truncation occurred.
509 */
510size_t
511strlcat(char *dst, const char *src, size_t siz)
512{
513 register char *d = dst;
514 register const char *s = src;
515 register size_t n = siz;
516 size_t dlen;
517
518 /* Find the end of dst and adjust bytes left but don't go past end */
519 while (n-- != 0 && *d != '\0')
520 d++;
521 dlen = d - dst;
522 n = siz - dlen;
523
524 if (n == 0)
525 return(dlen + strlen(s));
526 while (*s != '\0') {
527 if (n != 1) {
528 *d++ = *s;
529 n--;
530 }
531 s++;
532 }
533 *d = '\0';
534
535 return(dlen + (s - src)); /* count does not include NUL */
536}
537
538/********************************************************************/
539#if __cplusplus
540}
541#endif
542
543
544
545
546
547
548
Note: See TracBrowser for help on using the repository browser.