source: trunk/common_functions/sys_funcs.c

Last change on this file was 207, checked in by gyoung, 6 months ago

Move Trap/Debug log to LOGFILES directory

File size: 22.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 Documentation script can't handle the const keyword yet!!!!
77*/
78/*--->!**************************************************/
79/* */
80/* @@DESC */
81/* */
82/* This function may be used to write data into the */
83/* traplog file. The syntax is similar to printf(). */
84/* */
85/* @@REMARKS */
86/* */
87/* The filename of the traplog is defined in the */
88/* include file :hp2.excptLogName.h:ehp2. */
89/* */
90/* !parsing is broken at the moment because of const and (...) */
91/* */
92/*!!*************************************************/
93void SysWriteToTrapLog(const char* chrFormat, ...)
94{
95 char logNameLocal[CCHMAXPATH];
96 FILE *fHandle;
97 CHAR *LogPath;
98
99
100 LogPath = getenv( "LOGFILES" );
101 if (!LogPath)
102 sprintf(logNameLocal,"%c:\\%s",SysQueryBootDriveLetter(), EXCEPTION_LOGFILE_NAME);
103 else
104 sprintf(logNameLocal,"%s\\%s",LogPath, EXCEPTION_LOGFILE_NAME);
105
106 fHandle=fopen(logNameLocal,"a");
107 if(fHandle) {
108 va_list arg_ptr;
109
110 va_start (arg_ptr, chrFormat);
111 vfprintf(fHandle, chrFormat, arg_ptr);
112 va_end (arg_ptr);
113 fclose(fHandle);
114 }
115}
116
117/*!**************************************************/
118/* */
119/* @@DESC */
120/* */
121/* This function checks if the given file exists. */
122/* thread. */
123/* */
124/* @@RETURNS */
125/* */
126/* BOOL fExists */
127/* */
128/* TRUE: File exists, FALSE otherwise. */
129/* */
130/*!!*************************************************/
131BOOL SysCheckFileExists(char* chrFileName)
132{
133 struct stat statBuf;
134
135 /* Check file path */
136 if(stat(chrFileName , &statBuf)==-1)
137 return FALSE;
138 else
139 return TRUE;
140}
141
142
143/*!**************************************************/
144/* */
145/* @@DESC */
146/* */
147/* This function queries the size of a file. */
148/* */
149/* @@RETURNS */
150/* */
151/* ULONG ulSize */
152/* */
153/* Size of the file. */
154/* */
155/* @@REMARKS */
156/* */
157/* In case of a nonexistant file 0 is returned. So */
158/* it's necessary to first check if the file exists.*/
159/* */
160/*!!*************************************************/
161ULONG SysQueryFileSize(char* chrFileName)
162{
163 struct stat statBuf;
164
165 /* Check file path */
166 if(stat(chrFileName , &statBuf)==-1)
167 return 0;
168
169 return statBuf.st_size;
170}
171
172/*!**************************************************/
173/* */
174/* @@DESC */
175/* */
176/* This function returns the ID of the calling */
177/* thread. */
178/* */
179/* @@RETURNS */
180/* */
181/* ULONG ulTID */
182/* */
183/* ID of the current thread. */
184/* */
185/*!!*************************************************/
186ULONG SysQueryCurrentTID(void)
187{
188 PPIB ppib;
189 PTIB ptib;
190
191 DosGetInfoBlocks( &ptib, &ppib);
192
193 return ptib->tib_ptib2->tib2_ultid;
194}
195
196/*!**************************************************/
197/* */
198/* @@DESC */
199/* */
200/* To be written... */
201/* */
202/*!!*************************************************/
203ULONG SysQueryDriveType(ULONG ulDriveNum)
204{
205 ULONG ulDisk=0;
206 ULONG ulParamLen;
207 ULONG ulDataLen;
208 char chrParam[2]={0};
209 char chrData;
210 ULONG rc;
211
212 if(ulDriveNum>26 || ulDriveNum<1)
213 return QDT_ERROR_DISK;
214
215 // SysWriteToTrapLog("Drive nr.: %d\n", ulDriveNum);
216
217 /*
218 Query fixed, removable and remote drives in the system.
219 */
220 ulDataLen=sizeof(chrData);
221 ulParamLen=sizeof(chrParam);
222 chrParam[1]=(BYTE)ulDriveNum-1;
223 chrParam[0]=0;
224 /* Check if drive is fixed or removable */
225 rc = DosDevIOCtl(-1, IOCTL_DISK, DSK_BLOCKREMOVABLE,
226 chrParam, sizeof(chrParam), &ulParamLen, &chrData,
227 sizeof(chrData), &ulDataLen);
228
229 if(rc) {
230 // SysWriteToTrapLog("Drive nr.: %d, rc for DSK_BLOCKREMOVABLE", ulDriveNum);
231 if(rc==ERROR_NOT_SUPPORTED) {
232 /* This may be a remote drive */
233 char chrDev[8];
234 ULONG cbBuf;
235 BYTE buf[sizeof(FSQBUFFER2)+3*CCHMAXPATH];
236 PFSQBUFFER2 pBuf=(PFSQBUFFER2)buf;
237
238 // SysWriteToTrapLog("Drive nr.: %d, rc for DSK_BLOCKREMOVABLE-> ERROR_NOT_SUPPORTED\n", ulDriveNum);
239 sprintf(chrDev, "%c:", 'a'+(char)(ulDriveNum-1));
240 cbBuf=sizeof(buf);
241
242 /* Check if local or remote (or anything else) */
243 rc= DosQueryFSAttach(chrDev, 0, FSAIL_QUERYNAME,(PFSQBUFFER2) &buf, &cbBuf);
244 if(!rc) {
245 if(pBuf->iType==FSAT_REMOTEDRV)
246 ulDisk=QDT_REMOTE_DRIVE;
247 }
248 }/* ERROR_NOT_SUPPPORTED */
249 else
250 ulDisk=QDT_NO_DRIVE; /* Everything we don't understand is no disk */
251 }/* rc != NO_ERROR */
252 else {
253 /* We end here for HDs, floppies and CD-ROMS */
254 // SysWriteToTrapLog("Drive nr.: %d, local ones\n", ulDriveNum);
255 if(chrData ) /* chrData is 1 if Fixed disk */
256 return QDT_FIXED_DISK;
257 else {
258 /* Check for floppy drives */
259 BYTE abParm[2] = { 0, (BYTE)ulDriveNum-1 };
260 ULONG ulParmLen = 2;
261 ULONG ulDataLen = 0;
262 BIOSPARAMETERBLOCK bpb;
263
264 // SysWriteToTrapLog("Drive nr.: %d, not a fixed disk\n", ulDriveNum);
265
266 /* The disk is local. Look for floppy */
267 if (DosDevIOCtl ((HFILE)-1, IOCTL_DISK, DSK_GETDEVICEPARAMS,
268 (PVOID)&abParm[0], sizeof (abParm), &ulParmLen, (PVOID)&bpb, sizeof (bpb), &ulDataLen) == NO_ERROR) {
269 {
270 ULONG ulDevType;
271 BOOL fRemovable;
272
273 if (ulDataLen < 36)
274 return (QDT_NO_DRIVE); /* Error */
275
276 ulDevType = bpb.bDeviceType;
277 fRemovable = (BOOL)((bpb.fsDeviceAttr & 0x01) ? FALSE : TRUE);
278 // SysWriteToTrapLog("Drive nr.: %d, ulDevType: %d\n", ulDriveNum, ulDevType);
279
280 if ((ulDevType == 7) && fRemovable) {
281 // 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).
282 switch (bpb.usBytesPerSector * bpb.cSectors) {
283 case 512*1440:
284 return QDT_DISKETTE_DRIVE; //DEVTYPE_35INCH_1MB_DISKETTE;
285 case 512*2880:
286 return QDT_DISKETTE_DRIVE;//DEVTYPE_35INCH_2MB_DISKETTE;
287 case 512*5760:
288 return QDT_DISKETTE_DRIVE;//DEVTYPE_35INCH_2MB_DISKETTE;
289 }/* switch()*/
290 }else {
291 switch (ulDevType) {
292 case 0: /* 48TPI diskette */
293 case 1: /* 96TPI diskette */
294 case 2: /* 3.5 inch diskette */
295 case 3: /* 8inch single diskette */
296 case 4: /* 8inch double diskette */
297 case 9: /* 3.5 4MB (2.88 formatted) diskette */
298 return QDT_DISKETTE_DRIVE;
299 }/* switch() */
300 }/* else */
301 // SysWriteToTrapLog("Drive nr.: %d, ????\n", ulDriveNum);
302 /* Assume CD-ROM here */
303 return (QDT_CDROM_DRIVE); /* For example tape */
304 return (QDT_NO_DRIVE); /* For example tape */
305 }
306 }/* DSK_GETDEVICEPARAMS */
307 }/* else */
308 ulDisk=QDT_REMOVABLE_DISK;
309 }
310
311 return ulDisk;
312}
313
314#if 0
315ULONG SysQueryDriveType_(ULONG ulDriveNum)
316{
317 static char chrDisks[26]={0};
318 static BOOL bInit=FALSE;
319 int a;
320
321 if(ulDriveNum>26 || ulDriveNum<1)
322 return QDT_ERROR_DISK;
323
324 if(!bInit) {
325 /*
326 Query fixed, removable and remote drives in the system.
327 */
328 for(a=0; a<26; a++) {
329 ULONG ulParamLen;
330 ULONG ulDataLen;
331 char chrParam[2]={0};
332 char chrData;
333 ULONG rc;
334
335 ulDataLen=sizeof(chrData);
336 ulParamLen=sizeof(chrParam);
337 chrParam[1]=a;
338 chrParam[0]=0;
339 /* Check if drive is fixed or removable */
340 rc = DosDevIOCtl(-1, IOCTL_DISK, DSK_BLOCKREMOVABLE,
341 chrParam, sizeof(chrParam), &ulParamLen, &chrData,
342 sizeof(chrData), &ulDataLen);
343
344 if(rc) {
345 if(rc==ERROR_NOT_SUPPORTED) {
346 /* This may be a remote drive */
347 char chrDev[8];
348 ULONG cbBuf;
349 BYTE buf[sizeof(FSQBUFFER2)+3*CCHMAXPATH];
350 PFSQBUFFER2 pBuf=(PFSQBUFFER2)buf;
351
352 sprintf(chrDev, "%c:", 'a'+a);
353 cbBuf=sizeof(buf);
354
355 /* Check if local or remote (or anything else) */
356 rc= DosQueryFSAttach(chrDev, 0, FSAIL_QUERYNAME,(PFSQBUFFER2) &buf, &cbBuf);
357 if(!rc) {
358 if(pBuf->iType==FSAT_REMOTEDRV)
359 chrDisks[a]=QDT_REMOTE_DRIVE;
360 }
361 }/* ERROR_NOT_SUPPPORTED */
362 else
363 chrDisks[a]=QDT_NO_DRIVE; /* Everything we don't understand is no disk */
364 }/* rc */
365 else
366 chrDisks[a]=(chrData & QDT_FIXED_DISK); /* chrData is 1 if Fixed disk */
367 }/* for */
368 bInit=TRUE;
369 }/* bInit */
370 return chrDisks[ulDriveNum-1];
371}
372#endif
373
374/*!**************************************************/
375/* */
376/* @@DESC */
377/* */
378/* To be written... */
379/* */
380/*!!*************************************************/
381ULONG SysQueryFreeDriveSpace(ULONG ulDriveNum, ULONG *ulTotal, ULONG * ulFree, ULONG* ulBytesUnit)
382{
383 FSALLOCATE fsa;
384 ULONG rc;
385
386 if(!ulTotal || !ulFree || !ulBytesUnit)
387 return 1;
388
389 if( ulDriveNum<1 || ulDriveNum > 26)
390 return ERROR_INVALID_DRIVE;
391
392 if((rc=DosQueryFSInfo(ulDriveNum, FSIL_ALLOC, &fsa, sizeof(FSALLOCATE)))!=NO_ERROR)
393 return rc;
394
395 *ulTotal=fsa.cUnit;
396 *ulFree=fsa.cUnitAvail;
397 *ulBytesUnit=fsa.cbSector*fsa.cSectorUnit;
398
399 return(NO_ERROR);
400}
401
402/*!**************************************************/
403/* */
404/* @@DESC */
405/* */
406/* This function returns the home directory of the */
407/* current user. */
408/* */
409/* @@RETURNS */
410/* */
411/* ULONG rc */
412/* */
413/* NO_ERROR is returned if the function succeeds. */
414/* */
415/* @@PARAM */
416/* */
417/* ULONG ulDriveNum input */
418/* */
419/* The number of the drive to be queried (1-26). */
420/* */
421/* @@PARAM */
422/* */
423/* char* chrBuffer in/out */
424/* */
425/* Pointer to a buffer where the volume name will */
426/* be placed. */
427/* */
428/* @@PARAM */
429/* */
430/* ULONG ulSize input */
431/* */
432/* Size of the name buffer. */
433/* */
434/* @@REMARKS */
435/* */
436/* The returned name is always terminated by zero. */
437/* */
438/*!!*************************************************/
439
440ULONG SysQueryDiskName(ULONG ulDriveNum, char *chrBuffer, ULONG ulSize)
441{
442 FSINFO fsIBuf;
443 ULONG rc;
444
445 if(!chrBuffer || !ulSize)
446 return 1;
447
448 if( ulDriveNum<1 || ulDriveNum > 26)
449 return ERROR_INVALID_DRIVE;
450
451 if((rc=DosQueryFSInfo(ulDriveNum, FSIL_VOLSER, &fsIBuf, sizeof(FSINFO)))!=NO_ERROR)
452 return rc;
453 strncpy(chrBuffer, fsIBuf.vol.szVolLabel, ulSize);
454 chrBuffer[ulSize-1]=0;
455
456 return(NO_ERROR);
457}
458
459/*!**************************************************/
460/* */
461/* @@DESC */
462/* */
463/* This function returns the home directory of the */
464/* current user. */
465/* */
466/* @@RETURNS */
467/* */
468/* BOOL fSuccess */
469/* */
470/* TRUE if the home dir is specified and does exist */
471/* */
472/* @@PARAM */
473/* */
474/* char* chrBuf in/out */
475/* */
476/* Pointer to a buffer where the home directory */
477/* name will be placed. */
478/* */
479/* @@PARAM */
480/* */
481/* ULONG ulSize input */
482/* */
483/* Size of the name buffer. */
484/* */
485/* @@REMARKS */
486/* */
487/* The returned string is always terminated by zero.*/
488/* .p: */
489/* If a directory is specified in the HOME */
490/* environment variable but doesn't exist FALSE is */
491/* returned and a message is written to the apps */
492/* exception log. */
493/* */
494/*!!*************************************************/
495BOOL SysQueryHomeDir(char* chrBuf, ULONG ulSize)
496{
497 char *chrHome=getenv("HOME");
498
499 if(!chrHome || !chrBuf)
500 return FALSE;
501
502 if(strlcpy(chrBuf, chrHome, ulSize)>=ulSize) {
503 chrBuf[0]=0;
504 return FALSE;
505 }
506 if(SysCheckFileExists(chrHome))
507 return TRUE;
508
509 /* HOME dir specified but does not exist */
510 SysWriteToTrapLog("%s, %s: the in config.sys specified HOME directory '%s' does not exist.\n",
511 __DATE__, __TIME__, chrHome);
512 return FALSE;
513}
514
515char* my_strlcpy(char* string1, char* string2, size_t count)
516{
517
518 if((strlen(string2))<count)
519 strcpy(string1, string2);
520 else {
521 strncpy(string1, string2, count);
522 string1[count-1]=0;
523 }
524 return string1;
525}
526
527/********************************************************************/
528/* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */
529
530/*
531 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
532 *
533 * Permission to use, copy, modify, and distribute this software for any
534 * purpose with or without fee is hereby granted, provided that the above
535 * copyright notice and this permission notice appear in all copies.
536 *
537 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
538 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
539 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
540 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
541 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
542 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
543 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
544 */
545
546
547/*
548 * Copy src to string dst of size siz. At most siz-1 characters
549 * will be copied. Always NUL terminates (unless siz == 0).
550 * Returns strlen(src); if retval >= siz, truncation occurred.
551 */
552size_t
553strlcpy(char *dst, const char *src, size_t siz)
554{
555 register char *d = dst;
556 register const char *s = src;
557 register size_t n = siz;
558
559 /* Copy as many bytes as will fit */
560 if (n != 0 && --n != 0) {
561 do {
562 if ((*d++ = *s++) == 0)
563 break;
564 } while (--n != 0);
565 }
566
567 /* Not enough room in dst, add NUL and traverse rest of src */
568 if (n == 0) {
569 if (siz != 0)
570 *d = '\0'; /* NUL-terminate dst */
571 while (*s++)
572 ;
573 }
574
575 return(s - src - 1); /* count does not include NUL */
576}
577
578
579/* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */
580
581/*
582 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
583 *
584 * Permission to use, copy, modify, and distribute this software for any
585 * purpose with or without fee is hereby granted, provided that the above
586 * copyright notice and this permission notice appear in all copies.
587 *
588 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
589 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
590 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
591 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
592 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
593 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
594 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
595 */
596
597/*
598 * Appends src to string dst of size siz (unlike strncat, siz is the
599 * full size of dst, not space left). At most siz-1 characters
600 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
601 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
602 * If retval >= siz, truncation occurred.
603 */
604size_t
605strlcat(char *dst, const char *src, size_t siz)
606{
607 register char *d = dst;
608 register const char *s = src;
609 register size_t n = siz;
610 size_t dlen;
611
612 /* Find the end of dst and adjust bytes left but don't go past end */
613 while (n-- != 0 && *d != '\0')
614 d++;
615 dlen = d - dst;
616 n = siz - dlen;
617
618 if (n == 0)
619 return(dlen + strlen(s));
620 while (*s != '\0') {
621 if (n != 1) {
622 *d++ = *s;
623 n--;
624 }
625 s++;
626 }
627 *d = '\0';
628
629 return(dlen + (s - src)); /* count does not include NUL */
630}
631
632/********************************************************************/
633#if __cplusplus
634}
635#endif
636
637
638
639
640
641
642
Note: See TracBrowser for help on using the repository browser.