From c9679b795461f5e40ec51281ae3a9580e56cd0a1 Mon Sep 17 00:00:00 2001 From: nekral-guest Date: Fri, 13 Jun 2008 21:35:22 +0000 Subject: [PATCH] * lib/getdef.h, lib/getdef.c: Add getdef_ulong(). * lib/getdef.c: Added TODOs because of lack of error checking. * lib/getdef.c: The size argument of fgets is an int, not a size_t. * lib/getdef.c: Avoid multi-statements lines. --- ChangeLog | 8 ++++++++ lib/getdef.c | 34 +++++++++++++++++++++++++++++++--- lib/getdef.h | 1 + 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index f97bc3fc..906b79e3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2008-06-13 Nicolas François + + * lib/getdef.h, lib/getdef.c: Add getdef_ulong(). + * lib/getdef.c: Added TODOs because of lack of error checking. + * lib/getdef.c: The size argument of fgets is an int, not a + size_t. + * lib/getdef.c: Avoid multi-statements lines. + 2008-06-13 Nicolas François * src/id.c: Make sure malloc receives a size_t. diff --git a/lib/getdef.c b/lib/getdef.c index 2f3659a4..02057de5 100644 --- a/lib/getdef.c +++ b/lib/getdef.c @@ -203,6 +203,7 @@ int getdef_num (const char *item, int dflt) } return (int) strtol (d->value, (char **) NULL, 0); + /* TODO: check for errors */ } @@ -228,6 +229,7 @@ unsigned int getdef_unum (const char *item, unsigned int dflt) } return (unsigned int) strtoul (d->value, (char **) NULL, 0); + /* TODO: check for errors */ } @@ -253,8 +255,33 @@ long getdef_long (const char *item, long dflt) } return strtol (d->value, (char **) NULL, 0); + /* TODO: check for errors */ } +/* + * getdef_ulong - get unsigned long numerical value from table of definitions + * + * Returns numeric value of specified item, else the "dflt" value if + * the item is not defined. Octal (leading "0") and hex (leading "0x") + * values are handled. + */ + +unsigned long getdef_ulong (const char *item, unsigned int dflt) +{ + struct itemdef *d; + + if (!def_loaded) { + def_load (); + } + + d = def_find (item); + if ((NULL == d) || (NULL == d->value)) { + return dflt; + } + + return (unsigned long) strtoul (d->value, (char **) NULL, 0); + /* TODO: check for errors */ +} /* * putdef_str - override the value read from /etc/login.defs @@ -364,17 +391,18 @@ static void def_load (void) /* * Go through all of the lines in the file. */ - while (fgets (buf, sizeof (buf), fp) != NULL) { + while (fgets (buf, (int) sizeof (buf), fp) != NULL) { /* * Trim trailing whitespace. */ - for (i = strlen (buf) - 1; i >= 0; --i) { + for (i = (int) strlen (buf) - 1; i >= 0; --i) { if (!isspace (buf[i])) { break; } } - buf[++i] = '\0'; + i++; + buf[i] = '\0'; /* * Break the line into two fields. diff --git a/lib/getdef.h b/lib/getdef.h index 8398ac2a..ce7212de 100644 --- a/lib/getdef.h +++ b/lib/getdef.h @@ -36,6 +36,7 @@ extern bool getdef_bool (const char *); extern long getdef_long (const char *, long); extern int getdef_num (const char *, int); +extern unsigned int getdef_ulong (const char *, unsigned long); extern unsigned int getdef_unum (const char *, unsigned int); extern char *getdef_str (const char *); extern int putdef_str (const char *, const char *);