blob: 9d919755d183b1831e6ad3172f0eb30064789647 [file] [log] [blame]
Colin Cross7bb052a2015-02-03 12:59:37 -08001// +build !plan9
2
3/*
4Plan 9 from User Space src/lib9/errstr.c
5http://code.swtch.com/plan9port/src/tip/src/lib9/errstr.c
6
7Copyright 2001-2007 Russ Cox. All Rights Reserved.
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE SOFTWARE.
26*/
27
28/*
29 * We assume there's only one error buffer for the whole system.
30 * If you use ffork, you need to provide a _syserrstr. Since most
31 * people will use libthread (which provides a _syserrstr), this is
32 * okay.
33 */
34
35#include <u.h>
36#include <errno.h>
37#include <libc.h>
38
39enum
40{
41 EPLAN9 = 0x19283745
42};
43
44char *(*_syserrstr)(void);
45static char xsyserr[ERRMAX];
46static char*
47getsyserr(void)
48{
49 char *s;
50
51 s = nil;
52 if(_syserrstr)
53 s = (*_syserrstr)();
54 if(s == nil)
55 s = xsyserr;
56 return s;
57}
58
59int
60errstr(char *err, uint n)
61{
62 char tmp[ERRMAX];
63 char *syserr;
64
65 strecpy(tmp, tmp+ERRMAX, err);
66 rerrstr(err, n);
67 syserr = getsyserr();
68 strecpy(syserr, syserr+ERRMAX, tmp);
69 errno = EPLAN9;
70 return 0;
71}
72
73void
74rerrstr(char *err, uint n)
75{
76 char *syserr;
77
78 syserr = getsyserr();
79 if(errno == EINTR)
80 strcpy(syserr, "interrupted");
81 else if(errno != EPLAN9)
82 strcpy(syserr, strerror(errno));
83 strecpy(err, err+n, syserr);
84}
85
86/* replaces __errfmt in libfmt */
87
88int
89__errfmt(Fmt *f)
90{
91 if(errno == EPLAN9)
92 return fmtstrcpy(f, getsyserr());
93 return fmtstrcpy(f, strerror(errno));
94}
95
96void
97werrstr(char *fmt, ...)
98{
99 va_list arg;
100 char buf[ERRMAX];
101
102 va_start(arg, fmt);
103 vseprint(buf, buf+ERRMAX, fmt, arg);
104 va_end(arg);
105 errstr(buf, ERRMAX);
106}
107