Index: cat.1 =================================================================== RCS file: /usr/ncvs/src/bin/cat/cat.1,v retrieving revision 1.25 diff -u -r1.25 cat.1 --- cat.1 16 Jan 2005 16:41:55 -0000 1.25 +++ cat.1 4 Sep 2006 19:17:26 -0000 @@ -41,6 +41,7 @@ .Sh SYNOPSIS .Nm .Op Fl benstuv +.Op Fl D Ar date_format .Op Ar .Sh DESCRIPTION The @@ -73,6 +74,16 @@ .Bl -tag -width indent .It Fl b Number the non-blank output lines, starting at 1. +.It Fl D Ar date_format +Prefix each output line with a date and/or time as specified by +.Ar date_format . +The +.Ar date_format +argument may contain any of the conversion specifications described in the +.Xr strftime 3 +manual page, as well as any arbitrary text. +Note that no separator is inserted between the date/time and the +remainder of the output line. .It Fl e Display non-printing characters (see the .Fl v @@ -152,6 +163,18 @@ when it encountered the first .Ql \&- operand. +.Pp +The command: +.Pp +.Dl "command | cat -D '%F %T '" +.Pp +will prepend the current date/time in the form 'YYYY-MM-DD HH:MM:SS' and +a space to each line of the stdout from +.Nm command . +This is useful for timestamping output. +Note that +.Nm command +must have unbuffered or line-buffered output for this to work. .Sh SEE ALSO .Xr head 1 , .Xr more 1 , @@ -160,7 +183,8 @@ .Xr tail 1 , .Xr vis 1 , .Xr zcat 1 , -.Xr setbuf 3 +.Xr setbuf 3 , +.Xr strftime 3 .Rs .%A Rob Pike .%T "UNIX Style, or cat -v Considered Harmful" @@ -175,7 +199,7 @@ specification. .Pp The flags -.Op Fl benstv +.Op Fl bDenstv are extensions to the specification. .Sh HISTORY A @@ -199,3 +223,9 @@ or .Fl v option is in effect. +.Pp +The +.Nm +utility does not correctly handle multibyte characters in the +argument to +.Fl D . Index: cat.c =================================================================== RCS file: /usr/ncvs/src/bin/cat/cat.c,v retrieving revision 1.32 diff -u -r1.32 cat.c --- cat.c 10 Jan 2005 08:39:20 -0000 1.32 +++ cat.c 4 Sep 2006 19:11:50 -0000 @@ -67,6 +67,7 @@ int bflag, eflag, nflag, sflag, tflag, vflag; int rval; const char *filename; +const char *datefmt; static void usage(void); static void scanfiles(char *argv[], int cooked); @@ -84,11 +85,14 @@ setlocale(LC_CTYPE, ""); - while ((ch = getopt(argc, argv, "benstuv")) != -1) + while ((ch = getopt(argc, argv, "bD:enstuv")) != -1) switch (ch) { case 'b': bflag = nflag = 1; /* -b implies -n */ break; + case 'D': + datefmt = optarg; + break; case 'e': eflag = vflag = 1; /* -e implies -v */ break; @@ -112,7 +116,8 @@ } argv += optind; - if (bflag || eflag || nflag || sflag || tflag || vflag) + if (bflag || eflag || nflag || sflag || tflag || vflag || + datefmt != NULL) scanfiles(argv, 1); else scanfiles(argv, 0); @@ -177,6 +182,8 @@ cook_cat(FILE *fp) { int ch, gobble, line, prev; + char datebuf[1024]; + time_t now; /* Reset EOF condition on stdin. */ if (fp == stdin && feof(stdin)) @@ -198,6 +205,15 @@ if (ferror(stdout)) break; } + if (datefmt != NULL) { + time(&now); + if (strftime(datebuf, sizeof(datebuf), datefmt, + localtime(&now)) == 0) + err(1, "Date format too large"); + (void)fputs(datebuf, stdout); + if (ferror(stdout)) + break; + } } if (ch == '\n') { if (eflag && putchar('$') == EOF)