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 // 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 // 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 //
12 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
13 // 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 // 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 // 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 mike 1.19 //
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 #include <sys/types.h>
40 #include <cstdio>
41 #include <time.h>
42
43 mike 1.19 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
61 String System::getCurrentASCIITime()
62 {
63 char str[50];
64 mike 1.19 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
82 Boolean System::canRead(const char* path)
83 {
84 return access(path, R_OK) == 0;
85 mike 1.19 }
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 return false;
103
104 return S_ISDIR(st.st_mode);
105 }
106 mike 1.19
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
124 size = st.st_size;
125 return true;
126 }
127 mike 1.19
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 }
145
146 DynamicLibraryHandle System::loadDynamicLibrary(const char* fileName)
147 {
|
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
|
170 mike 1.19 #endif
171 }
172
173 String System::dynamicLoadError() {
174 #ifdef PEGASUS_OS_HPUX
175 return String();
176 #else
177 String dlerr = dlerror();
178 return dlerr;
179 #endif
180 }
181
182
183 DynamicSymbolHandle System::loadDynamicSymbol(
184 DynamicLibraryHandle libraryHandle,
185 const char* symbolName)
186 {
187 #ifdef PEGASUS_OS_HPUX
188 char* p = (char*)symbolName;
189 void* proc = 0;
190
191 mike 1.19 if (shl_findsym((shl_t*)&libraryHandle, p, TYPE_UNDEFINED, &proc) == 0)
192 return DynamicSymbolHandle(proc);
193
194 p = strcpy(new char[strlen(symbolName) + 2], symbolName);
195 strcpy(p, "_");
196 strcat(p, symbolName);
197
198 if (shl_findsym((shl_t*)libraryHandle, p, TYPE_UNDEFINED, &proc) == 0)
199 {
200 delete [] p;
201 return DynamicSymbolHandle(proc);
202 }
203
204 return 0;
205
206 #else
207
208 return DynamicSymbolHandle(dlsym(libraryHandle, (char*)symbolName));
209
210 #endif
211 }
212 mike 1.19
213 String System::getHostName()
214 {
215 static char hostname[64];
216
217 if (!*hostname)
218 gethostname(hostname, sizeof(hostname));
219
220 return hostname;
221 }
222
223 PEGASUS_NAMESPACE_END
|