Xinqi Bao's Git

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