diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h index bf2ca90a0..fa56373f6 100644 --- a/include/haproxy/tools.h +++ b/include/haproxy/tools.h @@ -1388,4 +1388,6 @@ static inline const char *errname(int err_num, char **out) } } +int path_base(const char *path, const char *base, char *dst, char **err); + #endif /* _HAPROXY_TOOLS_H */ diff --git a/src/tools.c b/src/tools.c index 1f107b372..ce927f90c 100644 --- a/src/tools.c +++ b/src/tools.c @@ -7215,6 +7215,32 @@ void free_all_file_names() HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &file_names.lock); } + +/* + * Fill a buffer with a path. <*dst> must be at least of size PATH_MAX. + * If a is specified and the path does not start with "/", concatenate / + * + */ +int path_base(const char *path, const char *base, char *dst, char **err) +{ + int err_code = 0; + int rv = 0; + + if (base && *base && *path != '/') + rv = snprintf(dst, PATH_MAX, "%s/%s", base, path); + else + rv = snprintf(dst, PATH_MAX, "%s", path); + + if (rv >= PATH_MAX) { + memprintf(err, "'%s/%s' : path too long", base, path); + err_code |= ERR_ALERT | ERR_FATAL; + goto out; + } + +out: + return err_code; +} + /* * Local variables: * c-indent-level: 8