+2010-03-18 Nicolas François <nicolas.francois@centraliens.net>
+
+ * libmisc/strtoday.c: Add support for numerical dates, assuming
+ they are already specified in number of days since Epoch. Return
+ -2 in case of errors to support the specification of -1.
+ * src/usermod.c, src/useradd.c: Adapt to the new error value of
+ strtoday().
+ * src/chage.c: Remove isnum(). Adapt to the new error value of
+ strtoday(). Support for numerical dates is moved to strtoday().
+
2010-03-18 Nicolas François <nicolas.francois@centraliens.net>
* man/po/fr.po: Harmonize name of parameters.
#include <config.h>
+#include <ctype.h>
+
#ident "$Id$"
#include "defines.h"
#ifndef USE_GETDATE
#define USE_GETDATE 1
#endif
+
#if USE_GETDATE
#include "getdate.h"
/*
long strtoday (const char *str)
{
time_t t;
+ bool isnum = true;
+ const char *s = str;
/*
* get_date() interprets an empty string as the current date,
* (useradd sets sp_expire = current date for new lusers)
*/
if ((NULL == str) || ('\0' == *str)) {
- return -1;
+ return -2;
+ }
+
+ /* If a numerical value is provided, this is already a number of
+ * days since EPOCH.
+ */
+ if ('-' == *s) {
+ s++;
+ }
+ while (' ' == *s) {
+ s++;
+ }
+ while (isnum && ('\0' != *s)) {
+ if (!isdigit (*s)) {
+ isnum = false;
+ }
+ s++;
+ }
+ if (isnum) {
+ long retdate;
+ if (getlong (str, &retdate) == 0) {
+ return -2;
+ }
+ return retdate;
}
- t = get_date (str, (time_t *) 0);
+ t = get_date (str, NULL);
if ((time_t) - 1 == t) {
- return -1;
+ return -2;
}
/* convert seconds to days since 1970-01-01 */
return (long) (t + DAY / 2) / DAY;
#define EPOCH "1969-12-31"
/* local function prototypes */
-static bool isnum (const char *s);
static void usage (int status);
static void date_to_str (char *buf, size_t maxsize, time_t date);
static int new_fields (void);
exit (code);
}
-/*
- * isnum - determine whether or not a string is a number
- */
-static bool isnum (const char *s)
-{
- while ('\0' != *s) {
- if (!isdigit (*s)) {
- return false;
- }
- s++;
- }
- return true;
-}
-
/*
* usage - print command line syntax and exit
*/
lstchgdate = -1;
} else {
lstchgdate = strtoday (buf);
- if (lstchgdate == -1) {
+ if (lstchgdate < -1) {
return 0;
}
}
expdate = -1;
} else {
expdate = strtoday (buf);
- if (expdate == -1) {
+ if (expdate < -1) {
return 0;
}
}
switch (c) {
case 'd':
dflg = true;
- if (!isnum (optarg)) {
- lstchgdate = strtoday (optarg);
- } else if ( (getlong (optarg, &lstchgdate) == 0)
- || (lstchgdate < -1)) {
+ lstchgdate = strtoday (optarg);
+ if (lstchgdate < -1) {
fprintf (stderr,
_("%s: invalid date '%s'\n"),
Prog, optarg);
break;
case 'E':
Eflg = true;
- if (!isnum (optarg)) {
- expdate = strtoday (optarg);
- } else if ( (getlong (optarg, &expdate) == 0)
- || (expdate < -1)) {
+ expdate = strtoday (optarg);
+ if (expdate < -1) {
fprintf (stderr,
_("%s: invalid date '%s'\n"),
Prog, optarg);
case 'e':
if ('\0' != *optarg) {
user_expire = strtoday (optarg);
- if (user_expire == -1) {
+ if (user_expire < -1) {
fprintf (stderr,
_("%s: invalid date '%s'\n"),
Prog, optarg);
case 'e':
if ('\0' != *optarg) {
user_newexpire = strtoday (optarg);
- if (user_newexpire == -1) {
+ if (user_newexpire < -1) {
fprintf (stderr,
_("%s: invalid date '%s'\n"),
Prog, optarg);