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