/*
 * idle.c -- output time in minutes of last write to device file.
 *
 * Designed to report the time in minutes of the last write to the provided
 * device file.  Can be used on device files used by interactive processes to
 * determine how long they have been unused.  This was written back in 1995 or
 * so when resources were not as plentiful, and system administrator routinely
 * killed unused interactive processes (shells, et cetera).  This could be used
 * in conjunction with a shell script to automatically kill processes on unused
 * ttys/ptys.
 *
 * This and other hacks can be found at: http://oddgeek.info/
 *
 * Copyright (c) 2005 Jason A. Dour
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *     1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software in
 *     a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *     2. Altered source versions must be plainly marked as such, and must not
 *     be misrepresented as being the original software.
 *
 *     3. This notice may not be removed or altered from any source
 *     distribution.
 *
 */

/*
 * Version Information
 *
 * 1.0	2005.05.25
 * 
 * 	First public release.  Nothing really changed other than comments and
 * 	adding one include to ensure a -Wall returns no warnings.
 *
 * primordial ooze
 *
 * 	Used for years privately on various systems.  Given to a few others
 * 	privately when they had need of it.  Never distributed widely.
 *
 */

/*
 *
 * ORIGINAL DISCLAIMER TEXT FROM A DECADE AGO...
 *
 * This software comes with no guarantees implicit or implied, and the
 * author(s) of this software cannot be held responsible for loss, damage, acts
 * of god(s), large amounts of small rodentia, deafness, plague, baldness, or
 * nose-bleeds occurring as a direct -- or indirect -- result of the use of
 * this MotherSoft product.  This software is to be used for MOTHERing,
 * weirdness, taking care of animals, peace, love, and spreading genuine
 * feelings of well being.  All other uses are denounced by the author(s).
 *
 * Love, Peace, Gerbils, & Hair Grease,
 * Jason A. Dour
 *
 */

/*
 * Necessary includes.
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <sys/stat.h>
#include <time.h>

/*
 * Usage string.
 */
#define USAGE "usage: idle [-v] <device_filename>\n"

/*
 * MAIN
 */
int main (int argc, char **argv) {
	/* Argument handling loop counter. */
	int	args = 0;
	/* Verbose argument flag. */
	int	verbose = 0;
	/* Current time. */
	time_t	now;
	/* Stats on provided device file. */
	struct	stat	buffer;
	/* Information on the owner of the provided device file.*/
	struct	passwd	*pwd;

	/* Argument checking. */
	if ( argc < 2 ) {
		/* Improper args. */
		printf(USAGE);
		exit(1);
	}

	/* Argument checking. */
	if ( argc == 2 && strcmp("-v",argv[1]) == 0 ) {
		/* Improper args. */
		printf(USAGE);
		exit(1);
	}

	/* Check all args for verbose flag. */
	while ( argv[++args] != NULL ) {
		/* Set verbose flag if requested. */
		if ( strcmp("-v",argv[args]) == 0 )
			verbose = 1;
	}

	/* Initialize and begin loop over arguments. */
	args = 0;
	while ( argv[++args] != NULL ) {
		/* If argument is a device file... */
		if ( strcmp("-v",argv[args]) ) {
			/* Gather stat on the device file. */
			if ( stat(argv[args],&buffer) ) {
				/* Could not gather stat. */
				printf("CANNOT STAT FILE! (%s)\n",argv[args]);
				exit(2);
			}

			/* Get the current time. */
			time(&now);

			/* Check whether verbose output required... */
			if ( verbose ) {
				/* Verbose output, so... */

				/* Gather user information. */
				pwd = getpwuid(buffer.st_uid);

				/* Output information. */
				printf("%s has been idle on %s for %.2f minutes.\n",
						pwd->pw_name,argv[args],( (now - buffer.st_atime) / 60.0 ) );
			} else {
				/* Normal/Terse output, so... */

				/* Output information. */
				printf("%.2f\n", ( (now - buffer.st_atime) / 60.0 ) );
			}
		}
	}

	/* We're done.  Time for a beer. */
	exit(0); 
}
