1 1.1 christos /*- 2 1.1 christos * Copyright (c) 2003-2007 Tim Kientzle 3 1.1 christos * All rights reserved. 4 1.1 christos * 5 1.1 christos * Redistribution and use in source and binary forms, with or without 6 1.1 christos * modification, are permitted provided that the following conditions 7 1.1 christos * are met: 8 1.1 christos * 1. Redistributions of source code must retain the above copyright 9 1.1 christos * notice, this list of conditions and the following disclaimer 10 1.1 christos * in this position and unchanged. 11 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 christos * notice, this list of conditions and the following disclaimer in the 13 1.1 christos * documentation and/or other materials provided with the distribution. 14 1.1 christos * 15 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 1.1 christos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 1.1 christos */ 26 1.1 christos 27 1.1 christos #include "lafe_platform.h" 28 1.1 christos #ifdef HAVE_STDARG_H 29 1.1 christos #include <stdarg.h> 30 1.1 christos #endif 31 1.1 christos #include <stdio.h> 32 1.1 christos #ifdef HAVE_STDLIB_H 33 1.1 christos #include <stdlib.h> 34 1.1 christos #endif 35 1.1 christos #ifdef HAVE_STRING_H 36 1.1 christos #include <string.h> 37 1.1 christos #endif 38 1.1 christos 39 1.1 christos #include "lafe_err.h" 40 1.1 christos 41 1.1 christos static void lafe_vwarnc(int, const char *, va_list) __LA_PRINTFLIKE(2, 0); 42 1.1 christos 43 1.1 christos static const char *lafe_progname; 44 1.1 christos 45 1.1 christos const char * 46 1.1 christos lafe_getprogname(void) 47 1.1 christos { 48 1.1 christos 49 1.1 christos return lafe_progname; 50 1.1 christos } 51 1.1 christos 52 1.1 christos void 53 1.1 christos lafe_setprogname(const char *name, const char *defaultname) 54 1.1 christos { 55 1.1 christos 56 1.1 christos if (name == NULL) 57 1.1 christos name = defaultname; 58 1.1 christos #if defined(_WIN32) && !defined(__CYGWIN__) 59 1.1 christos lafe_progname = strrchr(name, '\\'); 60 1.1 christos if (strrchr(name, '/') > lafe_progname) 61 1.1 christos #endif 62 1.1 christos lafe_progname = strrchr(name, '/'); 63 1.1 christos if (lafe_progname != NULL) 64 1.1 christos lafe_progname++; 65 1.1 christos else 66 1.1 christos lafe_progname = name; 67 1.1 christos } 68 1.1 christos 69 1.1 christos static void 70 1.1 christos lafe_vwarnc(int code, const char *fmt, va_list ap) 71 1.1 christos { 72 1.1 christos fprintf(stderr, "%s: ", lafe_progname); 73 1.1 christos vfprintf(stderr, fmt, ap); 74 1.1 christos if (code != 0) 75 1.1 christos fprintf(stderr, ": %s", strerror(code)); 76 1.1 christos fprintf(stderr, "\n"); 77 1.1 christos } 78 1.1 christos 79 1.1 christos void 80 1.1 christos lafe_warnc(int code, const char *fmt, ...) 81 1.1 christos { 82 1.1 christos va_list ap; 83 1.1 christos 84 1.1 christos va_start(ap, fmt); 85 1.1 christos lafe_vwarnc(code, fmt, ap); 86 1.1 christos va_end(ap); 87 1.1 christos } 88 1.1 christos 89 1.1 christos void 90 1.1 christos lafe_errc(int eval, int code, const char *fmt, ...) 91 1.1 christos { 92 1.1 christos va_list ap; 93 1.1 christos 94 1.1 christos va_start(ap, fmt); 95 1.1 christos lafe_vwarnc(code, fmt, ap); 96 1.1 christos va_end(ap); 97 1.1 christos exit(eval); 98 1.1 christos } 99