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 / finger / finger.c,v < prev    next >
Encoding:
Text File  |  1994-05-23  |  7.0 KB  |  276 lines

  1. head    1.1;
  2. access;
  3. symbols;
  4. locks
  5.     rzsfl:1.1; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.1
  10. date    94.05.23.09.03.28;    author rzsfl;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @Original
  17. @
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*
  26.  * Copyright (c) 1989 The Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * This code is derived from software contributed to Berkeley by
  30.  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
  31.  *
  32.  * Redistribution and use in source and binary forms, with or without
  33.  * modification, are permitted provided that the following conditions
  34.  * are met:
  35.  * 1. Redistributions of source code must retain the above copyright
  36.  *    notice, this list of conditions and the following disclaimer.
  37.  * 2. Redistributions in binary form must reproduce the above copyright
  38.  *    notice, this list of conditions and the following disclaimer in the
  39.  *    documentation and/or other materials provided with the distribution.
  40.  * 3. All advertising materials mentioning features or use of this software
  41.  *    must display the following acknowledgement:
  42.  *    This product includes software developed by the University of
  43.  *    California, Berkeley and its contributors.
  44.  * 4. Neither the name of the University nor the names of its contributors
  45.  *    may be used to endorse or promote products derived from this software
  46.  *    without specific prior written permission.
  47.  *
  48.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  49.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  52.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  53.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  54.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  58.  * SUCH DAMAGE.
  59.  */
  60.  
  61. /*
  62.  * Mail status reporting added 931007 by Luke Mewburn, <zak@@rmit.edu.au>.
  63.  */
  64.  
  65. #ifndef lint
  66. char copyright[] =
  67. "@@(#) Copyright (c) 1989 The Regents of the University of California.\n\
  68.  All rights reserved.\n";
  69. #endif /* not lint */
  70.  
  71. #ifndef lint
  72. /*static char sccsid[] = "from: @@(#)finger.c    5.22 (Berkeley) 6/29/90";*/
  73. static char rcsid[] = "$Id: finger.c,v 1.3 1993/10/07 19:58:29 brezak Exp $";
  74. #endif /* not lint */
  75.  
  76. /*
  77.  * Finger prints out information about users.  It is not portable since
  78.  * certain fields (e.g. the full user name, office, and phone numbers) are
  79.  * extracted from the gecos field of the passwd file which other UNIXes
  80.  * may not have or may use for other things.
  81.  *
  82.  * There are currently two output formats; the short format is one line
  83.  * per user and displays login name, tty, login time, real name, idle time,
  84.  * and office location/phone number.  The long format gives the same
  85.  * information (in a more legible format) as well as home directory, shell,
  86.  * mail info, and .plan/.project files.
  87.  */
  88.  
  89. #include <sys/param.h>
  90. #include <sys/file.h>
  91. #include <stdio.h>
  92. #include "finger.h"
  93.  
  94. time_t now;
  95. int lflag, sflag, mflag, pplan;
  96. char tbuf[1024];
  97.  
  98. main(argc, argv)
  99.     int argc;
  100.     char **argv;
  101. {
  102.     extern int optind;
  103.     int ch;
  104.     time_t time();
  105.  
  106.     while ((ch = getopt(argc, argv, "lmps")) != EOF)
  107.         switch(ch) {
  108.         case 'l':
  109.             lflag = 1;        /* long format */
  110.             break;
  111.         case 'm':
  112.             mflag = 1;        /* force exact match of names */
  113.             break;
  114.         case 'p':
  115.             pplan = 1;        /* don't show .plan/.project */
  116.             break;
  117.         case 's':
  118.             sflag = 1;        /* short format */
  119.             break;
  120.         case '?':
  121.         default:
  122.             (void)fprintf(stderr,
  123.                 "usage: finger [-lmps] [login ...]\n");
  124.             exit(1);
  125.         }
  126.     argc -= optind;
  127.     argv += optind;
  128.  
  129.     (void)time(&now);
  130.     setpassent(1);
  131.     if (!*argv) {
  132.         /*
  133.          * Assign explicit "small" format if no names given and -l
  134.          * not selected.  Force the -s BEFORE we get names so proper
  135.          * screening will be done.
  136.          */
  137.         if (!lflag)
  138.             sflag = 1;    /* if -l not explicit, force -s */
  139.         loginlist();
  140.         if (entries == 0)
  141.             (void)printf("No one logged on.\n");
  142.     } else {
  143.         userlist(argc, argv);
  144.         /*
  145.          * Assign explicit "large" format if names given and -s not
  146.          * explicitly stated.  Force the -l AFTER we get names so any
  147.          * remote finger attempts specified won't be mishandled.
  148.          */
  149.         if (!sflag)
  150.             lflag = 1;    /* if -s not explicit, force -l */
  151.     }
  152.     if (entries != 0) {
  153.         if (lflag)
  154.             lflag_print();
  155.         else
  156.             sflag_print();
  157.     }
  158.     exit(0);
  159. }
  160.  
  161. loginlist()
  162. {
  163.     register PERSON *pn;
  164.     struct passwd *pw;
  165.     struct utmp user;
  166.     char name[UT_NAMESIZE + 1];
  167.  
  168.     if (!freopen(_PATH_UTMP, "r", stdin)) {
  169.         (void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP);
  170.         exit(2);
  171.     }
  172.     name[UT_NAMESIZE] = NULL;
  173.     while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
  174.         if (!user.ut_name[0])
  175.             continue;
  176.         if ((pn = find_person(user.ut_name)) == NULL) {
  177.             bcopy(user.ut_name, name, UT_NAMESIZE);
  178.             if ((pw = getpwnam(name)) == NULL)
  179.                 continue;
  180.             pn = enter_person(pw);
  181.         }
  182.         enter_where(&user, pn);
  183.     }
  184.     for (pn = phead; lflag && pn != NULL; pn = pn->next)
  185.         enter_lastlog(pn);
  186. }
  187.  
  188. userlist(argc, argv)
  189.     register argc;
  190.     register char **argv;
  191. {
  192.     register i;
  193.     register PERSON *pn;
  194.     PERSON *nethead, **nettail;
  195.     struct utmp user;
  196.     struct passwd *pw;
  197.     int dolocal, *used;
  198.     char *index();
  199.  
  200.     if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int)))) {
  201.         (void)fprintf(stderr, "finger: out of space.\n");
  202.         exit(1);
  203.     }
  204.  
  205.     /* pull out all network requests */
  206.     for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
  207.         if (!index(argv[i], '@@')) {
  208.             dolocal = 1;
  209.             continue;
  210.         }
  211.         pn = palloc();
  212.         *nettail = pn;
  213.         nettail = &pn->next;
  214.         pn->name = argv[i];
  215.         used[i] = -1;
  216.     }
  217.     *nettail = NULL;
  218.  
  219.     if (!dolocal)
  220.         goto net;
  221.  
  222.     /*
  223.      * traverse the list of possible login names and check the login name
  224.      * and real name against the name specified by the user.
  225.      */
  226.     if (mflag) {
  227.         for (i = 0; i < argc; i++)
  228.             if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
  229.                 enter_person(pw);
  230.                 used[i] = 1;
  231.             }
  232.     } else while (pw = getpwent())
  233.         for (i = 0; i < argc; i++)
  234.             if (used[i] >= 0 &&
  235.                 (!strcasecmp(pw->pw_name, argv[i]) ||
  236.                 match(pw, argv[i]))) {
  237.                 enter_person(pw);
  238.                 used[i] = 1;
  239.             }
  240.  
  241.     /* list errors */
  242.     for (i = 0; i < argc; i++)
  243.         if (!used[i])
  244.             (void)fprintf(stderr,
  245.                 "finger: %s: no such user.\n", argv[i]);
  246.  
  247.     /* handle network requests */
  248. net:    for (pn = nethead; pn; pn = pn->next) {
  249.         netfinger(pn->name);
  250.         if (pn->next || entries)
  251.             putchar('\n');
  252.     }
  253.  
  254.     if (entries == 0)
  255.         return;
  256.  
  257.     /*
  258.      * Scan thru the list of users currently logged in, saving
  259.      * appropriate data whenever a match occurs.
  260.      */
  261.     if (!freopen(_PATH_UTMP, "r", stdin)) {
  262.         (void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP);
  263.         exit(1);
  264.     }
  265.     while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
  266.         if (!user.ut_name[0])
  267.             continue;
  268.         if ((pn = find_person(user.ut_name)) == NULL)
  269.             continue;
  270.         enter_where(&user, pn);
  271.     }
  272.     for (pn = phead; pn != NULL; pn = pn->next)
  273.         enter_lastlog(pn);
  274. }
  275. @
  276.