Path_UNIX.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // Path_UNIX.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Path_UNIX.cpp#3 $
  5. //
  6. // Library: Foundation
  7. // Package: Filesystem
  8. // Module: Path
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Path_UNIX.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/Environment_UNIX.h"
  18. #include "Poco/Ascii.h"
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #if !defined(POCO_VXWORKS)
  23. #include <pwd.h>
  24. #endif
  25. #include <climits>
  26. #ifndef PATH_MAX
  27. #define PATH_MAX 1024 // fallback
  28. #endif
  29. namespace Poco {
  30. std::string PathImpl::currentImpl()
  31. {
  32. std::string path;
  33. char cwd[PATH_MAX];
  34. if (getcwd(cwd, sizeof(cwd)))
  35. path = cwd;
  36. else
  37. throw SystemException("cannot get current directory");
  38. std::string::size_type n = path.size();
  39. if (n > 0 && path[n - 1] != '/') path.append("/");
  40. return path;
  41. }
  42. std::string PathImpl::homeImpl()
  43. {
  44. #if defined(POCO_VXWORKS)
  45. if (EnvironmentImpl::hasImpl("HOME"))
  46. return EnvironmentImpl::getImpl("HOME");
  47. else
  48. return "/";
  49. #else
  50. std::string path;
  51. struct passwd* pwd = getpwuid(getuid());
  52. if (pwd)
  53. path = pwd->pw_dir;
  54. else
  55. {
  56. pwd = getpwuid(geteuid());
  57. if (pwd)
  58. path = pwd->pw_dir;
  59. else
  60. path = EnvironmentImpl::getImpl("HOME");
  61. }
  62. std::string::size_type n = path.size();
  63. if (n > 0 && path[n - 1] != '/') path.append("/");
  64. return path;
  65. #endif
  66. }
  67. std::string PathImpl::tempImpl()
  68. {
  69. std::string path;
  70. char* tmp = getenv("TMPDIR");
  71. if (tmp)
  72. {
  73. path = tmp;
  74. std::string::size_type n = path.size();
  75. if (n > 0 && path[n - 1] != '/') path.append("/");
  76. }
  77. else
  78. {
  79. path = "/tmp/";
  80. }
  81. return path;
  82. }
  83. std::string PathImpl::nullImpl()
  84. {
  85. #if defined(POCO_VXWORKS)
  86. return "/null";
  87. #else
  88. return "/dev/null";
  89. #endif
  90. }
  91. std::string PathImpl::expandImpl(const std::string& path)
  92. {
  93. std::string result;
  94. std::string::const_iterator it = path.begin();
  95. std::string::const_iterator end = path.end();
  96. if (it != end && *it == '~')
  97. {
  98. ++it;
  99. if (it != end && *it == '/')
  100. {
  101. result += homeImpl(); ++it;
  102. }
  103. else result += '~';
  104. }
  105. while (it != end)
  106. {
  107. if (*it == '$')
  108. {
  109. std::string var;
  110. ++it;
  111. if (it != end && *it == '{')
  112. {
  113. ++it;
  114. while (it != end && *it != '}') var += *it++;
  115. if (it != end) ++it;
  116. }
  117. else
  118. {
  119. while (it != end && (Ascii::isAlphaNumeric(*it) || *it == '_')) var += *it++;
  120. }
  121. char* val = getenv(var.c_str());
  122. if (val) result += val;
  123. }
  124. else result += *it++;
  125. }
  126. return result;
  127. }
  128. void PathImpl::listRootsImpl(std::vector<std::string>& roots)
  129. {
  130. roots.clear();
  131. roots.push_back("/");
  132. }
  133. } // namespace Poco