diff --git a/apps/app_sayunixtime.c b/apps/app_sayunixtime.c index c6db4f14044260af3827db66b16542b1afc761e2..ad96c6d6f9c5c28d4f3afadf056948e47fe01609 100644 --- a/apps/app_sayunixtime.c +++ b/apps/app_sayunixtime.c @@ -76,45 +76,26 @@ static int sayunixtime_exec(struct ast_channel *chan, void *data) 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; - - 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; - } - } - } + format = "c"; /* default datetime */ + + 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); diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c index da3f90c0ef15ef941443c39eeef31142bc946699..13ad2f786ecaaefb14bc829450abf76fb6e1a4c5 100644 --- a/apps/app_voicemail.c +++ b/apps/app_voicemail.c @@ -3608,25 +3608,21 @@ static int play_message_datetime(struct ast_channel *chan, struct ast_vm_user *v 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; } } diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c index b54f17d21aef4ab5450e96cf225fd2d8d7932436..094a6ab63db3723988c04a3b0ec6350069c216bc 100644 --- a/channels/chan_iax2.c +++ b/channels/chan_iax2.c @@ -2562,7 +2562,7 @@ static struct iax2_peer *realtime_peer(const char *peername, struct sockaddr_in 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) @@ -2603,8 +2603,7 @@ static struct iax2_peer *realtime_peer(const char *peername, struct sockaddr_in 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")) { diff --git a/channels/chan_sip.c b/channels/chan_sip.c index 468514a961576ea01d9f1ab6a7983b35959cd28b..db3d3f1c1082014aa14fabb43d142a137c1d646c 100644 --- a/channels/chan_sip.c +++ b/channels/chan_sip.c @@ -12152,15 +12152,12 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, int ast_variables_destroy(peer->chanvars); peer->chanvars = NULL; } - 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")) @@ -12309,10 +12306,6 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, int 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->flags_page2), SIP_PAGE2_DYNAMIC) && realtime) { time_t nowtime; diff --git a/funcs/func_strings.c b/funcs/func_strings.c index 5dafb1f335f8b49d7a5402be217367679ccf7c32..4d992b1b538712781e7b8fdd3f73f40966986ae7 100644 --- a/funcs/func_strings.c +++ b/funcs/func_strings.c @@ -232,8 +232,8 @@ static int acf_strftime(struct ast_channel *chan, char *cmd, char *parse, AST_APP_ARG(timezone); AST_APP_ARG(format); ); - long epochi; - struct tm time; + time_t epochi; + struct tm tm; buf[0] = '\0'; @@ -245,14 +245,13 @@ static int acf_strftime(struct ast_channel *chan, char *cmd, char *parse, 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'; diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h index 5399c32da14eecececee8d2f69ce36051f340008..a48520ad60d77a8ee543a10a903b30ecc5582200 100644 --- a/include/asterisk/strings.h +++ b/include/asterisk/strings.h @@ -1,7 +1,7 @@ /* * Asterisk -- An open source telephony toolkit. * - * Copyright (C) 1999 - 2005, Digium, Inc. + * Copyright (C) 1999 - 2006, Digium, Inc. * * Mark Spencer <markster@digium.com> * @@ -223,6 +223,15 @@ int ast_false(const char *val); */ void ast_join(char *s, size_t len, char * const w[]); +/* + \brief Parse a time (integer) string. + \param src String to parse + \param dst Destination + \param _default Value to use if the string does not contain a valid time + \return zero on success, non-zero on failure +*/ +int ast_get_time_t(const char *src, time_t *dst, time_t _default); + /* The realloca lets us ast_restrdupa(), but you can't mix any other ast_strdup calls! */ struct ast_realloca { diff --git a/res/res_agi.c b/res/res_agi.c index 85436a6726c826dacf79fd365795e8f61eb29d70..ef7576fcdc02a0fa845d388fcdb13a1d75de42b5 100644 --- a/res/res_agi.c +++ b/res/res_agi.c @@ -742,16 +742,13 @@ static int handle_saytime(struct ast_channel *chan, AGI *agi, int argc, char *ar 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) @@ -770,19 +767,15 @@ static int handle_saydatetime(struct ast_channel *chan, AGI *agi, int argc, char 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[]) diff --git a/utils.c b/utils.c index 8e4cc4acc62143ab8c3fcfea4808b370d29257f0..23c23929593f44a8648c589c12f34cb7cbf7660d 100644 --- a/utils.c +++ b/utils.c @@ -1046,3 +1046,27 @@ void __ast_string_field_index_build(struct ast_string_field_mgr *mgr, va_end(ap2); } + + +/* + * 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; + } else + return -1; +}