Index: config.c =================================================================== --- config.c (revision 8431) +++ config.c (working copy) @@ -1088,4 +1076,24 @@ static struct ast_cli_entry config_comma int register_config_cli() { return ast_cli_register(&config_command_struct); +} + +/* + * get values from config variables. + */ +int ast_get_time_t(const char *src, time_t *dst, time_t _default) +{ + long t; + + if (dst == NULL) + return -1; + *dst = _default; + if (ast_strlen_zero(src)) + return -1; + /* only integer at the moment, but one day we could accept more formats */ + if (sscanf(src, "%ld", &t) == 1) { + *dst = t; + return 0; + } + return -1; } Index: channels/chan_sip.c =================================================================== --- channels/chan_sip.c (revision 8431) +++ channels/chan_sip.c (working copy) @@ -12116,15 +11786,12 @@ static struct sip_peer *build_peer(const peer->capability = global_capability; peer->rtptimeout = global_rtptimeout; peer->rtpholdtimeout = global_rtpholdtimeout; - while(v) { - if (handle_common_options(&peerflags, &mask, v)) { - v = v->next; + for (; v; v = v->next) { + if (handle_common_options(&peerflags, &mask, v)) continue; - } if (realtime && !strcasecmp(v->name, "regseconds")) { - if (sscanf(v->value, "%ld", (time_t *)®seconds) != 1) - regseconds = 0; + ast_get_time_t(v->value, ®seconds, 0); } else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) { inet_aton(v->value, &(peer->addr.sin_addr)); } else if (realtime && !strcasecmp(v->name, "name")) @@ -12272,11 +11939,7 @@ static struct sip_peer *build_peer(const ast_log(LOG_WARNING, "Qualification of peer '%s' should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", peer->name, v->lineno); peer->maxms = 0; } } - /* else if (strcasecmp(v->name,"type")) - * ast_log(LOG_WARNING, "Ignoring %s\n", v->name); - */ - v=v->next; } if (!ast_test_flag((&global_flags_page2), SIP_PAGE2_IGNOREREGEXPIRE) && ast_test_flag(peer, SIP_DYNAMIC) && realtime) { time_t nowtime; Index: channels/chan_iax2.c =================================================================== --- channels/chan_iax2.c (revision 8431) +++ channels/chan_iax2.c (working copy) @@ -2559,7 +2557,7 @@ static struct iax2_peer *realtime_peer(c struct ast_variable *var; struct ast_variable *tmp; struct iax2_peer *peer=NULL; - time_t regseconds, nowtime; + time_t regseconds = 0, nowtime; int dynamic=0; if (peername) @@ -2600,8 +2598,7 @@ static struct iax2_peer *realtime_peer(c break; } } else if (!strcasecmp(tmp->name, "regseconds")) { - if (sscanf(tmp->value, "%ld", (time_t *)®seconds) != 1) - regseconds = 0; + ast_get_time_t(tmp->value, ®seconds, 0); } else if (!strcasecmp(tmp->name, "ipaddr")) { inet_aton(tmp->value, &(peer->addr.sin_addr)); } else if (!strcasecmp(tmp->name, "port")) { Index: apps/app_voicemail.c =================================================================== --- apps/app_voicemail.c (revision 8431) +++ apps/app_voicemail.c (working copy) @@ -3586,25 +3583,21 @@ static int play_message_datetime(struct int res = 0; struct vm_zone *the_zone = NULL; time_t t; - long tin; - if (sscanf(origtime,"%ld",&tin) < 1) { + if (ast_get_time_t(origtime, &t, 0)) { ast_log(LOG_WARNING, "Couldn't find origtime in %s\n", filename); return 0; } - t = tin; /* Does this user have a timezone specified? */ if (!ast_strlen_zero(vmu->zonetag)) { /* Find the zone in the list */ struct vm_zone *z; - z = zones; - while (z) { + for (z = zones; z; z = z->next) { if (!strcmp(z->name, vmu->zonetag)) { the_zone = z; break; } - z = z->next; } } Index: apps/app_sayunixtime.c =================================================================== --- apps/app_sayunixtime.c (revision 8431) +++ apps/app_sayunixtime.c (working copy) @@ -76,45 +76,26 @@ static int sayunixtime_exec(struct ast_c struct localuser *u; char *s,*zone=NULL,*timec,*format; time_t unixtime; - struct timeval tv; + s = ast_strdupa(data); + if (!s) + return data ? -1 : 0; LOCAL_USER_ADD(u); - tv = ast_tvnow(); - unixtime = (time_t)tv.tv_sec; + format = "c"; /* default datetime */ - if( !strcasecmp(chan->language, "da" ) ) { - format = "A dBY HMS"; - } else if ( !strcasecmp(chan->language, "de" ) ) { - format = "A dBY HMS"; - } else { - format = "ABdY 'digits/at' IMp"; - } - - if (data) { - s = data; - if ((s = ast_strdupa(s))) { - timec = strsep(&s,"|"); - if ((timec) && (*timec != '\0')) { - long timein; - if (sscanf(timec,"%ld",&timein) == 1) { - unixtime = (time_t)timein; - } - } - if (s) { - zone = strsep(&s,"|"); - if (zone && (*zone == '\0')) - zone = NULL; - if (s) { - format = s; - } - } - } + timec = strsep(&s,"|"); + ast_get_time_t(timec, &unixtime, time(NULL)); + if (s) { + zone = strsep(&s,"|"); + if (ast_strlen_zero(zone)) + zone = NULL; } + if (s) /* override format */ + format = s; - if (chan->_state != AST_STATE_UP) { + if (chan->_state != AST_STATE_UP) res = ast_answer(chan); - } if (!res) res = ast_say_date_with_format(chan, unixtime, AST_DIGIT_ANY, chan->language, format, zone); Index: include/asterisk/config.h =================================================================== --- include/asterisk/config.h (revision 8431) +++ include/asterisk/config.h (working copy) @@ -186,6 +186,17 @@ int ast_variable_delete(struct ast_confi int config_text_file_save(const char *filename, const struct ast_config *cfg, const char *generator); struct ast_config *ast_config_internal_load(const char *configfile, struct ast_config *cfg); + +/* + * helper functions to load values from strings. + * In all cases, the return value is 0 on success, -1 on error. + */ + +/*! \brief Set *dst to the value in the string, or to _default on error. + * Returns -1 on error, 0 on success. + * In the future the syntax may be extended to parse other time specifications. + */ +int ast_get_time_t(const char *src, time_t *dst, time_t _default); #if defined(__cplusplus) || defined(c_plusplus) } Index: funcs/func_strings.c =================================================================== --- funcs/func_strings.c (revision 8431) +++ funcs/func_strings.c (working copy) @@ -256,8 +256,8 @@ static char *acf_strftime(struct ast_cha AST_APP_ARG(timezone); AST_APP_ARG(format); ); - long epochi; - struct tm time; + time_t epochi; + struct tm tm; buf[0] = '\0'; @@ -271,16 +271,14 @@ static char *acf_strftime(struct ast_cha AST_STANDARD_APP_ARGS(args, parse); - if (ast_strlen_zero(args.epoch) || !sscanf(args.epoch, "%ld", &epochi)) { - struct timeval tv = ast_tvnow(); - epochi = tv.tv_sec; - } + ast_get_time_t(args.epoch, &epochi, time(NULL)); + ast_localtime(&epochi, &tm, args.timezone); - ast_localtime(&epochi, &time, args.timezone); + if (!args.format) + args.format = "%c"; - if (!strftime(buf, len, args.format?args.format:"%c", &time)) { + if (!strftime(buf, len, args.format, &tm)) ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n"); - } buf[len - 1] = '\0'; return buf; Index: res/res_agi.c =================================================================== --- res/res_agi.c (revision 8431) +++ res/res_agi.c (working copy) @@ -741,16 +705,13 @@ static int handle_saytime(struct ast_cha if (res == 1) return RESULT_SUCCESS; fdprintf(agi->fd, "200 result=%d\n", res); - if (res >= 0) - return RESULT_SUCCESS; - else - return RESULT_FAILURE; + return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } static int handle_saydatetime(struct ast_channel *chan, AGI *agi, int argc, char *argv[]) { int res=0; - long unixtime; + time_t unixtime; char *format, *zone=NULL; if (argc < 4) @@ -769,19 +731,15 @@ static int handle_saydatetime(struct ast if (argc > 5 && !ast_strlen_zero(argv[5])) zone = argv[5]; - if (sscanf(argv[2], "%ld", &unixtime) != 1) + if (ast_get_time_t(argv[2], &unixtime, 0)) return RESULT_SHOWUSAGE; - res = ast_say_date_with_format(chan, (time_t) unixtime, argv[3], chan->language, format, zone); + res = ast_say_date_with_format(chan, unixtime, argv[3], chan->language, format, zone); if (res == 1) return RESULT_SUCCESS; fdprintf(agi->fd, "200 result=%d\n", res); - - if (res >= 0) - return RESULT_SUCCESS; - else - return RESULT_FAILURE; + return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } static int handle_sayphonetic(struct ast_channel *chan, AGI *agi, int argc, char *argv[])