]> git.lizzy.rs Git - bspwm.git/blob - src/helpers.c
Revert "Escape shell chars in the erc arguments"
[bspwm.git] / src / helpers.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
12  *
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.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdarg.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <ctype.h>
34 #include "bspwm.h"
35
36 void warn(char *fmt, ...)
37 {
38         va_list ap;
39         va_start(ap, fmt);
40         vfprintf(stderr, fmt, ap);
41         va_end(ap);
42 }
43
44 __attribute__((noreturn))
45 void err(char *fmt, ...)
46 {
47         va_list ap;
48         va_start(ap, fmt);
49         vfprintf(stderr, fmt, ap);
50         va_end(ap);
51         exit(EXIT_FAILURE);
52 }
53
54 char *read_string(const char *file_path, size_t *tlen)
55 {
56         if (file_path == NULL) {
57                 return NULL;
58         }
59
60         int fd = open(file_path, O_RDONLY);
61
62         if (fd == -1) {
63                 perror("Read file: open");
64                 return NULL;
65         }
66
67         char buf[BUFSIZ], *content = NULL;
68         size_t len = sizeof(buf);
69
70         if ((content = calloc(len, sizeof(char))) == NULL) {
71                 perror("Read file: calloc");
72                 goto end;
73         }
74
75         int nb;
76         *tlen = 0;
77
78         while (true) {
79                 nb = read(fd, buf, sizeof(buf));
80                 if (nb < 0) {
81                         perror("Restore tree: read");
82                         free(content);
83                         content = NULL;
84                         goto end;
85                 } else if (nb == 0) {
86                         break;
87                 } else {
88                         *tlen += nb;
89                         if (*tlen > len) {
90                                 len *= 2;
91                                 char *rcontent = realloc(content, len * sizeof(char));
92                                 if (rcontent == NULL) {
93                                         perror("Read file: realloc");
94                                         free(content);
95                                         content = NULL;
96                                         goto end;
97                                 } else {
98                                         content = rcontent;
99                                 }
100                         }
101                         strncpy(content + (*tlen - nb), buf, nb);
102                 }
103         }
104
105 end:
106         close(fd);
107         return content;
108 }
109
110 char *copy_string(char *str, size_t len)
111 {
112         char *cpy = calloc(1, ((len+1) * sizeof(char)));
113         if (cpy == NULL) {
114                 perror("Copy string: calloc");
115                 return NULL;
116         }
117         strncpy(cpy, str, len);
118         cpy[len] = '\0';
119         return cpy;
120 }
121
122 char *mktempfifo(const char *template)
123 {
124         int tempfd;
125         char *runtime_dir = getenv(RUNTIME_DIR_ENV);
126         if (runtime_dir == NULL) {
127                 runtime_dir = "/tmp";
128         }
129
130         char *fifo_path = malloc(strlen(runtime_dir)+1+strlen(template)+1);
131         if (fifo_path == NULL) {
132                 return NULL;
133         }
134
135         sprintf(fifo_path, "%s/%s", runtime_dir, template);
136
137         if ((tempfd = mkstemp(fifo_path)) == -1) {
138                 free(fifo_path);
139                 return NULL;
140         }
141
142         close(tempfd);
143         unlink(fifo_path);
144
145         if (mkfifo(fifo_path, 0666) == -1) {
146                 free(fifo_path);
147                 return NULL;
148         }
149
150         return fifo_path;
151 }
152
153 int asprintf(char **buf, const char *fmt, ...)
154 {
155         int size = 0;
156         va_list args;
157         va_start(args, fmt);
158         size = vasprintf(buf, fmt, args);
159         va_end(args);
160         return size;
161 }
162
163 int vasprintf(char **buf, const char *fmt, va_list args)
164 {
165         va_list tmp;
166         va_copy(tmp, args);
167         int size = vsnprintf(NULL, 0, fmt, tmp);
168         va_end(tmp);
169
170         if (size < 0) {
171                 return -1;
172         }
173
174         *buf = malloc(size + 1);
175
176         if (*buf == NULL) {
177                 return -1;
178         }
179
180         size = vsprintf(*buf, fmt, args);
181         return size;
182 }
183
184 bool is_hex_color(const char *color)
185 {
186         if (color[0] != '#' || strlen(color) != 7) {
187                 return false;
188         }
189         for (int i = 1; i < 7; i++) {
190                 if (!isxdigit(color[i])) {
191                         return false;
192                 }
193         }
194         return true;
195 }