summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/unix-misc.c
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2023-08-07 01:07:09 +0200
committerFlorin Coras <florin.coras@gmail.com>2023-08-07 18:00:10 +0000
commit40f481037ef98442b061e310a04ee5e110120f72 (patch)
tree20ecc6d1c7bcc701417b5bbbfd51cd2d1eadfa24 /src/vppinfra/unix-misc.c
parent993735913c6764d7a66c8fae4196a61206611186 (diff)
vppinfra: add clib_file_get_resolved_basename
more generic version of clib_sysfs_link_to_name with support for format strings... Type: improvement Change-Id: I0cb263748970378c661415196eb7e08450370677 Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vppinfra/unix-misc.c')
-rw-r--r--src/vppinfra/unix-misc.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/vppinfra/unix-misc.c b/src/vppinfra/unix-misc.c
index e73d01a15d6..0c5d96c6bb7 100644
--- a/src/vppinfra/unix-misc.c
+++ b/src/vppinfra/unix-misc.c
@@ -38,12 +38,14 @@
#include <vppinfra/error.h>
#include <vppinfra/os.h>
#include <vppinfra/unix.h>
+#include <vppinfra/format.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h> /* writev */
#include <fcntl.h>
#include <stdio.h> /* for sprintf */
+#include <limits.h>
__clib_export __thread uword __os_thread_index = 0;
__clib_export __thread uword __os_numa_index = 0;
@@ -131,6 +133,35 @@ clib_file_contents (char *file, u8 ** result)
return error;
}
+__clib_export u8 *
+clib_file_get_resolved_basename (char *fmt, ...)
+{
+ va_list va;
+ char *p, buffer[PATH_MAX];
+ u8 *link, *s = 0;
+ int r;
+
+ va_start (va, fmt);
+ link = va_format (0, fmt, &va);
+ va_end (va);
+ vec_add1 (link, 0);
+
+ r = readlink ((char *) link, buffer, sizeof (buffer) - 1);
+ vec_free (link);
+
+ if (r < 1)
+ return 0;
+
+ p = buffer + r - 1;
+ while (p > buffer && p[-1] != '/')
+ p--;
+
+ while (p[0])
+ vec_add1 (s, p++[0]);
+
+ return s;
+}
+
clib_error_t *
unix_proc_file_contents (char *file, u8 ** result)
{