path: SCBasename - function to return basename

This commit returns the basename of a file, if it exists
in the same way that `basename(1)` works.
pull/5464/head
Jeff Lucovsky 6 years ago committed by Victor Julien
parent db9776af64
commit 73567272cd

@ -28,6 +28,12 @@
#include "util-debug.h"
#include "util-path.h"
#ifdef OS_WIN32
#define DIRECTORY_SEPARATOR '\\'
#else
#define DIRECTORY_SEPARATOR '/'
#endif
/**
* \brief Check if a path is absolute
*
@ -81,11 +87,6 @@ int PathIsRelative(const char *path)
TmEcode PathJoin (char *out_buf, uint16_t buf_len, const char *const dir, const char *const fname)
{
SCEnter();
#ifdef OS_WIN32
#define DIRECTORY_SEPARATOR '\\'
#else
#define DIRECTORY_SEPARATOR '/'
#endif
uint16_t max_path_len = MAX(buf_len, PATH_MAX);
int bytes_written = snprintf(out_buf, max_path_len, "%s%c%s", dir, DIRECTORY_SEPARATOR, fname);
if (bytes_written <= 0) {
@ -225,3 +226,24 @@ char *SCRealPath(const char *path, char *resolved_path)
return realpath(path, resolved_path);
#endif
}
/*
* \brief Return the basename of the provided path.
* \param path The path on which to compute the basename
*
* \retval the basename of the path or NULL if the path lacks a non-leaf
*/
const char *SCBasename(const char *path)
{
if (!path || strlen(path) == 0)
return NULL;
char *final = strrchr(path, DIRECTORY_SEPARATOR);
if (!final)
return path;
if (*(final + 1) == '\0')
return NULL;
return final + 1;
}

@ -40,5 +40,6 @@ bool SCPathExists(const char *path);
bool SCIsRegularDirectory(const struct dirent *const dir_entry);
bool SCIsRegularFile(const struct dirent *const dir_entry);
char *SCRealPath(const char *path, char *resolved_path);
const char *SCBasename(const char *path);
#endif /* __UTIL_PATH_H__ */

Loading…
Cancel
Save