int main(int argc, char *argv[]) { char tty[PATH_MAX], *mytty, *cp; int msgsok, myttyfd; time_t atime; uid_t myuid; /* check that sender has write enabled */ if (isatty(fileno(stdin))) myttyfd = fileno(stdin); else if (isatty(fileno(stdout))) myttyfd = fileno(stdout); else if (isatty(fileno(stderr))) myttyfd = fileno(stderr); else errx(1, "can't find your tty"); if (!(mytty = ttyname(myttyfd))) errx(1, "can't find your tty's name"); if ((cp = strrchr(mytty, '/'))) mytty = cp + 1; if (term_chk(mytty, &msgsok, &atime, 1)) exit(1); if (!msgsok) warnx("you have write permission turned off"); myuid = getuid(); /* check args */ switch (argc) { case 2: search_utmp(argv[1], tty, sizeof tty, mytty, myuid); do_write(tty, mytty, myuid); break; case 3: if (!strncmp(argv[2], _PATH_DEV, sizeof(_PATH_DEV) - 1)) argv[2] += sizeof(_PATH_DEV) - 1; if (utmp_chk(argv[1], argv[2])) errx(1, "%s is not logged in on %s", argv[1], argv[2]); if (term_chk(argv[2], &msgsok, &atime, 1)) exit(1); if (myuid && !msgsok) errx(1, "%s has messages disabled on %s", argv[1], argv[2]); do_write(argv[2], mytty, myuid); break; default: (void)fprintf(stderr, "usage: write user [ttyname]\n"); exit(1); } done(0); /* NOTREACHED */ return (0); }
int main(int argc, char **argv) { time_t atime; uid_t myuid; int msgsok, mymsgok, myttyfd; char tty[MAXPATHLEN], *mytty; setlocale(LC_CTYPE, ""); /* check that sender has write enabled */ if (isatty(fileno(stdin))) myttyfd = fileno(stdin); else if (isatty(fileno(stdout))) myttyfd = fileno(stdout); else if (isatty(fileno(stderr))) myttyfd = fileno(stderr); else errx(1, "can't find your tty"); if (!(mytty = ttyname(myttyfd))) errx(1, "can't find your tty's name"); if (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV))) mytty += strlen(_PATH_DEV); if (term_chk(mytty, &mymsgok, &atime, 1)) exit(1); if (!mymsgok) warnx("you have write permission turned off (man mesg)"); myuid = getuid(); /* check args */ switch (argc) { case 2: search_utmp(argv[1], tty, sizeof tty, mytty, myuid); do_write(tty, mytty, myuid, &mymsgok); break; case 3: if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV))) argv[2] += strlen(_PATH_DEV); if (utmp_chk(argv[1], argv[2])) errx(1, "%s is not logged in on %s", argv[1], argv[2]); if (term_chk(argv[2], &msgsok, &atime, 1)) exit(1); if (myuid && !msgsok) errx(1, "%s has messages disabled on %s", argv[1], argv[2]); do_write(argv[2], mytty, myuid, &mymsgok); break; default: usage(); } done(0); return (0); }