Xinqi Bao's Git

More robust preprocessor switches
[slstatus.git] / util.c
1 /* See LICENSE file for copyright and license details. */
2 #include <err.h>
3 #include <errno.h>
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #include "util.h"
9
10 const char *
11 bprintf(const char *fmt, ...)
12 {
13 va_list ap;
14 size_t len;
15
16 va_start(ap, fmt);
17 len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
18 va_end(ap);
19
20 if (len >= sizeof(buf))
21 buf[sizeof(buf)-1] = '\0';
22
23 return buf;
24 }
25
26 int
27 pscanf(const char *path, const char *fmt, ...)
28 {
29 FILE *fp;
30 va_list ap;
31 int n;
32
33 if (!(fp = fopen(path, "r"))) {
34 warn("fopen %s: %s\n", path, strerror(errno));
35 return -1;
36 }
37 va_start(ap, fmt);
38 n = vfscanf(fp, fmt, ap);
39 va_end(ap);
40 fclose(fp);
41
42 return (n == EOF) ? -1 : n;
43 }