]> git.sur5r.net Git - i3/i3status/blob - src/general.c
fix: use SYSCONFDIR in error message
[i3/i3status] / src / general.c
1 // vim:ts=4:sw=4:expandtab
2 #include <config.h>
3 #include <sys/types.h>
4 #include <string.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <sys/fcntl.h>
10 #include <sys/stat.h>
11
12 #include "i3status.h"
13
14 /*
15  * Reads size bytes into the destination buffer from filename.
16  *
17  */
18 bool slurp(const char *filename, char *destination, int size) {
19     int fd;
20
21     if ((fd = open(filename, O_RDONLY)) == -1)
22         return false;
23
24     /* We need one byte for the trailing 0 byte */
25     int n = read(fd, destination, size - 1);
26     if (n != -1)
27         destination[n] = '\0';
28     (void)close(fd);
29
30     return true;
31 }
32
33 /*
34  * Skip the given character for exactly 'amount' times, returns
35  * a pointer to the first non-'character' character in 'input'.
36  *
37  */
38 char *skip_character(char *input, char character, int amount) {
39     char *walk;
40     size_t len = strlen(input);
41     int blanks = 0;
42
43     for (walk = input; ((size_t)(walk - input) < len) && (blanks < amount); walk++)
44         if (*walk == character)
45             blanks++;
46
47     return (walk == input ? walk : walk - 1);
48 }
49
50 /*
51  * Write errormessage to statusbar and exit
52  *
53  */
54 void die(const char *fmt, ...) {
55     va_list ap;
56     va_start(ap, fmt);
57     (void)vfprintf(stderr, fmt, ap);
58     va_end(ap);
59
60     exit(EXIT_FAILURE);
61 }