Date: Tue, 5 Sep 2006 05:20:06 +1000 From: Peter Jeremy <peterjeremy@optushome.com.au> To: freebsd-current@freebsd.org Subject: Re: suggested addition to 'date' Message-ID: <20060904192006.GA3292@turion.vk2pj.dyndns.org> In-Reply-To: <20060902031247.GE749@turion.vk2pj.dyndns.org> References: <200608281545.k7SFjn6l063922@lurza.secnetix.de> <p06230928c11e2298ca97@[128.113.24.47]> <200609020956.54008.Lucas.James@ldjcs.com.au> <20060902031247.GE749@turion.vk2pj.dyndns.org>
next in thread | previous in thread | raw e-mail | index | archive | help
--MfFXiAuoTsnnDAfZ Content-Type: multipart/mixed; boundary="W/nzBZO5zC0uMSeA" Content-Disposition: inline --W/nzBZO5zC0uMSeA Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, 2006-Sep-02 13:12:48 +1000, I wrote: >The justification for extending date(1) is that it already has the code >to handle date/time stamps. In reality, this code is all in strftime(3) >and cat(1) already has hooks to insert a string at the beginning of a >line so I believe that the attached patch is more appropriate. (Man >page update on request). Following some off-list comments, I have updated the patch to correct a bug, change the option character to 'D' (for date) and provide a man page update. --=20 Peter Jeremy --W/nzBZO5zC0uMSeA Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="cat.diff" Content-Transfer-Encoding: quoted-printable Index: cat.1 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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; =20 static void usage(void); static void scanfiles(char *argv[], int cooked); @@ -84,11 +85,14 @@ =20 setlocale(LC_CTYPE, ""); =20 - while ((ch =3D getopt(argc, argv, "benstuv")) !=3D -1) + while ((ch =3D getopt(argc, argv, "bD:enstuv")) !=3D -1) switch (ch) { case 'b': bflag =3D nflag =3D 1; /* -b implies -n */ break; + case 'D': + datefmt =3D optarg; + break; case 'e': eflag =3D vflag =3D 1; /* -e implies -v */ break; @@ -112,7 +116,8 @@ } argv +=3D optind; =20 - if (bflag || eflag || nflag || sflag || tflag || vflag) + if (bflag || eflag || nflag || sflag || tflag || vflag || + datefmt !=3D 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; =20 /* Reset EOF condition on stdin. */ if (fp =3D=3D stdin && feof(stdin)) @@ -198,6 +205,15 @@ if (ferror(stdout)) break; } + if (datefmt !=3D NULL) { + time(&now); + if (strftime(datebuf, sizeof(datebuf), datefmt, + localtime(&now)) =3D=3D 0) + err(1, "Date format too large"); + (void)fputs(datebuf, stdout); + if (ferror(stdout)) + break; + } } if (ch =3D=3D '\n') { if (eflag && putchar('$') =3D=3D EOF) --W/nzBZO5zC0uMSeA-- --MfFXiAuoTsnnDAfZ Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (FreeBSD) iD8DBQFE/Hxm/opHv/APuIcRApkiAJ9OO1pal026XzhxRCon1H+4YBYtbwCeJG0g /Utfh0q0Ahd4OFS/x6uPvlQ= =4RBn -----END PGP SIGNATURE----- --MfFXiAuoTsnnDAfZ--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060904192006.GA3292>