home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / comm / net / amitcp / amitcp-2.2 / src / appl / finger / sprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-21  |  5.4 KB  |  183 lines

  1. RCS_ID_C = "$Id: sprint.c,v 6.2 93/10/15 03:02:28 ppessi Exp $";
  2.  
  3. /*
  4.  * sprint.c - print finger information in short format
  5.  *
  6.  * Copyright © 1993 AmiTCP/IP Group, <AmiTCP-group@hut.fi>
  7.  *                  Helsinki University of Technology, Finland.
  8.  *
  9.  * This program is derived from original work distributed in BSD Net 2.
  10.  *
  11.  * Created      : Fri Oct 15 01:29:07 1993 ppessi
  12.  * Last modified: Fri Oct 15 01:49:31 1993 ppessi
  13.  *
  14.  * $Log:    sprint.c,v $
  15.  * Revision 6.2  93/10/15  03:02:28  ppessi
  16.  * Removed -s support (until we got utmp from somewhere)
  17.  * 
  18.  */
  19.  
  20. /*
  21.  * Copyright (c) 1989 The Regents of the University of California.
  22.  * All rights reserved.
  23.  *
  24.  * This code is derived from software contributed to Berkeley by
  25.  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
  26.  *
  27.  * Redistribution and use in source and binary forms, with or without
  28.  * modification, are permitted provided that the following conditions
  29.  * are met:
  30.  * 1. Redistributions of source code must retain the above copyright
  31.  *    notice, this list of conditions and the following disclaimer.
  32.  * 2. Redistributions in binary form must reproduce the above copyright
  33.  *    notice, this list of conditions and the following disclaimer in the
  34.  *    documentation and/or other materials provided with the distribution.
  35.  * 3. All advertising materials mentioning features or use of this software
  36.  *    must display the following acknowledgement:
  37.  *    This product includes software developed by the University of
  38.  *    California, Berkeley and its contributors.
  39.  * 4. Neither the name of the University nor the names of its contributors
  40.  *    may be used to endorse or promote products derived from this software
  41.  *    without specific prior written permission.
  42.  *
  43.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  44.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  46.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  47.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  48.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  49.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  50.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  52.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  53.  * SUCH DAMAGE.
  54.  */
  55.  
  56. #include <sys/types.h>
  57. #include <sys/time.h>
  58. #include <tzfile.h>
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <stdlib.h>
  62. #include "finger.h"
  63.  
  64. extern int entries;
  65.  
  66. PERSON ** sort(void);
  67. int psort(PERSON **p, PERSON **t);
  68. void stimeprint(WHERE *w);
  69.  
  70. void
  71. sflag_print(void)
  72. {
  73.   extern time_t now;
  74.   register PERSON *pn;
  75.   register WHERE *w;
  76.   register int cnt;
  77.   register char *p;
  78.   PERSON **list, **sort();
  79.   time_t time();
  80.   char *ctime(), *prphone();
  81.  
  82.   list = sort();
  83.   /*
  84.    * short format --
  85.    *    login name
  86.    *    real name
  87.    *    terminal name (the XX of ttyXX)
  88.    *    if terminal writeable (add an '*' to the terminal name
  89.    *        if not)
  90.    *    if logged in show idle time and day logged in, else
  91.    *        show last login date and time.  If > 6 moths,
  92.    *        show year instead of time.
  93.    *    office location
  94.    *    office phone
  95.    */
  96. #define    MAXREALNAME    20
  97. #define MAXREALNAME_S  "20"
  98.   (void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
  99.            "Name", "Tty  Idle  Login Time   Office     Office Phone");
  100.   for (cnt = 0; cnt < entries; ++cnt) {
  101.     pn = list[cnt];
  102.     for (w = pn->whead; w != NULL; w = w->next) {
  103.       (void)printf("%-8.8s "
  104.            "%-" MAXREALNAME_S "." MAXREALNAME_S "s ", 
  105.            pn->name, 
  106.            pn->realname ? pn->realname : "");
  107.       if (!w->loginat) {
  108.     (void)printf("  *     *  No logins   ");
  109.     goto office;
  110.       }
  111.       (void)putchar(w->info == LOGGEDIN && !w->writable ?
  112.             '*' : ' ');
  113.       if (*w->tty)
  114.     (void)printf("%-2.2s ",
  115.              w->tty[0] != 't' || w->tty[1] != 't' ||
  116.              w->tty[2] != 'y' ? w->tty : w->tty + 3);
  117.       else
  118.     (void)printf("   ");
  119.       if (w->info == LOGGEDIN) {
  120.     stimeprint(w);
  121.     (void)printf("  ");
  122.       } else
  123.     (void)printf("    *  ");
  124.       p = ctime(&w->loginat);
  125.       (void)printf("%.6s", p + 4);
  126.       if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
  127.     (void)printf("  %.4s", p + 20);
  128.       else
  129.     (void)printf(" %.5s", p + 11);
  130.     office:            if (pn->office)
  131.       (void)printf(" %-10.10s", pn->office);
  132.     else if (pn->officephone)
  133.       (void)printf(" %-10.10s", " ");
  134.       if (pn->officephone)
  135.     (void)printf(" %-.15s",
  136.              prphone(pn->officephone));
  137.       putchar('\n');
  138.     }
  139.   }
  140. }
  141.  
  142. PERSON **
  143. sort(void)
  144. {
  145.   register PERSON *pn, **lp;
  146.   PERSON **list;
  147.   int psort();
  148.  
  149.   if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
  150.     (void)fprintf(stderr, "finger: out of space.\n");
  151.     exit(1);
  152.   }
  153.   for (lp = list, pn = phead; pn != NULL; pn = pn->next)
  154.     *lp++ = pn;
  155.   (void)qsort(list, entries, sizeof(PERSON *), psort);
  156.   return(list);
  157. }
  158.  
  159. int
  160. psort(PERSON **p, PERSON **t)
  161. {
  162.   return(strcmp((*p)->name, (*t)->name));
  163. }
  164.  
  165. void
  166. stimeprint(WHERE *w)
  167. {
  168.   register struct tm *delta;
  169.  
  170.   delta = gmtime(&w->idletime);
  171.   if (!delta->tm_yday)
  172.     if (!delta->tm_hour)
  173.       if (!delta->tm_min)
  174.     (void)printf("     ");
  175.       else
  176.     (void)printf("%5d", delta->tm_min);
  177.     else
  178.       (void)printf("%2d:%02d",
  179.            delta->tm_hour, delta->tm_min);
  180.   else
  181.     (void)printf("%4dd", delta->tm_yday);
  182. }
  183.