diff options
| author | Rodrigo Balerdi | 2024-09-24 23:59:29 +0000 |
|---|---|---|
| committer | Daniel Golle | 2026-03-17 16:16:16 +0000 |
| commit | 1bf2d490484ed2f0a251baabc2fa830a0e995d06 (patch) | |
| tree | 188747a666b305ff3ddbb57d971bbfd0bf11185a | |
| parent | 8d377aa627be1ae538f8cdeb3295c0c39b9b1d90 (diff) | |
| download | fstools-1bf2d490484ed2f0a251baabc2fa830a0e995d06.tar.gz | |
libfstools: make get_var_from_file() reusable
Move get_var_from_file() to common library.
Signed-off-by: Rodrigo Balerdi <lanchon@gmail.com>
| -rw-r--r-- | libfstools/common.c | 41 | ||||
| -rw-r--r-- | libfstools/common.h | 1 | ||||
| -rw-r--r-- | libfstools/partname.c | 34 |
3 files changed, 38 insertions, 38 deletions
diff --git a/libfstools/common.c b/libfstools/common.c index 82362f7..46b8114 100644 --- a/libfstools/common.c +++ b/libfstools/common.c @@ -3,8 +3,7 @@ #include "common.h" #define BUFLEN 128 -int -read_uint_from_file(char *dirname, char *filename, unsigned int *i) +int read_uint_from_file(char *dirname, char *filename, unsigned int *i) { FILE *f; char fname[BUFLEN]; @@ -23,8 +22,7 @@ read_uint_from_file(char *dirname, char *filename, unsigned int *i) return ret; } -char -*read_string_from_file(const char *dirname, const char *filename, char *buf, size_t bufsz) +char *read_string_from_file(const char *dirname, const char *filename, char *buf, size_t bufsz) { FILE *f; char fname[BUFLEN]; @@ -54,6 +52,41 @@ char return buf; } +/* adapted from procd/utils.c -> should go to libubox */ +char *get_var_from_file(const char *filename, const char *name, char *out, int len) +{ + char line[1024], *c, *sptr; + + int fd = open(filename, O_RDONLY); + if (fd == -1) + return NULL; + + ssize_t r = read(fd, line, sizeof(line) - 1); + close(fd); + + if (r <= 0) + return NULL; + + line[r] = 0; + + for (c = strtok_r(line, " \t\n", &sptr); c; + c = strtok_r(NULL, " \t\n", &sptr)) { + char *sep = strchr(c, '='); + if (sep == NULL) + continue; + + ssize_t klen = sep - c; + if (strncmp(name, c, klen) || name[klen] != 0) + continue; + + strncpy(out, &sep[1], len); + out[len-1] = '\0'; + return out; + } + + return NULL; +} + int block_file_identify(FILE *f, uint64_t offset) { uint32_t magic = 0; diff --git a/libfstools/common.h b/libfstools/common.h index 28b82d2..f34be50 100644 --- a/libfstools/common.h +++ b/libfstools/common.h @@ -23,6 +23,7 @@ int read_uint_from_file(char *dirname, char *filename, unsigned int *i); char *read_string_from_file(const char *dirname, const char *filename, char *buf, size_t bufsz); +char *get_var_from_file(const char *filename, const char *name, char *out, int len); int block_file_identify(FILE *f, uint64_t offset); int block_volume_format(struct volume *v, uint64_t offset, const char *bdev); diff --git a/libfstools/partname.c b/libfstools/partname.c index 9f2347a..1a221a9 100644 --- a/libfstools/partname.c +++ b/libfstools/partname.c @@ -59,40 +59,6 @@ static int partname_volume_init(struct volume *v) return block_volume_format(v, 0, p->parent_dev.devpathstr); } -/* adapted from procd/utils.c -> should go to libubox */ -static char* get_var_from_file(const char* filename, const char* name, char* out, int len) -{ - char line[1024], *c, *sptr; - int fd = open(filename, O_RDONLY); - if (fd == -1) - return NULL; - - ssize_t r = read(fd, line, sizeof(line) - 1); - close(fd); - - if (r <= 0) - return NULL; - - line[r] = 0; - - for (c = strtok_r(line, " \t\n", &sptr); c; - c = strtok_r(NULL, " \t\n", &sptr)) { - char *sep = strchr(c, '='); - if (sep == NULL) - continue; - - ssize_t klen = sep - c; - if (strncmp(name, c, klen) || name[klen] != 0) - continue; - - strncpy(out, &sep[1], len); - out[len-1] = '\0'; - return out; - } - - return NULL; -} - static char *rootdevname(char *devpath) { int l; |