Plan 9 from Bell Labs’s /usr/web/sources/contrib/akumar/relic/v8/byteyears.c

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


/*
 *	byte-years --
 *	the size of a file times the length of time since
 *	last modified, expressed in years.
 *	Also print size, date, name
 */
#include <u.h>
#include <libc.h>

#define YEAR (24L*60L*60L*365L)

long now, year;
int aflag, errflag;

int
consider(char *name, Dir *d)
{
	double by;
	char *p;
	long t;

	t = aflag? d->atime: d->mtime;
	by = (double) d->length * (double) (now - t) / YEAR;

	print ("%12.0f %10d", by + 0.5, d->length);
	p = ctime (t);
	if (t < year)
		print(" %-7.7s %-4.4s ", p+4, p+20);
	else
		print(" %-12.12s ", p+4);
	print ("%s\n", name);

	return 0;
}

int
check(char *s, int (*by) (char*, Dir*))
{
	Dir *d;
	int fd, rc;
	int n, i;

	d = dirstat (s);
	if (d == nil) {
		perror (s);
		return -1;
	}

	if ((d->mode&DMDIR) == 0)
		return by (s, d);

	if ((fd = open (s, OREAD)) == -1) {
		perror (s);
		return -1;
	}

	if ((n = dirreadall (fd, &d)) < 0) {
		perror (s);
		return -1;
	}

	for (i = 0; i < n; i++)
		if ((rc = by ((d+i)->name, d+i)) < 0)
			return rc;		/* in case */

	close (fd);

	return 0;
}
	
int
main (int argc, char **argv)
{
	int i;
	int rc = 1;

	ARGBEGIN {

		case 'a':
			aflag++;
			break;

		case '?':
			errflag++;
	} ARGEND

	if (errflag) {
		fprint (2, "usage: byteyears [-a] [file]...\n");
		exits ("usage");
	}

	time (&now);
	year = now - (YEAR/2);

	if (!argc) {
		if (check (".", consider) < 0) {
			fprint (2, "cannot access current directory\n");
			rc++;
		}
	} else {
		for (i = 0; i < argc; i++) {
			if (check (argv[i], consider) < 0) {
				fprint (2, "cannot access %s\n", argv[i]);
				rc++;
			}
		}
	}
	return rc;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.