1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer
10228753Smm *    in this position and unchanged.
11228753Smm * 2. Redistributions in binary form must reproduce the above copyright
12228753Smm *    notice, this list of conditions and the following disclaimer in the
13228753Smm *    documentation and/or other materials provided with the distribution.
14228753Smm *
15228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25228753Smm */
26228753Smm
27228753Smm#include "lafe_platform.h"
28228753Smm__FBSDID("$FreeBSD$");
29228753Smm
30228753Smm#ifdef HAVE_STDARG_H
31228753Smm#include <stdarg.h>
32228753Smm#endif
33228753Smm#include <stdio.h>
34228753Smm#ifdef HAVE_STDLIB_H
35228753Smm#include <stdlib.h>
36228753Smm#endif
37228753Smm#ifdef HAVE_STRING_H
38228753Smm#include <string.h>
39228753Smm#endif
40228753Smm
41228753Smm#include "err.h"
42228753Smm
43238856Smmstatic void lafe_vwarnc(int, const char *, va_list) __LA_PRINTFLIKE(2, 0);
44238856Smm
45299529Smmstatic const char *lafe_progname;
46228753Smm
47299529Smmconst char *
48299529Smmlafe_getprogname(void)
49299529Smm{
50299529Smm
51299529Smm	return lafe_progname;
52299529Smm}
53299529Smm
54299529Smmvoid
55299529Smmlafe_setprogname(const char *name, const char *defaultname)
56299529Smm{
57299529Smm
58299529Smm	if (name == NULL)
59299529Smm		name = defaultname;
60299529Smm#if defined(_WIN32) && !defined(__CYGWIN__)
61299529Smm	lafe_progname = strrchr(name, '\\');
62299529Smm	if (strrchr(name, '/') > lafe_progname)
63299529Smm#endif
64299529Smm	lafe_progname = strrchr(name, '/');
65299529Smm	if (lafe_progname != NULL)
66299529Smm		lafe_progname++;
67299529Smm	else
68299529Smm		lafe_progname = name;
69299529Smm}
70299529Smm
71228753Smmstatic void
72228753Smmlafe_vwarnc(int code, const char *fmt, va_list ap)
73228753Smm{
74228753Smm	fprintf(stderr, "%s: ", lafe_progname);
75228753Smm	vfprintf(stderr, fmt, ap);
76228753Smm	if (code != 0)
77228753Smm		fprintf(stderr, ": %s", strerror(code));
78228753Smm	fprintf(stderr, "\n");
79228753Smm}
80228753Smm
81228753Smmvoid
82228753Smmlafe_warnc(int code, const char *fmt, ...)
83228753Smm{
84228753Smm	va_list ap;
85228753Smm
86228753Smm	va_start(ap, fmt);
87228753Smm	lafe_vwarnc(code, fmt, ap);
88228753Smm	va_end(ap);
89228753Smm}
90228753Smm
91228753Smmvoid
92228753Smmlafe_errc(int eval, int code, const char *fmt, ...)
93228753Smm{
94228753Smm	va_list ap;
95228753Smm
96228753Smm	va_start(ap, fmt);
97228753Smm	lafe_vwarnc(code, fmt, ap);
98228753Smm	va_end(ap);
99228753Smm	exit(eval);
100228753Smm}
101