Xinqi Bao's Git

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