home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / rwho / rwho.c < prev   
Encoding:
C/C++ Source or Header  |  1994-05-24  |  4.9 KB  |  186 lines

  1. /*
  2.  * Copyright (c) 1983 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. /*static char sccsid[] = "from: @(#)rwho.c    5.5 (Berkeley) 6/1/90";*/
  42. static char rcsid[] = "$Id: rwho.c,v 1.1 1994/05/24 07:56:25 rzsfl Exp rzsfl $";
  43. #endif /* not lint */
  44.  
  45. #include <sys/param.h>
  46. #include <sys/dir.h>
  47. #include <sys/file.h>
  48. #include <protocols/rwhod.h>
  49. #include <stdio.h>
  50.  
  51. DIR    *dirp;
  52.  
  53. struct    whod wd;
  54. int    utmpcmp();
  55. #define    NUSERS    1000
  56. struct    myutmp {
  57.     char    myhost[MAXHOSTNAMELEN];
  58.     int    myidle;
  59.     struct    outmp myutmp;
  60. } myutmp[NUSERS];
  61. int    nusers;
  62.  
  63. #define    WHDRSIZE    (sizeof (wd) - sizeof (wd.wd_we))
  64. /* 
  65.  * this macro should be shared with ruptime.
  66.  */
  67. #define    down(w,now)    ((now) - (w)->wd_recvtime > 11 * 60)
  68.  
  69. char    *ctime(), *strcpy();
  70. time_t    now;
  71. int    aflg;
  72.  
  73. main(argc, argv)
  74.     int argc;
  75.     char **argv;
  76. {
  77.     extern char *optarg;
  78.     extern int optind;
  79.     int ch;
  80.     struct direct *dp;
  81.     int cc, width;
  82.     register struct whod *w = &wd;
  83.     register struct whoent *we;
  84.     register struct myutmp *mp;
  85.     int f, n, i;
  86.     time_t time();
  87.  
  88.     while ((ch = getopt(argc, argv, "a")) != EOF)
  89.         switch((char)ch) {
  90.         case 'a':
  91.             aflg = 1;
  92.             break;
  93.         case '?':
  94.         default:
  95.             fprintf(stderr, "usage: rwho [-a]\n");
  96.             exit(1);
  97.         }
  98.     if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
  99.         perror(_PATH_RWHODIR);
  100.         exit(1);
  101.     }
  102.     mp = myutmp;
  103.     (void)time(&now);
  104.     while (dp = readdir(dirp)) {
  105.         if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
  106.             continue;
  107.         f = open(dp->d_name, O_RDONLY);
  108.         if (f < 0)
  109.             continue;
  110.         cc = read(f, (char *)&wd, sizeof (struct whod));
  111.         if (cc < WHDRSIZE) {
  112.             (void) close(f);
  113.             continue;
  114.         }
  115.         if (down(w,now)) {
  116.             (void) close(f);
  117.             continue;
  118.         }
  119.         cc -= WHDRSIZE;
  120.         we = w->wd_we;
  121.         for (n = cc / sizeof (struct whoent); n > 0; n--) {
  122.             if (aflg == 0 && we->we_idle >= 60*60) {
  123.                 we++;
  124.                 continue;
  125.             }
  126.             if (nusers >= NUSERS) {
  127.                 printf("too many users\n");
  128.                 exit(1);
  129.             }
  130.             mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
  131.             (void) strcpy(mp->myhost, w->wd_hostname);
  132.             nusers++; we++; mp++;
  133.         }
  134.         (void) close(f);
  135.     }
  136.     qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
  137.     mp = myutmp;
  138.     width = 0;
  139.     for (i = 0; i < nusers; i++) {
  140.         int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
  141.         if (j > width)
  142.             width = j;
  143.         mp++;
  144.     }
  145.     mp = myutmp;
  146.     for (i = 0; i < nusers; i++) {
  147.         char buf[BUFSIZ];
  148.         (void)sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line);
  149.         printf("%-8.8s %-*s %.12s",
  150.            mp->myutmp.out_name,
  151.            width,
  152.            buf,
  153.            ctime((time_t *)&mp->myutmp.out_time)+4);
  154.         mp->myidle /= 60;
  155.         if (mp->myidle) {
  156.             if (aflg) {
  157.                 if (mp->myidle >= 100*60)
  158.                     mp->myidle = 100*60 - 1;
  159.                 if (mp->myidle >= 60)
  160.                     printf(" %2d", mp->myidle / 60);
  161.                 else
  162.                     printf("   ");
  163.             } else
  164.                 printf(" ");
  165.             printf(":%02d", mp->myidle % 60);
  166.         }
  167.         printf("\n");
  168.         mp++;
  169.     }
  170.     exit(0);
  171. }
  172.  
  173. utmpcmp(u1, u2)
  174.     struct myutmp *u1, *u2;
  175. {
  176.     int rc;
  177.  
  178.     rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
  179.     if (rc)
  180.         return (rc);
  181.     rc = strncmp(u1->myhost, u2->myhost, 8);
  182.     if (rc)
  183.         return (rc);
  184.     return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
  185. }
  186.