19fde5391Sjoerg /*-
29fde5391Sjoerg  * Copyright (c) 2003-2007 Tim Kientzle
39fde5391Sjoerg  * All rights reserved.
49fde5391Sjoerg  *
59fde5391Sjoerg  * Redistribution and use in source and binary forms, with or without
69fde5391Sjoerg  * modification, are permitted provided that the following conditions
79fde5391Sjoerg  * are met:
89fde5391Sjoerg  * 1. Redistributions of source code must retain the above copyright
99fde5391Sjoerg  *    notice, this list of conditions and the following disclaimer
109fde5391Sjoerg  *    in this position and unchanged.
119fde5391Sjoerg  * 2. Redistributions in binary form must reproduce the above copyright
129fde5391Sjoerg  *    notice, this list of conditions and the following disclaimer in the
139fde5391Sjoerg  *    documentation and/or other materials provided with the distribution.
149fde5391Sjoerg  *
159fde5391Sjoerg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
169fde5391Sjoerg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
179fde5391Sjoerg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
189fde5391Sjoerg  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
199fde5391Sjoerg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
209fde5391Sjoerg  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
219fde5391Sjoerg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
229fde5391Sjoerg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
239fde5391Sjoerg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
249fde5391Sjoerg  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
259fde5391Sjoerg  */
269fde5391Sjoerg 
279fde5391Sjoerg #include "lafe_platform.h"
289fde5391Sjoerg __FBSDID("$FreeBSD$");
299fde5391Sjoerg 
309fde5391Sjoerg #ifdef HAVE_STDARG_H
319fde5391Sjoerg #include <stdarg.h>
329fde5391Sjoerg #endif
339fde5391Sjoerg #include <stdio.h>
349fde5391Sjoerg #ifdef HAVE_STDLIB_H
359fde5391Sjoerg #include <stdlib.h>
369fde5391Sjoerg #endif
379fde5391Sjoerg #ifdef HAVE_STRING_H
389fde5391Sjoerg #include <string.h>
399fde5391Sjoerg #endif
409fde5391Sjoerg 
419fde5391Sjoerg #include "err.h"
429fde5391Sjoerg 
43*6567980fSjoerg static void lafe_vwarnc(int, const char *, va_list) __LA_PRINTFLIKE(2, 0);
449fde5391Sjoerg 
45*6567980fSjoerg static const char *lafe_progname;
46*6567980fSjoerg 
47*6567980fSjoerg const char *
lafe_getprogname(void)48*6567980fSjoerg lafe_getprogname(void)
49*6567980fSjoerg {
50*6567980fSjoerg 
51*6567980fSjoerg 	return lafe_progname;
52*6567980fSjoerg }
53*6567980fSjoerg 
54*6567980fSjoerg void
lafe_setprogname(const char * name,const char * defaultname)55*6567980fSjoerg lafe_setprogname(const char *name, const char *defaultname)
56*6567980fSjoerg {
57*6567980fSjoerg 
58*6567980fSjoerg 	if (name == NULL)
59*6567980fSjoerg 		name = defaultname;
60*6567980fSjoerg #if defined(_WIN32) && !defined(__CYGWIN__)
61*6567980fSjoerg 	lafe_progname = strrchr(name, '\\');
62*6567980fSjoerg 	if (strrchr(name, '/') > lafe_progname)
63*6567980fSjoerg #endif
64*6567980fSjoerg 	lafe_progname = strrchr(name, '/');
65*6567980fSjoerg 	if (lafe_progname != NULL)
66*6567980fSjoerg 		lafe_progname++;
67*6567980fSjoerg 	else
68*6567980fSjoerg 		lafe_progname = name;
69*6567980fSjoerg }
70*6567980fSjoerg 
71*6567980fSjoerg static void
lafe_vwarnc(int code,const char * fmt,va_list ap)729fde5391Sjoerg lafe_vwarnc(int code, const char *fmt, va_list ap)
739fde5391Sjoerg {
749fde5391Sjoerg 	fprintf(stderr, "%s: ", lafe_progname);
759fde5391Sjoerg 	vfprintf(stderr, fmt, ap);
769fde5391Sjoerg 	if (code != 0)
779fde5391Sjoerg 		fprintf(stderr, ": %s", strerror(code));
789fde5391Sjoerg 	fprintf(stderr, "\n");
799fde5391Sjoerg }
809fde5391Sjoerg 
819fde5391Sjoerg void
lafe_warnc(int code,const char * fmt,...)829fde5391Sjoerg lafe_warnc(int code, const char *fmt, ...)
839fde5391Sjoerg {
849fde5391Sjoerg 	va_list ap;
859fde5391Sjoerg 
869fde5391Sjoerg 	va_start(ap, fmt);
879fde5391Sjoerg 	lafe_vwarnc(code, fmt, ap);
889fde5391Sjoerg 	va_end(ap);
899fde5391Sjoerg }
909fde5391Sjoerg 
919fde5391Sjoerg void
lafe_errc(int eval,int code,const char * fmt,...)929fde5391Sjoerg lafe_errc(int eval, int code, const char *fmt, ...)
939fde5391Sjoerg {
949fde5391Sjoerg 	va_list ap;
959fde5391Sjoerg 
969fde5391Sjoerg 	va_start(ap, fmt);
979fde5391Sjoerg 	lafe_vwarnc(code, fmt, ap);
989fde5391Sjoerg 	va_end(ap);
999fde5391Sjoerg 	exit(eval);
1009fde5391Sjoerg }
101