2 * Copyright (C) 2006 by Latchesar Ionkov <lucho@ionkov.net>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * LATCHESAR IONKOV AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
30 typedef struct Nperror Nperror
;
36 static pthread_key_t error_key
;
37 static pthread_once_t error_once
= PTHREAD_ONCE_INIT
;
40 np_destroy_error(void *a
)
56 np_werror(Ennomem
, ENOMEM
);
62 np_init_error_key(void)
64 pthread_key_create(&error_key
, np_destroy_error
);
68 np_vwerror(Nperror
*err
, char *ename
, int ecode
, va_list ap
)
70 if (err
->ename
&& err
->ename
!= Ennomem
) {
78 vasprintf(&err->ename, ename, ap);
80 err
->ename
= malloc(1024);
85 vsnprintf(err
->ename
, 1024, ename
, ap
);
89 np_werror(char *ename
, int ecode
, ...)
94 pthread_once(&error_once
, np_init_error_key
);
95 err
= pthread_getspecific(error_key
);
97 err
= malloc(sizeof(*err
));
99 fprintf(stderr
, "not enough memory\n");
105 pthread_setspecific(error_key
, err
);
109 np_vwerror(err
, ename
, ecode
, ap
);
114 np_rerror(char **ename
, int *ecode
)
118 pthread_once(&error_once
, np_init_error_key
);
119 err
= pthread_getspecific(error_key
);
134 pthread_once(&error_once
, np_init_error_key
);
135 err
= pthread_getspecific(error_key
);
137 return err
->ename
!= NULL
;
149 snprintf(buf
, sizeof buf
, "error %d\n", ecode
);
150 np_werror(buf
, ecode
);
154 np_suerror(char *s
, int ecode
)
159 snprintf(buf
, sizeof buf
, "error %d\n", ecode
);
160 snprintf(buf
, sizeof(buf
), "%s: error %d", s
, ecode
);
161 np_werror(buf
, ecode
);
170 strerror_r(ecode
, buf
, sizeof(buf
));
171 np_werror(buf
, ecode
);
175 np_suerror(char *s
, int ecode
)
180 strerror_r(ecode
, err
, sizeof(err
));
181 snprintf(buf
, sizeof(buf
), "%s: %s", s
, err
);
182 np_werror(buf
, ecode
);