]> git.sur5r.net Git - i3/i3status/blob - src/general.c
Correctly handle the order of items
[i3/i3status] / src / general.c
1 #include <sys/types.h>
2 #include <string.h>
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <sys/fcntl.h>
8
9 #include "i3status.h"
10
11 /*
12  * Reads size bytes into the destination buffer from filename.
13  *
14  */
15 void slurp(char *filename, char *destination, int size) {
16         int fd;
17
18         if ((fd = open(filename, O_RDONLY)) == -1)
19                 die("Could not open \"%s\"\n", filename);
20
21         (void)read(fd, destination, size);
22         (void)close(fd);
23 }
24
25 /*
26  * Skip the given character for exactly 'amount' times, returns
27  * a pointer to the first non-'character' character in 'input'.
28  *
29  */
30 char *skip_character(char *input, char character, int amount) {
31         char *walk;
32         size_t len = strlen(input);
33         int blanks = 0;
34
35         for (walk = input; ((size_t)(walk - input) < len) && (blanks < amount); walk++)
36                 if (*walk == character)
37                         blanks++;
38
39         return (walk == input ? walk : walk-1);
40 }
41
42 /*
43  * Write errormessage to statusbar and exit
44  *
45  */
46 void die(const char *fmt, ...) {
47         char buffer[512];
48         va_list ap;
49         va_start(ap, fmt);
50         (void)vsnprintf(buffer, sizeof(buffer), fmt, ap);
51         va_end(ap);
52
53         if (wmii_path != NULL)
54                 write_error_to_statusbar(buffer);
55         else
56                 fprintf(stderr, "%s", buffer);
57         exit(EXIT_FAILURE);
58 }
59
60 /*
61  * This function just concats two strings in place, it should only be used
62  * for concatting order to the name of a file or concatting color codes.
63  * Otherwise, the buffer size would have to be increased.
64  *
65  */
66 char *order_to_str(int number, char *name) {
67         static char buf[32];
68         (void)snprintf(buf, sizeof(buf), "%d%s", number, name);
69         return buf;
70 }