home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / 3b1 / volume02 / uptime / part01 < prev    next >
Encoding:
Text File  |  1993-03-08  |  7.8 KB  |  238 lines

  1. Path: comp-sources-3b1
  2. From: tkacik@kyzyl.mi.org (Tom Tkacik)
  3. Subject:  v02i034:  uptime program for 3B1, Part01/01
  4. Newsgroups: comp.sources.3b1
  5. Approved: dave@galaxia.network23.com
  6. X-Checksum-Snefru: dfebb492 13ec6f56 30a2026b 70ff00a5
  7.  
  8. Submitted-by: tkacik@kyzyl.mi.org (Tom Tkacik)
  9. Posting-number: Volume 2, Issue 34
  10. Archive-name: uptime/part01
  11.  
  12.  
  13. There have been several requests for the uptime program.
  14. Although sysinfo has all of the information of uptime, I prefer
  15. running it standalone.  So here it is.
  16.  
  17. Note that uptime is simply Lenny Tropiano's sysinfo program modified
  18. to look like uptime.  You will need loadavgd that comes with sysinfo
  19. to run this.
  20.  
  21. Tom Tkacik
  22. tkacik@kyzyl.mi.org
  23.  
  24. #--------------------------------CUT HERE-------------------------------------
  25. #! /bin/sh
  26. #
  27. # This is a shell archive.  Save this into a file, edit it
  28. # and delete all lines above this comment.  Then give this
  29. # file to sh by executing the command "sh file".  The files
  30. # will be extracted into the current directory owned by
  31. # you with default permissions.
  32. #
  33. # The files contained herein are:
  34. #
  35. # -rw-r--r--  1 tkacik  users       885 Mar  2 21:55 README
  36. # -rw-r--r--  1 tkacik  users        78 Mar  2 21:59 Makefile
  37. # -rw-r--r--  1 tkacik  users      4340 Mar  2 21:21 uptime.c
  38. #
  39. echo 'x - README'
  40. if test -f README; then echo 'shar: not overwriting README'; else
  41. sed 's/^X//' << '________This_Is_The_END________' > README
  42. X
  43. X
  44. XFor those who want to have the uptime command on your 3b1, but
  45. Xdo not want to run sysinfo, here it is.
  46. X
  47. XHere is what the output of uptime looks like:
  48. X 9:32 pm  up 72 days,  2:12,  load average:  0.14, 0.09, 0.07        
  49. X
  50. XIt shows the current time, the time since last boot and
  51. Xthe load averaged for the last 1, 5 and 15 minutes.
  52. X
  53. XI modified Lenny Tropiano's sysinfo program, so that it prints out
  54. Xa line that is very close to that produced by BSD's uptime(1).
  55. XThe difference is that this version does not print the number
  56. Xof users logged on.
  57. X
  58. XSimply type make, and move uptime to your favorite bin directory.
  59. XUptime will not run unless you are running loadavgd.
  60. XLoadavd is found along with sysinfo, so you will need to get that,
  61. Xif you have not already got it.
  62. X
  63. XLenny gets all of the credit for this progam (except for that which
  64. Xhe gave to others).
  65. X
  66. XTom Tkacik
  67. Xtkacik@kyzyl.mi.org
  68. X
  69. ________This_Is_The_END________
  70. if test `wc -c < README` -ne 885; then
  71.     echo 'shar: README was damaged during transit (should have been 885 bytes)'
  72. fi
  73. fi        ; : end of overwriting check
  74. echo 'x - Makefile'
  75. if test -f Makefile; then echo 'shar: not overwriting Makefile'; else
  76. sed 's/^X//' << '________This_Is_The_END________' > Makefile
  77. X
  78. XCFLAGS=-O
  79. XLDFLAGS=-s
  80. X
  81. Xuptime: uptime.o
  82. X    $(CC) $(LDFLAGS) -o uptime uptime.o
  83. X
  84. ________This_Is_The_END________
  85. if test `wc -c < Makefile` -ne 78; then
  86.     echo 'shar: Makefile was damaged during transit (should have been 78 bytes)'
  87. fi
  88. fi        ; : end of overwriting check
  89. echo 'x - uptime.c'
  90. if test -f uptime.c; then echo 'shar: not overwriting uptime.c'; else
  91. sed 's/^X//' << '________This_Is_The_END________' > uptime.c
  92. X/************************************************************************\
  93. X** Program name: uptime.c   (Print uptime and load average)             **
  94. X** Modified by:  Thomas Tkacik  (tkacik@kyzyl.mi.org)                   **
  95. X**                                                                      **
  96. X** This is a modified (mainly simplified) version of sysinfo.c          **
  97. X** All credit goes to Lenny Tropiano                                    **
  98. X**                                                                      **
  99. X** Program name: sysinfo.c (System Info)                                **
  100. X** Programmer:   Lenny Tropiano            UUCP: ...icus!lenny          **
  101. X** Organization: ICUS Software Systems     (c)1988, 1989                **
  102. X** Date:         January 23, 1988                                       **
  103. X** Version 2.0:  June 27, 1988                                          **
  104. X** Version 2.1:  Februrary 19, 1989 (Revision Level 4)                  **
  105. X**                                                                      **
  106. X**************************************************************************
  107. X**                                                                      **
  108. X** Credits:      The idea of displaying the file system information     **
  109. X**               came from Scott Hazen Mueller's  newmgr, the replace-  **
  110. X**               ment to the smgr.                                      **
  111. X**                                                                      **
  112. X**************************************************************************
  113. X**                                                                      **
  114. X** Program use:  Program is run as a info process from your .profile    **
  115. X**               This program the file system and displays the          **
  116. X**               pertinent information on the bottom of the screen      **
  117. X**               where the software labels would normally be displayed. **
  118. X**               The program also displays the uptime.                  **
  119. X**                                                                      **
  120. X**************************************************************************
  121. X** Permission is granted to distribute this program by any means as     **
  122. X** long as credit is given for my work.     I would also like to see    **
  123. X** any modifications or enhancements to this program.                   **
  124. X\************************************************************************/
  125. X
  126. X#include <stdio.h>
  127. X#include <time.h>
  128. X#include <sys/shm.h>
  129. X#include <utmp.h>
  130. X
  131. Xstatic char *LINE = "%2d:%02d %cm  up %2d day%1.1s, %2d:%02d,  load average: %-25.25s\n";
  132. X
  133. X#define    MINUTE    60L
  134. X#define    HOUR    (60L * 60L)
  135. X#define    DAY    (24L * 60L * 60L)
  136. X#define AM    0
  137. X#define PM    1
  138. X                             
  139. X/* time since boot */
  140. Xunsigned long    days = 0L;
  141. Xunsigned long    hrs  = 0L;
  142. Xunsigned long    mins = 0L;
  143. X
  144. X/* todays time */
  145. Xint hour;
  146. Xint minute;
  147. Xint am;        /* AM or PM */
  148. X
  149. X/************************************************************************/
  150. X
  151. Xmain(argc,argv)
  152. Xint    argc;
  153. Xchar    *argv[];
  154. X{
  155. X    void    exit();
  156. X    char    loadbuf[30];
  157. X
  158. X    uptime();
  159. X    loadaverage(loadbuf);
  160. X
  161. X    printf(LINE,
  162. X        hour, minute, ((am == AM) ? 'a' : 'p'),
  163. X        days,(days == 1) ? " " : "s",
  164. X        hrs, mins, loadbuf
  165. X    );
  166. X
  167. X    exit(0);
  168. X}
  169. X
  170. Xuptime()
  171. X{
  172. X    struct    utmp    *utent, *getutent();
  173. X    time_t   boottime, curtime, bootsec;
  174. X    struct  tm *current_time;
  175. X
  176. X    /* Get the boot time */
  177. X    setutent();
  178. X    while ((utent = getutent()) != (struct utmp *)NULL) {
  179. X        if (utent->ut_type == BOOT_TIME) {
  180. X            boottime = utent->ut_time;
  181. X            break;
  182. X        }
  183. X    }
  184. X    endutent();
  185. X
  186. X    time(&curtime);
  187. X    bootsec = curtime - boottime;
  188. X
  189. X    /* Convert uptime to days, minutes and seconds since boot */
  190. X    days = bootsec / DAY;
  191. X    bootsec -= DAY * days;
  192. X    hrs = bootsec / HOUR;
  193. X    bootsec -= HOUR * hrs;
  194. X    mins = bootsec / MINUTE;
  195. X    bootsec -= MINUTE * mins;
  196. X
  197. X    /* Convert to the current local time */
  198. X    current_time = localtime(&curtime);
  199. X    minute = current_time->tm_min;
  200. X    hour = current_time->tm_hour;
  201. X    if(hour == 12) {
  202. X        am = PM;
  203. X    } else if(hour > 12) {
  204. X        am = PM;
  205. X        hour -= 12;
  206. X    } else {
  207. X        am = AM;
  208. X        if(hour == 0) {
  209. X            hour = 12;
  210. X        }
  211. X    }
  212. X}
  213. X
  214. Xloadaverage(lbuf)
  215. Xchar *lbuf;
  216. X{
  217. X    int    shm;
  218. X    double    *shmseg;
  219. X
  220. X    shm = shmget(ftok("/unix",'a'),12,0);
  221. X    shmseg = (double *)shmat(shm, (char *) 0, SHM_RDONLY);
  222. X    sprintf(lbuf,"%5.2f,%5.2f,%5.2f", 
  223. X        shmseg[0], shmseg[1], shmseg[2]);
  224. X    shmdt(shmseg);
  225. X}
  226. X
  227. ________This_Is_The_END________
  228. if test `wc -c < uptime.c` -ne 4340; then
  229.     echo 'shar: uptime.c was damaged during transit (should have been 4340 bytes)'
  230. fi
  231. fi        ; : end of overwriting check
  232. exit 0
  233. -- 
  234. David H. Brierley
  235. Home: dave@galaxia.network23.com; Work: dhb@quahog.ssd.ray.com
  236. Send comp.sources.3b1 submissions to comp-sources-3b1@galaxia.network23.com
  237. %% Pardon me Professor, can I be excused, my brain is full. **
  238.