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 libraries 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"
37 #if defined(linux) || defined(CSRG_BASED) || defined(sun)
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)
58 UnixEnvironment::UnixEnvironment()
60 dtMountPoint = getEnvironmentVariable("DTMOUNTPOINT");
61 if (dtMountPoint.isNull())
63 dtMountPoint = "/net/";
65 dtMountPoint = "/nfs/";
68 CString temp = getEnvironmentVariable("MANPATH");
71 manpath = "/usr/share/man";
73 manpath = "/usr/share/man:/usr/lpp/info";
75 manpath = "/usr/man:/usr/contrib/man:/usr/local/man";
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";
88 lang = getEnvironmentVariable("LANG");
92 localHost = "localhost";
94 CString sh(getEnvironmentVariable("SHELL"));
98 shell_ = new CShell();
102 shell_ = new KShell();
109 UnixEnvironment::~UnixEnvironment()
114 int UnixEnvironment::FileExists
116 const CString & filename
121 if (stat((const char *)filename.data(),&file) == 0)
127 void UnixEnvironment::MakeDirectory
129 const CString & dirname,
133 if (mkdir (dirname.data(), permissions) == -1) {
134 CString errorStr("MakeDirectory: " + dirname);
135 perror(errorStr.data());
139 CString UnixEnvironment::getEnvironmentVariable
144 char * value = getenv(envvar);
148 return CString(value);
151 int UnixEnvironment::isDirectory
153 const CString & directory
158 if (stat((const char *)directory.data(),&file) == 0)
159 if (S_ISDIR(file.st_mode))
165 int UnixEnvironment::isFile
167 const CString & filespec
172 if (stat((const char *)filespec.data(),&file) == 0)
173 if (S_ISREG(file.st_mode))
179 int UnixEnvironment::isLink
181 const CString & filespec
186 if (lstat((const char *)filespec.data(),&file) == 0)
187 if (S_ISLNK(file.st_mode))
193 void UnixEnvironment::symbolicLink
195 const CString & linkto,
196 const CString & linkee
199 if (symlink (linkto.data(), linkee.data()) == -1) {
200 CString errorStr("symbolicLink: " + linkee + " -> " + linkto + "| ");
201 perror(errorStr.data());
205 void UnixEnvironment::setUserId
210 uid_t uid = UID_NO_CHANGE;
212 if (name && name[0]) {
213 struct passwd * pwent = getpwnam(name);
219 if (setuid(uid) == -1) {
220 CString errorStr("setUserId: ");
223 perror(errorStr.data());
227 void UnixEnvironment::changePermissions
229 const CString & filespec,
233 if (chmod (filespec.data(), mode) == -1) {
234 CString errorStr("changePermissions: " + filespec + "| ");
235 perror(errorStr.data());
239 void UnixEnvironment::changeOwnerGroup
241 const CString & filespec,
246 uid_t uid = UID_NO_CHANGE;
247 gid_t gid = GID_NO_CHANGE;
251 struct passwd * pwent = getpwnam(owner);
259 struct group * grent = getgrnam(group);
266 if (chown(filespec.data(),uid,gid) == -1) {
267 CString errorStr("changeOwnerGroup: " + filespec + "| ");
268 perror(errorStr.data());
272 void UnixEnvironment::removeDirectory
274 const CString & dirspec
278 removeFiles(dirspec, ".~*");
279 removeFiles(dirspec, "*");
281 removeFiles(dirspec, "[.]~*");
282 removeFiles(dirspec, "[.]*");
284 if (rmdir (dirspec.data()) == -1) {
285 CString errorStr("removeDirectory: " + dirspec + "| ");
286 perror(errorStr.data());
290 void UnixEnvironment::removeFiles
292 const CString & dirspec,
293 const CString & filespec
298 sprintf(buffer,"rm -f %s/%s", dirspec.data(),filespec.data());
302 DirectoryIterator dir(dirspec);
303 struct dirent * direntry;
304 while (direntry = dir()) {
305 /*# ifdef should_be_sun_but_this_dont_work*/
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)
312 removeFile(dirspec + "/" + direntry->d_name);
318 void UnixEnvironment::removeFile
320 const CString & filespec
323 if (isDirectory(filespec) && !isLink(filespec))
324 removeDirectory(filespec);
326 if (unlink (filespec.data()) == -1) {
327 CString errorStr("removeFile(unlink): " + filespec + "| ");
328 perror(errorStr.data());
333 void UnixEnvironment::removeDeadLinks
335 const CString & dirspec
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);