Remove Unixware and openserver support
[oweals/cde.git] / cde / programs / dtinfo / dtinfogen / infolib / etc / SearchPath.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /* $XConsortium: SearchPath.C /main/7 1996/08/21 15:47:21 drk $ */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <strings.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sstream>
31 using namespace std;
32
33 #include "Exceptions.hh"
34 #include "Task.h"
35 #include "api/utility.h"
36
37 /* exported interface */
38 #include "SearchPath.h"
39
40 #ifdef NEED_STRCASECMP
41 #include <ctype.h>
42 /*
43  * In case strcasecmp and strncasecmp are not provided by the system
44  * here are ones which do the trick.
45  */
46
47 int
48 strcasecmp(register const char *s1,
49            register const char *s2)
50 {
51     register int c1, c2;
52
53     while (*s1 && *s2) {
54         c1 = isupper(*s1) ? tolower(*s1) : *s1;
55         c2 = isupper(*s2) ? tolower(*s2) : *s2;
56         if (c1 != c2)
57             return (c1 - c2);
58         s1++;
59         s2++;
60     }
61     return (int) (*s1 - *s2);
62 }
63
64
65 int
66 strncasecmp(register const char *s1,
67             register const char *s2,
68             register size_t count)
69 {
70     register int c1, c2;
71
72     if (!count)
73       return 0;
74
75     while (*s1 && *s2) {
76         c1 = isupper(*s1) ? tolower(*s1) : *s1;
77         c2 = isupper(*s2) ? tolower(*s2) : *s2;
78         if ((c1 != c2) || (! --count))
79             return (c1 - c2);
80         s1++;
81         s2++;
82     }
83     return (int) (*s1 - *s2);
84 }
85 #endif
86
87
88 //--------------------------------------------------------------------
89 static int isdir(char* filename)
90 {
91   int ret = 0;
92   struct stat sb;
93
94   if(stat(filename, &sb) == 0){
95     if(S_ISDIR(sb.st_mode)){
96       ret = 1;
97     }
98   }
99
100   return ret;
101 }
102
103   
104 //-------------------------------------------------------------------
105 SearchPath::SearchPath( const char *path, ... )
106 {
107   
108   search_path_table = new CC_TPtrSlist<CC_String>;
109   new_path = 0;
110
111   va_list ap;
112   va_start ( ap , path);
113
114   const char *spath = path;
115   while ( spath ) {
116     CC_String *key = new CC_String(spath);
117     search_path_table->append( key );
118     spath = va_arg ( ap, const char * );
119   }
120   
121   va_end ( ap );
122 }
123
124 //-------------------------------------------------------------------
125 char *
126 SearchPath::get_real_path( const char *file_name )
127 {
128   CC_TPtrSlistIterator<CC_String> path_it( *search_path_table );
129   FILE *fp = NULL;
130
131   if (file_name == NULL || *file_name == '\0')
132     return NULL;
133
134   // remove storage object specifier
135   if (strncasecmp(file_name, "<OSFILE", 7) == 0) {
136     if ((file_name = strchr(file_name, '>')))
137       file_name++;
138     else
139       return NULL;
140   }
141
142   while ( path_it() ) {
143     const char *path = (const char *)*path_it.key();
144
145     char *full_path_name = form( "%s/%s", path, file_name );
146     if (( fp = fopen( full_path_name , "r" )) && !isdir(full_path_name) ) {
147       fclose( fp );
148       return ( full_path_name );
149     }
150     if(fp) {
151       fclose( fp );
152     }
153   }
154
155   return NULL;
156 }
157
158
159 //-------------------------------------------------------------------    
160 void
161 SearchPath::replace_file_scope( const char *f_path )
162 {
163   if ( new_path ) {
164     if ( !search_path_table->remove( new_path ) ) {
165       throw(Unexpected("Cannot replace file scope\n"));
166     }
167     delete new_path; new_path = 0;
168   }
169
170   new_path = new CC_String(f_path);
171
172   search_path_table->prepend( new_path );
173
174 }
175