zdev: Add function for reading text files

Reduce code complexity by adding a utility function for reading text
files based on a variable path.

Note: chzdev/lszdev tools apply special path-handling via command line
option --base to allow redirecting hard-coded paths therefore libutil's
util_file functions cannot easily be used.

Reviewed-by: Vineeth Vijayan <vneethv@linux.ibm.com>
Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Peter Oberparleiter
2024-07-04 15:51:00 +02:00
committed by Jan Höppner
parent c0f02d2f68
commit cad4e9e15b
2 changed files with 19 additions and 0 deletions

View File

@@ -65,6 +65,7 @@ char *path_get_zfcp_lun_dev(struct zfcp_lun_devid *);
char *path_get_zfcp_port_dev(struct zfcp_lun_devid *);
char *path_get_scsi_hctl_dev(const char *);
char *path_get_bus_attr(const char *, const char *);
char *path_read_text_file(int chomp, err_t err, char *fmt, ...);
exit_code_t path_for_each(const char *,
exit_code_t (*callback)(const char *, const char *,

View File

@@ -18,6 +18,7 @@
#include <unistd.h>
#include "devtype.h"
#include "lib/util_libc.h"
#include "misc.h"
#include "path.h"
#include "zfcp.h"
@@ -404,3 +405,20 @@ char *path_get_bus_attr(const char *bus, const char *attr)
{
return path_get("/sys/bus/%s/%s", bus, attr);
}
/* Read text file from adjusted path. */
char *path_read_text_file(int chomp, err_t err, char *fmt, ...)
{
char *path, *path2, *text;
va_list args;
va_start(args, fmt);
util_vasprintf(&path, fmt, args);
va_end(args);
path2 = path_get(path);
text = misc_read_text_file(path2, chomp, err);
free(path2);
free(path);
return text;
}