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.
30 #include "common/util.h"
32 /** Print error and exit with EXIT_FAILURE code.
35 _fatal(int line
, const char *fct
, const char *fmt
, ...)
40 fprintf(stderr
, "E: awesome: %s:%d: ", fct
, line
);
41 vfprintf(stderr
, fmt
, ap
);
43 fprintf(stderr
, "\n");
47 /** Print error message on stderr.
50 _warn(int line
, const char *fct
, const char *fmt
, ...)
54 fprintf(stderr
, "W: awesome: %s:%d: ", fct
, line
);
55 vfprintf(stderr
, fmt
, 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.
73 a_strncpy(char *dst
, ssize_t n
, const char *src
, ssize_t l
)
75 ssize_t len
= MIN(a_strlen(src
), l
);
79 ssize_t dlen
= MIN(n
- 1, len
);
80 memcpy(dst
, src
, dlen
);
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
99 a_strcpy(char *dst
, ssize_t n
, const char *src
)
101 ssize_t len
= a_strlen(src
);
105 ssize_t dlen
= MIN(n
- 1, len
);
106 memcpy(dst
, src
, dlen
);
113 /** Execute a command and replace the current process.
114 * \param cmd The command to execute.
117 a_exec(const char *cmd
)
119 static const char *shell
= NULL
;
121 if(!shell
&& !(shell
= getenv("SHELL")))
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