home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / FINGER.TAR / finger / lprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-23  |  8.2 KB  |  301 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. /*static char sccsid[] = "from: @(#)lprint.c    5.13 (Berkeley) 10/31/90";*/
  39. static char rcsid[] = "$Id: lprint.c,v 1.3 1993/10/07 19:58:31 brezak Exp $";
  40. #endif /* not lint */
  41.  
  42. #include <sys/types.h>
  43. #include <sys/file.h>
  44. #include <sys/stat.h>
  45. #include <sys/time.h>
  46. #include <tzfile.h>
  47. #include <stdio.h>
  48. #include <ctype.h>
  49. #include <paths.h>
  50. #include "finger.h"
  51.  
  52. #define    LINE_LEN    80
  53. #define    TAB_LEN        8        /* 8 spaces between tabs */
  54. #define    _PATH_PLAN    ".plan"
  55. #define    _PATH_PROJECT    ".project"
  56.  
  57. lflag_print()
  58. {
  59.     extern int pplan;
  60.     register PERSON *pn;
  61.  
  62.     for (pn = phead;;) {
  63.         lprint(pn);
  64.         if (!pplan) {
  65.             (void)show_text(pn->dir, _PATH_PROJECT, "Project:");
  66.             if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
  67.                 (void)printf("No Plan.\n");
  68.         }
  69.         if (!(pn = pn->next))
  70.             break;
  71.         putchar('\n');
  72.     }
  73. }
  74.  
  75. lprint(pn)
  76.     register PERSON *pn;
  77. {
  78.     extern time_t now;
  79.     register struct tm *delta;
  80.     register WHERE *w;
  81.     register int cpr, len, maxlen;
  82.     struct tm *tp;
  83.     int oddfield;
  84.     time_t time();
  85.     char *t, *tzn, *prphone();
  86.  
  87.     /*
  88.      * long format --
  89.      *    login name
  90.      *    real name
  91.      *    home directory
  92.      *    shell
  93.      *    office, office phone, home phone if available
  94.      */
  95.     (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
  96.         pn->name, pn->realname, pn->dir);
  97.     (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
  98.  
  99.     /*
  100.      * try and print office, office phone, and home phone on one line;
  101.      * if that fails, do line filling so it looks nice.
  102.      */
  103. #define    OFFICE_TAG        "Office"
  104. #define    OFFICE_PHONE_TAG    "Office Phone"
  105.     oddfield = 0;
  106.     if (pn->office && pn->officephone &&
  107.         strlen(pn->office) + strlen(pn->officephone) +
  108.         sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
  109.         (void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
  110.             prphone(pn->officephone));
  111.         oddfield = demi_print(tbuf, oddfield);
  112.     } else {
  113.         if (pn->office) {
  114.             (void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office);
  115.             oddfield = demi_print(tbuf, oddfield);
  116.         }
  117.         if (pn->officephone) {
  118.             (void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
  119.                 prphone(pn->officephone));
  120.             oddfield = demi_print(tbuf, oddfield);
  121.         }
  122.     }
  123.     if (pn->homephone) {
  124.         (void)sprintf(tbuf, "%s: %s", "Home Phone",
  125.             prphone(pn->homephone));
  126.         oddfield = demi_print(tbuf, oddfield);
  127.     }
  128.     if (oddfield)
  129.         putchar('\n');
  130.  
  131.     /*
  132.      * long format con't: * if logged in
  133.      *    terminal
  134.      *    idle time
  135.      *    if messages allowed
  136.      *    where logged in from
  137.      * if not logged in
  138.      *    when last logged in
  139.      */
  140.     /* find out longest device name for this user for formatting */
  141.     for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
  142.         if ((len = strlen(w->tty)) > maxlen)
  143.             maxlen = len;
  144.     /* find rest of entries for user */
  145.     for (w = pn->whead; w != NULL; w = w->next) {
  146.         switch (w->info) {
  147.         case LOGGEDIN:
  148.             tp = localtime(&w->loginat);
  149.             t = asctime(tp);
  150.             tzset();
  151.             tzn = tzname[1 - daylight];
  152.             cpr = printf("On since %.16s (%s) on %s",
  153.                 t, tzn, w->tty);
  154.             /*
  155.              * idle time is tough; if have one, print a comma,
  156.              * then spaces to pad out the device name, then the
  157.              * idle time.  Follow with a comma if a remote login.
  158.              */
  159.             delta = gmtime(&w->idletime);
  160.             if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
  161.                 cpr += printf("%-*s idle ",
  162.                     maxlen - strlen(w->tty) + 1, ",");
  163.                 if (delta->tm_yday > 0) {
  164.                     cpr += printf("%d day%s ",
  165.                        delta->tm_yday,
  166.                        delta->tm_yday == 1 ? "" : "s");
  167.                 }
  168.                 cpr += printf("%d:%02d",
  169.                     delta->tm_hour, delta->tm_min);
  170.                 if (*w->host) {
  171.                     putchar(',');
  172.                     ++cpr;
  173.                 }
  174.             }
  175.             if (!w->writable)
  176.                 cpr += printf(" (messages off)");
  177.             break;
  178.         case LASTLOG:
  179.             if (w->loginat == 0) {
  180.                 (void)printf("Never logged in.");
  181.                 break;
  182.             }
  183.             tp = localtime(&w->loginat);
  184.             t = asctime(tp);
  185.             tzset();
  186.             tzn = tzname[1 - daylight];
  187.             if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
  188.                 cpr =
  189.                     printf("Last login %.16s %.4s (%s) on %s",
  190.                     t, t + 20, tzn, w->tty);
  191.             else
  192.                 cpr = printf("Last login %.16s (%s) on %s",
  193.                     t, tzn, w->tty);
  194.             break;
  195.         }
  196.         if (*w->host) {
  197.             if (LINE_LEN < (cpr + 6 + strlen(w->host)))
  198.                 (void)printf("\n   ");
  199.             (void)printf(" from %s", w->host);
  200.         }
  201.         putchar('\n');
  202.     }
  203.     if (pn->mailrecv == -1)
  204.         printf("No Mail.\n");
  205.     else if (pn->mailrecv > pn->mailread) {
  206.         tp = localtime(&pn->mailrecv);
  207.         t = asctime(tp);
  208.         tzset();
  209.         tzn = tzname[1 - daylight];
  210.         printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn);
  211.         tp = localtime(&pn->mailread);
  212.         t = asctime(tp);
  213.         tzset();
  214.         tzn = tzname[1 - daylight];
  215.         printf("     Unread since %.16s %.4s (%s)\n", t, t + 20, tzn);
  216.     } else {
  217.         tp = localtime(&pn->mailread);
  218.         t = asctime(tp);
  219.         tzset();
  220.         tzn = tzname[1 - daylight];
  221.         printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn);
  222.     }
  223. }
  224.  
  225. demi_print(str, oddfield)
  226.     char *str;
  227.     int oddfield;
  228. {
  229.     static int lenlast;
  230.     int lenthis, maxlen;
  231.  
  232.     lenthis = strlen(str);
  233.     if (oddfield) {
  234.         /*
  235.          * We left off on an odd number of fields.  If we haven't
  236.          * crossed the midpoint of the screen, and we have room for
  237.          * the next field, print it on the same line; otherwise,
  238.          * print it on a new line.
  239.          *
  240.          * Note: we insist on having the right hand fields start
  241.          * no less than 5 tabs out.
  242.          */
  243.         maxlen = 5 * TAB_LEN;
  244.         if (maxlen < lenlast)
  245.             maxlen = lenlast;
  246.         if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
  247.             lenthis) <= LINE_LEN) {
  248.             while(lenlast < (4 * TAB_LEN)) {
  249.                 putchar('\t');
  250.                 lenlast += TAB_LEN;
  251.             }
  252.             (void)printf("\t%s\n", str);    /* force one tab */
  253.         } else {
  254.             (void)printf("\n%s", str);    /* go to next line */
  255.             oddfield = !oddfield;    /* this'll be undone below */
  256.         }
  257.     } else
  258.         (void)printf("%s", str);
  259.     oddfield = !oddfield;            /* toggle odd/even marker */
  260.     lenlast = lenthis;
  261.     return(oddfield);
  262. }
  263.  
  264. show_text(directory, file_name, header)
  265.     char *directory, *file_name, *header;
  266. {
  267.     register int ch, lastc;
  268.     register FILE *fp;
  269.  
  270.     (void)sprintf(tbuf, "%s/%s", directory, file_name);
  271.     if ((fp = fopen(tbuf, "r")) == NULL)
  272.         return(0);
  273.     (void)printf("%s\n", header);
  274.     while ((ch = getc(fp)) != EOF)
  275.         vputc(lastc = ch);
  276.     if (lastc != '\n')
  277.         (void)putchar('\n');
  278.     (void)fclose(fp);
  279.     return(1);
  280. }
  281.  
  282. vputc(ch)
  283.     register int ch;
  284. {
  285.     int meta;
  286.  
  287.     if (!isascii(ch)) {
  288.         (void)putchar('M');
  289.         (void)putchar('-');
  290.         ch = toascii(ch);
  291.         meta = 1;
  292.     } else
  293.         meta = 0;
  294.     if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
  295.         (void)putchar(ch);
  296.     else {
  297.         (void)putchar('^');
  298.         (void)putchar(ch == '\177' ? '?' : ch | 0100);
  299.     }
  300. }
  301.