Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  This function searches for the specified file in the
  28. *               1) current directory or, failing that,
  29. *               2) the paths listed in the specified environment variable
  30. *               until it finds the first occurrence of the file.
  31. *
  32. ****************************************************************************/
  33.  
  34.  
  35. #include "variety.h"
  36. #include "widechar.h"
  37. #undef __INLINE_FUNCTIONS__
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <unistd.h>
  41. #ifndef __UNIX__
  42.     #include <direct.h>
  43. #endif
  44. #include <string.h>
  45. #include "rtdata.h"
  46. #include "seterrno.h"
  47. #ifdef __WIDECHAR__
  48.     #include "wenviron.h"
  49. #endif
  50.  
  51. #if defined(__UNIX__)
  52.     #ifdef __WIDECHAR__
  53.         #define PATH_SEPARATOR L'/'
  54.         #define LIST_SEPARATOR L':'
  55.     #else
  56.         #define PATH_SEPARATOR '/'
  57.         #define LIST_SEPARATOR ':'
  58.     #endif
  59. #else
  60.     #ifdef __WIDECHAR__
  61.         #define PATH_SEPARATOR L'\\'
  62.         #define LIST_SEPARATOR L';'
  63.     #else
  64.         #define PATH_SEPARATOR '\\'
  65.         #define LIST_SEPARATOR ';'
  66.     #endif
  67. #endif
  68.  
  69.  
  70. _WCRTLINK void __F_NAME(_searchenv,_wsearchenv)( const CHAR_TYPE *name, const CHAR_TYPE *env_var, CHAR_TYPE *buffer )
  71. {
  72.     CHAR_TYPE   *p, *p2;
  73.     int         prev_errno;
  74.     size_t      len;
  75.  
  76. #ifdef __WIDECHAR__
  77.     if( _RWD_wenviron == NULL )  __create_wide_environment();
  78. #endif
  79.  
  80.     prev_errno = _RWD_errno;
  81.     if( __F_NAME(access,_waccess)( name, F_OK ) == 0 ) {
  82.         p = buffer;                                 /* JBS 90/3/30 */
  83.         len = 0;                                    /* JBS 04/1/06 */
  84.         for( ;; ) {
  85.             if( name[0] == PATH_SEPARATOR ) break;
  86.             if( name[0] == __F_NAME('.',L'.') ) break;
  87. #ifndef __UNIX__
  88.             if( name[0] == __F_NAME('/',L'/') ) break;
  89.             if( (name[0] != __F_NAME('\0',L'\0')) && (name[1] == __F_NAME(':',L':')) ) break;
  90. #endif
  91.             __F_NAME(getcwd,_wgetcwd)( buffer, _MAX_PATH );
  92.             len = __F_NAME(strlen,wcslen)( buffer );
  93.             p = &buffer[ len ];
  94.             if( p[-1] != PATH_SEPARATOR ) {
  95.                 if( len < (_MAX_PATH - 1) ) {
  96.                     *p++ = PATH_SEPARATOR;
  97.                     len++;
  98.                 }
  99.             }
  100.             break;
  101.         }
  102.         *p = __F_NAME('\0',L'\0');
  103.         __F_NAME(strncat,wcsncat)( p, name, (_MAX_PATH - 1) - len );
  104.         return;
  105.     }
  106.     p = __F_NAME(getenv,_wgetenv)( env_var );
  107.     if( p != NULL ) {
  108.         for( ;; ) {
  109.             if( *p == __F_NAME('\0',L'\0') ) break;
  110.             p2 = buffer;
  111.             len = 0;                                /* JBS 04/1/06 */
  112.             while( *p ) {
  113.                 if( *p == LIST_SEPARATOR ) break;
  114.                 if( *p != __F_NAME('"',L'"') ) {
  115.                     if( len < (_MAX_PATH-1) ) {
  116.                         *p2++ = *p; /* JBS 00/9/29 */
  117.                         len++;
  118.                     }
  119.                 }
  120.                 p++;
  121.             }
  122.             /* check for zero-length prefix which represents CWD */
  123.             if( p2 != buffer ) {                    /* JBS 90/3/30 */
  124.                 if( p2[-1] != PATH_SEPARATOR
  125. #ifndef __UNIX__
  126.                     &&  p2[-1] != __F_NAME('/','/')
  127.                     &&  p2[-1] != __F_NAME(':',':')
  128. #endif
  129.                     ) {
  130.                     if( len < (_MAX_PATH - 1) ) {
  131.                         *p2++ = PATH_SEPARATOR;
  132.                         len++;
  133.                     }
  134.                 }
  135.                 *p2 = __F_NAME('\0',L'\0');
  136.                 len += __F_NAME(strlen,wcslen)( name );/* JBS 04/12/23 */
  137.                 if( len < _MAX_PATH ) {
  138.                     __F_NAME(strcat,wcscat)( p2, name );
  139.                     /* check to see if file exists */
  140.                     if( __F_NAME(access,_waccess)( buffer, 0 ) == 0 ) {
  141.                         __set_errno( prev_errno );
  142.                         return;
  143.                     }
  144.                 }
  145.             }
  146.             if( *p == '\0' ) break;
  147.             ++p;
  148.         }
  149.     }
  150.     buffer[0] = __F_NAME( '\0',L'\0' );
  151. }
  152.