2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
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 ****************************<+>*************************************/
33 #include "DirIterator.h"
35 #if defined(USL) || defined(__uxp__)
36 #define S_ISLNK(mode) ((mode & S_IFMT) == S_IFLNK)
45 #if defined(sun) || defined(USL) || defined(__uxp__)
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)
57 UnixEnvironment::UnixEnvironment()
59 dtMountPoint = getEnvironmentVariable("DTMOUNTPOINT");
60 if (dtMountPoint.isNull())
61 #if defined(sun) || defined(USL) || defined(__uxp__)
62 dtMountPoint = "/net/";
64 dtMountPoint = "/nfs/";
67 CString temp = getEnvironmentVariable("MANPATH");
69 #if defined(sun) || defined(USL) || defined(__uxp__)
70 manpath = "/usr/share/man";
72 manpath = "/usr/share/man:/usr/lpp/info";
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";
78 manpath = "/usr/share/man/%L:/usr/share/man:/usr/contrib/man/%L:/usr/contrib/man:/usr/local/man/%L:/usr/local/man";
83 lang = getEnvironmentVariable("LANG");
87 localHost = "localhost";
89 CString sh(getEnvironmentVariable("SHELL"));
93 shell_ = new CShell();
97 shell_ = new KShell();
104 UnixEnvironment::~UnixEnvironment()
109 int UnixEnvironment::FileExists
111 const CString & filename
116 if (stat((const char *)filename.data(),&file) == 0)
122 void UnixEnvironment::MakeDirectory
124 const CString & dirname,
128 if (mkdir (dirname.data(), permissions) == -1) {
129 CString errorStr("MakeDirectory: " + dirname);
130 perror(errorStr.data());
134 CString UnixEnvironment::getEnvironmentVariable
139 char * value = getenv(envvar);
143 return CString(value);
146 int UnixEnvironment::isDirectory
148 const CString & directory
153 if (stat((const char *)directory.data(),&file) == 0)
154 if (S_ISDIR(file.st_mode))
160 int UnixEnvironment::isFile
162 const CString & filespec
167 if (stat((const char *)filespec.data(),&file) == 0)
168 if (S_ISREG(file.st_mode))
174 int UnixEnvironment::isLink
176 const CString & filespec
181 if (lstat((const char *)filespec.data(),&file) == 0)
182 if (S_ISLNK(file.st_mode))
188 void UnixEnvironment::symbolicLink
190 const CString & linkto,
191 const CString & linkee
194 if (symlink (linkto.data(), linkee.data()) == -1) {
195 CString errorStr("symbolicLink: " + linkee + " -> " + linkto + "| ");
196 perror(errorStr.data());
200 void UnixEnvironment::setUserId
205 uid_t uid = UID_NO_CHANGE;
207 if (name && name[0]) {
208 struct passwd * pwent = getpwnam(name);
214 if (setuid(uid) == -1) {
215 CString errorStr("setUserId: ");
218 perror(errorStr.data());
222 void UnixEnvironment::changePermissions
224 const CString & filespec,
228 if (chmod (filespec.data(), mode) == -1) {
229 CString errorStr("changePermissions: " + filespec + "| ");
230 perror(errorStr.data());
234 void UnixEnvironment::changeOwnerGroup
236 const CString & filespec,
241 uid_t uid = UID_NO_CHANGE;
242 gid_t gid = GID_NO_CHANGE;
246 struct passwd * pwent = getpwnam(owner);
254 struct group * grent = getgrnam(group);
261 if (chown(filespec.data(),uid,gid) == -1) {
262 CString errorStr("changeOwnerGroup: " + filespec + "| ");
263 perror(errorStr.data());
267 void UnixEnvironment::removeDirectory
269 const CString & dirspec
273 removeFiles(dirspec, ".~*");
274 removeFiles(dirspec, "*");
276 removeFiles(dirspec, "[.]~*");
277 removeFiles(dirspec, "[.]*");
279 if (rmdir (dirspec.data()) == -1) {
280 CString errorStr("removeDirectory: " + dirspec + "| ");
281 perror(errorStr.data());
285 void UnixEnvironment::removeFiles
287 const CString & dirspec,
288 const CString & filespec
291 #if defined(sun) || defined(__uxp__)
293 sprintf(buffer,"rm -f %s/%s", dirspec.data(),filespec.data());
297 DirectoryIterator dir(dirspec);
298 struct dirent * direntry;
299 while (direntry = dir()) {
300 /*# ifdef should_be_sun_but_this_dont_work*/
303 re = compile (filespec.data(), NULL, NULL);
304 if (step (direntry->d_name,re)) {
307 regcomp (&re, filespec.data(), 0);
308 if (regexec (&re, direntry->d_name, 0, NULL, 0) == 0) {
310 if (strcmp(direntry->d_name,".") == 0 ||
311 strcmp(direntry->d_name,"..") == 0)
313 removeFile(dirspec + "/" + direntry->d_name);
319 void UnixEnvironment::removeFile
321 const CString & filespec
324 if (isDirectory(filespec) && !isLink(filespec))
325 removeDirectory(filespec);
327 if (unlink (filespec.data()) == -1) {
328 CString errorStr("removeFile(unlink): " + filespec + "| ");
329 perror(errorStr.data());
334 void UnixEnvironment::removeDeadLinks
336 const CString & dirspec
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);