document the new binlog stats
[beanstalkd.git] / util.c
blob29d96c521f2a634469c0b990972f6678a77ddedd
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdarg.h>
7 #include "dat.h"
9 const char *progname;
11 void
12 v()
16 static void
17 vwarnx(const char *err, const char *fmt, va_list args)
19 fprintf(stderr, "%s: ", progname);
20 if (fmt) {
21 vfprintf(stderr, fmt, args);
22 if (err) fprintf(stderr, ": %s", err);
24 fputc('\n', stderr);
27 void
28 warn(const char *fmt, ...)
30 char *err = strerror(errno); /* must be done first thing */
31 va_list args;
33 va_start(args, fmt);
34 vwarnx(err, fmt, args);
35 va_end(args);
38 void
39 warnx(const char *fmt, ...)
41 va_list args;
42 va_start(args, fmt);
43 vwarnx(NULL, fmt, args);
44 va_end(args);
48 char*
49 fmtalloc(char *fmt, ...)
51 int n;
52 char *buf;
53 va_list ap;
55 // find out how much space is needed
56 va_start(ap, fmt);
57 n = vsnprintf(0, 0, fmt, ap) + 1; // include space for trailing NUL
58 va_end(ap);
60 buf = malloc(n);
61 if (buf) {
62 va_start(ap, fmt);
63 vsnprintf(buf, n, fmt, ap);
64 va_end(ap);
66 return buf;
70 // Zalloc allocates n bytes of zeroed memory and
71 // returns a pointer to it.
72 // If insufficient memory is available, zalloc returns 0.
73 void*
74 zalloc(int n)
76 void *p;
78 p = malloc(n);
79 if (p) {
80 memset(p, 0, n);
82 return p;