Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 23 Oct 2001 10:26:35 -0700 (PDT)
From:      John Baldwin <jhb@FreeBSD.org>
To:        hackers@FreeBSD.org
Subject:   getenv_foo and TUNABLE_FOO_FETCH change
Message-ID:  <XFMail.011023102635.jhb@FreeBSD.org>

next in thread | raw e-mail | index | archive | help
Currently getenv_quad() claims to return a quad_t, but it's actual return value
is 1 if it found the environment variable in question and converted it ok and 0
if it didn't.  getenv_int() has the same return value.  I'd like to apply the
same to TUNABLE_*_FETCH so that one can do:

        if (TUNABLE_INT_FETCH("some.tunable.var", &temp_var))
                if (bounds_check(temp_var))
                        real_var = temp_var;

The resulting changes look like this:

Index: kern/kern_environment.c
===================================================================
RCS file: /usr/cvs/src/sys/kern/kern_environment.c,v
retrieving revision 1.18
diff -u -r1.18 kern_environment.c
--- kern/kern_environment.c     10 Oct 2001 23:06:53 -0000      1.18
+++ kern/kern_environment.c     22 Oct 2001 22:41:08 -0000
@@ -46,6 +46,9 @@
 
 static char    *kernenv_next(char *cp);
 
+/*
+ * Look up an environment variable by name.
+ */
 char *
 getenv(const char *name)
 {
@@ -65,6 +68,23 @@
 }
 
 /*
+ * Return a string value from an environment variable.
+ */
+int
+getenv_string(const char *name, char *data, int size)
+{
+    char *tmp;
+
+    tmp = getenv(name);
+    if (tmp == NULL) {
+       strncpy(data, tmp, size);
+       data[size - 1] = 0;
+       return (1);
+    } else
+       return (0);
+}
+
+/*
  * Return an integer value from an environment variable.
  */
 int
@@ -83,7 +103,7 @@
 /*
  * Return a quad_t value from an environment variable.
  */
-quad_t
+int
 getenv_quad(const char *name, quad_t *data)
 {
     const char *value;
Index: sys/systm.h
===================================================================
RCS file: /usr/cvs/src/sys/sys/systm.h,v
retrieving revision 1.154
diff -u -r1.154 systm.h
--- sys/systm.h 20 Sep 2001 21:45:31 -0000      1.154
+++ sys/systm.h 23 Oct 2001 17:24:12 -0000
@@ -192,7 +192,8 @@
 
 char   *getenv __P((const char *name));
 int    getenv_int __P((const char *name, int *data));
-quad_t getenv_quad __P((const char *name, quad_t *data));
+int    getenv_string __P((const char *name, char *data, int size));
+int    getenv_quad __P((const char *name, quad_t *data));
 
 #ifdef APM_FIXUP_CALLTODO 
 struct timeval;
Index: sys/kernel.h
===================================================================
RCS file: /usr/cvs/src/sys/sys/kernel.h,v
retrieving revision 1.94
diff -u -r1.94 kernel.h
--- sys/kernel.h        10 Oct 2001 23:06:54 -0000      1.94
+++ sys/kernel.h        22 Oct 2001 23:39:18 -0000
@@ -277,10 +277,7 @@
        SYSINIT(__Tunable_init_ ## line, SI_SUB_TUNABLES, SI_ORDER_MIDDLE, \
             tunable_int_init, &__tunable_int_ ## line)
 
-#define        TUNABLE_INT_FETCH(path, var)                    \
-do {                                                   \
-       getenv_int((path), (var));                      \
-} while (0)
+#define        TUNABLE_INT_FETCH(path, var)    getenv_int((path), (var))
 
 extern void tunable_quad_init(void *);
 struct tunable_quad {
@@ -300,10 +297,7 @@
        SYSINIT(__Tunable_init_ ## line, SI_SUB_TUNABLES, SI_ORDER_MIDDLE, \
            tunable_quad_init, &__tunable_quad_ ## line)
 
-#define        TUNABLE_QUAD_FETCH(path, var)                   \
-do {                                                   \
-       getenv_quad((path), (var));                     \
-} while (0)
+#define        TUNABLE_QUAD_FETCH(path, var)   getenv_quad((path), (var))
 
 extern void tunable_str_init(void *);
 struct tunable_str {
@@ -325,15 +319,8 @@
        SYSINIT(__Tunable_init_ ## line, SI_SUB_TUNABLES, SI_ORDER_MIDDLE, \
             tunable_str_init, &__tunable_str_ ## line)
 
-#define        TUNABLE_STR_FETCH(path, var, size)              \
-do {                                                   \
-       char *tmp;                                      \
-       tmp = getenv((path));                           \
-       if (tmp != NULL) {                              \
-               strncpy((var), tmp, (size));            \
-               (var)[(size) - 1] = 0;                  \
-       }                                               \
-} while (0)
+#define        TUNABLE_STR_FETCH(path, var, size)                      \
+       getenv_string((path), (var), (size))
 
 struct intr_config_hook {
        TAILQ_ENTRY(intr_config_hook) ich_links;

Also, one final note about using do { } while(0).  If you actually read
style(9), you will see that you are supposed to use it for compound statements,
not just for any macro that happens to be more than one line long.  If the
macro's body is a single statement, it doesn't need the do { } while (0) bit.

-- 

John Baldwin <john@baldwin.cx> -- http://www.FreeBSD.org/~jhb/
PGP Key: http://www.baldwin.cx/~john/pgpkey.asc
"Power Users Use the Power to Serve!"  -  http://www.FreeBSD.org/

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?XFMail.011023102635.jhb>