xref: /freebsd/usr.bin/ar/util.c (revision 821df508e882ca532bec415852399c6558f22f7a)
1d192f3d3SKai Wang /*-
2d192f3d3SKai Wang  * Copyright (c) 2003-2007 Tim Kientzle
3d192f3d3SKai Wang  * All rights reserved.
4d192f3d3SKai Wang  *
5d192f3d3SKai Wang  * Redistribution and use in source and binary forms, with or without
6d192f3d3SKai Wang  * modification, are permitted provided that the following conditions
7d192f3d3SKai Wang  * are met:
8d192f3d3SKai Wang  * 1. Redistributions of source code must retain the above copyright
9d192f3d3SKai Wang  *    notice, this list of conditions and the following disclaimer
10d192f3d3SKai Wang  *    in this position and unchanged.
11d192f3d3SKai Wang  * 2. Redistributions in binary form must reproduce the above copyright
12d192f3d3SKai Wang  *    notice, this list of conditions and the following disclaimer in the
13d192f3d3SKai Wang  *    documentation and/or other materials provided with the distribution.
14d192f3d3SKai Wang  *
15d192f3d3SKai Wang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16d192f3d3SKai Wang  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17d192f3d3SKai Wang  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18d192f3d3SKai Wang  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19d192f3d3SKai Wang  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20d192f3d3SKai Wang  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21d192f3d3SKai Wang  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22d192f3d3SKai Wang  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23d192f3d3SKai Wang  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24d192f3d3SKai Wang  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25d192f3d3SKai Wang  */
26d192f3d3SKai Wang 
27d192f3d3SKai Wang #include <sys/cdefs.h>
28d192f3d3SKai Wang __FBSDID("$FreeBSD$");
29d192f3d3SKai Wang 
30d192f3d3SKai Wang #include <sys/queue.h>
31d192f3d3SKai Wang #include <sys/types.h>
32821df508SXin LI #include <errno.h>
33d192f3d3SKai Wang #include <stdarg.h>
34d192f3d3SKai Wang #include <stdio.h>
35d192f3d3SKai Wang #include <stdlib.h>
36d192f3d3SKai Wang #include <string.h>
37d192f3d3SKai Wang 
38d192f3d3SKai Wang #include "ar.h"
39d192f3d3SKai Wang 
40d192f3d3SKai Wang static void	bsdar_vwarnc(struct bsdar *, int code,
41d192f3d3SKai Wang 		    const char *fmt, va_list ap);
42d192f3d3SKai Wang static void	bsdar_verrc(struct bsdar *bsdar, int code,
43d192f3d3SKai Wang 		    const char *fmt, va_list ap);
44d192f3d3SKai Wang 
45d192f3d3SKai Wang static void
46d192f3d3SKai Wang bsdar_vwarnc(struct bsdar *bsdar, int code, const char *fmt, va_list ap)
47d192f3d3SKai Wang {
48d192f3d3SKai Wang 
49d192f3d3SKai Wang 	fprintf(stderr, "%s: warning: ", bsdar->progname);
50d192f3d3SKai Wang 	vfprintf(stderr, fmt, ap);
51d192f3d3SKai Wang 	if (code != 0)
52d192f3d3SKai Wang 		fprintf(stderr, ": %s", strerror(code));
53d192f3d3SKai Wang 	fprintf(stderr, "\n");
54d192f3d3SKai Wang }
55d192f3d3SKai Wang 
56d192f3d3SKai Wang void
57d192f3d3SKai Wang bsdar_warnc(struct bsdar *bsdar, int code, const char *fmt, ...)
58d192f3d3SKai Wang {
59d192f3d3SKai Wang 	va_list ap;
60d192f3d3SKai Wang 
61d192f3d3SKai Wang 	va_start(ap, fmt);
62d192f3d3SKai Wang 	bsdar_vwarnc(bsdar, code, fmt, ap);
63d192f3d3SKai Wang 	va_end(ap);
64d192f3d3SKai Wang }
65d192f3d3SKai Wang 
66d192f3d3SKai Wang static void
67d192f3d3SKai Wang bsdar_verrc(struct bsdar *bsdar, int code, const char *fmt, va_list ap)
68d192f3d3SKai Wang {
69d192f3d3SKai Wang 
70d192f3d3SKai Wang 	fprintf(stderr, "%s: fatal: ", bsdar->progname);
71d192f3d3SKai Wang 	vfprintf(stderr, fmt, ap);
72d192f3d3SKai Wang 	if (code != 0)
73d192f3d3SKai Wang 		fprintf(stderr, ": %s", strerror(code));
74d192f3d3SKai Wang 	fprintf(stderr, "\n");
75d192f3d3SKai Wang }
76d192f3d3SKai Wang 
77d192f3d3SKai Wang void
78d192f3d3SKai Wang bsdar_errc(struct bsdar *bsdar, int eval, int code, const char *fmt, ...)
79d192f3d3SKai Wang {
80d192f3d3SKai Wang 	va_list ap;
81d192f3d3SKai Wang 
82d192f3d3SKai Wang 	va_start(ap, fmt);
83d192f3d3SKai Wang 	bsdar_verrc(bsdar, code, fmt, ap);
84d192f3d3SKai Wang 	va_end(ap);
85d192f3d3SKai Wang 	exit(eval);
86d192f3d3SKai Wang }
87