another conversion from ast_build_str to ast_str

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@48517 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Luigi Rizzo
2006-12-16 09:56:31 +00:00
parent c1220dd27e
commit 97ce2f8fd8

View File

@@ -332,8 +332,7 @@ static int modlist_modentry(const char *module, const char *description, int use
static void print_uptimestr(int fd, time_t timeval, const char *prefix, int printsec) static void print_uptimestr(int fd, time_t timeval, const char *prefix, int printsec)
{ {
int x; /* the main part - years, weeks, etc. */ int x; /* the main part - years, weeks, etc. */
char timestr[256]="", *s = timestr; struct ast_str *out;
size_t maxbytes = sizeof(timestr);
#define SECOND (1) #define SECOND (1)
#define MINUTE (SECOND*60) #define MINUTE (SECOND*60)
@@ -344,40 +343,41 @@ static void print_uptimestr(int fd, time_t timeval, const char *prefix, int prin
#define NEEDCOMMA(x) ((x)? ",": "") /* define if we need a comma */ #define NEEDCOMMA(x) ((x)? ",": "") /* define if we need a comma */
if (timeval < 0) /* invalid, nothing to show */ if (timeval < 0) /* invalid, nothing to show */
return; return;
if (printsec) { /* plain seconds output */ if (printsec) { /* plain seconds output */
ast_build_string(&s, &maxbytes, "%lu", (u_long)timeval); ast_cli(fd, "%s: %lu\n", prefix, (u_long)timeval);
timeval = 0; /* bypass the other cases */ return;
} }
out = ast_str_alloca(256);
if (timeval > YEAR) { if (timeval > YEAR) {
x = (timeval / YEAR); x = (timeval / YEAR);
timeval -= (x * YEAR); timeval -= (x * YEAR);
ast_build_string(&s, &maxbytes, "%d year%s%s ", x, ESS(x),NEEDCOMMA(timeval)); ast_str_append(&out, 0, "%d year%s%s ", x, ESS(x),NEEDCOMMA(timeval));
} }
if (timeval > WEEK) { if (timeval > WEEK) {
x = (timeval / WEEK); x = (timeval / WEEK);
timeval -= (x * WEEK); timeval -= (x * WEEK);
ast_build_string(&s, &maxbytes, "%d week%s%s ", x, ESS(x),NEEDCOMMA(timeval)); ast_str_append(&out, 0, "%d week%s%s ", x, ESS(x),NEEDCOMMA(timeval));
} }
if (timeval > DAY) { if (timeval > DAY) {
x = (timeval / DAY); x = (timeval / DAY);
timeval -= (x * DAY); timeval -= (x * DAY);
ast_build_string(&s, &maxbytes, "%d day%s%s ", x, ESS(x),NEEDCOMMA(timeval)); ast_str_append(&out, 0, "%d day%s%s ", x, ESS(x),NEEDCOMMA(timeval));
} }
if (timeval > HOUR) { if (timeval > HOUR) {
x = (timeval / HOUR); x = (timeval / HOUR);
timeval -= (x * HOUR); timeval -= (x * HOUR);
ast_build_string(&s, &maxbytes, "%d hour%s%s ", x, ESS(x),NEEDCOMMA(timeval)); ast_str_append(&out, 0, "%d hour%s%s ", x, ESS(x),NEEDCOMMA(timeval));
} }
if (timeval > MINUTE) { if (timeval > MINUTE) {
x = (timeval / MINUTE); x = (timeval / MINUTE);
timeval -= (x * MINUTE); timeval -= (x * MINUTE);
ast_build_string(&s, &maxbytes, "%d minute%s%s ", x, ESS(x),NEEDCOMMA(timeval)); ast_str_append(&out, 0, "%d minute%s%s ", x, ESS(x),NEEDCOMMA(timeval));
} }
x = timeval; x = timeval;
if (x > 0) if (x > 0 || out->used == 0) /* if there is nothing, print 0 seconds */
ast_build_string(&s, &maxbytes, "%d second%s ", x, ESS(x)); ast_str_append(&out, 0, "%d second%s ", x, ESS(x));
if (timestr[0] != '\0') ast_cli(fd, "%s: %s\n", prefix, out->str);
ast_cli(fd, "%s: %s\n", prefix, timestr);
} }
static char * handle_showuptime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) static char * handle_showuptime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)