64#include "xbps_api_impl.h"
71 const char *dotdot = dst;
73 bool rooted = *dst ==
'/';
89 if (p[1] ==
'\0' || p[1] ==
'/') {
93 }
else if (p[1] ==
'.' && (p[2] ==
'\0' || p[2] ==
'/')) {
99 for (; d > dotdot && *d !=
'/'; d--)
101 }
else if (!rooted) {
115 if (d > dst+(rooted ? 1 : 0))
117 for (; *p && *p !=
'/'; p++)
133 char frombuf[PATH_MAX], tobuf[PATH_MAX];
134 const char *fromp = frombuf, *top = tobuf, *suffix = tobuf;
140 if (
xbps_strlcpy(frombuf, from,
sizeof frombuf) >=
sizeof frombuf ||
147 for (; *fromp == *top && *to; fromp++, top++)
151 for (up = -1, fromp--; fromp && *fromp; fromp = strchr(fromp+1,
'/'), up++)
155 for (
const char *x =
"../"; *x; x++) {
161 if (*suffix !=
'\0') {
162 for (suffix += 1; *suffix; suffix++) {
169 dst[len < dstlen ? len : dstlen - 1] =
'\0';
174xbps_path_vjoin(
char *dst,
size_t dstlen, va_list ap)
180 if ((val = va_arg(ap,
const char *)) == NULL)
185 if ((n =
xbps_strlcat(dst+len, val, dstlen-len)) >= dstlen-len)
188 if ((val = va_arg(ap,
const char *)) == NULL)
190 if (len > 0 && dst[len-1] !=
'/') {
197 if (len > 0 && *val ==
'/')
201 return (ssize_t)len < 0 ? -1 : (ssize_t)len;
212 va_start(ap, dstlen);
213 len = xbps_path_vjoin(dst, dstlen, ap);
221 size_t len = strlen(dst);
227 if ((len =
xbps_strlcpy(dst, suffix, dstlen)) >= dstlen)
232 if (dst[len-1] !=
'/' && len+1 < dstlen) {
239 if ((len =
xbps_strlcat(dst, suffix, dstlen)) >= dstlen)
242 return (ssize_t)len < 0 ? -1 : (ssize_t)len;
260 if ((len =
xbps_strlcpy(dst, prefix, dstlen)) >= dstlen)
265 prelen = strlen(prefix);
266 if (prefix[prelen-1] ==
'/')
275 if (len+prelen+2 > dstlen)
278 memmove(dst+prelen+1, p, len);
284 memcpy(dst, prefix, prelen);
288 return (ssize_t)len < 0 ? -1 : (ssize_t)len;
ssize_t xbps_path_append(char *dst, size_t dstlen, const char *suffix)
size_t xbps_strlcpy(char *dst, const char *src, size_t dstsize)
ssize_t xbps_path_clean(char *dst)
ssize_t xbps_path_prepend(char *dst, size_t dstlen, const char *prefix)
ssize_t xbps_path_join(char *dst, size_t dstlen,...)
size_t xbps_strlcat(char *dst, const char *src, size_t dstsize)
ssize_t xbps_path_rel(char *dst, size_t dstlen, const char *from, const char *to)