| 1 | 48 | robfinch | // ============================================================================
 | 
      
         | 2 |  |  | //        __
 | 
      
         | 3 |  |  | //   \\__/ o\    (C) 2012-2017  Robert Finch, Waterloo
 | 
      
         | 4 |  |  | //    \  __ /    All rights reserved.
 | 
      
         | 5 |  |  | //     \/_//     robfinch<remove>@finitron.ca
 | 
      
         | 6 |  |  | //       ||
 | 
      
         | 7 |  |  | //
 | 
      
         | 8 |  |  | // C64 - 'C' derived language compiler
 | 
      
         | 9 |  |  | //  - 64 bit CPU
 | 
      
         | 10 |  |  | //
 | 
      
         | 11 |  |  | // This source file is free software: you can redistribute it and/or modify 
 | 
      
         | 12 |  |  | // it under the terms of the GNU Lesser General Public License as published 
 | 
      
         | 13 |  |  | // by the Free Software Foundation, either version 3 of the License, or     
 | 
      
         | 14 |  |  | // (at your option) any later version.                                      
 | 
      
         | 15 |  |  | //                                                                          
 | 
      
         | 16 |  |  | // This source file is distributed in the hope that it will be useful,      
 | 
      
         | 17 |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of           
 | 
      
         | 18 |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
 | 
      
         | 19 |  |  | // GNU General Public License for more details.                             
 | 
      
         | 20 |  |  | //                                                                          
 | 
      
         | 21 |  |  | // You should have received a copy of the GNU General Public License        
 | 
      
         | 22 |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.    
 | 
      
         | 23 |  |  | //                                                                          
 | 
      
         | 24 |  |  | // ============================================================================
 | 
      
         | 25 |  |  | //
 | 
      
         | 26 |  |  | #include "stdafx.h"
 | 
      
         | 27 |  |  | #include <stdio.h>
 | 
      
         | 28 |  |  | #include <stdlib.h>
 | 
      
         | 29 |  |  | #include <string.h>
 | 
      
         | 30 |  |  |  
 | 
      
         | 31 |  |  | #ifndef __GNUC__
 | 
      
         | 32 |  |  | #include <io.h>
 | 
      
         | 33 |  |  | #endif
 | 
      
         | 34 |  |  |  
 | 
      
         | 35 |  |  | //#include <unistd.h>
 | 
      
         | 36 |  |  |  
 | 
      
         | 37 |  |  | /* ---------------------------------------------------------------------------
 | 
      
         | 38 |  |  |    void searchenv(filename, envname, pathname);
 | 
      
         | 39 |  |  |    char *filename;
 | 
      
         | 40 |  |  |    char *envname;
 | 
      
         | 41 |  |  |    char *pathname;
 | 
      
         | 42 |  |  |  
 | 
      
         | 43 |  |  |    Description :
 | 
      
         | 44 |  |  |       Search for a file by looking in the directories listed in the envname
 | 
      
         | 45 |  |  |    environment. Puts the full path name (if you find it) into pathname.
 | 
      
         | 46 |  |  |    Otherwise set *pathname to 0. Unlike the DOS PATH command (and the
 | 
      
         | 47 |  |  |    microsoft _searchenv), you can use either a space or a semicolon to
 | 
      
         | 48 |  |  |    separate directory names. The pathname array must be at least 128
 | 
      
         | 49 |  |  |    characters.
 | 
      
         | 50 |  |  |  
 | 
      
         | 51 |  |  |    Returns :
 | 
      
         | 52 |  |  |       nothing
 | 
      
         | 53 |  |  | --------------------------------------------------------------------------- */
 | 
      
         | 54 |  |  |  
 | 
      
         | 55 |  |  | void searchenv(char *filename, int fnsz, char *envname, char *pathname, int pthsz)
 | 
      
         | 56 |  |  | {
 | 
      
         | 57 |  |  |    static char pbuf[5000];
 | 
      
         | 58 |  |  |    char *p, *np;
 | 
      
         | 59 |  |  |    size_t len;
 | 
      
         | 60 |  |  | //   char *strpbrk(), *strtok(), *getenv();
 | 
      
         | 61 |  |  |  
 | 
      
         | 62 |  |  |    strcpy_s(pathname, pthsz, filename);
 | 
      
         | 63 |  |  |    if (_access(pathname, 0) != -1)
 | 
      
         | 64 |  |  |       return;
 | 
      
         | 65 |  |  |  
 | 
      
         | 66 |  |  |    /* ----------------------------------------------------------------------
 | 
      
         | 67 |  |  |          The file doesn't exist in the current directory. If a specific
 | 
      
         | 68 |  |  |       path was requested (ie. file contains \ or /) or if the environment
 | 
      
         | 69 |  |  |       isn't set, return a NULL, else search for the file on the path.
 | 
      
         | 70 |  |  |    ---------------------------------------------------------------------- */
 | 
      
         | 71 |  |  |    _dupenv_s(&p, &len, envname);
 | 
      
         | 72 |  |  |    if (len==0)
 | 
      
         | 73 |  |  |    {
 | 
      
         | 74 |  |  |       *pathname = '\0';
 | 
      
         | 75 |  |  |       return;
 | 
      
         | 76 |  |  |    }
 | 
      
         | 77 |  |  |  
 | 
      
         | 78 |  |  |    strncpy_s(pbuf, sizeof(pbuf), p, sizeof(pbuf));
 | 
      
         | 79 |  |  |    np = nullptr;
 | 
      
         | 80 |  |  |    if (p = strtok_s(pbuf, ";", &np))
 | 
      
         | 81 |  |  |    {
 | 
      
         | 82 |  |  |       do
 | 
      
         | 83 |  |  |       {
 | 
      
         | 84 |  |  |                   if (p[strlen(p)-1]=='\\')
 | 
      
         | 85 |  |  |                  sprintf_s(pathname, pthsz-1, "%0.90s%s", p, filename);
 | 
      
         | 86 |  |  |                   else
 | 
      
         | 87 |  |  |                  sprintf_s(pathname, pthsz, "%0.90s\\%s", p, filename);
 | 
      
         | 88 |  |  |  
 | 
      
         | 89 |  |  |          if (_access(pathname, 0) >= 0)
 | 
      
         | 90 |  |  |             return;
 | 
      
         | 91 |  |  |       }
 | 
      
         | 92 |  |  |       while(p = strtok_s(NULL, ";", &np));
 | 
      
         | 93 |  |  |    }
 | 
      
         | 94 |  |  |    *pathname = 0;
 | 
      
         | 95 |  |  | }
 |