
/* ----------------------------------------------------------------------
 * FILE: $HOME/.rhrc
 * VERSION: 2
 * This file is a sample .rhrc that should live in your home
 * directory.
 * The contents of this file are read BEFORE any other input is read
 * from the user. Functions defined here will be useable elsewhere.
 *
 */

/*
 * The dir() function tests a file to see if it is a directory.
 *	The command "rh -e dir", would be enough to find just directories
 *
 */

dir()
{
	return( (mode & IFMT) == IFDIR );
}

/*
 * months - This function can be used like a constant.
 *	It evaluates to the number of seconds in a month (average).
 *	Expressions like the following are now possible:
 *		(mtime > NOW-2*months)
 *	Would find files that have been modified in the last 2
 *	months.
 *
 */

months
{
	return days*30;
}

/*
 * This defines a useful "alias" for the 'nlink' variable,
 * which may be easier to remember.
 *
 */

nlinks { return nlink; }

/*
 * This function returns the number of seconds passed as its argument
 * minus NOW. Used to make some time comparisons "cleaner".
 *
 */

ago(d)
{
	return( NOW - d );
}

/*
 * returns true if a file is writable by others (and group).
 *
 */

writable()
{
	return mode & 022;
}

MINE
{
	return( uid == $$ );
}

/*
 * This is my "bad" function which can be easily invoked by the command:
 *	% rh -le bad
 * This allows me to find files that are not a good idea to have around:
 *	- core files.
 *	- old a.out files.
 *	- files writeable by other people.
 *	- checkpoint and backup files.
 *
 */

bad()
{
	return( "core" || ("a.out" && mtime <= ago(2*days) ) ||
		"*.BAK" || "*.CKP" || writable);
}

/*
 * Find C related files.
 *
 */

csrc()
{
	return("*.c" || "*.h" || "[Mm]akefile" );
}

/*
 * Find files that have been modified in the last
 * 1 hour.
 *
 */

changed()
{
	return( mtime > NOW-1*hours );

	/* ALTERNATELY:
	 * (using the ago() function)
	 *
	 * 	return( mtime > ago(1*hours) );
	 *
	 */
}

/*
 * This function can be used as a constant.
 *
 */

K()
{
	return 1024;
}

/* megs { return K*K; }  or ...*/
megs { return K<<10; }

/*
 * This function shows that recursion is quite possible.
 * Call this function with any number, the higher the number
 * deeper in recursion it goes. (for large values this function
 * overflows the stack). This can be used to impose a delay, but that
 * would be useless on my machine, cause it is sooooo slow already.
 * Neat mathamatical functions can be calculated this way.
 *
 */

recursion(n)
{
	return (n>0) ? recursion(n-1) : 0;
}

/*
 * sqrt(n), returns the integer square root of the number n.
 *	Useful for expressions like:
 *		sqrt(uid) <= gid;
 *	Which finds files where the square root of the user-id is
 *	less than the gid. This one will be used rarely if ever.
 *	sqrt1() is a helper function.
 *
 */

sqrt1(n,odd) { return( (n <= 0 ) ? 0 : sqrt1(n-odd,odd+2)+1 ); }
sqrt(n) { return( sqrt1(n,1) ); }

fact(n) { return (n<=0) ? 1 : fact(n-1)*n; }
