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>
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
27 #include <X11/Xutil.h>
33 #include <X11/Xatom.h>
35 #include <sys/types.h>
48 std::string
bt::basename(const std::string
& path
) {
49 std::string::size_type slash
= path
.rfind('/');
50 if (slash
== std::string::npos
)
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
)
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:
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.
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,
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
);
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
;
99 if ((it
+ 1) == end
) {
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)
110 std::string
p(begin
, it
);
111 if (mkdir(p
.c_str(), it
== end
? mode
: 0777) != 0) {
114 if (stat(p
.c_str(), &st
) != 0) {
118 } else if (!S_ISDIR(st
.st_mode
)) {
128 (void) umask(save_umask
);
134 std::string
bt::expandTilde(const std::string
& s
) {
137 const char* const home
= getenv("HOME");
140 return std::string(home
+ s
.substr(s
.find('/')));
144 void bt::bexec(const std::string
& command
, const std::string
& displaystring
) {
147 #ifndef __QNXTO__ // apparently, setsid interferes with signals on QNX
150 int ret
= putenv(const_cast<char *>(displaystring
.c_str()));
152 std::string cmd
= "exec ";
154 ret
= execl("/bin/sh", "/bin/sh", "-c", cmd
.c_str(), NULL
);
158 spawnlp(P_NOWAIT
, "cmd.exe", "cmd.exe", "/c", command
.c_str(), NULL
);
163 std::string
bt::itostring(unsigned long i
) {
165 return std::string("0");
167 const char nums
[] = "0123456789";
170 for (; i
> 0; i
/= 10)
171 tmp
.insert(tmp
.begin(), nums
[i
%10]);
176 std::string
bt::itostring(long i
) {
177 std::string tmp
= bt::itostring(static_cast<unsigned long>(abs(i
)));
179 tmp
.insert(tmp
.begin(), '-');
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
));
196 std::string
bt::textPropertyToString(::Display
*display
,
197 ::XTextProperty
& text_prop
) {
200 if (text_prop
.value
&& text_prop
.nitems
> 0) {
201 if (text_prop
.encoding
== XA_STRING
) {
202 ret
= reinterpret_cast<char *>(text_prop
.value
);
204 text_prop
.nitems
= strlen(reinterpret_cast<char *>(text_prop
.value
));
208 if (XmbTextPropertyToTextList(display
, &text_prop
,
209 &list
, &num
) == Success
&&
212 XFreeStringList(list
);