1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
36 void warn(char *fmt, ...)
40 vfprintf(stderr, fmt, ap);
44 __attribute__((noreturn))
45 void err(char *fmt, ...)
49 vfprintf(stderr, fmt, ap);
54 char *read_string(const char *file_path, size_t *tlen)
56 if (file_path == NULL) {
60 int fd = open(file_path, O_RDONLY);
63 perror("Read file: open");
67 char buf[BUFSIZ], *content = NULL;
68 size_t len = sizeof(buf);
70 if ((content = malloc(len * sizeof(char))) == NULL) {
71 perror("Read file: malloc");
79 nb = read(fd, buf, sizeof(buf));
81 perror("Restore tree: read");
91 char *rcontent = realloc(content, len * sizeof(char));
92 if (rcontent == NULL) {
93 perror("Read file: realloc");
101 strncpy(content + (*tlen - nb), buf, nb);
110 char *copy_string(char *str, size_t len)
112 char *cpy = malloc((len+1) * sizeof(char));
114 perror("Copy string: malloc");
117 strncpy(cpy, str, len);
122 /* Adapted from i3wm */
123 uint32_t get_color_pixel(const char *color)
125 unsigned int red, green, blue;
126 if (sscanf(color + 1, "%02x%02x%02x", &red, &green, &blue) == 3) {
127 /* We set the first 8 bits high to have 100% opacity in case of a 32 bit
128 * color depth visual. */
129 return (0xFF << 24) | (red << 16 | green << 8 | blue);
131 return screen->black_pixel;
135 bool is_hex_color(const char *color)
137 if (color[0] != '#' || strlen(color) != 7) {
140 for (int i = 1; i < 7; i++) {
141 if (!isxdigit(color[i])) {