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