Add more path utility functions
This commit is contained in:
parent
eceec52194
commit
add2c5638a
@ -112,3 +112,47 @@ path_filename(const char *path)
|
||||
|
||||
return start;
|
||||
}
|
||||
|
||||
int
|
||||
path_append(char dst[PATH_MAX], const char *src, const char *extra)
|
||||
{
|
||||
if (snprintf(dst, PATH_MAX, "%s/%s", src, extra) >= PATH_MAX) {
|
||||
err("path too long: %s/%s", src, extra);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Copy the path src into dst. */
|
||||
int
|
||||
path_copy(char dst[PATH_MAX], const char *src)
|
||||
{
|
||||
if (snprintf(dst, PATH_MAX, "%s", src) >= PATH_MAX) {
|
||||
err("path too long: %s", src);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Strip last component from path */
|
||||
void
|
||||
path_dirname(char path[PATH_MAX])
|
||||
{
|
||||
path_remove_trailing(path);
|
||||
int n = (int) strlen(path);
|
||||
int i;
|
||||
for (i = n - 1; i >= 0; i--) {
|
||||
if (path[i] == '/') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Remove all '/' */
|
||||
for (; i >= 0; i--) {
|
||||
if (path[i] != '/')
|
||||
break;
|
||||
else
|
||||
path[i] = '\0';
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
|
||||
/* Copyright (c) 2021-2024 Barcelona Supercomputing Center (BSC)
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
|
||||
#ifndef PATH_H
|
||||
#define PATH_H
|
||||
|
||||
#include <limits.h>
|
||||
#include "common.h"
|
||||
|
||||
USE_RET int path_has_prefix(const char *path, const char *prefix);
|
||||
@ -13,5 +14,8 @@ USE_RET int path_keep(char *path, int n);
|
||||
USE_RET int path_strip(const char *path, int n, const char (**next));
|
||||
void path_remove_trailing(char *path);
|
||||
USE_RET const char *path_filename(const char *path);
|
||||
USE_RET int path_append(char dst[PATH_MAX], const char *src, const char *extra);
|
||||
USE_RET int path_copy(char dst[PATH_MAX], const char *src);
|
||||
void path_dirname(char path[PATH_MAX]);
|
||||
|
||||
#endif /* PATH_H */
|
||||
|
Loading…
Reference in New Issue
Block a user