home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / bcfamily / source / srchpath.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-12  |  2.5 KB  |  105 lines

  1. //
  2. //      *******************************************************************
  3. //        JdeBP C++ Library Routines          General Public Licence v1.00
  4. //            Copyright (c) 1991,1992     Jonathan de Boyne Pollard
  5. //      *******************************************************************
  6. //
  7. // Part of FamAPI.LIB
  8. //
  9.  
  10. #include "famapi.h"
  11. #include "dosdos.h"
  12.  
  13. //
  14. //    Search path for a file (allowing wildcards)
  15. //
  16. //    NOTE: We must not use the str...() functions from the C++ library.
  17. //
  18. USHORT _APICALL
  19. DosSearchPath    ( unsigned short Search,
  20.                   const char far *PtrPath,
  21.                   const char far *PtrFileName,
  22.                   unsigned char far *PtrBuffer,
  23.                   unsigned short BufferLength )
  24. {
  25.     const char far *TheEnv ;
  26.  
  27.     if (Search & 0x0002) {
  28.         USHORT err = DosScanEnv(PtrPath, &TheEnv) ;
  29.         if (err) return err ;
  30.     } else
  31.         TheEnv = PtrPath ;
  32.  
  33.     char namebuf[256] ;
  34.     struct DOSFind_t find ;
  35.  
  36.     if (Search & 0x0001) {
  37.         const char far *PtrName = PtrFileName ;
  38.         char far *PtrBuf = namebuf ;
  39.  
  40.         *PtrBuf++ = '.';
  41.         *PtrBuf++ = '\\';
  42.  
  43.         // strcat (namebuf, PtrFileName)
  44.         while (PtrBuf < &namebuf[sizeof(namebuf)] && *PtrName)
  45.             *PtrBuf++ = *PtrName++;
  46.  
  47.         *PtrBuf++ = '\000';
  48.  
  49.         if (!DosDosFindFirst ((const char far *)namebuf,
  50.                               _A_NORMAL|_A_SUBDIR|_A_HIDDEN|_A_SYSTEM,
  51.                               &find)) {
  52.             // strncpy(PtrBuffer, namebuf, BufferLength)
  53.             PtrBuf = namebuf ;
  54.             while (BufferLength--)
  55.                 if (*PtrBuffer = *PtrBuf)
  56.                     PtrBuffer++, PtrBuf++;
  57.                 else
  58.                     break ;
  59.  
  60.             return *PtrBuf ? ERROR_BUFFER_OVERFLOW : NO_ERROR ;
  61.         }
  62.  
  63.     }
  64.  
  65.     while (*TheEnv) {
  66.         const char far *PtrName = PtrFileName ;
  67.         char far *PtrBuf = namebuf ;
  68.  
  69.         while (PtrBuf < &namebuf[sizeof(namebuf)] && *TheEnv && *TheEnv != ';')
  70.             *PtrBuf++ = *TheEnv++ ;
  71.  
  72.         if (PtrBuf > namebuf
  73.         &&  PtrBuf < &namebuf[sizeof(namebuf)]
  74.         &&  (PtrBuf[-1] != '\\')
  75.         &&  (PtrBuf[-1] != '/'))
  76.             *PtrBuf++ = '\\';
  77.  
  78.         // strcat (namebuf, PtrFileName)
  79.         while (PtrBuf < &namebuf[sizeof(namebuf)] && *PtrName)
  80.             *PtrBuf++ = *PtrName++;
  81.  
  82.         *PtrBuf++ = '\000';
  83.  
  84.         if (!DosDosFindFirst ((const char far *)namebuf,
  85.                               _A_NORMAL|_A_SUBDIR|_A_HIDDEN|_A_SYSTEM,
  86.                               &find)) {
  87.             // strncpy(PtrBuffer, namebuf, BufferLength)
  88.             PtrBuf = namebuf ;
  89.             while (BufferLength--)
  90.                 if (*PtrBuffer = *PtrBuf)
  91.                     PtrBuffer++, PtrBuf++;
  92.                 else
  93.                     break ;
  94.  
  95.             return *PtrBuf ? ERROR_BUFFER_OVERFLOW : NO_ERROR ;
  96.         }
  97.  
  98.         while (*TheEnv && *TheEnv != ';') TheEnv++ ;
  99.  
  100.         if (*TheEnv == ';') TheEnv++ ;
  101.     }
  102.  
  103.     return ERROR_FILE_NOT_FOUND ;
  104. }
  105.