* 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:
@@ -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;
|
||||
|
Reference in New Issue
Block a user