tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Proposal: Add option to add dates to write(1)



Hi,

I'm using write(1) on machines where people are logged in for days, weeks,
months, running a tmux, as well as their write-sessions.
It's annoying to not know the date when somebody talked to you.

What do you think about the attached patch (write.1 modification would
follow)? It just adds a flag '-t', which prepends the messages being sent
with a date (i.e., you and your communication partner have to agree on using
that flag to have it useful to yourself).

Regards, Julian
Index: write.c
===================================================================
RCS file: /cvsroot/src/usr.bin/write/write.c,v
retrieving revision 1.27
diff -u -r1.27 write.c
--- write.c     6 Sep 2011 18:46:35 -0000       1.27
+++ write.c     17 May 2012 13:02:13 -0000
@@ -65,6 +65,8 @@
 #include "utmpentry.h"
 #include "term_chk.h"
 
+static int senddate = 0;
+
 __dead static void done(int);
 static void do_write(int, const char *, const uid_t);
 static void wr_fputs(char *);
@@ -76,7 +78,7 @@
 {
        time_t atime;
        uid_t myuid, uid;
-       int msgsok, ttyfd;
+       int msgsok, ttyfd, ch;
        char *mytty;
        gid_t saved_egid = getegid();
 
@@ -88,27 +90,39 @@
        mytty = check_sender(&atime, myuid, saved_egid);
 
        /* check args */
+       while ((ch = getopt(argc, argv, "t")) != -1)
+               switch (ch) {
+               case 't':
+                       senddate = 1;
+                       break;
+               default:
+                       (void)fprintf(stderr, "usage: write [-t] user [tty]\n");
+                       exit(1);
+               }
+       argc -= optind;
+       argv += optind;
+
        switch (argc) {
-       case 2:
-               ttyfd = search_utmp(argv[1], mytty, myuid, saved_egid);
+       case 1:
+               ttyfd = search_utmp(argv[0], mytty, myuid, saved_egid);
                break;
-       case 3:
-               if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV)))
-                       argv[2] += strlen(_PATH_DEV);
-               if (uid_from_user(argv[1], &uid) == -1)
-                       errx(1, "%s: unknown user", argv[1]);
-               if (utmp_chk(argv[1], argv[2]))
+       case 2:
+               if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV)))
+                       argv[1] += strlen(_PATH_DEV);
+               if (uid_from_user(argv[0], &uid) == -1)
+                       errx(1, "%s: unknown user", argv[0]);
+               if (utmp_chk(argv[0], argv[1]))
                        errx(1, "%s is not logged in on %s",
-                           argv[1], argv[2]);
-               ttyfd = term_chk(uid, argv[2], &msgsok, &atime, 0, saved_egid);
+                           argv[0], argv[1]);
+               ttyfd = term_chk(uid, argv[1], &msgsok, &atime, 0, saved_egid);
                if (ttyfd == -1)
-                       err(1, "%s%s", _PATH_DEV, argv[2]);
+                       err(1, "%s%s", _PATH_DEV, argv[1]);
                if (myuid && !msgsok)
                        errx(1, "%s has messages disabled on %s",
-                           argv[1], argv[2]);
+                           argv[0], argv[1]);
                break;
        default:
-               (void)fprintf(stderr, "usage: write user [tty]\n");
+               (void)fprintf(stderr, "usage: write [-t] user [tty]\n");
                exit(1);
        }
        if (setgid(getgid()) == -1)
@@ -272,9 +286,18 @@
 wr_fputs(char *s)
 {
        unsigned char c;
+       char *nows;
+       time_t now;
 
 #define        PUTC(c) if (putchar(c) == EOF) goto err;
 
+       if (senddate) {
+               now = time(NULL);
+               nows = ctime(&now);
+               nows[19] = '\0';
+               (void)printf("%s: ", nows + 4);
+       }
+
        for (; *s != '\0'; ++s) {
                c = toascii(*s);
                if (c == '\n') {

Attachment: signature.asc
Description: PGP signature



Home | Main Index | Thread Index | Old Index