aboutsummaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorOmar Polo <op@omarpolo.com>2021-01-27 15:35:09 +0000
committerOmar Polo <op@omarpolo.com>2021-01-27 15:35:09 +0000
commit44ee1bac8bc4ca2f216297d00ee6677f49fe3342 (patch)
treef5dd2f4d107eb8aade8d20f4e81c746f1b95064c /utils.c
parent22c6d6334deef920cd0212ca92f61d315860177a (diff)
use starts_with in puny.c
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/utils.c b/utils.c
new file mode 100644
index 0000000..0ea0a59
--- /dev/null
+++ b/utils.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <errno.h>
+#include <string.h>
+
+#include "gmid.h"
+
+int
+starts_with(const char *str, const char *prefix)
+{
+ size_t i;
+
+ if (prefix == NULL)
+ return 0;
+
+ for (i = 0; prefix[i] != '\0'; ++i)
+ if (str[i] != prefix[i])
+ return 0;
+ return 1;
+}
+
+int
+ends_with(const char *str, const char *sufx)
+{
+ size_t i, j;
+
+ i = strlen(str);
+ j = strlen(sufx);
+
+ if (j > i)
+ return 0;
+
+ i -= j;
+ for (j = 0; str[i] != '\0'; i++, j++)
+ if (str[i] != sufx[j])
+ return 0;
+ return 1;
+}
+
+ssize_t
+filesize(int fd)
+{
+ ssize_t len;
+
+ if ((len = lseek(fd, 0, SEEK_END)) == -1)
+ return -1;
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ return -1;
+ return len;
+}