Fixed bounding shape size to take into account border size.
[awesome.git] / common / util.c
blob9d4615ebdcceddc1e7b12b91e4e2aa41ded8cb7e
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>
28 #include <errno.h>
30 #include "common/util.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 /** \brief safe limited strcpy.
62 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into \c dst,
63 * always adding a final \c \\0 in \c dst.
65 * \param[in] dst destination buffer.
66 * \param[in] n size of the buffer. Negative sizes are allowed.
67 * \param[in] src source string.
68 * \param[in] l maximum number of chars to copy.
70 * \return minimum of \c src \e length and \c l.
72 ssize_t
73 a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
75 ssize_t len = MIN(a_strlen(src), l);
77 if (n > 0)
79 ssize_t dlen = MIN(n - 1, len);
80 memcpy(dst, src, dlen);
81 dst[dlen] = '\0';
84 return len;
87 /** \brief safe strcpy.
89 * Copies at most <tt>n-1</tt> characters from \c src into \c dst, always
90 * adding a final \c \\0 in \c dst.
92 * \param[in] dst destination buffer.
93 * \param[in] n size of the buffer. Negative sizes are allowed.
94 * \param[in] src source string.
95 * \return \c src \e length. If this value is \>= \c n then the copy was
96 * truncated.
98 ssize_t
99 a_strcpy(char *dst, ssize_t n, const char *src)
101 ssize_t len = a_strlen(src);
103 if (n > 0)
105 ssize_t dlen = MIN(n - 1, len);
106 memcpy(dst, src, dlen);
107 dst[dlen] = '\0';
110 return len;
113 /** Execute a command and replace the current process.
114 * \param cmd The command to execute.
116 void
117 a_exec(const char *cmd)
119 static const char *shell = NULL;
121 if(!shell && !(shell = getenv("SHELL")))
122 shell = "/bin/sh";
124 execl(shell, shell, "-c", cmd, NULL);
125 fatal("execv() failed: %s", strerror(errno));
128 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80