Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtsearchpath / libCliSrv / UnixEnv.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 librararies 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 /* $TOG: UnixEnv.C /main/11 1998/12/14 17:06:02 mgreess $ */
24 /*******************************************************************
25 **  (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
26 **  All rights are reserved.  Copying or other reproduction of this
27 **  program except for archival purposes is prohibited without prior
28 **  written consent of Hewlett-Packard Company.
29 ********************************************************************
30 ****************************<+>*************************************/
31
32 #include "Environ.h"
33 #include "DirIterator.h"
34 #include <sys/stat.h>
35 #if defined(USL) || defined(__uxp__)
36 #define S_ISLNK(mode) ((mode & S_IFMT) == S_IFLNK)
37 #endif
38 #include <stdlib.h>
39 #include <string.h>
40 #include <iostream.h>
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <grp.h>
44 #include <pwd.h>
45 #if defined(sun) || defined(USL) || defined(__uxp__)
46 #include <regexpr.h>
47 #else
48 #include <regex.h>
49 #endif
50 #include <errno.h>
51
52 #if defined(sun) || defined(_AIX) || defined(__osf__) || defined(USL) || defined(__uxp__) || (linux)
53 #define UID_NO_CHANGE ((uid_t) -1)
54 #define GID_NO_CHANGE ((gid_t) -1)
55 #endif
56
57 UnixEnvironment::UnixEnvironment()
58 {
59     dtMountPoint = getEnvironmentVariable("DTMOUNTPOINT");
60     if (dtMountPoint.isNull())
61 #if defined(sun) || defined(USL) || defined(__uxp__)
62         dtMountPoint = "/net/";
63 #else
64         dtMountPoint = "/nfs/";
65 #endif
66
67     CString temp = getEnvironmentVariable("MANPATH");
68     if (temp.isNull())
69 #if defined(sun) || defined(USL) || defined(__uxp__)
70         manpath = "/usr/share/man";
71 #elif defined(_AIX)
72         manpath = "/usr/share/man:/usr/lpp/info";
73 #elif defined(hpux)
74         manpath = "/usr/man:/usr/contrib/man:/usr/local/man";
75 #elif defined(__osf__)
76         manpath = "/usr/share/%L/man:/usr/share/man:/usr/local/man";
77 #elif defined(linux)
78         manpath = "/usr/share/man/%L:/usr/share/man:/usr/contrib/man/%L:/usr/contrib/man:/usr/local/man/%L:/usr/local/man";
79 #endif
80     else
81         manpath = temp;
82
83     lang = getEnvironmentVariable("LANG");
84     if (lang.isNull())
85         lang = "C";
86
87     localHost = "localhost";
88
89     CString sh(getEnvironmentVariable("SHELL"));
90
91     cshFormat = 0;
92     if (isCsh(sh)) {
93         shell_ = new CShell();
94         cshFormat = 1;
95     }
96     else
97         shell_ = new KShell();
98         
99     uid_ = getuid();
100     gid_ = getgid();
101 }
102
103
104 UnixEnvironment::~UnixEnvironment()
105 {
106         delete shell_;
107 }
108
109 int UnixEnvironment::FileExists 
110         (
111         const CString & filename
112         ) const
113 {
114 struct stat file;
115
116     if (stat((const char *)filename.data(),&file) == 0)
117         return 1;
118
119     return 0;
120 }
121
122 void UnixEnvironment::MakeDirectory 
123         (
124         const CString & dirname,
125         mode_t          permissions
126         )
127 {
128     if (mkdir (dirname.data(), permissions) == -1) {
129         CString errorStr("MakeDirectory: " + dirname);
130         perror(errorStr.data());
131     }   
132 }
133
134 CString UnixEnvironment::getEnvironmentVariable 
135         (
136         const char * envvar
137         )
138 {
139   char * value = getenv(envvar);
140   if (value == 0)
141     return CString("");
142
143   return CString(value);
144 }
145
146 int UnixEnvironment::isDirectory 
147         (
148         const CString & directory
149         )
150 {
151 struct stat file;
152
153     if (stat((const char *)directory.data(),&file) == 0)
154         if (S_ISDIR(file.st_mode))
155             return 1;
156
157     return 0;
158 }
159
160 int UnixEnvironment::isFile
161         (
162         const CString & filespec
163         )
164 {
165 struct stat file;
166
167     if (stat((const char *)filespec.data(),&file) == 0)
168         if (S_ISREG(file.st_mode))
169             return 1;
170
171     return 0;
172 }
173
174 int UnixEnvironment::isLink
175         (
176         const CString & filespec
177         )
178 {
179 struct stat file;
180
181     if (lstat((const char *)filespec.data(),&file) == 0)
182         if (S_ISLNK(file.st_mode))
183             return 1;
184
185     return 0;
186 }
187
188 void UnixEnvironment::symbolicLink
189         (
190         const CString & linkto,
191         const CString & linkee
192         )
193 {
194     if (symlink (linkto.data(), linkee.data()) == -1) {
195         CString errorStr("symbolicLink: " + linkee + " -> " + linkto + "| ");
196         perror(errorStr.data());
197     }
198 }
199
200 void UnixEnvironment::setUserId
201         (
202         const char * name
203         )
204 {
205     uid_t uid = UID_NO_CHANGE;
206
207     if (name && name[0]) {
208         struct passwd * pwent = getpwnam(name);
209         uid = pwent->pw_uid;
210     }
211     else if (name == 0)
212         uid = uid_;
213
214     if (setuid(uid) == -1) {
215         CString errorStr("setUserId: ");
216         errorStr += name;
217         errorStr += "| ";
218         perror(errorStr.data());
219     }
220 }
221
222 void UnixEnvironment::changePermissions
223         (
224         const CString & filespec,
225         mode_t mode
226         )
227 {
228     if (chmod (filespec.data(), mode) == -1) {
229         CString errorStr("changePermissions: " + filespec + "| ");
230         perror(errorStr.data());
231     }
232 }
233
234 void UnixEnvironment::changeOwnerGroup
235         (
236         const CString & filespec,
237         const char *    owner,
238         const char *    group
239         )
240 {
241     uid_t uid = UID_NO_CHANGE;
242     gid_t gid = GID_NO_CHANGE;
243
244     if (owner) {
245         if (owner[0]) {
246             struct passwd * pwent = getpwnam(owner);
247             uid = pwent->pw_uid;
248         }
249         else
250             uid = getuid();
251     }
252     if (group) {
253         if (group[0]) {
254             struct group * grent = getgrnam(group);
255             gid = grent->gr_gid;
256         }
257         else
258             gid = getgid();
259     }
260
261     if (chown(filespec.data(),uid,gid) == -1) {
262         CString errorStr("changeOwnerGroup: " + filespec + "| ");
263         perror(errorStr.data());
264     }
265 }
266
267 void UnixEnvironment::removeDirectory 
268         (
269         const CString & dirspec
270         )
271 {
272 #ifdef sun
273     removeFiles(dirspec, ".~*");
274     removeFiles(dirspec, "*");
275 #else
276     removeFiles(dirspec, "[.]~*");
277     removeFiles(dirspec, "[.]*");
278 #endif
279     if (rmdir (dirspec.data()) == -1) {
280         CString errorStr("removeDirectory: " + dirspec + "| ");
281         perror(errorStr.data());
282     }
283 }
284
285 void UnixEnvironment::removeFiles 
286         (
287         const CString & dirspec,
288         const CString & filespec
289         )
290 {
291 #if defined(sun) || defined(__uxp__)
292 char buffer[100];
293     sprintf(buffer,"rm -f %s/%s", dirspec.data(),filespec.data());
294     system(buffer);
295 #else
296
297     DirectoryIterator dir(dirspec);
298     struct dirent * direntry;
299     while (direntry = dir()) {
300         /*# ifdef should_be_sun_but_this_dont_work*/
301 #if defined(USL)
302         char * re = NULL;
303         re = compile (filespec.data(), NULL, NULL);
304         if (step (direntry->d_name,re)) {
305 # else
306         regex_t re;
307         regcomp (&re, filespec.data(), 0);
308         if (regexec (&re, direntry->d_name, 0, NULL, 0) == 0) {
309 # endif
310             if (strcmp(direntry->d_name,".") == 0 ||
311                 strcmp(direntry->d_name,"..") == 0)
312                 continue;
313             removeFile(dirspec + "/" + direntry->d_name);
314         }
315     }
316 #endif
317 }
318
319 void UnixEnvironment::removeFile 
320         (
321         const CString & filespec
322         )
323 {
324     if (isDirectory(filespec) && !isLink(filespec))
325         removeDirectory(filespec);
326     else {
327         if (unlink (filespec.data()) == -1) {
328             CString errorStr("removeFile(unlink): " + filespec + "| ");
329             perror(errorStr.data());
330         }
331     }
332 }
333
334 void UnixEnvironment::removeDeadLinks 
335         (
336         const CString & dirspec
337         )
338 {
339     DIR * dir = opendir(dirspec.data());
340     struct dirent * direntry;
341     while (direntry = readdir(dir)) {
342         if (isLink(dirspec + "/" + direntry->d_name))
343             if (!isFile(dirspec + "/" + direntry->d_name) &&
344                 !isDirectory(dirspec + "/" + direntry->d_name))
345                 removeFile(dirspec + "/" + direntry->d_name);
346     }
347     closedir(dir);          
348 }
349