]> git.sur5r.net Git - i3/i3status/blob - src/general.c
Fix Dragonfly BSD CPU temperature gauge
[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 #include <sys/stat.h>
10
11 #include "i3status.h"
12
13 /*
14  * Reads size bytes into the destination buffer from filename.
15  *
16  */
17 bool slurp(const char *filename, char *destination, int size) {
18         int fd;
19
20         if ((fd = open(filename, O_RDONLY)) == -1)
21                 return false;
22
23         /* We need one byte for the trailing 0 byte */
24         int n = read(fd, destination, size-1);
25         if (n != -1)
26                 destination[n] = '\0';
27         (void)close(fd);
28
29         return true;
30 }
31
32 /*
33  * Skip the given character for exactly 'amount' times, returns
34  * a pointer to the first non-'character' character in 'input'.
35  *
36  */
37 char *skip_character(char *input, char character, int amount) {
38         char *walk;
39         size_t len = strlen(input);
40         int blanks = 0;
41
42         for (walk = input; ((size_t)(walk - input) < len) && (blanks < amount); walk++)
43                 if (*walk == character)
44                         blanks++;
45
46         return (walk == input ? walk : walk-1);
47 }
48
49 /*
50  * Write errormessage to statusbar and exit
51  *
52  */
53 void die(const char *fmt, ...) {
54         char buffer[512];
55         va_list ap;
56         va_start(ap, fmt);
57         (void)vsnprintf(buffer, sizeof(buffer), fmt, ap);
58         va_end(ap);
59
60         fprintf(stderr, "%s", buffer);
61         exit(EXIT_FAILURE);
62 }