add show_all option to tasklist
[awesome.git] / util.c
blobf43181f84aae198683ba27103696f211a6bb1673
1 /*
2 * util.c - useful functions
4 * Copyright © 2007 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 void
32 eprint(const char *fmt, ...)
34 va_list ap;
36 va_start(ap, fmt);
37 fprintf(stderr, "awesome: ");
38 vfprintf(stderr, fmt, ap);
39 va_end(ap);
40 exit(EXIT_FAILURE);
43 void
44 warn(const char *fmt, ...)
46 va_list ap;
47 va_start(ap, fmt);
48 fprintf(stderr, "awesome: ");
49 vfprintf(stderr, fmt, ap);
50 va_end(ap);
53 double
54 compute_new_value_from_arg(const char *arg, double current_value)
56 double delta;
58 if(arg && sscanf(arg, "%lf", &delta) == 1)
60 if(arg[0] == '+' || arg[0] == '-')
61 current_value += delta;
62 else
63 current_value = delta;
66 return current_value;
70 /** Lookup for a function pointer from its name
71 * in the given NameFuncLink list
72 * \param funcname Function name
73 * \param list Function and name link list
74 * \return function pointer
76 void *
77 name_func_lookup(const char *funcname, const NameFuncLink * list)
79 int i;
81 if(funcname && list)
82 for(i = 0; list[i].name; i++)
83 if(!a_strcmp(funcname, list[i].name))
84 return list[i].func;
86 return NULL;
89 /** \brief safe limited strcpy.
91 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into \c dst,
92 * always adding a final \c \\0 in \c dst.
94 * \param[in] dst destination buffer.
95 * \param[in] n size of the buffer. Negative sizes are allowed.
96 * \param[in] src source string.
97 * \param[in] l maximum number of chars to copy.
99 * \return minimum of \c src \e length and \c l.
101 ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
103 ssize_t len = MIN(a_strlen(src), l);
105 if (n > 0)
107 ssize_t dlen = MIN(n - 1, len);
108 memcpy(dst, src, dlen);
109 dst[dlen] = '\0';
112 return len;
115 /** \brief safe strcpy.
117 * Copies at most <tt>n-1</tt> characters from \c src into \c dst, always
118 * adding a final \c \\0 in \c dst.
120 * \param[in] dst destination buffer.
121 * \param[in] n size of the buffer. Negative sizes are allowed.
122 * \param[in] src source string.
123 * \return \c src \e length. If this value is \>= \c n then the copy was
124 * truncated.
126 ssize_t a_strcpy(char *dst, ssize_t n, const char *src)
128 ssize_t len = a_strlen(src);
130 if (n > 0)
132 ssize_t dlen = MIN(n - 1, len);
133 memcpy(dst, src, dlen);
134 dst[dlen] = '\0';
137 return len;
139 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80