From b9ffbb0a94fc0aac41c5956117b566d4b7b40c22 Mon Sep 17 00:00:00 2001 From: Pawel Jakub Dawidek Date: Sun, 24 Oct 2010 17:24:08 +0000 Subject: [PATCH] Implement nv_exists() function that returns true if argument of the given name exists. MFC after: 3 days --- sbin/hastd/nv.c | 28 +++++++++++++++++++++++++++- sbin/hastd/nv.h | 1 + 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/sbin/hastd/nv.c b/sbin/hastd/nv.c index a0de09adb0d5..aa37fcf2ec18 100644 --- a/sbin/hastd/nv.c +++ b/sbin/hastd/nv.c @@ -46,6 +46,8 @@ __FBSDID("$FreeBSD$"); #include #include +#define NV_TYPE_NONE 0 + #define NV_TYPE_INT8 1 #define NV_TYPE_UINT8 2 #define NV_TYPE_INT16 3 @@ -561,6 +563,29 @@ nv_get_string(struct nv *nv, const char *namefmt, ...) return (str); } +bool +nv_exists(struct nv *nv, const char *namefmt, ...) +{ + struct nvhdr *nvh; + va_list nameap; + int snverror, serrno; + + if (nv == NULL) + return (false); + + serrno = errno; + snverror = nv->nv_error; + + va_start(nameap, namefmt); + nvh = nv_find(nv, NV_TYPE_NONE, namefmt, nameap); + va_end(nameap); + + errno = serrno; + nv->nv_error = snverror; + + return (nvh != NULL); +} + /* * Dump content of the nv structure. */ @@ -797,7 +822,8 @@ nv_find(struct nv *nv, int type, const char *namefmt, va_list nameap) assert(size >= NVH_SIZE(nvh)); nv_swap(nvh, true); if (strcmp(nvh->nvh_name, name) == 0) { - if ((nvh->nvh_type & NV_TYPE_MASK) != type) { + if (type != NV_TYPE_NONE && + (nvh->nvh_type & NV_TYPE_MASK) != type) { errno = EINVAL; if (nv->nv_error == 0) nv->nv_error = EINVAL; diff --git a/sbin/hastd/nv.h b/sbin/hastd/nv.h index 7aee3c44aa96..664557ec3441 100644 --- a/sbin/hastd/nv.h +++ b/sbin/hastd/nv.h @@ -126,6 +126,7 @@ const uint64_t *nv_get_uint64_array(struct nv *nv, size_t *sizep, const char *nv_get_string(struct nv *nv, const char *namefmt, ...) __printflike(2, 3); +bool nv_exists(struct nv *nv, const char *namefmt, ...) __printflike(2, 3); void nv_dump(struct nv *nv); #endif /* !_NV_H_ */