Hi,
my previous version was wrong, sorry, it didn't compile. A compiling one is
attached.
> > > 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
>
> prependate() { s=$(date +"${1:-%X %x: }"); sed -e "s,^,$s," ; }
this won't work, the date stays always the same and output isn't flushed.
The following function would do the work:
prependate()
{
while true; do
read line
s=$(date +"${1:-%X %x: }")
echo "$line" | awk "{ sub(\"^\", \"$s\"); print; fflush }"
done
}
Anyway, with comments, error handling, proper indenting, etc., you won't end
up with thaat much less lines than in C.
Regards, Julian
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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(EXIT_FAILURE, "asprintf");
}
} else {
if (asprintf(&format, "%%x %%X: ") <= 0)
err(EXIT_FAILURE, "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(EXIT_FAILURE, "strftime");
if (snprintf(oline, sizeof oline - 1, "%s%s", fline, line) <= 0)
err(EXIT_FAILURE, "snprintf");
fputs(oline, stdout);
fflush(stdout);
}
return EXIT_SUCCESS;
}
Attachment:
signature.asc
Description: PGP signature