[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[dmidecode] [PATCH 2/3] util: Let callers pass an offset to read_file
From: |
Jean Delvare |
Subject: |
[dmidecode] [PATCH 2/3] util: Let callers pass an offset to read_file |
Date: |
Wed, 5 Apr 2017 15:03:31 +0200 |
When reading from a dump file, read_file would be more convenient to
use than mem_chunk, but it lacks an offset parameter.
---
dmidecode.c | 4 ++--
util.c | 12 ++++++++++--
util.h | 2 +-
3 files changed, 13 insertions(+), 5 deletions(-)
--- dmidecode.orig/util.c 2017-04-05 14:38:20.159930299 +0200
+++ dmidecode/util.c 2017-04-05 14:38:34.148063132 +0200
@@ -89,7 +89,7 @@ int checksum(const u8 *buf, size_t len)
}
/*
- * Reads all of file, up to max_len bytes.
+ * Reads all of file from given offset, up to max_len bytes.
* A buffer of max_len bytes is allocated by this function, and
* needs to be freed by the caller.
* This provides a similar usage model to mem_chunk()
@@ -98,7 +98,7 @@ int checksum(const u8 *buf, size_t len)
* sets max_len to the length actually read.
*
*/
-void *read_file(size_t *max_len, const char *filename)
+void *read_file(off_t base, size_t *max_len, const char *filename)
{
int fd;
size_t r2 = 0;
@@ -116,6 +116,14 @@ void *read_file(size_t *max_len, const c
return NULL;
}
+ if (lseek(fd, base, SEEK_SET) == -1)
+ {
+ fprintf(stderr, "%s: ", filename);
+ perror("lseek");
+ p = NULL;
+ goto out;
+ }
+
if ((p = malloc(*max_len)) == NULL)
{
perror("malloc");
--- dmidecode.orig/util.h 2017-04-05 14:38:20.159930299 +0200
+++ dmidecode/util.h 2017-04-05 14:38:34.148063132 +0200
@@ -25,7 +25,7 @@
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
int checksum(const u8 *buf, size_t len);
-void *read_file(size_t *len, const char *filename);
+void *read_file(off_t base, size_t *len, const char *filename);
void *mem_chunk(off_t base, size_t len, const char *devmem);
int write_dump(size_t base, size_t len, const void *data, const char
*dumpfile, int add);
u64 u64_range(u64 start, u64 end);
--- dmidecode.orig/dmidecode.c 2017-04-05 14:38:20.303931666 +0200
+++ dmidecode/dmidecode.c 2017-04-05 14:38:34.149063141 +0200
@@ -4550,7 +4550,7 @@ static void dmi_table(off_t base, u32 le
* result of the kernel truncating the table on parse error.
*/
size_t size = len;
- buf = read_file(&size, devmem);
+ buf = read_file(0, &size, devmem);
if (!(opt.flags & FLAG_QUIET) && num && size != (size_t)len)
{
fprintf(stderr, "Wrong DMI structures length: %u bytes "
@@ -4862,7 +4862,7 @@ int main(int argc, char * const argv[])
*/
size = 0x20;
if (!(opt.flags & FLAG_NO_SYSFS)
- && (buf = read_file(&size, SYS_ENTRY_FILE)) != NULL)
+ && (buf = read_file(0, &size, SYS_ENTRY_FILE)) != NULL)
{
if (!(opt.flags & FLAG_QUIET))
printf("Getting SMBIOS data from sysfs.\n");
--
Jean Delvare
SUSE L3 Support
- [dmidecode] [PATCH 2/3] util: Let callers pass an offset to read_file,
Jean Delvare <=