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