744f664f4748c9c9ca73071dd4fa539fcd7c5231
[userinfo.git] / src / err.c
blob744f664f4748c9c9ca73071dd4fa539fcd7c5231
1 /*
2 Copyright (C) 2001-2011 Ben Kibbey <bjk@luxsci.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
26 #ifdef HAVE_LIMITS_H
27 #include <limits.h>
28 #ifndef LINE_MAX
29 #ifdef _POSIX2_LINE_MAX
30 #define LINE_MAX _POSIX2_LINE_MAX
31 #else
32 #define LINE_MAX 2048
33 #endif
34 #endif
35 #endif
37 #define err(eval, fmt, ...) print_string(eval, 1, fmt, ## __VA_ARGS__)
38 #define errx(eval, fmt, ...) print_string(eval, 0, fmt, ## __VA_ARGS__)
39 #define warn(fmt, ...) print_string(-1, 0, fmt, ## __VA_ARGS__)
40 #define warnx(fmt, ...) print_string(-1, 1, fmt, ## __VA_ARGS__)
42 void print_string(int eval, int prog, const char *fmt, ...)
44 va_list ap;
45 char line[LINE_MAX];
47 #ifndef HAVE___PROGNAME
48 char *__progname = "ui";
49 #else
50 extern char *__progname;
51 #endif
53 va_start(ap, fmt);
54 vsnprintf(line, sizeof(line), fmt, ap);
55 va_end(ap);
57 fprintf(stderr, "%s%s%s\n", (prog) ? __progname : "", (prog) ? ": " : "",
58 line);
60 if (eval > -1)
61 exit(eval);