* 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>
* man/po/fr.po: Harmonize name of parameters.

View File

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

View File

@ -93,7 +93,6 @@ static long expdate;
#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);
@ -138,20 +137,6 @@ static void fail_exit (int 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
*/
@ -226,7 +211,7 @@ static int new_fields (void)
lstchgdate = -1;
} else {
lstchgdate = strtoday (buf);
if (lstchgdate == -1) {
if (lstchgdate < -1) {
return 0;
}
}
@ -254,7 +239,7 @@ static int new_fields (void)
expdate = -1;
} else {
expdate = strtoday (buf);
if (expdate == -1) {
if (expdate < -1) {
return 0;
}
}
@ -409,10 +394,8 @@ static void process_flags (int argc, char **argv)
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);
@ -421,10 +404,8 @@ static void process_flags (int argc, char **argv)
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);

View File

@ -1041,7 +1041,7 @@ static void process_flags (int argc, char **argv)
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);

View File

@ -940,7 +940,7 @@ static void process_flags (int argc, char **argv)
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);