]> git.sur5r.net Git - i3/i3status/blob - src/print_path_exists.c
clang-format-3.5 -i **/*.[ch], update modeline
[i3/i3status] / src / print_path_exists.c
1 // vim:ts=4:sw=4:expandtab
2 #include <stdio.h>
3 #include <string.h>
4 #include <yajl/yajl_gen.h>
5 #include <yajl/yajl_version.h>
6 #include <sys/stat.h>
7 #include "i3status.h"
8
9 void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format) {
10     const char *walk;
11     char *outwalk = buffer;
12     struct stat st;
13     const bool exists = (stat(path, &st) == 0);
14
15     INSTANCE(path);
16
17     START_COLOR((exists ? "color_good" : "color_bad"));
18
19     for (walk = format; *walk != '\0'; walk++) {
20         if (*walk != '%') {
21             *(outwalk++) = *walk;
22             continue;
23         }
24
25         if (BEGINS_WITH(walk + 1, "title")) {
26             outwalk += sprintf(outwalk, "%s", title);
27             walk += strlen("title");
28         } else if (BEGINS_WITH(walk + 1, "status")) {
29             outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no"));
30             walk += strlen("status");
31         }
32     }
33
34     END_COLOR;
35     OUTPUT_FULL_TEXT(buffer);
36 }