Stop using libxcb-property
[awesome.git] / common / util.c
blob8e27dd6717fd2235db114ee4f590394a8c08f00b
1 /*
2 * util.c - useful functions
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2006 Pierre Habouzit <madcoder@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdarg.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26 #include <limits.h>
27 #include <fcntl.h>
29 #include "common/util.h"
30 #include "common/tokenize.h"
32 /** Print error and exit with EXIT_FAILURE code.
34 void
35 _fatal(int line, const char *fct, const char *fmt, ...)
37 va_list ap;
39 va_start(ap, fmt);
40 fprintf(stderr, "E: awesome: %s:%d: ", fct, line);
41 vfprintf(stderr, fmt, ap);
42 va_end(ap);
43 fprintf(stderr, "\n");
44 exit(EXIT_FAILURE);
47 /** Print error message on stderr.
49 void
50 _warn(int line, const char *fct, const char *fmt, ...)
52 va_list ap;
53 va_start(ap, fmt);
54 fprintf(stderr, "W: awesome: %s:%d: ", fct, line);
55 vfprintf(stderr, fmt, ap);
56 va_end(ap);
57 fprintf(stderr, "\n");
60 /** Get a position type from a string.
61 * \param pos The position.
62 * \param len The string length, -1 if unknown.
63 * \return A position.
65 position_t
66 position_fromstr(const char *pos, ssize_t len)
68 switch(a_tokenize(pos, len))
70 default:
71 return Top;
72 case A_TK_BOTTOM:
73 return Bottom;
74 case A_TK_RIGHT:
75 return Right;
76 case A_TK_LEFT:
77 return Left;
81 /** Convert a position type to a string.
82 * \param p The position.
83 * \return A position string.
85 const char *
86 position_tostr(position_t p)
88 switch(p)
90 case Top: return "top";
91 case Bottom: return "bottom";
92 case Right: return "right";
93 case Left: return "left";
94 default: return NULL;
98 /** Get a orientation type from a string.
99 * \param pos The orientation.
100 * \param len The string length, -1 if unknown.
101 * \return A orientation.
103 orientation_t
104 orientation_fromstr(const char *pos, ssize_t len)
106 switch(a_tokenize(pos, len))
108 default:
109 return North;
110 case A_TK_SOUTH:
111 return South;
112 case A_TK_EAST:
113 return East;
117 /** Convert a orientation type to a string.
118 * \param p The orientation.
119 * \return A orientation string.
121 const char *
122 orientation_tostr(orientation_t p)
124 switch(p)
126 case North: return "north";
127 case South: return "south";
128 case East: return "east";
129 default: return NULL;
133 /** \brief safe limited strcpy.
135 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into \c dst,
136 * always adding a final \c \\0 in \c dst.
138 * \param[in] dst destination buffer.
139 * \param[in] n size of the buffer. Negative sizes are allowed.
140 * \param[in] src source string.
141 * \param[in] l maximum number of chars to copy.
143 * \return minimum of \c src \e length and \c l.
145 ssize_t
146 a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
148 ssize_t len = MIN(a_strlen(src), l);
150 if (n > 0)
152 ssize_t dlen = MIN(n - 1, len);
153 memcpy(dst, src, dlen);
154 dst[dlen] = '\0';
157 return len;
160 /** \brief safe strcpy.
162 * Copies at most <tt>n-1</tt> characters from \c src into \c dst, always
163 * adding a final \c \\0 in \c dst.
165 * \param[in] dst destination buffer.
166 * \param[in] n size of the buffer. Negative sizes are allowed.
167 * \param[in] src source string.
168 * \return \c src \e length. If this value is \>= \c n then the copy was
169 * truncated.
171 ssize_t
172 a_strcpy(char *dst, ssize_t n, const char *src)
174 ssize_t len = a_strlen(src);
176 if (n > 0)
178 ssize_t dlen = MIN(n - 1, len);
179 memcpy(dst, src, dlen);
180 dst[dlen] = '\0';
183 return len;
186 /** Execute a command and replace the current process.
187 * \param cmd The command to execute.
189 void
190 a_exec(const char *cmd)
192 static const char *shell = NULL;
194 if(!shell && !(shell = getenv("SHELL")))
195 shell = "/bin/sh";
197 execl(shell, shell, "-c", cmd, NULL);
200 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80