1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-27 10:25:37 +03:00

util: use format_timestamp() instead of ctime() wherever possible

This commit is contained in:
Lennart Poettering 2010-08-16 21:24:50 +02:00
parent 7774cdc1d8
commit 116205924e
2 changed files with 13 additions and 22 deletions

View File

@ -107,15 +107,10 @@ static void warn_wall(struct shutdownd_command *c) {
if (c->wall_message[0]) if (c->wall_message[0])
utmp_wall(c->wall_message); utmp_wall(c->wall_message);
else { else {
time_t s; char date[FORMAT_TIMESTAMP_MAX];
char buf[27];
const char* prefix; const char* prefix;
char *l; char *l;
s = c->elapse / USEC_PER_SEC;
ctime_r(&s, buf);
if (c->mode == 'H') if (c->mode == 'H')
prefix = "The system is going down for system halt at"; prefix = "The system is going down for system halt at";
else if (c->mode == 'P') else if (c->mode == 'P')
@ -125,7 +120,9 @@ static void warn_wall(struct shutdownd_command *c) {
else else
assert_not_reached("Unknown mode!"); assert_not_reached("Unknown mode!");
if (asprintf(&l, "%s %s!", prefix, strstrip(buf)) < 0) if (asprintf(&l, "%s %s!",
prefix,
format_timestamp(date, sizeof(date), c->elapse)) < 0)
log_error("Failed to allocate wall message"); log_error("Failed to allocate wall message");
else { else {
utmp_wall(l); utmp_wall(l);
@ -260,8 +257,7 @@ int main(int argc, char *argv[]) {
goto finish; goto finish;
else if (k > 0 && c.elapse > 0) { else if (k > 0 && c.elapse > 0) {
struct itimerspec its; struct itimerspec its;
char buf[27]; char date[FORMAT_TIMESTAMP_MAX];
if (c.warn_wall) { if (c.warn_wall) {
/* Send wall messages every so often */ /* Send wall messages every so often */
@ -294,11 +290,9 @@ int main(int argc, char *argv[]) {
goto finish; goto finish;
} }
ctime_r(&its.it_value.tv_sec, buf);
sd_notifyf(false, sd_notifyf(false,
"STATUS=Shutting down at %s...", "STATUS=Shutting down at %s...",
strstrip(buf)); format_timestamp(date, sizeof(date), c.elapse));
} }
} }

View File

@ -290,10 +290,9 @@ finish:
int utmp_wall(const char *message) { int utmp_wall(const char *message) {
struct utmpx *u; struct utmpx *u;
char date[26]; char date[FORMAT_TIMESTAMP_MAX];
char *text = NULL, *hn = NULL, *un = NULL, *tty = NULL; char *text = NULL, *hn = NULL, *un = NULL, *tty = NULL;
int r; int r;
time_t t;
if (!(hn = gethostname_malloc()) || if (!(hn = gethostname_malloc()) ||
!(un = getlogname_malloc())) { !(un = getlogname_malloc())) {
@ -301,18 +300,16 @@ int utmp_wall(const char *message) {
goto finish; goto finish;
} }
if ((r = getttyname_malloc(&tty)) < 0) getttyname_malloc(&tty);
goto finish;
time(&t);
assert_se(ctime_r(&t, date));
delete_chars(date, "\n\r");
if (asprintf(&text, if (asprintf(&text,
"\a\r\n" "\a\r\n"
"Broadcast message from %s@%s on %s (%s):\r\n\r\n" "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
"%s\r\n\r\n", "%s\r\n\r\n",
un, hn, tty, date, message) < 0) { un, hn,
tty ? " on " : "", strempty(tty),
format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
message) < 0) {
r = -ENOMEM; r = -ENOMEM;
goto finish; goto finish;
} }