mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
libutil: add util_file_read_i()/util_file_read_ui()
These functions parse a sysfs file and return either an unsigned integer or signed integer. Signed-off-by: Sven Schnelle <svens@linux.ibm.com> Reviewed-by: Niklas Schnelle <schnelle@linux.ibm.com> Acked-by: Jan Höppner <hoeppner@linux.ibm.com> Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
committed by
Jan Höppner
parent
644432ba23
commit
37348ef662
@@ -12,8 +12,10 @@
|
|||||||
#define LIB_UTIL_FILE_H
|
#define LIB_UTIL_FILE_H
|
||||||
|
|
||||||
int util_file_read_line(char *str, size_t size, const char *fmt, ...);
|
int util_file_read_line(char *str, size_t size, const char *fmt, ...);
|
||||||
|
int util_file_read_i(int *val, int base, const char *fmt, ...);
|
||||||
int util_file_read_l(long *val, int base, const char *fmt, ...);
|
int util_file_read_l(long *val, int base, const char *fmt, ...);
|
||||||
int util_file_read_ll(long long *val, int base, const char *fmt, ...);
|
int util_file_read_ll(long long *val, int base, const char *fmt, ...);
|
||||||
|
int util_file_read_ui(unsigned int *val, int base, const char *fmt, ...);
|
||||||
int util_file_read_ul(unsigned long *val, int base, const char *fmt, ...);
|
int util_file_read_ul(unsigned long *val, int base, const char *fmt, ...);
|
||||||
int util_file_read_ull(unsigned long long *val, int base, const char *fmt, ...);
|
int util_file_read_ull(unsigned long long *val, int base, const char *fmt, ...);
|
||||||
|
|
||||||
|
|||||||
@@ -281,6 +281,44 @@ int util_file_write_ull(unsigned long long val, int base, const char *fmt, ...)
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a file and convert it to signed int according to given base
|
||||||
|
*
|
||||||
|
* @param[out] val Buffer for value
|
||||||
|
* @param[in] base Base for conversion, either 8, 10, or 16
|
||||||
|
* @param[in] fmt Format string for generation of the path name
|
||||||
|
* @param[in] ... Parameters for format string
|
||||||
|
*
|
||||||
|
* @retval 0 Integer has been read correctly
|
||||||
|
* @retval -1 Error while reading file
|
||||||
|
*/
|
||||||
|
int util_file_read_i(int *val, int base, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
char path[PATH_MAX], buf[512];
|
||||||
|
va_list ap;
|
||||||
|
int count;
|
||||||
|
|
||||||
|
/* Construct the file name */
|
||||||
|
UTIL_VSPRINTF(path, fmt, ap);
|
||||||
|
|
||||||
|
if (file_gets(buf, sizeof(buf), path))
|
||||||
|
return -1;
|
||||||
|
switch (base) {
|
||||||
|
case 8:
|
||||||
|
count = sscanf(buf, "%do", val);
|
||||||
|
break;
|
||||||
|
case 10:
|
||||||
|
count = sscanf(buf, "%dd", val);
|
||||||
|
break;
|
||||||
|
case 16:
|
||||||
|
count = sscanf(buf, "%dx", val);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
util_panic("Invalid base: %d\n", base);
|
||||||
|
}
|
||||||
|
return (count == 1) ? 0 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a file and convert it to signed long according to given base
|
* Read a file and convert it to signed long according to given base
|
||||||
*
|
*
|
||||||
@@ -357,6 +395,44 @@ int util_file_read_ll(long long *val, int base, const char *fmt, ...)
|
|||||||
return (count == 1) ? 0 : -1;
|
return (count == 1) ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a file and convert it to unsigned int according to given base
|
||||||
|
*
|
||||||
|
* @param[out] val Buffer for value
|
||||||
|
* @param[in] base Base for conversion, either 8, 10, or 16
|
||||||
|
* @param[in] fmt Format string for generation of the path name
|
||||||
|
* @param[in] ... Parameters for format string
|
||||||
|
*
|
||||||
|
* @retval 0 Integer has been read correctly
|
||||||
|
* @retval -1 Error while reading file
|
||||||
|
*/
|
||||||
|
int util_file_read_ui(unsigned int *val, int base, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
char path[PATH_MAX], buf[512];
|
||||||
|
va_list ap;
|
||||||
|
int count;
|
||||||
|
|
||||||
|
/* Construct the file name */
|
||||||
|
UTIL_VSPRINTF(path, fmt, ap);
|
||||||
|
|
||||||
|
if (file_gets(buf, sizeof(buf), path))
|
||||||
|
return -1;
|
||||||
|
switch (base) {
|
||||||
|
case 8:
|
||||||
|
count = sscanf(buf, "%uo", val);
|
||||||
|
break;
|
||||||
|
case 10:
|
||||||
|
count = sscanf(buf, "%uu", val);
|
||||||
|
break;
|
||||||
|
case 16:
|
||||||
|
count = sscanf(buf, "%ux", val);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
util_panic("Invalid base: %d\n", base);
|
||||||
|
}
|
||||||
|
return (count == 1) ? 0 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a file and convert it to unsigned long according to given base
|
* Read a file and convert it to unsigned long according to given base
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user