2 d2b941f3 2021-01-28 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 44ee1bac 2021-01-27 op #include <errno.h>
18 346f28ee 2021-02-02 op #include <fcntl.h>
19 44ee1bac 2021-01-27 op #include <string.h>
21 44ee1bac 2021-01-27 op #include "gmid.h"
24 44ee1bac 2021-01-27 op starts_with(const char *str, const char *prefix)
28 44ee1bac 2021-01-27 op if (prefix == NULL)
31 44ee1bac 2021-01-27 op for (i = 0; prefix[i] != '\0'; ++i)
32 44ee1bac 2021-01-27 op if (str[i] != prefix[i])
38 44ee1bac 2021-01-27 op ends_with(const char *str, const char *sufx)
42 44ee1bac 2021-01-27 op i = strlen(str);
43 44ee1bac 2021-01-27 op j = strlen(sufx);
49 44ee1bac 2021-01-27 op for (j = 0; str[i] != '\0'; i++, j++)
50 44ee1bac 2021-01-27 op if (str[i] != sufx[j])
56 44ee1bac 2021-01-27 op filesize(int fd)
60 44ee1bac 2021-01-27 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
62 44ee1bac 2021-01-27 op if (lseek(fd, 0, SEEK_SET) == -1)
68 346f28ee 2021-02-02 op mark_nonblock(int fd)
72 346f28ee 2021-02-02 op if ((flags = fcntl(fd, F_GETFL)) == -1)
73 346f28ee 2021-02-02 op fatal("fcntl(F_GETFL): %s", strerror(errno));
74 346f28ee 2021-02-02 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
75 346f28ee 2021-02-02 op fatal("fcntl(F_SETFL): %s", strerror(errno));
79 bcf5d929 2021-02-01 op absolutify_path(const char *path)
83 bcf5d929 2021-02-01 op if (*path == '/') {
84 bcf5d929 2021-02-01 op if ((r = strdup(path)) == NULL)
85 bcf5d929 2021-02-01 op err(1, "strdup");
89 bcf5d929 2021-02-01 op wd = getcwd(NULL, 0);
90 bcf5d929 2021-02-01 op if (asprintf(&r, "%s/%s", wd, path) == -1)
91 bcf5d929 2021-02-01 op err(1, "asprintf");