git: 37aaacd41016 - stable/14 - New login_getcapenum(): Allows to read named enum values

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Thu, 01 Feb 2024 21:31:07 UTC
The branch stable/14 has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=37aaacd410163ec3c2ffb3e402ba8c192a26c171

commit 37aaacd410163ec3c2ffb3e402ba8c192a26c171
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2023-06-20 16:41:32 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2024-02-01 21:28:40 +0000

    New login_getcapenum(): Allows to read named enum values
    
    Reviewed by:            emaste
    Approved by:            emaste (mentor)
    MFC after:              3 days
    Sponsored by:           Kumacom SAS
    Differential Revision:  https://reviews.freebsd.org/D40684
    
    (cherry picked from commit 90e914cd5ac1c8ecbf1ea88e9a65e7fa866c17a9)
    
    Approved by:            markj (mentor)
---
 lib/libutil/login_cap.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/libutil/login_cap.h |  2 ++
 2 files changed, 48 insertions(+)

diff --git a/lib/libutil/login_cap.c b/lib/libutil/login_cap.c
index aeda7d5e828b..eeaf12836597 100644
--- a/lib/libutil/login_cap.c
+++ b/lib/libutil/login_cap.c
@@ -761,6 +761,52 @@ login_getcapnum(login_cap_t *lc, const char *cap, rlim_t def, rlim_t error)
     return val;
 }
 
+/*
+ * Extract a string capability expected to hold a specific value from a list.
+ *
+ * 'values' must be a NULL-terminated array of strings listing the possible
+ * values.
+ *
+ * A non-negative return code indicates success, and is the index of the value
+ * in 'values' the capability is set to.
+ *
+ * Negative return codes indicate an error:
+ * -4: 'lc' or 'cap' insufficiently initialized or not valid.
+ * -3: System error (allocation failure).
+ * -2: Capability not found or not a string.
+ * -1: Capability has a string value, but not one listed in 'values'.
+ */
+int
+login_getcapenum(login_cap_t *lc, const char *cap, const char * const *values)
+{
+    int ret, i;
+    char *cand;
+    const char * const *val;
+
+    if (lc == NULL || lc->lc_cap == NULL || cap == NULL || *cap == '\0')
+	return (-4);
+
+    ret = cgetstr(lc->lc_cap, cap, &cand);
+
+    if (ret == -1)
+	/* Cap not found. */
+	return (-2);
+    else if (ret < 0)
+	/* System error (normally, allocation failure). */
+	return (-3);
+
+    ret = -1;
+
+    for (i = 0, val = values; *val != NULL; val++)
+	if (strcmp(cand, *val) == 0) {
+	    ret = i;
+	    break;
+	}
+
+    free(cand);
+    return (ret);
+}
+
 
 
 /*
diff --git a/lib/libutil/login_cap.h b/lib/libutil/login_cap.h
index 7312914900a8..0037b41d9055 100644
--- a/lib/libutil/login_cap.h
+++ b/lib/libutil/login_cap.h
@@ -110,6 +110,8 @@ const char **login_getcaplist(login_cap_t *, const char *, const char *);
 const char *login_getstyle(login_cap_t *, const char *, const char *);
 rlim_t login_getcaptime(login_cap_t *, const char *, rlim_t, rlim_t);
 rlim_t login_getcapnum(login_cap_t *, const char *, rlim_t, rlim_t);
+int login_getcapenum(login_cap_t *lc, const char *cap,
+    const char * const *values);
 rlim_t login_getcapsize(login_cap_t *, const char *, rlim_t, rlim_t);
 const char *login_getpath(login_cap_t *, const char *, const char *);
 int login_getcapbool(login_cap_t *, const char *, int);