From 5592bf0d9aa71d617ae540071ef7d6a8b611449f Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sun, 8 May 2016 21:00:21 +0100 Subject: write: improve coding style Signed-off-by: Sami Kerola --- term-utils/write.c | 71 +++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/term-utils/write.c b/term-utils/write.c index 40be5c2de..8855c1e93 100644 --- a/term-utils/write.c +++ b/term-utils/write.c @@ -45,19 +45,19 @@ * */ -#include -#include -#include #include -#include +#include +#include #include -#include -#include #include +#include +#include +#include #include #include -#include -#include +#include +#include +#include #include "c.h" #include "carefulputc.h" @@ -76,7 +76,7 @@ struct write_control { char dst_tty[PATH_MAX]; }; -static void __attribute__ ((__noreturn__)) usage(FILE * out) +static void __attribute__((__noreturn__)) usage(FILE *out) { fputs(USAGE_HEADER, out); fprintf(out, @@ -103,7 +103,7 @@ static int check_tty(char *tty, int *tty_writeable, time_t *tty_atime, int showe struct stat s; char path[PATH_MAX]; - if (strlen(tty) + 6 > sizeof(path)) + if (sizeof(path) < strlen(tty) + 6) return 1; sprintf(path, "/dev/%s", tty); if (stat(path, &s) < 0) { @@ -165,27 +165,28 @@ static void search_utmp(struct write_control *ctl) setutent(); while ((u = getutent())) { - if (strncmp(ctl->dst_login, u->ut_user, sizeof(u->ut_user)) == 0) { - num_ttys++; - if (check_tty(u->ut_line, &tty_writeable, &tty_atime, 0)) - /* bad term? skip */ - continue; - if (ctl->src_uid && !tty_writeable) - /* skip ttys with msgs off */ - continue; - if (strcmp(u->ut_line, ctl->src_tty) == 0) { - user_is_me = 1; - /* don't write to yourself */ - continue; - } - if (u->ut_type != USER_PROCESS) - /* it's not a valid entry */ - continue; - valid_ttys++; - if (tty_atime > best_atime) { - best_atime = tty_atime; - xstrncpy(ctl->dst_tty, u->ut_line, sizeof(ctl->dst_tty)); - } + if (strncmp(ctl->dst_login, u->ut_user, sizeof(u->ut_user)) != 0) + continue; + num_ttys++; + if (check_tty(u->ut_line, &tty_writeable, &tty_atime, 0)) + /* bad term? skip */ + continue; + if (ctl->src_uid && !tty_writeable) + /* skip ttys with msgs off */ + continue; + if (strcmp(u->ut_line, ctl->src_tty) == 0) { + user_is_me = 1; + /* don't write to yourself */ + continue; + } + if (u->ut_type != USER_PROCESS) + /* it's not a valid entry */ + continue; + valid_ttys++; + if (best_atime < tty_atime) { + best_atime = tty_atime; + xstrncpy(ctl->dst_tty, u->ut_line, + sizeof(ctl->dst_tty)); } } @@ -199,11 +200,10 @@ static void search_utmp(struct write_control *ctl) return; } errx(EXIT_FAILURE, _("%s has messages disabled"), ctl->dst_login); - } else if (valid_ttys > 1) { + } + if (1 < valid_ttys) warnx(_("%s is logged in more than once; writing to %s"), ctl->dst_login, ctl->dst_tty); - } - } /* @@ -249,7 +249,7 @@ static void do_write(const struct write_control *ctl) if ((login = getlogin()) == NULL) login = pwuid; - if (strlen(ctl->dst_tty) + 6 > sizeof(path)) + if (sizeof(path) < strlen(ctl->dst_tty) + 6) errx(EXIT_FAILURE, _("tty path %s too long"), ctl->dst_tty); snprintf(path, sizeof(path), "/dev/%s", ctl->dst_tty); if ((freopen(path, "w", stdout)) == NULL) @@ -328,7 +328,6 @@ int main(int argc, char **argv) if (!(ctl.src_tty = ttyname(src_fd))) errx(EXIT_FAILURE, _("can't find your tty's name")); - /* * We may have /dev/ttyN but also /dev/pts/xx. Below, * check_tty() will put "/dev/" in front, so remove that -- cgit v1.2.3-55-g7522