/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred."
 *
 */
/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved.  The Berkeley Software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#ifndef lint
static char *sccsid = "@(#)sh.time.c	5.6 (Berkeley) 5/19/88";
#endif

#include "sh.h"

/*
 * C Shell - routines handling process timing and niceing
 */

settimes()
{
    /*	struct rusage ruch;		   XXX */
	struct process_stats ruch;	/* XXX */

    /*	(void) gettimeofday(&time0, (struct timezone *)0);	   XXX */
    /*	(void) getrusage(RUSAGE_SELF, &ru0);			   XXX */
    /*	(void) getrusage(RUSAGE_CHILDREN, &ruch);		   XXX */
	(void) get_process_stats(&time0, PS_SELF, &ru0, &ruch);	/* XXX */
	ruadd(&ru0, &ruch);
}

/*
 * dotime is only called if it is truly a builtin function and not a
 * prefix to another command
 */
dotime()
{
    /*	struct timeval timedol;		   XXX */
	timeval_t timedol;		/* XXX */
    /*	struct rusage ru1, ruch;	   XXX */
	struct process_stats ru1, ruch;	/* XXX */

    /*	(void) getrusage(RUSAGE_SELF, &ru1);			      XXX */
    /*	(void) getrusage(RUSAGE_CHILDREN, &ruch);		      XXX */
    /*	ruadd(&ru1, &ruch);					      XXX */
	(void) get_process_stats(&timedol, PS_SELF, &ru1, &ruch);  /* XXX */
    	ruadd(&ru1, &ruch);					   /* XXX */
    /*	(void) gettimeofday(&timedol, (struct timezone *)0);	      XXX */
	prusage(&ru0, &ru1, &timedol, &time0);
}

/*
 * donice is only called when it on the line by itself or with a +- value
 */
donice(v)
	register char **v;
{
	printf("\ncsh:sh.time.c:donice(): no setpriority() call\n"); /* XXX */

#ifdef NOTDEF						/* XXX */
	register char *cp;
	int nval;

	v++, cp = *v++;
	if (cp == 0)
		nval = 4;
	else if (*v == 0 && any(cp[0], "+-"))
		nval = getn(cp);
	(void) setpriority(PRIO_PROCESS, 0, nval);
#endif /* NOTDEF */					/* XXX */
}

ruadd(ru, ru2)
    /*	register struct rusage *ru, *ru2;		   XXX */
    	register struct process_stats *ru, *ru2;	/* XXX */
{
   /*	register long *lp, *lp2;			   XXX */
 	register ulong *lp, *lp2;			/* XXX */
	register int cnt;

    /*	tvadd(&ru->ru_utime, &ru2->ru_utime);		   XXX */
	tvadd(&ru->ps_utime, &ru2->ps_utime);		/* XXX */
    /*	tvadd(&ru->ru_stime, &ru2->ru_stime);		   XXX */
	tvadd(&ru->ps_stime, &ru2->ps_stime);		/* XXX */
    /*	if (ru2->ru_maxrss > ru->ru_maxrss)		   XXX */
    /*		ru->ru_maxrss = ru2->ru_maxrss;		   XXX */
	if (ru2->ps_maxrss > ru->ps_maxrss)		/* XXX */
		ru->ps_maxrss = ru2->ps_maxrss;		/* XXX */

	/*
	 * XXX   Object of this code is to add all elements of the
	 * XXX   structures.  All remaining elements are ulong.
	 */

    /*	cnt = &ru->ru_last - &ru->ru_first + 1;		   XXX */
	cnt = &ru->ps_phwrite - &ru->ps_pagein + 1;	/* XXX */
    /*	lp = &ru->ru_first; lp2 = &ru2->ru_first;	   XXX */
	lp = &ru->ps_pagein; lp2 = &ru2->ps_pagein;	/* XXX */
	do
		*lp++ += *lp2++;
	while (--cnt > 0);

	


}

prusage(r0, r1, e, b)
    /*	register struct rusage *r0, *r1;		   XXX */
    	register struct process_stats *r0, *r1;		/* XXX */
    /*	struct timeval *e, *b;				   XXX */
	timeval_t *e, *b;				/* XXX */
{
    /*	register time_t t =						XXX */
    /*	    (r1->ru_utime.tv_sec-r0->ru_utime.tv_sec)*100+		XXX */
    /*	    (r1->ru_utime.tv_usec-r0->ru_utime.tv_usec)/10000+		XXX */
    /*	    (r1->ru_stime.tv_sec-r0->ru_stime.tv_sec)*100+		XXX */
    /*	    (r1->ru_stime.tv_usec-r0->ru_stime.tv_usec)/10000;		XXX */
	register time_t t =
	    (r1->ps_utime.tv_sec-r0->ps_utime.tv_sec)*100+
	    (r1->ps_utime.tv_usec-r0->ps_utime.tv_usec)/10000+
	    (r1->ps_stime.tv_sec-r0->ps_stime.tv_sec)*100+
	    (r1->ps_stime.tv_usec-r0->ps_stime.tv_usec)/10000;
	register char *cp;
	register long i;
	register struct varent *vp = adrof("time");
	long ms =
	    (e->tv_sec-b->tv_sec)*100 + (e->tv_usec-b->tv_usec)/10000;

    /*	cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";		   XXX */
	cp = "%Uu %Ss %E %P %I+%Oio %Fpf+%Ww";			/* XXX */
	if (vp && vp->vec[0] && vp->vec[1])
		cp = vp->vec[1];
	for (; *cp; cp++)
	if (*cp != '%')
		cshputchar(*cp);
	else if (cp[1]) switch(*++cp) {

	case 'U':
	    /*	pdeltat(&r1->ru_utime, &r0->ru_utime);		   XXX */
	    	pdeltat(&r1->ps_utime, &r0->ps_utime);		/* XXX */
		break;

	case 'S':
	    /*	pdeltat(&r1->ru_stime, &r0->ru_stime);		   XXX */
		pdeltat(&r1->ps_stime, &r0->ps_stime);		/* XXX */
		break;

	case 'E':
		psecs(ms / 100);
		break;

	case 'P':
		printf("%d%%", (int) (t*100 / ((ms ? ms : 1))));
		break;

	case 'W':
	    /*	i = r1->ru_nswap - r0->ru_nswap;		   XXX */
		i = r1->ps_swap - r0->ps_swap;			/* XXX */
		printf("%ld", i);
		break;

#ifdef NOTDEF							/* XXX */
	case 'X':
		printf("%ld", t == 0 ? 0L : (r1->ru_ixrss-r0->ru_ixrss)/t);
		break;

	case 'D':
		printf("%ld", t == 0 ? 0L :
		    (r1->ru_idrss+r1->ru_isrss-(r0->ru_idrss+r0->ru_isrss))/t);
		break;

	case 'K':
		printf("%ld", t == 0 ? 0L :
		    ((r1->ru_ixrss+r1->ru_isrss+r1->ru_idrss) -
		    (r0->ru_ixrss+r0->ru_idrss+r0->ru_isrss))/t);
		break;
#endif /* NOTDEF */						/* XXX */

	case 'M':
	    /*	printf("%ld", r1->ru_maxrss/2);			   XXX */
		printf("%ld", r1->ps_maxrss/2);			/* XXX */
		break;

	case 'F':
	    /*	printf("%ld", r1->ru_majflt-r0->ru_majflt);	   XXX */
		printf("%ld", r1->ps_pagein-r0->ps_pagein);	/* XXX */
		break;

	case 'R':
	    /*	printf("%ld", r1->ru_minflt-r0->ru_minflt);	   XXX */
		printf("%ld", r1->ps_reclaim-r0->ps_reclaim);	/* XXX */
		break;

	case 'I':
	    /*	printf("%ld", r1->ru_inblock-r0->ru_inblock);	   XXX */
		printf("%ld", r1->ps_bread-r0->ps_bread);	/* XXX */
		break;

	case 'O':
	    /*	printf("%ld", r1->ru_oublock-r0->ru_oublock);	   XXX */
		printf("%ld", r1->ps_bwrite-r0->ps_bwrite);	/* XXX */
		break;
	}
	cshputchar('\n');
}

pdeltat(t1, t0)
    /*	struct timeval *t1, *t0;		   XXX */
	timeval_t *t1, *t0;			/* XXX */
{
    /*	struct timeval td;			   XXX */
	timeval_t td;				/* XXX */

	tvsub(&td, t1, t0);
	printf("%d.%01d", td.tv_sec, td.tv_usec/100000);
}

tvadd(tsum, t0)
    /*	struct timeval *tsum, *t0;		   XXX */
	timeval_t *tsum, *t0;			/* XXX */
{

	tsum->tv_sec += t0->tv_sec;
	tsum->tv_usec += t0->tv_usec;
	if (tsum->tv_usec > 1000000)
		tsum->tv_sec++, tsum->tv_usec -= 1000000;
}

tvsub(tdiff, t1, t0)
    /*	struct timeval *tdiff, *t1, *t0;	   XXX */
	timeval_t *tdiff, *t1, *t0;		/* XXX */
{

	tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
	tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
	if (tdiff->tv_usec < 0)
		tdiff->tv_sec--, tdiff->tv_usec += 1000000;
}
