source: branches/v2.9_Lars/common_functions/sys_funcs.c

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

Import modifications from cwmm-0_2_9-work-01_10_2006.zip dated 2006-08-27

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