summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-06-19 12:08:10 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-06-20 14:09:46 -0400
commit9ceb2b27577f1be3f30edb40a45117066fc78c51 (patch)
treebc85b811b9ec25c83e3918f8cdc8c046516af953 /src
parente062158e0a855cddcd3838fef415a7531929686b (diff)
downloadbfs-9ceb2b27577f1be3f30edb40a45117066fc78c51.tar.xz
bfstd: New xmemdup() function
Diffstat (limited to 'src')
-rw-r--r--src/bfstd.c8
-rw-r--r--src/bfstd.h12
-rw-r--r--src/parse.c7
3 files changed, 22 insertions, 5 deletions
diff --git a/src/bfstd.c b/src/bfstd.c
index cfff426..856c76c 100644
--- a/src/bfstd.c
+++ b/src/bfstd.c
@@ -243,6 +243,14 @@ static char type_char(mode_t mode) {
return '?';
}
+void *xmemdup(const void *src, size_t size) {
+ void *ret = malloc(size);
+ if (ret) {
+ memcpy(ret, src, size);
+ }
+ return ret;
+}
+
void xstrmode(mode_t mode, char str[11]) {
strcpy(str, "----------");
diff --git a/src/bfstd.h b/src/bfstd.h
index 750847e..6f2e21e 100644
--- a/src/bfstd.h
+++ b/src/bfstd.h
@@ -127,6 +127,18 @@ int ynprompt(void);
// #include <string.h>
/**
+ * Allocate a copy of a region of memory.
+ *
+ * @param src
+ * The memory region to copy.
+ * @param size
+ * The size of the memory region.
+ * @return
+ * A copy of the region, allocated with malloc(), or NULL on failure.
+ */
+void *xmemdup(const void *src, size_t size);
+
+/**
* Format a mode like ls -l (e.g. -rw-r--r--).
*
* @param mode
diff --git a/src/parse.c b/src/parse.c
index 5c55076..64e08cd 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -3666,14 +3666,11 @@ struct bfs_ctx *bfs_parse_cmdline(int argc, char *argv[]) {
}
ctx->argc = argc;
- ctx->argv = malloc((argc + 1)*sizeof(*ctx->argv));
+ ctx->argv = xmemdup(argv, sizeof_array(char *, argc + 1));
if (!ctx->argv) {
- perror("malloc()");
+ perror("xmemdup()");
goto fail;
}
- for (int i = 0; i <= argc; ++i) {
- ctx->argv[i] = argv[i];
- }
enum use_color use_color = COLOR_AUTO;
if (getenv("NO_COLOR")) {