1
0
mirror of https://github.com/irssi/irssi.git synced 2024-12-04 14:46:39 -05:00

Merge pull request #722 from dequis/back-to-the-future

parse_time_interval: Allow negative time in settings
This commit is contained in:
ailin-nemui 2017-06-23 18:00:52 +02:00 committed by GitHub
commit 2b209348bd

View File

@ -781,24 +781,35 @@ int parse_uint(const char *nptr, char **endptr, int base, guint *number)
return TRUE; return TRUE;
} }
static int parse_number_sign(const char *input, char **endptr, int *sign)
{
int sign_ = 1;
while (i_isspace(*input))
input++;
if (*input == '-') {
sign_ = -sign_;
input++;
}
*sign = sign_;
*endptr = (char *) input;
return TRUE;
}
static int parse_time_interval_uint(const char *time, guint *msecs) static int parse_time_interval_uint(const char *time, guint *msecs)
{ {
const char *desc; const char *desc;
guint number; guint number;
int sign, len, ret, digits; int len, ret, digits;
*msecs = 0; *msecs = 0;
/* max. return value is around 24 days */ /* max. return value is around 24 days */
number = 0; sign = 1; ret = TRUE; digits = FALSE; number = 0; ret = TRUE; digits = FALSE;
while (i_isspace(*time)) while (i_isspace(*time))
time++; time++;
if (*time == '-') {
sign = -sign;
time++;
while (i_isspace(*time))
time++;
}
for (;;) { for (;;) {
if (i_isdigit(*time)) { if (i_isdigit(*time)) {
char *endptr; char *endptr;
@ -828,7 +839,6 @@ static int parse_time_interval_uint(const char *time, guint *msecs)
if (*time != '\0') if (*time != '\0')
return FALSE; return FALSE;
*msecs += number * 1000; /* assume seconds */ *msecs += number * 1000; /* assume seconds */
*msecs *= sign;
return TRUE; return TRUE;
} }
@ -866,7 +876,6 @@ static int parse_time_interval_uint(const char *time, guint *msecs)
digits = FALSE; digits = FALSE;
} }
*msecs *= sign;
return ret; return ret;
} }
@ -960,15 +969,18 @@ int parse_size(const char *size, int *bytes)
int parse_time_interval(const char *time, int *msecs) int parse_time_interval(const char *time, int *msecs)
{ {
guint msecs_; guint msecs_;
int ret; char *number;
int ret, sign;
ret = parse_time_interval_uint(time, &msecs_); parse_number_sign(time, &number, &sign);
ret = parse_time_interval_uint(number, &msecs_);
if (msecs_ > (1U << 31)) { if (msecs_ > (1U << 31)) {
return FALSE; return FALSE;
} }
*msecs = msecs_; *msecs = msecs_ * sign;
return ret; return ret;
} }