/[machmon]/machmon/linux/source/time.c
ViewVC logotype

Diff of /machmon/linux/source/time.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by pbastos, Mon Jul 8 17:11:57 2002 UTC revision 1.6 by pbastos, Tue Jul 9 12:38:59 2002 UTC
# Line 42  Line 42 
42    
43  #include <limits.h>  #include <limits.h>
44  #include <string.h>  #include <string.h>
45    #include <utmp.h>
46  #include <unistd.h>  #include <unistd.h>
47    #include <err.h>
48  #include <stdlib.h>  #include <stdlib.h>
49  #include <stdio.h>  #include <stdio.h>
50    
# Line 54  Line 56 
56  #include <bits/posix2_lim.h>  #include <bits/posix2_lim.h>
57  #endif  #endif
58    
59    #ifndef _PATH_UTMP
60    #define _PATH_UTMP "/etc/utmp"
61    #endif
62    
63  struct timeval boottime;  struct timeval boottime;
64    
65  int  int
# Line 77  d_date(void) Line 83  d_date(void)
83  int  int
84  d_uptime(void)  d_uptime(void)
85  {  {
86          int mib[] = { CTL_KERN, KERN_BOOTTIME };     struct utmp utmp_str;
87          int f, tmp = 0;     time_t boottime = 0;
88          size_t size = sizeof(boottime);     FILE *utmp_file;
89          time_t uptime, now;          
90       if ((utmp_file = (FILE *) fopen(_PATH_UTMP, "r")) == NULL)
91          (void) time(&now);       err(1, "d_uptime");
92      
93          output("~WUp: ~w");     while (fread((char *) &utmp_str, sizeof(utmp_str), 1, utmp_file) == 1) {
94          if (!strcmp(utmp_str.ut_line, (char *) BOOT_MSG))
95          if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) {          boottime = (time_t) utmp_str.ut_time;
96                  uptime = now - boottime.tv_sec;     }
97      
98                  if (uptime > 59) {     if (!boottime)
99                          uptime += 30; /* round */       errx(1, "d_uptime");
100                          tmp = (uptime / SECSPERDAY); /* get days */    
101       return (1);
                         if (tmp) {  
                                 output("~C%d~w day%s", tmp, plural((int)tmp));  
                                 f++;  
                         } else {  
                                 f = 0;  
                         }  
   
                         uptime %= SECSPERDAY;  
                         tmp = (uptime / SECSPERHOUR); /* get hours */  
   
                         if (tmp) {  
                                 output("%s~C%d~w hour%s", and(f), tmp,  
                                 plural((int)tmp));  
                                 f++;  
                         } else {  
                                 f = 0;  
                         }  
                                   
                         uptime %= SECSPERHOUR;  
                         tmp = (uptime / SECSPERMIN);  
   
                         if (tmp) {  
                                 output("%s~C%d~w minute%s", and(f), tmp,  
                                 plural((int)tmp));  
                                 f++;  
                         }  
                           
                         output("\n");  
                         return (1);  
                 } else {  
                         output("~RCouldn't find out");  
   
                         return (0);  
                 }  
         }  
   
         return (1); /* default */  
102  }  }

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26