1 // -*- mode: c++; c-set-style: "stroustrup"; tab-width: 4; -*-
2 //
3 // error.c
4 //
5 // Copyright (C) 2004 Koji Nakamaru
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software Foundation,
19 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 //
21 
22 #include <cstdarg>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring>
26 #include "error.h"
27 
28 static char *_progname = NULL;
29 
setProgname(char * progname)30 void setProgname(char *progname)
31 {
32 	if ((_progname = strrchr(progname, '/')) == NULL) {
33 		if ((_progname = strrchr(progname, '\\')) == NULL) {
34 			_progname = strrchr(progname, ':');
35 		}
36 	}
37 	_progname = (_progname != NULL) ? _progname + 1 : progname;
38 }
39 
progname()40 const char *progname()
41 {
42 	return _progname;
43 }
44 
message(char * format,...)45 void message(char *format, ...)
46 {
47 	va_list ap;
48 
49 	va_start(ap, format);
50 	if (_progname) {
51 		fprintf(stderr, "%s: ", _progname);
52 	}
53 	vfprintf(stderr, format, ap);
54 	fprintf(stderr, "\n");
55 	va_end(ap);
56 }
57 
error(char * format,...)58 void error(char *format, ...)
59 {
60 	va_list ap;
61 
62 	va_start(ap, format);
63 	if (_progname) {
64 		fprintf(stderr, "%s: ", _progname);
65 	}
66 	vfprintf(stderr, format, ap);
67 	fprintf(stderr, "\n");
68 	va_end(ap);
69 	exit(1);
70 }
71