Blame


1 44ee1bac 2021-01-27 op /*
2 d2b941f3 2021-01-28 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 44ee1bac 2021-01-27 op *
4 44ee1bac 2021-01-27 op * Permission to use, copy, modify, and distribute this software for any
5 44ee1bac 2021-01-27 op * purpose with or without fee is hereby granted, provided that the above
6 44ee1bac 2021-01-27 op * copyright notice and this permission notice appear in all copies.
7 44ee1bac 2021-01-27 op *
8 44ee1bac 2021-01-27 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 44ee1bac 2021-01-27 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 44ee1bac 2021-01-27 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 44ee1bac 2021-01-27 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 44ee1bac 2021-01-27 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 44ee1bac 2021-01-27 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 44ee1bac 2021-01-27 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 44ee1bac 2021-01-27 op */
16 44ee1bac 2021-01-27 op
17 44ee1bac 2021-01-27 op #include <errno.h>
18 44ee1bac 2021-01-27 op #include <string.h>
19 44ee1bac 2021-01-27 op
20 44ee1bac 2021-01-27 op #include "gmid.h"
21 44ee1bac 2021-01-27 op
22 ca21e100 2021-02-04 op static sigset_t set;
23 ca21e100 2021-02-04 op
24 ca21e100 2021-02-04 op void
25 ca21e100 2021-02-04 op block_signals(void)
26 ca21e100 2021-02-04 op {
27 ca21e100 2021-02-04 op sigset_t new;
28 ca21e100 2021-02-04 op
29 ca21e100 2021-02-04 op sigemptyset(&new);
30 ca21e100 2021-02-04 op sigaddset(&new, SIGHUP);
31 ca21e100 2021-02-04 op sigprocmask(SIG_BLOCK, &new, &set);
32 ca21e100 2021-02-04 op }
33 ca21e100 2021-02-04 op
34 ca21e100 2021-02-04 op void
35 ca21e100 2021-02-04 op unblock_signals(void)
36 ca21e100 2021-02-04 op {
37 ca21e100 2021-02-04 op sigprocmask(SIG_SETMASK, &set, NULL);
38 ca21e100 2021-02-04 op }
39 ca21e100 2021-02-04 op
40 44ee1bac 2021-01-27 op int
41 44ee1bac 2021-01-27 op starts_with(const char *str, const char *prefix)
42 44ee1bac 2021-01-27 op {
43 44ee1bac 2021-01-27 op size_t i;
44 44ee1bac 2021-01-27 op
45 44ee1bac 2021-01-27 op if (prefix == NULL)
46 44ee1bac 2021-01-27 op return 0;
47 44ee1bac 2021-01-27 op
48 44ee1bac 2021-01-27 op for (i = 0; prefix[i] != '\0'; ++i)
49 44ee1bac 2021-01-27 op if (str[i] != prefix[i])
50 44ee1bac 2021-01-27 op return 0;
51 44ee1bac 2021-01-27 op return 1;
52 44ee1bac 2021-01-27 op }
53 44ee1bac 2021-01-27 op
54 44ee1bac 2021-01-27 op int
55 44ee1bac 2021-01-27 op ends_with(const char *str, const char *sufx)
56 44ee1bac 2021-01-27 op {
57 44ee1bac 2021-01-27 op size_t i, j;
58 44ee1bac 2021-01-27 op
59 44ee1bac 2021-01-27 op i = strlen(str);
60 44ee1bac 2021-01-27 op j = strlen(sufx);
61 44ee1bac 2021-01-27 op
62 44ee1bac 2021-01-27 op if (j > i)
63 44ee1bac 2021-01-27 op return 0;
64 44ee1bac 2021-01-27 op
65 44ee1bac 2021-01-27 op i -= j;
66 44ee1bac 2021-01-27 op for (j = 0; str[i] != '\0'; i++, j++)
67 44ee1bac 2021-01-27 op if (str[i] != sufx[j])
68 44ee1bac 2021-01-27 op return 0;
69 44ee1bac 2021-01-27 op return 1;
70 44ee1bac 2021-01-27 op }
71 44ee1bac 2021-01-27 op
72 44ee1bac 2021-01-27 op ssize_t
73 44ee1bac 2021-01-27 op filesize(int fd)
74 44ee1bac 2021-01-27 op {
75 44ee1bac 2021-01-27 op ssize_t len;
76 44ee1bac 2021-01-27 op
77 44ee1bac 2021-01-27 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
78 44ee1bac 2021-01-27 op return -1;
79 44ee1bac 2021-01-27 op if (lseek(fd, 0, SEEK_SET) == -1)
80 44ee1bac 2021-01-27 op return -1;
81 44ee1bac 2021-01-27 op return len;
82 44ee1bac 2021-01-27 op }
83 bcf5d929 2021-02-01 op
84 bcf5d929 2021-02-01 op char *
85 bcf5d929 2021-02-01 op absolutify_path(const char *path)
86 bcf5d929 2021-02-01 op {
87 bcf5d929 2021-02-01 op char *wd, *r;
88 bcf5d929 2021-02-01 op
89 bcf5d929 2021-02-01 op if (*path == '/') {
90 bcf5d929 2021-02-01 op if ((r = strdup(path)) == NULL)
91 bcf5d929 2021-02-01 op err(1, "strdup");
92 bcf5d929 2021-02-01 op return r;
93 bcf5d929 2021-02-01 op }
94 bcf5d929 2021-02-01 op
95 bcf5d929 2021-02-01 op wd = getcwd(NULL, 0);
96 bcf5d929 2021-02-01 op if (asprintf(&r, "%s/%s", wd, path) == -1)
97 bcf5d929 2021-02-01 op err(1, "asprintf");
98 bcf5d929 2021-02-01 op free(wd);
99 bcf5d929 2021-02-01 op return r;
100 bcf5d929 2021-02-01 op }
101 ca21e100 2021-02-04 op
102 ca21e100 2021-02-04 op char *
103 ca21e100 2021-02-04 op xstrdup(const char *s)
104 ca21e100 2021-02-04 op {
105 ca21e100 2021-02-04 op char *d;
106 ca21e100 2021-02-04 op
107 ca21e100 2021-02-04 op if ((d = strdup(s)) == NULL)
108 ca21e100 2021-02-04 op err(1, "strdup");
109 ca21e100 2021-02-04 op return d;
110 ca21e100 2021-02-04 op }