home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / sysinfo / sysinfo.c < prev   
Encoding:
C/C++ Source or Header  |  1991-08-07  |  5.5 KB  |  206 lines

  1. /************************************************************************\
  2. **                                                                      **
  3. ** Program name: sysinfo.c (System Info)                                **
  4. ** Programmer:   Lenny Tropiano            UUCP: ...icus!lenny          **
  5. ** Organization: ICUS Computer Group       (c)1988                      **
  6. ** Date:         January 23, 1988                                       **
  7. **                                                                      **
  8. **************************************************************************
  9. **                                                                      **
  10. ** Credits:      The idea of displaying the file system information     **
  11. **               came from Scott Hazen Mueller's  newmgr, the replace-  **
  12. **               ment to the smgr.                                      **
  13. **                                                                      **
  14. **************************************************************************
  15. **                                                                      **
  16. ** Program use:  Program is run as a info process from your .profile    **
  17. **               This program the file system and displays the          **
  18. **               pertinent information on the bottom of the screen      **
  19. **               where the software labels would normally be displayed. **
  20. **               The program also displays the uptime.                  **
  21. **                                                                      **
  22. **************************************************************************
  23. ** Permission is granted to distribute this program by any means as     **
  24. ** long as credit is given for my work.     I would also like to see    **
  25. ** any modifications or enhancements to this program.                   **
  26. \************************************************************************/
  27.  
  28. #include <stdio.h>
  29. #include <fcntl.h>
  30. #include <errno.h>
  31. #include <signal.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <sys/window.h>
  35. #include <sys/filsys.h>
  36. #include <utmp.h>
  37.  
  38. #define    MINUTE    60L
  39. #define    HOUR    (60L * 60L)
  40. #define    DAY    (24L * 60L * 60L)
  41. #define    SLEEP    15            /* Sleep time (interval between)*/
  42. #define    NICE    5            /* Niceness value        */
  43.                              
  44. #define LINE_1    "%-40.40s         up  %2d day%1.1s %2d hour%1.1s %2d minute%1.1s"
  45. #define LINE_2    "Filesystem (%-5.5s) %6ld free  %6ld total blocks  %2d%% available %5d i-nodes"
  46. #define    FILESYS    "/dev/rfp002"
  47. #define    FNAME    "fp002"
  48. #ifndef TRUE
  49. #define TRUE    1
  50. #endif
  51.  
  52. char    *progname;            /* program name            */
  53. char    mailfile[30];            /* mailbox file name            */
  54. struct  utdata utd;            /* Window data structure        */
  55. struct  filsys fs;            /* Filesystem superblock struct */
  56. time_t    boottime;            /* system boot time in sec      */
  57. time_t    mailtime = 0L;            /* mail modification time       */
  58. int    msgs = 0;
  59. unsigned long    days = 0L;
  60. unsigned long    hrs  = 0L;
  61. unsigned long    mins = 0L;
  62.  
  63. /************************************************************************/
  64.  
  65. main(argc,argv)
  66. int    argc;
  67. char    *argv[];
  68. {
  69.     int    terminate();
  70.     char    *getenv();
  71.     struct    utmp    *utent, *getutent();
  72.  
  73.     if (fork() != 0)         /* detach process-info */
  74.         exit(0);
  75.         
  76.     nice(NICE);            /* Be a little nice    */
  77.     signal (SIGINT, SIG_IGN);
  78.     signal (SIGQUIT, SIG_IGN);
  79.      signal (SIGHUP, terminate);
  80.      signal (SIGTERM, terminate);
  81.  
  82.         progname = *argv;
  83.     sprintf(mailfile,"%s",getenv("MAIL"));
  84.  
  85.     setutent();
  86.     while ((utent = getutent()) != (struct utmp *)NULL) {
  87.         if (utent->ut_type == BOOT_TIME) {
  88.             boottime = utent->ut_time;
  89.             break;
  90.         }
  91.     }
  92.     endutent();
  93.  
  94.     info_process();
  95.     terminate();
  96.  
  97. }
  98.  
  99. info_process()
  100. {
  101.     char    mailbuffer[40];
  102.  
  103.     while (TRUE) {
  104.         filestatus();
  105.         uptime();
  106.         mailcheck(mailbuffer);
  107.  
  108.         utd.ut_num = WTXTSLK1;
  109.         sprintf(utd.ut_text,LINE_1,
  110.             mailbuffer,
  111.             days,(days == 1) ? " " : "s",
  112.             hrs, (hrs  == 1) ? " " : "s",
  113.             mins,(mins == 1) ? " " : "s"
  114.         );
  115.             ioctl(0, WIOCSETTEXT, &utd);
  116.  
  117.         utd.ut_num = WTXTSLK2;
  118.         sprintf(utd.ut_text,LINE_2,
  119.            FNAME, fs.s_tfree * 2, fs.s_fsize * 2, 
  120.            (int)((((float)fs.s_tfree / (float)fs.s_fsize) + 0.005) 
  121.                * 100.0), fs.s_tinode
  122.         );
  123.             ioctl(0, WIOCSETTEXT, &utd);
  124.  
  125.         sleep(SLEEP);
  126.     }
  127. }
  128.  
  129. int terminate()
  130. {
  131.     utd.ut_num = WTXTSLK1;        /* clear the label area */
  132.     utd.ut_text[0] = '\0';
  133.         ioctl(0, WIOCSETTEXT, &utd);
  134.     utd.ut_num = WTXTSLK2;
  135.         ioctl(0, WIOCSETTEXT, &utd);
  136.  
  137.     exit(0);
  138. }
  139.  
  140. filestatus()
  141. {
  142.     int    fd;
  143.  
  144.     if ((fd = open(FILESYS, O_RDONLY)) == -1) {
  145.         fprintf(stderr,"%s: cannot open %s for read\n",
  146.             progname, FILESYS);
  147.         terminate();
  148.     }
  149.     
  150.     if (lseek(fd, 512, 0) == -1) {
  151.         fprintf(stderr,"%s: cannot lseek to superblock\n", progname);
  152.         terminate();
  153.     }
  154.  
  155.     if (read(fd, &fs, sizeof(struct filsys)) == -1) {
  156.         fprintf(stderr,"%s: cannot read the superblock\n", progname);
  157.         terminate();
  158.     }
  159.  
  160.     close(fd);
  161.  
  162. }
  163.  
  164. uptime()
  165. {
  166.     time_t   curtime, bootsec;
  167.  
  168.     time(&curtime);
  169.     bootsec = curtime - boottime;
  170.  
  171.     days = bootsec / DAY;
  172.     bootsec -= DAY * days;
  173.     hrs = bootsec / HOUR;
  174.     bootsec -= HOUR * hrs;
  175.     mins = bootsec / MINUTE;
  176.     bootsec -= MINUTE * mins;
  177. }
  178.  
  179. mailcheck(buf)
  180. char *buf;
  181. {
  182.     FILE    *fp;
  183.     char    buffer[BUFSIZ];
  184.     struct    stat    statbuf;
  185.  
  186.     if (access(mailfile,4) == 0) {
  187.         if (stat(mailfile,&statbuf) != -1) {
  188.             if (statbuf.st_ctime > mailtime) {
  189.             msgs = 0;
  190.             if ((fp = fopen(mailfile,"r")) != NULL) {
  191.                while (fgets(buffer, BUFSIZ, fp) != NULL) 
  192.                 if (strncmp(buffer,"From ",5) == 0)
  193.                     msgs++;
  194.             }
  195.             fclose(fp);
  196.             mailtime = statbuf.st_ctime;
  197.             }
  198.         }
  199.     }
  200.     if (msgs) 
  201.         sprintf(buf,"%d mail message%s in your mailbox",msgs,
  202.             (msgs == 1) ? "" : "s");
  203.     else
  204.         buf[0] = '\0';
  205. }
  206.