1 // vim:ts=4:sw=4:expandtab
6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
10 #include <sys/fcntl.h>
15 static void *scalloc(size_t size) {
16 void *result = calloc(size, 1);
18 die("Error: out of memory (calloc(%zu))\n", size);
23 void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_bad, const int max_chars) {
24 const char *walk = format;
25 char *outwalk = buffer;
26 char *buf = scalloc(max_chars * sizeof(char));
29 int fd = open(path, O_RDONLY);
34 n = read(fd, buf, max_chars);
39 START_COLOR("color_good");
40 } else if (errno != 0) {
42 START_COLOR("color_bad");
45 for (; *walk != '\0'; walk++) {
48 } else if (BEGINS_WITH(walk + 1, "title")) {
49 outwalk += sprintf(outwalk, "%s", title);
50 walk += strlen("title");
51 } else if (BEGINS_WITH(walk + 1, "content")) {
52 for (char *s = buf; *s != '\0' && n > 0; s++, n--) {
57 walk += strlen("content");
58 } else if (BEGINS_WITH(walk + 1, "errno")) {
59 outwalk += sprintf(outwalk, "%d", errno);
60 walk += strlen("errno");
61 } else if (BEGINS_WITH(walk + 1, "error")) {
62 outwalk += sprintf(outwalk, "%s", strerror(errno));
63 walk += strlen("error");
72 OUTPUT_FULL_TEXT(buffer);