Plan 9 from Bell Labs’s /usr/web/sources/contrib/anothy/src/cmd/timestamp.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


/* 
 * Original from [email protected], sqweek.dnsdojo.org, sqweek on freenode
 * 2009-01-25
 * Local changes by anthony
 */

#include <u.h>
#include <libc.h>
#include <bio.h>

void
usage(void)
{
	fprint(2, "usage: %s [ -i ] [ -f format ]\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	Biobuf in;
	char *line, *format, *stamp;
	vlong prev, now, delta;
	int iflag;

	iflag = 0;
	format = 0;

	ARGBEGIN{
	case 'i':
		iflag = 1;
		break;
	case 'f':
		format = EARGF(usage());
		break;
	default:
		usage();
	}ARGEND;

	prev = nsec();
	stamp = smprint("");

	Binit(&in, 0, OREAD);
	while(line = Brdline(&in, '\n')){
		now = nsec();
		delta = now - prev;
		line[Blinelen(&in)-1] = '\0';
		if(format != 0){
			int pid;
			pid=fork();
			if(pid == 0){
//print("%s", format);
				execl("/bin/datefmt", "datefmt", smprint("%s", format), nil);
				exits("execl failed");
			}
			else
				wait();
		}
		else
			stamp=smprint("[+%lld.%09lld] ", delta/(vlong)(1000*1000*1000),
				delta%(vlong)(1000*1000*1000));
		print("%s%s\n", stamp, line);
		if(iflag)
			prev = now;
	}

	exits(0);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].