OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / misc / error / err.c
1 /* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
2  *
3  * GNU Library General Public License (LGPL) version 2 or later.
4  *
5  * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
6  */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdarg.h>
13 #include <errno.h>
14 #include <err.h>
15 #ifdef __UCLIBC_HAS_THREADS__
16 #include <pthread.h>
17 #endif
18
19 #ifdef __UCLIBC_MJN3_ONLY__
20 #warning REMINDER: Deal with wide oriented stderr case.
21 #endif
22
23 libc_hidden_proto(vwarn)
24 libc_hidden_proto(vwarnx)
25 libc_hidden_proto(err)
26 libc_hidden_proto(verr)
27 libc_hidden_proto(verrx)
28
29 libc_hidden_proto(fprintf)
30 libc_hidden_proto(vfprintf)
31 libc_hidden_proto(__xpg_strerror_r)
32 libc_hidden_proto(exit)
33 libc_hidden_proto(vfprintf)
34
35 static void vwarn_work(const char *format, va_list args, int showerr)
36 {
37         /*                         0123 45678 9 a b*/
38         static const char fmt[] = "%s: \0: %s\n\0\n";
39         const char *f;
40         char buf[64];
41         __STDIO_AUTO_THREADLOCK_VAR;
42
43         /* Do this first, in case something below changes errno. */
44         f = fmt + 11;                           /* At 11. */
45         if (showerr) {
46                 f -= 4;                                 /* At 7. */
47                 __xpg_strerror_r(errno, buf, sizeof(buf));
48         }
49
50         __STDIO_AUTO_THREADLOCK(stderr);
51
52         fprintf(stderr, fmt, __uclibc_progname);
53         if (format) {
54                 vfprintf(stderr, format, args);
55                 f -= 2;                                 /* At 5 (showerr) or 9. */
56         }
57         fprintf(stderr, f, buf);
58
59         __STDIO_AUTO_THREADUNLOCK(stderr);
60 }
61
62 void vwarn(const char *format, va_list args)
63 {
64         vwarn_work(format, args, 1);
65 }
66 libc_hidden_def(vwarn)
67
68 void warn(const char *format, ...)
69 {
70         va_list args;
71
72         va_start(args, format);
73         vwarn(format, args);
74         va_end(args);
75 }
76
77 void vwarnx(const char *format, va_list args)
78 {
79         vwarn_work(format, args, 0);
80 }
81 libc_hidden_def(vwarnx)
82
83 void warnx(const char *format, ...)
84 {
85         va_list args;
86
87         va_start(args, format);
88         vwarnx(format, args);
89         va_end(args);
90 }
91
92 void verr(int status, const char *format, va_list args)
93 {
94         vwarn(format, args);
95         exit(status);
96 }
97 libc_hidden_def(verr)
98
99 void attribute_noreturn err(int status, const char *format, ...)
100 {
101         va_list args;
102
103         va_start(args, format);
104         verr(status, format, args);
105         /* This should get optimized away.  We'll leave it now for safety. */
106         /* The loop is added only to keep gcc happy. */
107         while(1)
108                 va_end(args);
109 }
110
111 void verrx(int status, const char *format, va_list args)
112 {
113         vwarnx(format, args);
114         exit(status);
115 }
116 libc_hidden_def(verrx)
117
118 void attribute_noreturn errx(int status, const char *format, ...)
119 {
120         va_list args;
121
122         va_start(args, format);
123         verrx(status, format, args);
124         /* This should get optimized away.  We'll leave it now for safety. */
125         /* The loop is added only to keep gcc happy. */
126         while(1)
127                 va_end(args);
128 }