#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
-#include <utmp.h>
+#include <utmpx.h>
#include "c.h"
#include "carefulputc.h"
const char *dst_tty_name;
};
-static void __attribute__((__noreturn__)) usage(FILE *out)
+static void __attribute__((__noreturn__)) usage(void)
{
+ FILE *out = stdout;
fputs(USAGE_HEADER, out);
fprintf(out,
_(" %s [options] <user> [<ttyname>]\n"),
fputs(_("Send a message to another user.\n"), out);
fputs(USAGE_OPTIONS, out);
- fputs(USAGE_HELP, out);
- fputs(USAGE_VERSION, out);
-
- fprintf(out, USAGE_MAN_TAIL("write(1)"));
- exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+ printf(USAGE_HELP_OPTIONS(16));
+ printf(USAGE_MAN_TAIL("write(1)"));
+ exit(EXIT_SUCCESS);
}
/*
*/
static int check_utmp(const struct write_control *ctl)
{
- struct utmp *u;
+ struct utmpx *u;
int res = 1;
- utmpname(_PATH_UTMP);
- setutent();
+ utmpxname(_PATH_UTMP);
+ setutxent();
- while ((u = getutent())) {
+ while ((u = getutxent())) {
if (strncmp(ctl->dst_login, u->ut_user, sizeof(u->ut_user)) == 0 &&
strncmp(ctl->dst_tty_name, u->ut_line, sizeof(u->ut_line)) == 0) {
res = 0;
}
}
- endutent();
+ endutxent();
return res;
}
*/
static void search_utmp(struct write_control *ctl)
{
- struct utmp *u;
+ struct utmpx *u;
time_t best_atime = 0, tty_atime;
int num_ttys = 0, valid_ttys = 0, tty_writeable = 0, user_is_me = 0;
- char path[UT_LINESIZE + 6];
+ char path[sizeof(u->ut_line) + 6];
- utmpname(_PATH_UTMP);
- setutent();
+ utmpxname(_PATH_UTMP);
+ setutxent();
- while ((u = getutent())) {
+ while ((u = getutxent())) {
if (strncmp(ctl->dst_login, u->ut_user, sizeof(u->ut_user)) != 0)
continue;
num_ttys++;
}
}
- endutent();
+ endutxent();
if (num_ttys == 0)
errx(EXIT_FAILURE, _("%s is not logged in"), ctl->dst_login);
if (valid_ttys == 0) {
*/
static void do_write(const struct write_control *ctl)
{
- char *login, *pwuid, timestamp[6];
+ char *login, *pwuid;
struct passwd *pwd;
time_t now;
struct tm *tm;
now = time((time_t *)NULL);
tm = localtime(&now);
- strftime(timestamp, sizeof(timestamp), "%H:%M", tm);
/* print greeting */
printf("\r\n\a\a\a");
if (strcmp(login, pwuid))
- printf(_("Message from %s@%s (as %s) on %s at %s ..."),
- login, host, pwuid, ctl->src_tty_name, timestamp);
+ printf(_("Message from %s@%s (as %s) on %s at %02d:%02d ..."),
+ login, host, pwuid, ctl->src_tty_name,
+ tm->tm_hour, tm->tm_min);
else
- printf(_("Message from %s@%s on %s at %s ..."),
- login, host, ctl->src_tty_name, timestamp);
+ printf(_("Message from %s@%s on %s at %02d:%02d ..."),
+ login, host, ctl->src_tty_name,
+ tm->tm_hour, tm->tm_min);
free(host);
printf("\r\n");
int main(int argc, char **argv)
{
- int tty_writeable = 0, src_fd, c;
+ int tty_writeable = 0, c;
struct write_control ctl = { 0 };
static const struct option longopts[] = {
printf(UTIL_LINUX_VERSION);
return EXIT_SUCCESS;
case 'h':
- usage(stdout);
+ usage();
default:
- usage(stderr);
+ errtryhelp(EXIT_FAILURE);
}
- /* check that sender has write enabled */
- if (isatty(STDIN_FILENO))
- src_fd = STDIN_FILENO;
- else if (isatty(STDOUT_FILENO))
- src_fd = STDOUT_FILENO;
- else if (isatty(STDERR_FILENO))
- src_fd = STDERR_FILENO;
- else
- src_fd = -1;
-
- if (src_fd != -1 &&
- get_terminal_name(src_fd, &ctl.src_tty_path, &ctl.src_tty_name, NULL) == 0) {
+ if (get_terminal_name(&ctl.src_tty_path, &ctl.src_tty_name, NULL) == 0) {
+ /* check that sender has write enabled */
if (check_tty(ctl.src_tty_path, &tty_writeable, NULL, 1))
exit(EXIT_FAILURE);
if (!tty_writeable)
do_write(&ctl);
break;
default:
- usage(stderr);
+ errtryhelp(EXIT_FAILURE);
}
free(ctl.dst_tty_path);
return EXIT_SUCCESS;