summaryrefslogtreecommitdiffstats
path: root/term-utils/write.c
diff options
context:
space:
mode:
authorSami Kerola2016-05-08 22:00:21 +0200
committerSami Kerola2016-07-04 00:35:09 +0200
commit5592bf0d9aa71d617ae540071ef7d6a8b611449f (patch)
treedaf0a173d5398b1d15dcc4c486d7644c300f1e91 /term-utils/write.c
parentwrite: remove PUTC macro (diff)
downloadkernel-qcow2-util-linux-5592bf0d9aa71d617ae540071ef7d6a8b611449f.tar.gz
kernel-qcow2-util-linux-5592bf0d9aa71d617ae540071ef7d6a8b611449f.tar.xz
kernel-qcow2-util-linux-5592bf0d9aa71d617ae540071ef7d6a8b611449f.zip
write: improve coding style
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'term-utils/write.c')
-rw-r--r--term-utils/write.c71
1 files 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 <stdio.h>
-#include <unistd.h>
-#include <utmp.h>
#include <errno.h>
-#include <time.h>
+#include <getopt.h>
+#include <paths.h>
#include <pwd.h>
-#include <string.h>
-#include <stdlib.h>
#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
-#include <paths.h>
-#include <getopt.h>
+#include <time.h>
+#include <unistd.h>
+#include <utmp.h>
#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