home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mm / mm-0.90 / usage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  2.1 KB  |  104 lines

  1. /*
  2.  * Copyright (c) 1986, 1990 by The Trustees of Columbia University in
  3.  * the City of New York.  Permission is granted to any individual or
  4.  * institution to use, copy, or redistribute this software so long as it
  5.  * is not sold for profit, provided this copyright notice is retained.
  6.  */
  7.  
  8. #ifndef lint
  9. static char *rcsid = "$Header: /f/src2/encore.bin/cucca/mm/tarring-it-up/RCS/usage.c,v 2.1 90/10/04 18:26:58 melissa Exp $";
  10. #endif
  11.  
  12. #include <stdio.h>
  13. #include <sys/types.h>
  14. #include <sys/times.h>
  15. #include <pwd.h>
  16. #include <grp.h>
  17.  
  18. static long u_begtime= -1, u_endtime= -1;
  19. static time_t cpu_time = 0;
  20. char *whoami(), *ctime();
  21. static char *usertype(), *pbtime(), *petime(), *pxtime(), *grpnam();
  22.  
  23. usage_start() 
  24. {
  25.     u_begtime = time(0);
  26. }
  27.  
  28. usage_stop(logfile)
  29. {
  30.     FILE *fp;
  31.     struct tms buffer;
  32.     time_t new_time;
  33.  
  34.     if (u_begtime == -1)
  35.     return;
  36.     fp = fopen(logfile, "a");
  37.     if (fp == NULL)
  38.     return;
  39.     u_endtime = time(0);
  40.     times(&buffer);            /* get cpu usage */
  41.     new_time = buffer.tms_utime + buffer.tms_stime;
  42.     fprintf(fp, "%-8s %-8s (pid %5d) %s -- %s (%s) [%0.1fs]\n", whoami(),
  43.         usertype(whoami()), getpid(),
  44.         pbtime(u_begtime), petime(u_endtime), pxtime(u_endtime-u_begtime),
  45.         (float) (new_time - cpu_time)/60);
  46.     cpu_time = new_time;        /* save the most recent one */
  47.     fclose(fp);
  48. }
  49.  
  50. static char*
  51. usertype(uname) {
  52.     struct passwd *p;
  53.     
  54.     p = getpwnam(uname);
  55.     if (p == NULL) return(grpnam(getgid()));
  56.     return(grpnam(p->pw_gid));
  57. }
  58.  
  59. static char *
  60. grpnam(gid) {
  61.     struct group *g;
  62.     static char buf[20];
  63.     g = getgrgid(gid);
  64.     if (g != NULL)
  65.     return(g->gr_name);
  66.     sprintf(buf,"%d", gid);
  67.     return(buf);
  68. }
  69.  
  70. static char *
  71. pbtime(t)
  72. long t;
  73. {
  74.     static char buf[26];
  75.  
  76.     strcpy (buf,ctime(&t));
  77.     buf[16] = '\0';
  78.     return(buf);
  79. }
  80.  
  81. static char *
  82. petime(t)
  83. long t;
  84. {
  85.     static char buf[26];
  86.  
  87.     strcpy (buf,ctime(&t));
  88.     buf[16] = '\0';
  89.     return(buf+11);
  90. }
  91.  
  92. static char *
  93. pxtime(t)
  94. long t;
  95. {
  96.     static char buf[20];
  97.     int hr, min, sec;
  98.     hr = t/3600;
  99.     min = (t % 3600)/60;
  100.     sec = t % 60;
  101.     sprintf(buf,"%d:%02d:%02d", hr, min, sec);
  102.     return(buf);
  103. }
  104.