2 * copyright (c) 2001-2008 Emil Mikulic.
4 * err.c: BSD-like err() and warn() functions
6 * Permission to use, copy, modify, and distribute this file for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 err(const int code
, const char *format
, ...)
36 fprintf(stderr
, "%5d: error: ", (int)getpid());
38 vfprintf(stderr
, format
, va
);
40 fprintf(stderr
, ": %s\n", strerror(errno
));
46 errx(const int code
, const char *format
, ...)
50 fprintf(stderr
, "%5d: error: ", (int)getpid());
52 vfprintf(stderr
, format
, va
);
54 fprintf(stderr
, "\n");
60 warn(const char *format
, ...)
64 fprintf(stderr
, "%5d: warning: ", (int)getpid());
66 vfprintf(stderr
, format
, va
);
68 fprintf(stderr
, ": %s\n", strerror(errno
));
72 warnx(const char *format
, ...)
76 fprintf(stderr
, "%5d: warning: ", (int)getpid());
78 vfprintf(stderr
, format
, va
);
80 fprintf(stderr
, "\n");
83 /* We interlock verbosef() between processes by using a pipe with a single
84 * byte in it. This pipe must be initialized before the first fork() in order
85 * to work. Then, verbosef() will block on a read() until it is able to
86 * retrieve the byte. After doing its business, it will put a byte back into
89 * This is completely silly and largely unnecessary.
91 static int inited
= 0;
92 static int lockpipe
[2];
94 static void unlock(void);
99 if (pipe(lockpipe
) == -1)
100 err(1, "pipe(lockpipe)");
110 if (!inited
) initlock();
111 if (read(lockpipe
[0], buf
, 1) != 1) {
112 fprintf(stderr
, "lock failed!\n");
123 if (write(lockpipe
[1], &c
, 1) != 1) {
124 fprintf(stderr
, "unlock failed!\n");
130 int want_verbose
= 0;
133 verbosef(const char *format
, ...)
137 if (!want_verbose
) return;
139 fprintf(stderr
, "darkstat (%05d): ", (int)getpid());
140 va_start(va
, format
);
141 vfprintf(stderr
, format
, va
);
143 fprintf(stderr
, "\n");
148 dverbosef(const char *format _unused_
, ...)
150 /* disabled / do-nothing verbosef */
153 /* vim:set ts=3 sw=3 tw=78 expandtab: */