2
0

UnixEnv.C 7.3 KB

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