+ struct acct acct;
+ struct rusage *r;
+ int rv;
+ long i;
+ u_int cnt;
+ char *c;
+ comp_t int2comp();
+
+
+ if (acctp == NULL) /* accounting not turned on */
+ return;
+
+ /* Step 1. Get command name (remove path if necessary) */
+
+ strncpy(acct.ac_comm, p->p_comm, sizeof(acct.ac_comm));
+
+ /* Step 2. Get rest of information */
+
+ acct.ac_utime = int2comp((unsigned) p->p_utime.tv_sec * 1000000 + p->p_utime.tv_usec);
+ acct.ac_stime = int2comp((unsigned) p->p_stime.tv_sec * 1000000 + p->p_stime.tv_usec);
+ acct.ac_btime = p->p_stats->p_start.tv_sec;
+ /* elapse time = current - start */
+ i = (time.tv_sec - p->p_stats->p_start.tv_sec) * 1000000 +
+ (time.tv_usec - p->p_stats->p_start.tv_usec);
+ acct.ac_etime = int2comp((unsigned) i);
+
+ acct.ac_uid = p->p_cred->p_ruid;
+ acct.ac_gid = p->p_cred->p_rgid;
+
+ r = &p->p_stats->p_ru;
+ if (i = (p->p_utime.tv_sec + p->p_stime.tv_sec) * hz +
+ (p->p_utime.tv_usec + p->p_stime.tv_usec) / tick)
+ acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / i;
+ else
+ acct.ac_mem = 0;
+ acct.ac_io = int2comp((unsigned) (r->ru_inblock + r->ru_oublock) * 1000000);
+
+ if ((p->p_flag & SCTTY) && p->p_pgrp->pg_session->s_ttyp)
+ acct.ac_tty = p->p_pgrp->pg_session->s_ttyp->t_dev;
+ else
+ acct.ac_tty = NODEV;
+ acct.ac_flag = p->p_acflag;
+
+ /* Step 3. Write record to file */
+
+
+ rv = vn_rdwr(UIO_WRITE, acctp, (caddr_t) &acct, sizeof (acct),
+ (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, p->p_ucred, (int *) NULL,
+ p);
+}
+
+/* int2comp converts from ticks in a microsecond to ticks in 1/AHZ second
+ *
+ * comp_t is a psuedo-floating point number with 13 bits of
+ * mantissa and 3 bits of base 8 exponent and has resolution
+ * of 1/AHZ seconds.
+ *
+ * notice I already converted the incoming values into microseconds
+ * I need to convert back into AHZ ticks.
+ */
+
+/* Mark Tinguely (tinguely@plains.NoDak.edu) 8/10/93 */
+
+
+#define RES 13
+#define EXP 3
+#define MAXFRACT 1<<RES
+
+comp_t
+int2comp(mantissa)
+unsigned int mantissa;
+{
+ comp_t exp=0;
+
+ mantissa = mantissa * AHZ / 1000000; /* convert back to AHZ ticks */
+ while (mantissa > MAXFRACT) {
+ mantissa >>= EXP; /* base 8 exponent */
+ exp++;
+ }
+ exp <<= RES; /* move the exponent */
+ exp += mantissa; /* add on the manissa */
+ return (exp);