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 / lprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-08  |  9.4 KB  |  347 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.1 1994/05/23 09:03:29 rzsfl Exp rzsfl $";
  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_FORWARD    ".forward"
  55. #define    _PATH_PLAN    ".plan"
  56. #define    _PATH_PROJECT    ".project"
  57.  
  58. lflag_print()
  59. {
  60.     extern int pplan;
  61.     register PERSON *pn;
  62.  
  63.     for (pn = phead;;) {
  64.         lprint(pn);
  65.         if (!pplan) {
  66.             (void)show_text(pn->dir, _PATH_PROJECT, "Project:");
  67.             if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
  68.                 (void)printf("No Plan.\n");
  69.         }
  70.         if (!(pn = pn->next))
  71.             break;
  72.         putchar('\n');
  73.     }
  74. }
  75.  
  76. lprint(pn)
  77.     register PERSON *pn;
  78. {
  79.     extern time_t now;
  80.     register struct tm *delta;
  81.     register WHERE *w;
  82.     register int cpr, len, maxlen;
  83.     struct tm *tp;
  84.     int oddfield;
  85.     time_t time();
  86.     char *t, *tzn, *prphone();
  87.  
  88.     /*
  89.      * long format --
  90.      *    login name
  91.      *    real name
  92.      *    home directory
  93.      *    shell
  94.      *    office, office phone, home phone if available
  95.      */
  96.     (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
  97.         pn->name, pn->realname, pn->dir);
  98.     (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
  99.  
  100.     /*
  101.      * try and print office, office phone, and home phone on one line;
  102.      * if that fails, do line filling so it looks nice.
  103.      */
  104. #define    OFFICE_TAG        "Office"
  105. #define    OFFICE_PHONE_TAG    "Office Phone"
  106.     oddfield = 0;
  107.     if (pn->office && pn->officephone &&
  108.         strlen(pn->office) + strlen(pn->officephone) +
  109.         sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
  110.         (void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
  111.             prphone(pn->officephone));
  112.         oddfield = demi_print(tbuf, oddfield);
  113.     } else {
  114.         if (pn->office) {
  115.             (void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office);
  116.             oddfield = demi_print(tbuf, oddfield);
  117.         }
  118.         if (pn->officephone) {
  119.             (void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
  120.                 prphone(pn->officephone));
  121.             oddfield = demi_print(tbuf, oddfield);
  122.         }
  123.     }
  124.     if (pn->homephone) {
  125.         (void)sprintf(tbuf, "%s: %s", "Home Phone",
  126.             prphone(pn->homephone));
  127.         oddfield = demi_print(tbuf, oddfield);
  128.     }
  129.     if (oddfield)
  130.         putchar('\n');
  131.  
  132.     /*
  133.      * long format con't: * if logged in
  134.      *    terminal
  135.      *    idle time
  136.      *    if messages allowed
  137.      *    where logged in from
  138.      * if not logged in
  139.      *    when last logged in
  140.      */
  141.     /* find out longest device name for this user for formatting */
  142.     for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
  143.         if ((len = strlen(w->tty)) > maxlen)
  144.             maxlen = len;
  145.     /* find rest of entries for user */
  146.     for (w = pn->whead; w != NULL; w = w->next) {
  147.         switch (w->info) {
  148.         case LOGGEDIN:
  149.             tp = localtime(&w->loginat);
  150.             t = asctime(tp);
  151.             tzset();
  152.             tzn = tzname[1 - daylight];
  153.             cpr = printf("On since %.16s (%s) on %s",
  154.                 t, tzn, w->tty);
  155.             if (*w->host) {
  156.                 cpr += printf(" from %s", w->host);
  157.             }
  158.             /*
  159.              * idle time is tough; if have one, print a comma,
  160.              * then spaces to pad out the device name, then the
  161.              * idle time.  Follow with a comma if a remote login.
  162.              */
  163.             delta = gmtime(&w->idletime);
  164.             if (delta->tm_yday || delta->tm_hour
  165.                 || delta->tm_min || delta->tm_sec) {
  166.                 if (*w->host)
  167.                     putchar('\n');
  168.                 cpr += printf("%-*s",
  169.                     maxlen - strlen(w->tty) + 3);
  170.                 if (delta->tm_yday > 0) {
  171.                     cpr += printf("%d day%s ",
  172.                        delta->tm_yday,
  173.                        delta->tm_yday == 1 ? "" : "s");
  174.                 }
  175.                 if (delta->tm_hour > 0) {
  176.                     cpr += printf("%d hour%s ",
  177.                        delta->tm_hour,
  178.                        delta->tm_hour == 1 ? "" : "s");
  179.                 }
  180.                 if ((delta->tm_min > 0) && !delta->tm_yday) {
  181.                     cpr += printf("%d minute%s ",
  182.                        delta->tm_min,
  183.                        delta->tm_min == 1 ? "" : "s");
  184.                 }
  185.                 if ((delta->tm_sec > 0) && !delta->tm_yday
  186.                      && !delta->tm_hour) {
  187.                     cpr += printf("%d second%s ",
  188.                        delta->tm_sec,
  189.                        delta->tm_sec == 1 ? "" : "s");
  190.                 }
  191.                 cpr += printf("idle");
  192.             }
  193.             if (!w->writable) {
  194.                 if (delta->tm_yday || delta->tm_hour
  195.                     || delta->tm_min || delta->tm_sec)
  196.                     cpr += printf("\n    ");
  197.                 cpr += printf(" (messages off)");
  198.             }
  199.             break;
  200.         case LASTLOG:
  201.             if (w->loginat == 0) {
  202.                 (void)printf("Never logged in.");
  203.                 break;
  204.             }
  205.             tp = localtime(&w->loginat);
  206.             t = asctime(tp);
  207.             tzset();
  208.             tzn = tzname[1 - daylight];
  209.             if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
  210.                 cpr =
  211.                     printf("Last login %.16s %.4s (%s) on %s",
  212.                     t, t + 20, tzn, w->tty);
  213.             else
  214.                 cpr = printf("Last login %.16s (%s) on %s",
  215.                     t, tzn, w->tty);
  216.             if (*w->host) {
  217.                 cpr += printf(" from %s", w->host);
  218.             }
  219.             break;
  220.         }
  221.         putchar('\n');
  222.     }
  223.  
  224.     /* If the user forwards mail elsewhere, tell us about it */
  225.     print_forward(pn->dir, _PATH_FORWARD, "Mail forwarded to ");
  226.  
  227.     /* Print the standard mailbox information. */
  228.         if (pn->mailrecv == -1)
  229.             printf("No mail.\n");
  230.         else if (pn->mailrecv > pn->mailread) {
  231.             tp = localtime(&pn->mailrecv);
  232.             t = asctime(tp);
  233.             tzset();
  234.             tzn = tzname[1 - daylight];
  235.             printf("New mail received %.16s %.4s (%s)\n", t,
  236.                    t + 20, tzn);
  237.             tp = localtime(&pn->mailread);
  238.             t = asctime(tp);
  239.             tzset();
  240.             tzn = tzname[1 - daylight];
  241.             printf("     Unread since %.16s %.4s (%s)\n", t,
  242.                    t + 20, tzn);
  243.         } else {
  244.             tp = localtime(&pn->mailread);
  245.             t = asctime(tp);
  246.             tzset();
  247.             tzn = tzname[1 - daylight];
  248.             printf("Mail last read %.16s %.4s (%s)\n", t,
  249.                    t + 20, tzn);
  250.         }
  251. }
  252.  
  253. demi_print(str, oddfield)
  254.     char *str;
  255.     int oddfield;
  256. {
  257.     static int lenlast;
  258.     int lenthis, maxlen;
  259.  
  260.     lenthis = strlen(str);
  261.     if (oddfield) {
  262.         /*
  263.          * We left off on an odd number of fields.  If we haven't
  264.          * crossed the midpoint of the screen, and we have room for
  265.          * the next field, print it on the same line; otherwise,
  266.          * print it on a new line.
  267.          *
  268.          * Note: we insist on having the right hand fields start
  269.          * no less than 5 tabs out.
  270.          */
  271.         maxlen = 5 * TAB_LEN;
  272.         if (maxlen < lenlast)
  273.             maxlen = lenlast;
  274.         if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
  275.             lenthis) <= LINE_LEN) {
  276.             while(lenlast < (4 * TAB_LEN)) {
  277.                 putchar('\t');
  278.                 lenlast += TAB_LEN;
  279.             }
  280.             (void)printf("\t%s\n", str);    /* force one tab */
  281.         } else {
  282.             (void)printf("\n%s", str);    /* go to next line */
  283.             oddfield = !oddfield;    /* this'll be undone below */
  284.         }
  285.     } else
  286.         (void)printf("%s", str);
  287.     oddfield = !oddfield;            /* toggle odd/even marker */
  288.     lenlast = lenthis;
  289.     return(oddfield);
  290. }
  291.  
  292. print_forward(directory, file_name, header)
  293.     char *directory, *file_name, *header;
  294. {
  295.     register int ch, lastc;
  296.     register FILE *fp;
  297.  
  298.     (void)sprintf(tbuf, "%s/%s", directory, file_name);
  299.     if ((fp = fopen(tbuf, "r")) == NULL)
  300.         return(0);
  301.     (void)printf("%s", header);
  302.     while ((ch = getc(fp)) != EOF)
  303.         vputc(lastc = ch);
  304.     if (lastc != '\n')
  305.         (void)putchar('\n');
  306.     (void)fclose(fp);
  307.     return(1);
  308. }
  309.  
  310. show_text(directory, file_name, header)
  311.     char *directory, *file_name, *header;
  312. {
  313.     register int ch, lastc;
  314.     register FILE *fp;
  315.  
  316.     (void)sprintf(tbuf, "%s/%s", directory, file_name);
  317.     if ((fp = fopen(tbuf, "r")) == NULL)
  318.         return(0);
  319.     (void)printf("%s\n", header);
  320.     while ((ch = getc(fp)) != EOF)
  321.         vputc(lastc = ch);
  322.     if (lastc != '\n')
  323.         (void)putchar('\n');
  324.     (void)fclose(fp);
  325.     return(1);
  326. }
  327.  
  328. vputc(ch)
  329.     register int ch;
  330. {
  331.     int meta;
  332.  
  333.     if (!isascii(ch)) {
  334.         (void)putchar('M');
  335.         (void)putchar('-');
  336.         ch = toascii(ch);
  337.         meta = 1;
  338.     } else
  339.         meta = 0;
  340.     if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
  341.         (void)putchar(ch);
  342.     else {
  343.         (void)putchar('^');
  344.         (void)putchar(ch == '\177' ? '?' : ch | 0100);
  345.     }
  346. }
  347.