diff options
author | midipix <writeonce@midipix.org> | 2020-02-24 00:14:48 -0500 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2020-05-23 05:59:02 +0000 |
commit | 8fb8f962f58680a040390d42ab412e27f5e93a3d (patch) | |
tree | a1556816713f568e151b723089693800052f2b70 /src | |
parent | efbaf8132d09a6bd26b64556291a7ff62fd413b9 (diff) | |
download | tpax-8fb8f962f58680a040390d42ab412e27f5e93a3d.tar.bz2 tpax-8fb8f962f58680a040390d42ab412e27f5e93a3d.tar.xz |
helper interfaces: tpax_path_copy(): initial implementation.
Diffstat (limited to 'src')
-rw-r--r-- | src/helper/tpax_path_copy.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/helper/tpax_path_copy.c b/src/helper/tpax_path_copy.c new file mode 100644 index 0000000..0df6c17 --- /dev/null +++ b/src/helper/tpax_path_copy.c @@ -0,0 +1,86 @@ +/******************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */ +/******************************************************/ + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <sys/stat.h> + +#include <tpax/tpax.h> +#include "tpax_driver_impl.h" + +int tpax_path_copy( + char * dstpath, + const char * srcpath, + size_t bufsize, + uint32_t flags, + size_t * nwritten) +{ + const char * src; + char * dst; + char * cap; + + if (!bufsize) { + errno = ENOBUFS; + return -1; + } + + src = srcpath; + dst = dstpath; + cap = &dst[bufsize]; + + if ((src[0] == '/') && (src[1] == '/') && (src[2] != '/')) { + *dst++ = *src++; + *dst++ = *src++; + } + + if (flags & TPAX_DRIVER_PURE_PATH_OUTPUT) + if ((src[0] == '.') && (src[1] == '/')) + for (++src; *src=='/'; src++) + (void)0; + + for (; *src; ) { + if ((src[0] == '.') && (src[1] == '.')) { + if ((src[2] == '/') || (src[2] == '\0')) { + if (flags & TPAX_DRIVER_STRICT_PATH_INPUT) { + errno = EINVAL; + return -1; + } + } + } + + if (flags & TPAX_DRIVER_PURE_PATH_OUTPUT) { + if ((src[0] == '.') && (src[1] == '/') && (src[-1] == '/')) { + for (++src; *src=='/'; src++) + (void)0; + + } else if ((src[0] == '/')) { + for (src++; *src=='/'; src++) + (void)0; + + *dst++ = '/'; + + } else { + *dst++ = *src++; + } + } else { + *dst++ = *src++; + } + + if (dst == cap) { + errno = ENOBUFS; + return -1; + } + } + + if (nwritten) + *nwritten = dst - dstpath; + + *dst = '\0'; + + return 0; +} |