fix was_selected tag attribute handling
[awesome.git] / common / util.c
blob40052a29a47d7b55daf267c0148da9bd1dbd3b82
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 <stdio.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 #include <limits.h>
29 #include "util.h"
31 /** Print error and exit
32 * with EXIT_FAILURE code
34 void
35 eprint(const char *fmt, ...)
37 va_list ap;
39 va_start(ap, fmt);
40 fprintf(stderr, "awesome: ");
41 vfprintf(stderr, fmt, ap);
42 va_end(ap);
43 exit(EXIT_FAILURE);
46 /** Print error message
47 * on stderr
49 void
50 warn(const char *fmt, ...)
52 va_list ap;
53 va_start(ap, fmt);
54 fprintf(stderr, "awesome: ");
55 vfprintf(stderr, fmt, ap);
56 va_end(ap);
59 /** Compute a value from a string containing
60 * an absolute or a relative number. If relative,
61 * add it to current_value.
62 * \param arg the string with the number
63 * \param current_value value to add the number if it's relative
64 * \return new value
66 double
67 compute_new_value_from_arg(const char *arg, double current_value)
69 double delta;
71 if(arg && sscanf(arg, "%lf", &delta) == 1)
73 if(arg[0] == '+' || arg[0] == '-')
74 current_value += delta;
75 else
76 current_value = delta;
79 return current_value;
82 /** Lookup for a function pointer from its name
83 * in the given name_func_link_t list
84 * \param funcname Function name
85 * \param list Function and name link list
86 * \return function pointer
88 void *
89 name_func_lookup(const char *funcname, const name_func_link_t *list)
91 int i;
93 if(funcname && list)
94 for(i = 0; list[i].name; i++)
95 if(!a_strcmp(funcname, list[i].name))
96 return list[i].func;
98 return NULL;
101 /** \brief safe limited strcpy.
103 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into \c dst,
104 * always adding a final \c \\0 in \c dst.
106 * \param[in] dst destination buffer.
107 * \param[in] n size of the buffer. Negative sizes are allowed.
108 * \param[in] src source string.
109 * \param[in] l maximum number of chars to copy.
111 * \return minimum of \c src \e length and \c l.
113 ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
115 ssize_t len = MIN(a_strlen(src), l);
117 if (n > 0)
119 ssize_t dlen = MIN(n - 1, len);
120 memcpy(dst, src, dlen);
121 dst[dlen] = '\0';
124 return len;
127 /** \brief safe strcpy.
129 * Copies at most <tt>n-1</tt> characters from \c src into \c dst, always
130 * adding a final \c \\0 in \c dst.
132 * \param[in] dst destination buffer.
133 * \param[in] n size of the buffer. Negative sizes are allowed.
134 * \param[in] src source string.
135 * \return \c src \e length. If this value is \>= \c n then the copy was
136 * truncated.
138 ssize_t a_strcpy(char *dst, ssize_t n, const char *src)
140 ssize_t len = a_strlen(src);
142 if (n > 0)
144 ssize_t dlen = MIN(n - 1, len);
145 memcpy(dst, src, dlen);
146 dst[dlen] = '\0';
149 return len;
151 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80