Blame


1 7b19e0f1 2017-11-05 stsp /*
2 72bcf0f9 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 7b19e0f1 2017-11-05 stsp *
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 f334529e 2018-01-12 stsp #include <errno.h>
18 8251fdbc 2018-01-12 stsp #include <stdio.h>
19 f334529e 2018-01-12 stsp #include <stdlib.h>
20 f334529e 2018-01-12 stsp #include <string.h>
21 f334529e 2018-01-12 stsp
22 4027f31a 2017-11-04 stsp #include "got_error.h"
23 4027f31a 2017-11-04 stsp
24 2b4402a2 2017-11-05 stsp #ifndef nitems
25 2b4402a2 2017-11-05 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
26 2b4402a2 2017-11-05 stsp #endif
27 4027f31a 2017-11-04 stsp
28 4027f31a 2017-11-04 stsp const struct got_error *
29 4027f31a 2017-11-04 stsp got_error(int code)
30 4027f31a 2017-11-04 stsp {
31 4027f31a 2017-11-04 stsp int i;
32 4027f31a 2017-11-04 stsp
33 4027f31a 2017-11-04 stsp for (i = 0; i < nitems(got_errors); i++) {
34 4027f31a 2017-11-04 stsp if (code == got_errors[i].code)
35 4027f31a 2017-11-04 stsp return &got_errors[i];
36 4027f31a 2017-11-04 stsp }
37 4027f31a 2017-11-04 stsp
38 f334529e 2018-01-12 stsp abort();
39 4027f31a 2017-11-04 stsp }
40 f334529e 2018-01-12 stsp
41 f334529e 2018-01-12 stsp const struct got_error *
42 f334529e 2018-01-12 stsp got_error_from_errno()
43 f334529e 2018-01-12 stsp {
44 f334529e 2018-01-12 stsp static struct got_error err;
45 f334529e 2018-01-12 stsp
46 f334529e 2018-01-12 stsp err.code = GOT_ERR_ERRNO;
47 b4691ea5 2018-04-02 stsp err.msg = strerror(errno);
48 f334529e 2018-01-12 stsp return &err;
49 f334529e 2018-01-12 stsp }
50 8251fdbc 2018-01-12 stsp
51 8251fdbc 2018-01-12 stsp const struct got_error *
52 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
53 8251fdbc 2018-01-12 stsp {
54 8251fdbc 2018-01-12 stsp if (ferror(f))
55 8251fdbc 2018-01-12 stsp return got_error_from_errno();
56 8251fdbc 2018-01-12 stsp return got_error(code);
57 8251fdbc 2018-01-12 stsp }