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