Blame


1 c553191e 2022-01-11 op /*
2 62cbcdae 2024-02-02 op * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
3 c553191e 2022-01-11 op *
4 c553191e 2022-01-11 op * Permission to use, copy, modify, and distribute this software for any
5 c553191e 2022-01-11 op * purpose with or without fee is hereby granted, provided that the above
6 c553191e 2022-01-11 op * copyright notice and this permission notice appear in all copies.
7 c553191e 2022-01-11 op *
8 c553191e 2022-01-11 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c553191e 2022-01-11 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c553191e 2022-01-11 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c553191e 2022-01-11 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c553191e 2022-01-11 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c553191e 2022-01-11 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c553191e 2022-01-11 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c553191e 2022-01-11 op */
16 c553191e 2022-01-11 op
17 c553191e 2022-01-11 op #include "compat.h"
18 c553191e 2022-01-11 op
19 c553191e 2022-01-11 op #include <errno.h>
20 c553191e 2022-01-11 op #include <fcntl.h>
21 c553191e 2022-01-11 op #include <stdlib.h>
22 c553191e 2022-01-11 op #include <string.h>
23 c553191e 2022-01-11 op
24 c553191e 2022-01-11 op #include "utils.h"
25 c553191e 2022-01-11 op
26 c553191e 2022-01-11 op int
27 ee0aac2f 2022-05-25 op mark_nonblock_cloexec(int fd)
28 c553191e 2022-01-11 op {
29 c553191e 2022-01-11 op int flags;
30 c553191e 2022-01-11 op
31 c553191e 2022-01-11 op if ((flags = fcntl(fd, F_GETFL)) == -1)
32 c553191e 2022-01-11 op return 0;
33 c553191e 2022-01-11 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
34 c553191e 2022-01-11 op return 0;
35 ee0aac2f 2022-05-25 op if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
36 ee0aac2f 2022-05-25 op return 0;
37 c553191e 2022-01-11 op return 1;
38 c553191e 2022-01-11 op }
39 c553191e 2022-01-11 op
40 c553191e 2022-01-11 op int
41 c553191e 2022-01-11 op has_suffix(const char *str, const char *sufx)
42 c553191e 2022-01-11 op {
43 c553191e 2022-01-11 op size_t l, s;
44 c553191e 2022-01-11 op
45 c553191e 2022-01-11 op l = strlen(str);
46 c553191e 2022-01-11 op s = strlen(sufx);
47 c553191e 2022-01-11 op
48 c553191e 2022-01-11 op if (l < s)
49 c553191e 2022-01-11 op return 0;
50 c553191e 2022-01-11 op
51 c553191e 2022-01-11 op return !strcmp(str + (l - s), sufx);
52 c553191e 2022-01-11 op }
53 c553191e 2022-01-11 op
54 c553191e 2022-01-11 op void *
55 c553191e 2022-01-11 op hash_alloc(size_t len, void *d)
56 c553191e 2022-01-11 op {
57 c553191e 2022-01-11 op if ((d = malloc(len)) == NULL)
58 c553191e 2022-01-11 op abort();
59 c553191e 2022-01-11 op return d;
60 c553191e 2022-01-11 op }
61 c553191e 2022-01-11 op
62 c553191e 2022-01-11 op void *
63 c553191e 2022-01-11 op hash_calloc(size_t nmemb, size_t size, void *d)
64 c553191e 2022-01-11 op {
65 c553191e 2022-01-11 op if ((d = calloc(nmemb, size)) == NULL)
66 c553191e 2022-01-11 op abort();
67 c553191e 2022-01-11 op return d;
68 c553191e 2022-01-11 op }
69 c553191e 2022-01-11 op
70 c553191e 2022-01-11 op void
71 c553191e 2022-01-11 op hash_free(void *ptr, void *d)
72 c553191e 2022-01-11 op {
73 c553191e 2022-01-11 op free(ptr);
74 c553191e 2022-01-11 op }