home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / finger / part02 / uptime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-03  |  1.9 KB  |  94 lines

  1. /*
  2.  * uptime.c -- /usr/ucb/uptime replacement for USG systems
  3.  *
  4.  * Copyright (C) 1989, 1990  Philip L. Budne
  5.  *
  6.  * This file is part of "Phil's Finger Program".
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 1, or (at your option)
  11.  * any later version.
  12.  *
  13.  */
  14.  
  15. # ifndef lint
  16. static char *rcsid = "$Id: uptime.c,v 3.0 90/07/06 13:12:10 budd Rel $";
  17. # endif /* lint not defined */
  18.  
  19. # include <stdio.h>
  20. # include <sys/types.h>
  21. # include <time.h>
  22. # include <utmp.h>
  23.  
  24. main() {
  25.     struct utmp *ut;
  26.     time_t boot, now;
  27.     struct tm *tm;
  28.     int users, hour, min, day, items;
  29.     char am;
  30.  
  31.     users = boot = 0;
  32.     setutent();
  33.     while( (ut = getutent()) )
  34.     switch( ut->ut_type ) {
  35.     case BOOT_TIME:
  36.         boot = ut->ut_time;
  37.         break;
  38.     case USER_PROCESS:
  39.         users++;
  40.         break;
  41.     }
  42.  
  43.     time( &now );
  44.     tm = localtime( &now );
  45.  
  46.     hour = tm->tm_hour;
  47.     am = 'a';
  48.     if( hour == 0 )            /* fix midnight */
  49.     hour = 12;
  50.     else if( hour > 11 ) {        /* after 11AM */
  51.     am = 'p';            /* its after-noon */
  52.     if( hour > 12 )            /* but don't touch 12!! */
  53.         hour -= 12;
  54.     } /* pm */
  55.     printf("%2d:%02d%cm  ", hour, tm->tm_min, am );
  56.  
  57.     items = 0;
  58.     if( boot != 0 ) {
  59.     int min, day;
  60.     boot = now - boot;        /* get time since boot */
  61.     boot /= 60;            /* get minutes */
  62.  
  63.     min = boot % 60;
  64.     boot /= 60;            /* toss mins */
  65.  
  66.     hour = boot % 24;        /* get hours */
  67.     boot /= 24;
  68.  
  69.     day = boot % 7;
  70.     boot /= 7;
  71.  
  72.     printf("up ");
  73.  
  74. # define PUTF(what,n)\
  75.     { if( items++ > 0 ) { putchar(','); putchar(' '); } \
  76.     printf("%d %s", n, what); if( n != 1 ) putchar('s'); }
  77.  
  78.     if( boot > 0 )
  79.         PUTF("week", boot );
  80.  
  81.     if( day > 0 )
  82.         PUTF("day", day );
  83.  
  84.     if( hour > 0 )
  85.         PUTF("hour", hour );
  86.  
  87.     if( min > 0 )
  88.         PUTF("minute", min );
  89.     } /* have boot */
  90.  
  91.     PUTF("user", users );
  92.     putchar('\n');
  93. }
  94.