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