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 = calloc(len, sizeof(char))) == NULL) {
71 perror("Read file: calloc");
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 = calloc(1, ((len+1) * sizeof(char)));
114 perror("Copy string: calloc");
117 strncpy(cpy, str, len);
122 char *mktempfifo(const char *template)
125 char *runtime_dir = getenv(RUNTIME_DIR_ENV);
126 if (runtime_dir == NULL) {
127 runtime_dir = "/tmp";
130 char *fifo_path = malloc(strlen(runtime_dir)+1+strlen(template)+1);
131 if (fifo_path == NULL) {
135 sprintf(fifo_path, "%s/%s", runtime_dir, template);
137 if ((tempfd = mkstemp(fifo_path)) == -1) {
145 if (mkfifo(fifo_path, 0666) == -1) {
153 int asprintf(char **buf, const char *fmt, ...)
158 size = vasprintf(buf, fmt, args);
163 int vasprintf(char **buf, const char *fmt, va_list args)
167 int size = vsnprintf(NULL, 0, fmt, tmp);
174 *buf = malloc(size + 1);
180 size = vsprintf(*buf, fmt, args);
184 bool is_hex_color(const char *color)
186 if (color[0] != '#' || strlen(color) != 7) {
189 for (int i = 1; i < 7; i++) {
190 if (!isxdigit(color[i])) {