2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: SearchPath.C /main/7 1996/08/21 15:47:21 drk $ */
29 #include <sys/types.h>
33 #include "Exceptions.hh"
35 #include "api/utility.h"
37 /* exported interface */
38 #include "SearchPath.h"
40 #ifdef NEED_STRCASECMP
43 * In case strcasecmp and strncasecmp are not provided by the system
44 * here are ones which do the trick.
48 strcasecmp(register const char *s1,
49 register const char *s2)
54 c1 = isupper(*s1) ? tolower(*s1) : *s1;
55 c2 = isupper(*s2) ? tolower(*s2) : *s2;
61 return (int) (*s1 - *s2);
66 strncasecmp(register const char *s1,
67 register const char *s2,
68 register size_t count)
76 c1 = isupper(*s1) ? tolower(*s1) : *s1;
77 c2 = isupper(*s2) ? tolower(*s2) : *s2;
78 if ((c1 != c2) || (! --count))
83 return (int) (*s1 - *s2);
88 //--------------------------------------------------------------------
89 static int isdir(char* filename)
94 if(stat(filename, &sb) == 0){
95 if(S_ISDIR(sb.st_mode)){
104 //-------------------------------------------------------------------
105 SearchPath::SearchPath( const char *path, ... )
108 search_path_table = new CC_TPtrSlist<CC_String>;
112 va_start ( ap , path);
114 const char *spath = path;
116 CC_String *key = new CC_String(spath);
117 search_path_table->append( key );
118 spath = va_arg ( ap, const char * );
124 //-------------------------------------------------------------------
126 SearchPath::get_real_path( const char *file_name )
128 CC_TPtrSlistIterator<CC_String> path_it( *search_path_table );
131 if (file_name == NULL || *file_name == '\0')
134 // remove storage object specifier
135 if (strncasecmp(file_name, "<OSFILE", 7) == 0) {
136 if ((file_name = strchr(file_name, '>')))
142 while ( path_it() ) {
143 const char *path = (const char *)*path_it.key();
145 char *full_path_name = form( "%s/%s", path, file_name );
146 if (( fp = fopen( full_path_name , "r" )) && !isdir(full_path_name) ) {
148 return ( full_path_name );
159 //-------------------------------------------------------------------
161 SearchPath::replace_file_scope( const char *f_path )
164 if ( !search_path_table->remove( new_path ) ) {
165 throw(Unexpected("Cannot replace file scope\n"));
167 delete new_path; new_path = 0;
170 new_path = new CC_String(f_path);
172 search_path_table->prepend( new_path );