| 1 |
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*- |
| 2 |
// Util.cc for Blackbox - an X11 Window manager |
| 3 |
// Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <[email protected]> |
| 4 |
// Copyright (c) 1997 - 2000, 2002 - 2005 |
| 5 |
// Bradley T Hughes <bhughes at trolltech.com> |
| 6 |
// |
| 7 |
// Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
// copy of this software and associated documentation files (the "Software"), |
| 9 |
// to deal in the Software without restriction, including without limitation |
| 10 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
// and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
// Software is furnished to do so, subject to the following conditions: |
| 13 |
// |
| 14 |
// The above copyright notice and this permission notice shall be included in |
| 15 |
// all copies or substantial portions of the Software. |
| 16 |
// |
| 17 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 |
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 |
// DEALINGS IN THE SOFTWARE. |
| 24 |
|
| 25 |
// need to include these before Util.hh |
| 26 |
#include <X11/Xlib.h> |
| 27 |
#include <X11/Xutil.h> |
| 28 |
|
| 29 |
#include "Util.hh" |
| 30 |
|
| 31 |
#include <algorithm> |
| 32 |
|
| 33 |
#include <X11/Xatom.h> |
| 34 |
|
| 35 |
#include <sys/types.h> |
| 36 |
#include <sys/stat.h> |
| 37 |
#include <assert.h> |
| 38 |
#include <ctype.h> |
| 39 |
#include <errno.h> |
| 40 |
#if defined(__EMX__) |
| 41 |
# include <process.h> |
| 42 |
#endif // __EMX__ |
| 43 |
#include <stdlib.h> |
| 44 |
#include <string.h> |
| 45 |
#include <unistd.h> |
| 46 |
|
| 47 |
|
| 48 |
std::string bt::basename(const std::string& path) { |
| 49 |
std::string::size_type slash = path.rfind('/'); |
| 50 |
if (slash == std::string::npos) |
| 51 |
return path; |
| 52 |
return path.substr(slash+1); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
std::string bt::dirname(const std::string& path) { |
| 57 |
std::string::size_type slash = path.rfind('/'); |
| 58 |
if (slash == std::string::npos) |
| 59 |
return path; |
| 60 |
std::string::const_iterator it = path.begin(); |
| 61 |
return std::string(it, it + slash); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
bool bt::mkdirhier(const std::string &path, int mode) |
| 66 |
{ |
| 67 |
/* |
| 68 |
POSIX says this about mkdir(1) -p: |
| 69 |
|
| 70 |
<quote> |
| 71 |
Create any missing intermediate pathname components. For each |
| 72 |
dir operand that does not name an existing directory, effects |
| 73 |
equivalent to those caused by the following command shall occur: |
| 74 |
|
| 75 |
mkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode] dir |
| 76 |
|
| 77 |
where the -m mode option represents that option supplied to the |
| 78 |
original invocation of mkdir, if any. |
| 79 |
|
| 80 |
Each dir operand that names an existing directory shall be |
| 81 |
ignored without error. |
| 82 |
</quote> |
| 83 |
|
| 84 |
We do this by changing the umask. It is restored before creating |
| 85 |
the final component of the path or, in the case of an error, |
| 86 |
before returning. |
| 87 |
*/ |
| 88 |
// save umask |
| 89 |
mode_t save_umask = umask(0); |
| 90 |
mode_t tmp_umask = save_umask & ~(S_IWUSR | S_IXUSR); |
| 91 |
bool umask_restored = false; |
| 92 |
(void) umask(tmp_umask); |
| 93 |
|
| 94 |
bool success = true; |
| 95 |
const std::string::const_iterator begin = path.begin(); |
| 96 |
const std::string::const_iterator end = path.end(); |
| 97 |
std::string::const_iterator it = begin; |
| 98 |
for (; it != end;) { |
| 99 |
if ((it + 1) == end) { |
| 100 |
break; |
| 101 |
} |
| 102 |
it = std::find(it + 1, end, '/'); |
| 103 |
if (it == end || (it + 1) == end) { |
| 104 |
(void) umask(save_umask); |
| 105 |
umask_restored = true; |
| 106 |
// make sure we use the right mode to mkdir(2) |
| 107 |
it = end; |
| 108 |
} |
| 109 |
|
| 110 |
std::string p(begin, it); |
| 111 |
if (mkdir(p.c_str(), it == end ? mode : 0777) != 0) { |
| 112 |
const int e = errno; |
| 113 |
struct stat st; |
| 114 |
if (stat(p.c_str(), &st) != 0) { |
| 115 |
errno = e; |
| 116 |
success = false; |
| 117 |
break; |
| 118 |
} else if (!S_ISDIR(st.st_mode)) { |
| 119 |
errno = ENOTDIR; |
| 120 |
success = false; |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// restore umask |
| 127 |
if (!umask_restored) |
| 128 |
(void) umask(save_umask); |
| 129 |
|
| 130 |
return success; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
std::string bt::expandTilde(const std::string& s) { |
| 135 |
if (s[0] != '~') |
| 136 |
return s; |
| 137 |
const char* const home = getenv("HOME"); |
| 138 |
if (home == NULL) |
| 139 |
return s; |
| 140 |
return std::string(home + s.substr(s.find('/'))); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
void bt::bexec(const std::string& command, const std::string& displaystring) { |
| 145 |
#ifndef __EMX__ |
| 146 |
if (!fork()) { |
| 147 |
#ifndef __QNXTO__ // apparently, setsid interferes with signals on QNX |
| 148 |
setsid(); |
| 149 |
#endif |
| 150 |
int ret = putenv(const_cast<char *>(displaystring.c_str())); |
| 151 |
assert(ret != -1); |
| 152 |
std::string cmd = "exec "; |
| 153 |
cmd += command; |
| 154 |
ret = execl("/bin/sh", "/bin/sh", "-c", cmd.c_str(), NULL); |
| 155 |
exit(ret); |
| 156 |
} |
| 157 |
#else // __EMX__ |
| 158 |
spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command.c_str(), NULL); |
| 159 |
#endif // !__EMX__ |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
std::string bt::itostring(unsigned long i) { |
| 164 |
if (i == 0) |
| 165 |
return std::string("0"); |
| 166 |
|
| 167 |
const char nums[] = "0123456789"; |
| 168 |
|
| 169 |
std::string tmp; |
| 170 |
for (; i > 0; i /= 10) |
| 171 |
tmp.insert(tmp.begin(), nums[i%10]); |
| 172 |
return tmp; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
std::string bt::itostring(long i) { |
| 177 |
std::string tmp = bt::itostring(static_cast<unsigned long>(abs(i))); |
| 178 |
if (i < 0) |
| 179 |
tmp.insert(tmp.begin(), '-'); |
| 180 |
return tmp; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
std::string bt::tolower(const std::string &string) |
| 185 |
{ |
| 186 |
std::string returnValue; |
| 187 |
returnValue.reserve(string.size()); |
| 188 |
std::string::const_iterator it = string.begin(); |
| 189 |
const std::string::const_iterator end = string.end(); |
| 190 |
for (; it != end; ++it) |
| 191 |
returnValue.push_back(::tolower(*it)); |
| 192 |
return returnValue; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
std::string bt::textPropertyToString(::Display *display, |
| 197 |
::XTextProperty& text_prop) { |
| 198 |
std::string ret; |
| 199 |
|
| 200 |
if (text_prop.value && text_prop.nitems > 0) { |
| 201 |
if (text_prop.encoding == XA_STRING) { |
| 202 |
ret = reinterpret_cast<char *>(text_prop.value); |
| 203 |
} else { |
| 204 |
text_prop.nitems = strlen(reinterpret_cast<char *>(text_prop.value)); |
| 205 |
|
| 206 |
char **list; |
| 207 |
int num; |
| 208 |
if (XmbTextPropertyToTextList(display, &text_prop, |
| 209 |
&list, &num) == Success && |
| 210 |
num > 0 && *list) { |
| 211 |
ret = *list; |
| 212 |
XFreeStringList(list); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
return ret; |
| 218 |
} |