home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / libpath.zip / LIBPAT.TXT
Text File  |  1993-08-23  |  5KB  |  150 lines

  1.  
  2. LIBPATH
  3.  
  4. LIBPATH attempts to find out the value of the LIBPATH environment 
  5. variable. This cannot be returned from the regular environment SET 
  6. command, or a DosQuery...API. Occasionally the LIBPATH variable is a 
  7. handy thing to know. So, a not-so-clean solution is to find the value of 
  8. the boot drive, find the CONFIG.SYS file, and attempt to extract the 
  9. LIBPATH string from that file. This will only work in a case where there 
  10. have been no previous changes to the CONFIG.SYS file since the system 
  11. ha been booted, and specifically no direct manipulations of the LIBPATH 
  12. value. Although this example is a crude kluge, the method can actually be 
  13. useful on a number of occasions.
  14.  
  15. ***********************************************************
  16. *                       LIBPATH.C                         *
  17. ***********************************************************
  18.  
  19. #define INCL_DOSFILEMGR
  20. #define INCL_DOSMISC
  21.  
  22. #include <os2.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27.  
  28. #define CONFIG_FILE              "?:\\CONFIG.SYS"
  29. #define LIBPATH                  "LIBPATH"
  30. #define CR                       13
  31. #define LF                       10
  32.  
  33. INT main ( VOID )
  34. {
  35.    APIRET         arReturn ;
  36.    ULONG          ulDrive;
  37.    PCHAR          pchFile ;
  38.    HFILE          hfFile ;
  39.    ULONG          ulAction ;
  40.    ULONG          ulBytesRead ;
  41.    PCHAR          pchBuffer ;
  42.    PCHAR          pchLibpath ;
  43.    USHORT         usIndex ;
  44.    FILESTATUS3    fsStatus ;
  45.  
  46.    pchBuffer = NULL ;
  47.  
  48.    arReturn = DosQuerySysInfo ( QSV_BOOT_DRIVE,
  49.                                 QSV_BOOT_DRIVE,
  50.                                 &ulDrive,
  51.                                 sizeof ( ulDrive )) ;
  52.  
  53.    pchFile = CONFIG_FILE ;
  54.    pchFile [0] = ulDrive + 'A' - 1 ;
  55.  
  56.    arReturn = DosQueryPathInfo ( pchFile,
  57.                                  FIL_STANDARD,
  58.                                  &fsStatus,
  59.                                  sizeof ( fsStatus )) ;
  60.  
  61.    pchBuffer = malloc ( fsStatus.cbFile ) ;
  62.  
  63.    arReturn = DosOpen ( pchFile,
  64.                         &hfFile,
  65.                         &ulAction,
  66.                         0,
  67.                         FILE_NORMAL,
  68.                         FILE_OPEN,
  69.                         OPEN_FLAGS_FAIL_ON_ERROR |
  70.                         OPEN_FLAGS_SEQUENTIAL |
  71.                         OPEN_SHARE_DENYNONE |
  72.                         OPEN_ACCESS_READONLY,
  73.                         NULL ) ;
  74.  
  75.    arReturn = DosRead ( hfFile,
  76.                         pchBuffer,
  77.                         fsStatus.cbFile,
  78.                         &ulBytesRead ) ;
  79.  
  80.    arReturn = DosClose ( hfFile ) ;
  81.  
  82.    pchBuffer [fsStatus.cbFile] = 0 ;
  83.  
  84.    pchLibpath = strstr ( pchBuffer, LIBPATH ) ;
  85.  
  86.    if (pchLibpath == NULL){
  87.    /*will only execute this section of code if LIBPATH is NOT all caps*/
  88.       for ( usIndex = 0; usIndex < strlen (pchBuffer); usIndex++){
  89.       if (toupper(pchBuffer[usIndex]) == 'L')
  90.          if (toupper(pchBuffer[usIndex+1]) == 'I' &&
  91.              toupper(pchBuffer[usIndex+2]) == 'B' &&
  92.              toupper(pchBuffer[usIndex+3]) == 'P' &&
  93.              toupper(pchBuffer[usIndex+4]) == 'A' &&
  94.              toupper(pchBuffer[usIndex+5]) == 'T' &&
  95.              toupper(pchBuffer[usIndex+6]) == 'H'){
  96.            pchLibpath = pchBuffer + usIndex;
  97.            break;
  98.          }
  99.       }
  100.    }
  101.  
  102.    for ( usIndex = 0 ; usIndex < CCHMAXPATHCOMP ; usIndex++ ){
  103.       if ( pchLibpath [usIndex] == CR ){
  104.          if ( pchLibpath [usIndex + 1] == LF )
  105.          break ;
  106.       } /* endif */
  107.    } /* endfor */
  108.  
  109.    pchLibpath [usIndex] = 0 ;
  110.    printf ( "\n%s\n", pchLibpath ) ;
  111.    free ( pchBuffer ) ;
  112.    return arReturn ;
  113. }
  114.  
  115. ***********************************************************
  116. *                       LIBPATH.MAK                       *
  117. ***********************************************************
  118.  
  119. LIBPATH.EXE:                    LIBPATH.OBJ
  120.         LINK386 @<<
  121. LIBPATH
  122. LIBPATH
  123. LIBPATH
  124. OS2386
  125. LIBPATH
  126. <<
  127.  
  128. LIBPATH.OBJ:                    LIBPATH.C
  129.         ICC -C+ -Kb+ -Ss+ LIBPATH.C
  130.  
  131. ***********************************************************
  132. *                       LIBPATH.DEF                       *
  133. ***********************************************************
  134.  
  135. NAME LIBPATH WINDOWCOMPAT NEWFILES
  136. DESCRIPTION 'LIBPATH Example
  137.              Copyright (c) 1993 by Arthur Panov
  138.              All rights reserved.'
  139. PROTMODE
  140.  
  141. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  142. +                       NOTICE                            +
  143. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  144. The LIBPATH example is from The Art of OS/2 C Programming 
  145. by Kathleen Panov, Arthur Panov, and Larry Salomon, Jr.,
  146. August 1993, published by QED Publishing Group,
  147. ISBN 0-89435-446-9. The example was uploaded by the
  148. publisher with the assistance of the authors for use by 
  149. Forum members. Please address comments to QED at 76620,2720.
  150.