1 /* 4.4BSD utility functions for error messages.
2    Copyright (C) 1995-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <stdarg.h>
20 #include <err.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <stdio.h>
25 
26 #include <wchar.h>
27 #define flockfile(s) _IO_flockfile (s)
28 #define funlockfile(s) _IO_funlockfile (s)
29 
30 extern char *__progname;
31 
32 #define VA(call)							      \
33 {									      \
34   va_list ap;								      \
35   va_start (ap, format);						      \
36   call;									      \
37   va_end (ap);								      \
38 }
39 
40 void
__vwarnx_internal(const char * format,__gnuc_va_list ap,unsigned int mode_flags)41 __vwarnx_internal (const char *format, __gnuc_va_list ap,
42 		   unsigned int mode_flags)
43 {
44   flockfile (stderr);
45   __fxprintf (stderr, "%s: ", __progname);
46   if (format != NULL)
47     __vfxprintf (stderr, format, ap, mode_flags);
48   __fxprintf (stderr, "\n");
49   funlockfile (stderr);
50 }
51 
52 void
__vwarn_internal(const char * format,__gnuc_va_list ap,unsigned int mode_flags)53 __vwarn_internal (const char *format, __gnuc_va_list ap,
54 		   unsigned int mode_flags)
55 {
56   int error = errno;
57 
58   flockfile (stderr);
59   if (format != NULL)
60     {
61       __fxprintf (stderr, "%s: ", __progname);
62       __vfxprintf (stderr, format, ap, mode_flags);
63       __set_errno (error);
64       __fxprintf (stderr, ": %m\n");
65     }
66   else
67     {
68       __set_errno (error);
69       __fxprintf (stderr, "%s: %m\n", __progname);
70     }
71   funlockfile (stderr);
72 }
73 
74 void
vwarn(const char * format,__gnuc_va_list ap)75 vwarn (const char *format, __gnuc_va_list ap)
76 {
77   __vwarn_internal (format, ap, 0);
78 }
libc_hidden_def(vwarn)79 libc_hidden_def (vwarn)
80 
81 void
82 vwarnx (const char *format, __gnuc_va_list ap)
83 {
84   __vwarnx_internal (format, ap, 0);
85 }
libc_hidden_def(vwarnx)86 libc_hidden_def (vwarnx)
87 
88 void
89 warn (const char *format, ...)
90 {
91   VA (vwarn (format, ap))
92 }
libc_hidden_def(warn)93 libc_hidden_def (warn)
94 
95 void
96 warnx (const char *format, ...)
97 {
98   VA (vwarnx (format, ap))
99 }
libc_hidden_def(warnx)100 libc_hidden_def (warnx)
101 
102 void
103 verr (int status, const char *format, __gnuc_va_list ap)
104 {
105   vwarn (format, ap);
106   exit (status);
107 }
libc_hidden_def(verr)108 libc_hidden_def (verr)
109 
110 void
111 verrx (int status, const char *format, __gnuc_va_list ap)
112 {
113   vwarnx (format, ap);
114   exit (status);
115 }
libc_hidden_def(verrx)116 libc_hidden_def (verrx)
117 
118 void
119 err (int status, const char *format, ...)
120 {
121   VA (verr (status, format, ap))
122 }
123 
124 void
errx(int status,const char * format,...)125 errx (int status, const char *format, ...)
126 {
127   VA (verrx (status, format, ap))
128 }
129