Branch data Line data Source code
1 : : // Copyright 2014-2025 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #include "errno_status.h"
5 : :
6 : : #include <zix/attributes.h>
7 : : #include <zix/status.h>
8 : : #include <zix/warnings.h>
9 : :
10 : : #include <errno.h>
11 : : #include <stddef.h>
12 : :
13 : : #ifdef ENOTSUP
14 : : # define N_ERRNO_MAPPINGS 14U
15 : : #else
16 : : # define N_ERRNO_MAPPINGS 13U
17 : : #endif
18 : :
19 : : typedef struct {
20 : : int code;
21 : : ZixStatus status;
22 : : } ErrnoMapping;
23 : :
24 : : static const ErrnoMapping errno_map[N_ERRNO_MAPPINGS] = {
25 : : {0, ZIX_STATUS_SUCCESS},
26 : : {EACCES, ZIX_STATUS_BAD_PERMS},
27 : : {EAGAIN, ZIX_STATUS_UNAVAILABLE},
28 : : {EEXIST, ZIX_STATUS_EXISTS},
29 : : {EINVAL, ZIX_STATUS_BAD_ARG},
30 : : {EMLINK, ZIX_STATUS_MAX_LINKS},
31 : : {ENOENT, ZIX_STATUS_NOT_FOUND},
32 : : {ENOMEM, ZIX_STATUS_NO_MEM},
33 : : {ENOSPC, ZIX_STATUS_NO_SPACE},
34 : : {ENOSYS, ZIX_STATUS_NOT_SUPPORTED},
35 : : {EPERM, ZIX_STATUS_BAD_PERMS},
36 : : {ETIMEDOUT, ZIX_STATUS_TIMEOUT},
37 : : #ifdef ENOTSUP
38 : : {ENOTSUP, ZIX_STATUS_NOT_SUPPORTED},
39 : : #endif
40 : : {0, ZIX_STATUS_ERROR}, // Fallback mapping
41 : : };
42 : :
43 : : ZIX_REALTIME ZixStatus
44 : 2180 : zix_errno_status_if(const int r)
45 : : {
46 : : ZIX_DISABLE_EFFECT_WARNINGS // errno is sometimes a sneaky function call
47 : :
48 [ + + ]: 2180 : return r ? zix_errno_status(errno) : ZIX_STATUS_SUCCESS;
49 : :
50 : : ZIX_RESTORE_WARNINGS
51 : : }
52 : :
53 : : ZIX_REALTIME ZixStatus
54 : 37 : zix_errno_status(const int e)
55 : : {
56 : : // Find the index of the matching mapping (or leave it at the fallback entry)
57 : 37 : size_t m = 0;
58 [ + - + + ]: 211 : while (m < (N_ERRNO_MAPPINGS - 1) && errno_map[m].code != e) {
59 : 174 : ++m;
60 : : }
61 : :
62 : 37 : return errno_map[m].status;
63 : : }
|