Plan 9 from Bell Labs’s /usr/web/sources/contrib/lyndon/ruler.c

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


/* ruler [cols] -- print a column-count ruler */

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

void
main (int argc, char **argv)
{

	int cols, rows, i, j, skip;
	char *buf, *p;

	cols = 80;
	if (argc > 1)
		cols = atoi(argv[1]);
	if (cols < 1)
		exits("bad cols");
	buf = malloc(cols + 1);
	if (buf == nil)
		sysfatal("out of memory");

	rows = log10(cols);
	for (i = rows; i >= 0 ; i--) {
		skip = pow(10, i);
		p = buf;
		for (j = 1; j <= cols; j++) {
			if (j % skip)
				*p++ = ' ';
			else
				sprint(p++, "%d", (j / skip) % 10);
		}
		buf[cols] = '\0';
		print("%s\n", buf);
	}
	exits(nil);
}

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].