1 /*
2 * Copyright (c) 1999-2018 The strace developers.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <errno.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "error_prints.h"
39
40 #ifndef HAVE_PROGRAM_INVOCATION_NAME
41 extern char *program_invocation_name;
42 #endif
43
44 static void
verror_msg(int err_no,const char * fmt,va_list p)45 verror_msg(int err_no, const char *fmt, va_list p)
46 {
47 char *msg;
48
49 fflush(NULL);
50
51 /* We want to print entire message with single fprintf to ensure
52 * message integrity if stderr is shared with other programs.
53 * Thus we use vasprintf + single fprintf.
54 */
55 msg = NULL;
56 if (vasprintf(&msg, fmt, p) >= 0) {
57 if (err_no)
58 fprintf(stderr, "%s: %s: %s\n",
59 program_invocation_name, msg, strerror(err_no));
60 else
61 fprintf(stderr, "%s: %s\n",
62 program_invocation_name, msg);
63 free(msg);
64 } else {
65 /* malloc in vasprintf failed, try it without malloc */
66 fprintf(stderr, "%s: ", program_invocation_name);
67 vfprintf(stderr, fmt, p);
68 if (err_no)
69 fprintf(stderr, ": %s\n", strerror(err_no));
70 else
71 putc('\n', stderr);
72 }
73 /* We don't switch stderr to buffered, thus fprintf(stderr)
74 * always flushes its output and this is not necessary: */
75 /* fflush(stderr); */
76 }
77
78 void
error_msg(const char * fmt,...)79 error_msg(const char *fmt, ...)
80 {
81 va_list p;
82 va_start(p, fmt);
83 verror_msg(0, fmt, p);
84 va_end(p);
85 }
86
87 void
error_msg_and_die(const char * fmt,...)88 error_msg_and_die(const char *fmt, ...)
89 {
90 va_list p;
91 va_start(p, fmt);
92 verror_msg(0, fmt, p);
93 va_end(p);
94 die();
95 }
96
97 void
error_msg_and_help(const char * fmt,...)98 error_msg_and_help(const char *fmt, ...)
99 {
100 if (fmt != NULL) {
101 va_list p;
102 va_start(p, fmt);
103 verror_msg(0, fmt, p);
104 va_end(p);
105 }
106 fprintf(stderr, "Try '%s -h' for more information.\n",
107 program_invocation_name);
108 die();
109 }
110
111 void
perror_msg(const char * fmt,...)112 perror_msg(const char *fmt, ...)
113 {
114 va_list p;
115 va_start(p, fmt);
116 verror_msg(errno, fmt, p);
117 va_end(p);
118 }
119
120 void
perror_msg_and_die(const char * fmt,...)121 perror_msg_and_die(const char *fmt, ...)
122 {
123 va_list p;
124 va_start(p, fmt);
125 verror_msg(errno, fmt, p);
126 va_end(p);
127 die();
128 }
129