* 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().
This commit is contained in:
nekral-guest 2010-03-18 11:53:49 +00:00
parent 79fa4f3343
commit 0cbbdb32c4
5 changed files with 49 additions and 30 deletions

View File

@ -1,3 +1,13 @@
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> 2010-03-18 Nicolas François <nicolas.francois@centraliens.net>
* man/po/fr.po: Harmonize name of parameters. * man/po/fr.po: Harmonize name of parameters.

View File

@ -36,6 +36,8 @@
#include <config.h> #include <config.h>
#include <ctype.h>
#ident "$Id$" #ident "$Id$"
#include "defines.h" #include "defines.h"
@ -44,6 +46,7 @@
#ifndef USE_GETDATE #ifndef USE_GETDATE
#define USE_GETDATE 1 #define USE_GETDATE 1
#endif #endif
#if USE_GETDATE #if USE_GETDATE
#include "getdate.h" #include "getdate.h"
/* /*
@ -63,6 +66,8 @@
long strtoday (const char *str) long strtoday (const char *str)
{ {
time_t t; time_t t;
bool isnum = true;
const char *s = str;
/* /*
* get_date() interprets an empty string as the current date, * get_date() interprets an empty string as the current date,
@ -70,12 +75,35 @@ long strtoday (const char *str)
* (useradd sets sp_expire = current date for new lusers) * (useradd sets sp_expire = current date for new lusers)
*/ */
if ((NULL == str) || ('\0' == *str)) { if ((NULL == str) || ('\0' == *str)) {
return -1; return -2;
} }
t = get_date (str, (time_t *) 0); /* 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, NULL);
if ((time_t) - 1 == t) { if ((time_t) - 1 == t) {
return -1; return -2;
} }
/* convert seconds to days since 1970-01-01 */ /* convert seconds to days since 1970-01-01 */
return (long) (t + DAY / 2) / DAY; return (long) (t + DAY / 2) / DAY;

View File

@ -93,7 +93,6 @@ static long expdate;
#define EPOCH "1969-12-31" #define EPOCH "1969-12-31"
/* local function prototypes */ /* local function prototypes */
static bool isnum (const char *s);
static void usage (int status); static void usage (int status);
static void date_to_str (char *buf, size_t maxsize, time_t date); static void date_to_str (char *buf, size_t maxsize, time_t date);
static int new_fields (void); static int new_fields (void);
@ -138,20 +137,6 @@ static void fail_exit (int code)
exit (code); 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 * usage - print command line syntax and exit
*/ */
@ -226,7 +211,7 @@ static int new_fields (void)
lstchgdate = -1; lstchgdate = -1;
} else { } else {
lstchgdate = strtoday (buf); lstchgdate = strtoday (buf);
if (lstchgdate == -1) { if (lstchgdate < -1) {
return 0; return 0;
} }
} }
@ -254,7 +239,7 @@ static int new_fields (void)
expdate = -1; expdate = -1;
} else { } else {
expdate = strtoday (buf); expdate = strtoday (buf);
if (expdate == -1) { if (expdate < -1) {
return 0; return 0;
} }
} }
@ -409,10 +394,8 @@ static void process_flags (int argc, char **argv)
switch (c) { switch (c) {
case 'd': case 'd':
dflg = true; dflg = true;
if (!isnum (optarg)) {
lstchgdate = strtoday (optarg); lstchgdate = strtoday (optarg);
} else if ( (getlong (optarg, &lstchgdate) == 0) if (lstchgdate < -1) {
|| (lstchgdate < -1)) {
fprintf (stderr, fprintf (stderr,
_("%s: invalid date '%s'\n"), _("%s: invalid date '%s'\n"),
Prog, optarg); Prog, optarg);
@ -421,10 +404,8 @@ static void process_flags (int argc, char **argv)
break; break;
case 'E': case 'E':
Eflg = true; Eflg = true;
if (!isnum (optarg)) {
expdate = strtoday (optarg); expdate = strtoday (optarg);
} else if ( (getlong (optarg, &expdate) == 0) if (expdate < -1) {
|| (expdate < -1)) {
fprintf (stderr, fprintf (stderr,
_("%s: invalid date '%s'\n"), _("%s: invalid date '%s'\n"),
Prog, optarg); Prog, optarg);

View File

@ -1041,7 +1041,7 @@ static void process_flags (int argc, char **argv)
case 'e': case 'e':
if ('\0' != *optarg) { if ('\0' != *optarg) {
user_expire = strtoday (optarg); user_expire = strtoday (optarg);
if (user_expire == -1) { if (user_expire < -1) {
fprintf (stderr, fprintf (stderr,
_("%s: invalid date '%s'\n"), _("%s: invalid date '%s'\n"),
Prog, optarg); Prog, optarg);

View File

@ -940,7 +940,7 @@ static void process_flags (int argc, char **argv)
case 'e': case 'e':
if ('\0' != *optarg) { if ('\0' != *optarg) {
user_newexpire = strtoday (optarg); user_newexpire = strtoday (optarg);
if (user_newexpire == -1) { if (user_newexpire < -1) {
fprintf (stderr, fprintf (stderr,
_("%s: invalid date '%s'\n"), _("%s: invalid date '%s'\n"),
Prog, optarg); Prog, optarg);