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"
36 #define S_ISLNK(mode) ((mode & S_IFMT) == S_IFLNK)
40 #if defined(linux) || defined(CSRG_BASED) || defined(sun)
49 #if defined(sun) || defined(USL)
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)
61 UnixEnvironment::UnixEnvironment()
63 dtMountPoint = getEnvironmentVariable("DTMOUNTPOINT");
64 if (dtMountPoint.isNull())
65 #if defined(sun) || defined(USL)
66 dtMountPoint = "/net/";
68 dtMountPoint = "/nfs/";
71 CString temp = getEnvironmentVariable("MANPATH");
73 #if defined(sun) || defined(USL)
74 manpath = "/usr/share/man";
76 manpath = "/usr/share/man:/usr/lpp/info";
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";
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";
93 lang = getEnvironmentVariable("LANG");
97 localHost = "localhost";
99 CString sh(getEnvironmentVariable("SHELL"));
103 shell_ = new CShell();
107 shell_ = new KShell();
114 UnixEnvironment::~UnixEnvironment()
119 int UnixEnvironment::FileExists
121 const CString & filename
126 if (stat((const char *)filename.data(),&file) == 0)
132 void UnixEnvironment::MakeDirectory
134 const CString & dirname,
138 if (mkdir (dirname.data(), permissions) == -1) {
139 CString errorStr("MakeDirectory: " + dirname);
140 perror(errorStr.data());
144 CString UnixEnvironment::getEnvironmentVariable
149 char * value = getenv(envvar);
153 return CString(value);
156 int UnixEnvironment::isDirectory
158 const CString & directory
163 if (stat((const char *)directory.data(),&file) == 0)
164 if (S_ISDIR(file.st_mode))
170 int UnixEnvironment::isFile
172 const CString & filespec
177 if (stat((const char *)filespec.data(),&file) == 0)
178 if (S_ISREG(file.st_mode))
184 int UnixEnvironment::isLink
186 const CString & filespec
191 if (lstat((const char *)filespec.data(),&file) == 0)
192 if (S_ISLNK(file.st_mode))
198 void UnixEnvironment::symbolicLink
200 const CString & linkto,
201 const CString & linkee
204 if (symlink (linkto.data(), linkee.data()) == -1) {
205 CString errorStr("symbolicLink: " + linkee + " -> " + linkto + "| ");
206 perror(errorStr.data());
210 void UnixEnvironment::setUserId
215 uid_t uid = UID_NO_CHANGE;
217 if (name && name[0]) {
218 struct passwd * pwent = getpwnam(name);
224 if (setuid(uid) == -1) {
225 CString errorStr("setUserId: ");
228 perror(errorStr.data());
232 void UnixEnvironment::changePermissions
234 const CString & filespec,
238 if (chmod (filespec.data(), mode) == -1) {
239 CString errorStr("changePermissions: " + filespec + "| ");
240 perror(errorStr.data());
244 void UnixEnvironment::changeOwnerGroup
246 const CString & filespec,
251 uid_t uid = UID_NO_CHANGE;
252 gid_t gid = GID_NO_CHANGE;
256 struct passwd * pwent = getpwnam(owner);
264 struct group * grent = getgrnam(group);
271 if (chown(filespec.data(),uid,gid) == -1) {
272 CString errorStr("changeOwnerGroup: " + filespec + "| ");
273 perror(errorStr.data());
277 void UnixEnvironment::removeDirectory
279 const CString & dirspec
283 removeFiles(dirspec, ".~*");
284 removeFiles(dirspec, "*");
286 removeFiles(dirspec, "[.]~*");
287 removeFiles(dirspec, "[.]*");
289 if (rmdir (dirspec.data()) == -1) {
290 CString errorStr("removeDirectory: " + dirspec + "| ");
291 perror(errorStr.data());
295 void UnixEnvironment::removeFiles
297 const CString & dirspec,
298 const CString & filespec
303 sprintf(buffer,"rm -f %s/%s", dirspec.data(),filespec.data());
307 DirectoryIterator dir(dirspec);
308 struct dirent * direntry;
309 while (direntry = dir()) {
310 /*# ifdef should_be_sun_but_this_dont_work*/
313 re = compile (filespec.data(), NULL, NULL);
314 if (step (direntry->d_name,re)) {
317 regcomp (&re, filespec.data(), 0);
318 if (regexec (&re, direntry->d_name, 0, NULL, 0) == 0) {
320 if (strcmp(direntry->d_name,".") == 0 ||
321 strcmp(direntry->d_name,"..") == 0)
323 removeFile(dirspec + "/" + direntry->d_name);
329 void UnixEnvironment::removeFile
331 const CString & filespec
334 if (isDirectory(filespec) && !isLink(filespec))
335 removeDirectory(filespec);
337 if (unlink (filespec.data()) == -1) {
338 CString errorStr("removeFile(unlink): " + filespec + "| ");
339 perror(errorStr.data());
344 void UnixEnvironment::removeDeadLinks
346 const CString & dirspec
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);