tech-userlevel archive

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

Re: 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).
as obviously nobody wanted this solution (and maintaining my own write is
useless), I've done something different. Maybe it's of use for somebody...

Compile with
  cc -o prependate prependate.c
then call
  prependate | write $user
And have every message you write prepended with the date string "%X %x: ". If
you want to have your own format string, use
  prependate "$formatstr" | write $user
where formatstr is anything accepted by strftime.

Imho all in all nicer solution - it's reusable, has less than 50 lines of
code, needs no unnecessarily high permissions to run.

Regards, Julian
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

/*
 * Wait for input via fgets(2), and if entered, prepend the given string with a 
 * user-specified format string.
 */
int
main(int argc, char *argv[])
{
        struct tm *now;
        time_t nowt;
        char line[BUFSIZ], fline[128], oline[BUFSIZ];
        char *format;

        if (argc == 2) {
                if (strcmp(argv[1],"-h") == 0) {
                        printf("usage: prependate <format_string>\n");
                        exit(EXIT_FAILURE);
                } else {
                        if (asprintf(&format, "%s", argv[1]) <= 0)
                                err("asprintf");
                }
        } else {
                if (asprintf(&format, "%%x %%X: ") <= 0)
                        err("asprintf");
        }

        oline[sizeof oline - 1] = '\0';
        while (fgets(line, sizeof line - sizeof fline, stdin) != NULL) {
                nowt = time(NULL);
                now = localtime(&nowt);
                if (strftime(fline, sizeof fline, format, now) <= 0)
                        err(1, "strftime");
                if (snprintf(oline, sizeof oline - 1, "%s%s", fline, line) <= 0)
                        err(1, "snprintf");
                fputs(oline, stdout);
                fflush(stdout);
        }

        return EXIT_SUCCESS;
}

Attachment: signature.asc
Description: PGP signature



Home | Main Index | Thread Index | Old Index