1 mike 1.19 //%/////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 chip 1.20.2.1 // of this software and associated documentation files (the "Software"), to
7 // deal in the Software without restriction, including without limitation the
8 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
9 mike 1.19 // sell copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
|
11 chip 1.20.2.1 //
12 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
13 mike 1.19 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
14 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
15 chip 1.20.2.1 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
18 mike 1.19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 //
21 //==============================================================================
22 //
23 // Author: Mike Brasher (mbrasher@bmc.com)
24 //
25 // Modified By:
26 //
27 //%/////////////////////////////////////////////////////////////////////////////
28
29 #ifdef PEGASUS_OS_HPUX
30 # include <dl.h>
31 #else
32 # include <dlfcn.h>
33 #endif
34
35 # include <unistd.h>
36 #include <dirent.h>
37 #include "System.h"
38 #include <sys/stat.h>
39 mike 1.19 #include <sys/types.h>
40 #include <cstdio>
41 #include <time.h>
42
43 PEGASUS_NAMESPACE_BEGIN
44
45 #include <sys/time.h>
46 #include <unistd.h>
47
48 inline void sleep_wrapper(Uint32 seconds)
49 {
50 sleep(seconds);
51 }
52
53 void System::getCurrentTime(Uint32& seconds, Uint32& milliseconds)
54 {
55 timeval tv;
56 gettimeofday(&tv, 0);
57 seconds = int(tv.tv_sec);
58 milliseconds = int(tv.tv_usec) / 1000;
59 }
60 mike 1.19
61 String System::getCurrentASCIITime()
62 {
63 char str[50];
64 time_t rawTime;
65
66 time(&rawTime);
67 strftime(str, 40,"%T-%D", localtime(&rawTime));
68 String time = str;
69 return time;
70 }
71
72 void System::sleep(Uint32 seconds)
73 {
74 sleep_wrapper(seconds);
75 }
76
77 Boolean System::exists(const char* path)
78 {
79 return access(path, F_OK) == 0;
80 }
81 mike 1.19
82 Boolean System::canRead(const char* path)
83 {
84 return access(path, R_OK) == 0;
85 }
86
87 Boolean System::canWrite(const char* path)
88 {
89 return access(path, W_OK) == 0;
90 }
91
92 Boolean System::getCurrentDirectory(char* path, Uint32 size)
93 {
94 return getcwd(path, size) != NULL;
95 }
96
97 Boolean System::isDirectory(const char* path)
98 {
99 struct stat st;
100
101 if (stat(path, &st) != 0)
102 mike 1.19 return false;
103
104 return S_ISDIR(st.st_mode);
105 }
106
107 Boolean System::changeDirectory(const char* path)
108 {
109 return chdir(path) == 0;
110 }
111
112 Boolean System::makeDirectory(const char* path)
113 {
114 return mkdir(path, 0777) == 0;
115 }
116
117 Boolean System::getFileSize(const char* path, Uint32& size)
118 {
119 struct stat st;
120
121 if (stat(path, &st) != 0)
122 return false;
123 mike 1.19
124 size = st.st_size;
125 return true;
126 }
127
128 Boolean System::removeDirectory(const char* path)
129 {
130 return rmdir(path) == 0;
131 }
132
133 Boolean System::removeFile(const char* path)
134 {
135 return unlink(path) == 0;
136 }
137
138 Boolean System::renameFile(const char* oldPath, const char* newPath)
139 {
140 if (link(oldPath, newPath) != 0)
141 return false;
142
143 return unlink(oldPath) == 0;
144 mike 1.19 }
145
146 DynamicLibraryHandle System::loadDynamicLibrary(const char* fileName)
147 {
|
148 mike 1.20 #if defined(PEGASUS_OS_HPUX)
|
149 mike 1.19 char* p = strcpy(new char[strlen(fileName) + 4], fileName);
150 char* dot = strrchr(p, '.');
151
152 if (!dot)
153 return 0;
154
155 *dot = '\0';
156 strcat(p, ".sl");
157
158 void* handle = shl_load(p, BIND_IMMEDIATE | DYNAMIC_PATH, 0L);
159 delete [] p;
160
161 return DynamicLibraryHandle(handle);
162 #else
|
163 mike 1.20
164 # ifdef PEGASUS_OS_TRU64
165 return DynamicLibraryHandle(dlopen(fileName, RTLD_NOW));
166 # else
|
167 mike 1.19 return DynamicLibraryHandle(dlopen(fileName, RTLD_NOW | RTLD_GLOBAL));
|
168 mike 1.20 # endif
169
|
170 mike 1.19 #endif
|
171 chip 1.20.2.1 }
172
173 void System::unloadDynamicLibrary(DynamicLibraryHandle libraryHandle)
174 {
|
175 sage 1.20.2.2 #ifdef PEGASUS_OS_LINUX
176 dlclose(libraryHandle);
177 #endif
|
178 mike 1.19 }
179
180 String System::dynamicLoadError() {
181 #ifdef PEGASUS_OS_HPUX
182 return String();
183 #else
184 String dlerr = dlerror();
185 return dlerr;
186 #endif
187 }
188
189
190 DynamicSymbolHandle System::loadDynamicSymbol(
191 DynamicLibraryHandle libraryHandle,
192 const char* symbolName)
193 {
194 #ifdef PEGASUS_OS_HPUX
195 char* p = (char*)symbolName;
196 void* proc = 0;
197
198 if (shl_findsym((shl_t*)&libraryHandle, p, TYPE_UNDEFINED, &proc) == 0)
199 mike 1.19 return DynamicSymbolHandle(proc);
200
201 p = strcpy(new char[strlen(symbolName) + 2], symbolName);
202 strcpy(p, "_");
203 strcat(p, symbolName);
204
205 if (shl_findsym((shl_t*)libraryHandle, p, TYPE_UNDEFINED, &proc) == 0)
206 {
207 delete [] p;
208 return DynamicSymbolHandle(proc);
209 }
210
211 return 0;
212
213 #else
214
215 return DynamicSymbolHandle(dlsym(libraryHandle, (char*)symbolName));
216
217 #endif
218 }
219
220 mike 1.19 String System::getHostName()
221 {
222 static char hostname[64];
223
224 if (!*hostname)
225 gethostname(hostname, sizeof(hostname));
226
227 return hostname;
228 }
229
230 PEGASUS_NAMESPACE_END
|