libzutil: add zfs_{base,dir}name()

Reviewed-by: John Kennedy <john.kennedy@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Ryan Moeller <ryan@iXsystems.com>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #12105
This commit is contained in:
наб 2021-06-03 17:17:09 +02:00 committed by Brian Behlendorf
parent 3aa81a6635
commit 0854d4c186
3 changed files with 707 additions and 678 deletions

View File

@ -160,6 +160,9 @@ _LIBZUTIL_H void color_start(char *color);
_LIBZUTIL_H void color_end(void); _LIBZUTIL_H void color_end(void);
_LIBZUTIL_H int printf_color(char *color, char *format, ...); _LIBZUTIL_H int printf_color(char *color, char *format, ...);
_LIBZUTIL_H const char *zfs_basename(const char *path);
_LIBZUTIL_H ssize_t zfs_dirnamelen(const char *path);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@ -31,6 +31,22 @@
#include <libzutil.h> #include <libzutil.h>
/* Substring from after the last slash, or the string itself if none */
const char *
zfs_basename(const char *path)
{
const char *bn = strrchr(path, '/');
return (bn ? bn + 1 : path);
}
/* Return index of last slash or -1 if none */
ssize_t
zfs_dirnamelen(const char *path)
{
const char *end = strrchr(path, '/');
return (end ? end - path : -1);
}
/* /*
* Given a shorthand device name check if a file by that name exists in any * Given a shorthand device name check if a file by that name exists in any
* of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If * of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If