don't include "cvs" in the version (not using cvs anymore :D)
[blackbox.git] / lib / Util.cc
blob10dfdeb13f7e559cf50ff4d7605b282d04ea4d00
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 <shaleh@debian.org>
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:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
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.
25 // need to include these before Util.hh
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
29 #include "Util.hh"
31 #include <algorithm>
33 #include <X11/Xatom.h>
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>
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);
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);
65 bool bt::mkdirhier(const std::string &path, int mode)
68 POSIX says this about mkdir(1) -p:
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:
75 mkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode] dir
77 where the -m mode option represents that option supplied to the
78 original invocation of mkdir, if any.
80 Each dir operand that names an existing directory shall be
81 ignored without error.
82 </quote>
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.
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);
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;
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;
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;
126 // restore umask
127 if (!umask_restored)
128 (void) umask(save_umask);
130 return success;
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('/')));
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);
157 #else // __EMX__
158 spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command.c_str(), NULL);
159 #endif // !__EMX__
163 std::string bt::itostring(unsigned long i) {
164 if (i == 0)
165 return std::string("0");
167 const char nums[] = "0123456789";
169 std::string tmp;
170 for (; i > 0; i /= 10)
171 tmp.insert(tmp.begin(), nums[i%10]);
172 return tmp;
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;
184 std::string bt::tolower(const std::string &string)
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;
196 std::string bt::textPropertyToString(::Display *display,
197 ::XTextProperty& text_prop) {
198 std::string ret;
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));
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);
217 return ret;