386BSD 0.1 development
[unix-history] / usr / src / usr.bin / finger / lprint.c
index cd8959d..25ea256 100644 (file)
@@ -2,21 +2,40 @@
  * Copyright (c) 1989 The Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1989 The Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * This code is derived from software contributed to Berkeley by
+ * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)lprint.c   5.3 (Berkeley) %G%";
+static char sccsid[] = "@(#)lprint.c   5.13 (Berkeley) 10/31/90";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -25,8 +44,9 @@ static char sccsid[] = "@(#)lprint.c  5.3 (Berkeley) %G%";
 #include <sys/time.h>
 #include <tzfile.h>
 #include <stdio.h>
 #include <sys/time.h>
 #include <tzfile.h>
 #include <stdio.h>
+#include <ctype.h>
+#include <paths.h>
 #include "finger.h"
 #include "finger.h"
-#include "pathnames.h"
 
 #define        LINE_LEN        80
 #define        TAB_LEN         8               /* 8 spaces between tabs */
 
 #define        LINE_LEN        80
 #define        TAB_LEN         8               /* 8 spaces between tabs */
@@ -58,9 +78,10 @@ lprint(pn)
        register struct tm *delta;
        register WHERE *w;
        register int cpr, len, maxlen;
        register struct tm *delta;
        register WHERE *w;
        register int cpr, len, maxlen;
+       struct tm *tp;
        int oddfield;
        time_t time();
        int oddfield;
        time_t time();
-       char *t, *ctime();
+       char *t, *tzn, *prphone();
 
        /*
         * long format --
 
        /*
         * long format --
@@ -85,7 +106,7 @@ lprint(pn)
            strlen(pn->office) + strlen(pn->officephone) +
            sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
                (void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
            strlen(pn->office) + strlen(pn->officephone) +
            sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
                (void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
-                   pn->officephone);
+                   prphone(pn->officephone));
                oddfield = demi_print(tbuf, oddfield);
        } else {
                if (pn->office) {
                oddfield = demi_print(tbuf, oddfield);
        } else {
                if (pn->office) {
@@ -94,12 +115,13 @@ lprint(pn)
                }
                if (pn->officephone) {
                        (void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
                }
                if (pn->officephone) {
                        (void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
-                           pn->officephone);
+                           prphone(pn->officephone));
                        oddfield = demi_print(tbuf, oddfield);
                }
        }
        if (pn->homephone) {
                        oddfield = demi_print(tbuf, oddfield);
                }
        }
        if (pn->homephone) {
-               (void)sprintf(tbuf, "%s: %s", "Home Phone", pn->homephone);
+               (void)sprintf(tbuf, "%s: %s", "Home Phone",
+                   prphone(pn->homephone));
                oddfield = demi_print(tbuf, oddfield);
        }
        if (oddfield)
                oddfield = demi_print(tbuf, oddfield);
        }
        if (oddfield)
@@ -115,15 +137,18 @@ lprint(pn)
         *      when last logged in
         */
        /* find out longest device name for this user for formatting */
         *      when last logged in
         */
        /* find out longest device name for this user for formatting */
-       for (w = pn->whead; w != NULL; w = w->next)
+       for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
                if ((len = strlen(w->tty)) > maxlen)
                        maxlen = len;
        /* find rest of entries for user */
        for (w = pn->whead; w != NULL; w = w->next) {
                switch (w->info) {
                case LOGGEDIN:
                if ((len = strlen(w->tty)) > maxlen)
                        maxlen = len;
        /* find rest of entries for user */
        for (w = pn->whead; w != NULL; w = w->next) {
                switch (w->info) {
                case LOGGEDIN:
-                       cpr = printf("On since %16.16s on %s",
-                           ctime(&w->loginat), w->tty);
+                       tp = localtime(&w->loginat);
+                       t = asctime(tp);
+                       tzn = tp->tm_zone;
+                       cpr = printf("On since %.16s (%s) on %s",
+                           t, tzn, w->tty);
                        /*
                         * idle time is tough; if have one, print a comma,
                         * then spaces to pad out the device name, then the
                        /*
                         * idle time is tough; if have one, print a comma,
                         * then spaces to pad out the device name, then the
@@ -153,13 +178,16 @@ lprint(pn)
                                (void)printf("Never logged in.");
                                break;
                        }
                                (void)printf("Never logged in.");
                                break;
                        }
-                       t = ctime(&w->loginat);
+                       tp = localtime(&w->loginat);
+                       t = asctime(tp);
+                       tzn = tp->tm_zone;
                        if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
                        if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
-                               cpr = printf("Last login %10.10s, %4.4s on %s",
-                                   t, t + 20, w->tty);
+                               cpr =
+                                   printf("Last login %.16s %.4s (%s) on %s",
+                                   t, t + 20, tzn, w->tty);
                        else
                        else
-                               cpr = printf("Last login %16.16s on %s",
-                                       t, w->tty);
+                               cpr = printf("Last login %.16s (%s) on %s",
+                                   t, tzn, w->tty);
                        break;
                }
                if (*w->host) {
                        break;
                }
                if (*w->host) {
@@ -169,11 +197,6 @@ lprint(pn)
                }
                putchar('\n');
        }
                }
                putchar('\n');
        }
-       /*
-        * long format con't:
-        *      mail status
-        */
-       chkmail(pn);
 }
 
 demi_print(str, oddfield)
 }
 
 demi_print(str, oddfield)
@@ -218,50 +241,37 @@ demi_print(str, oddfield)
 show_text(directory, file_name, header)
        char *directory, *file_name, *header;
 {
 show_text(directory, file_name, header)
        char *directory, *file_name, *header;
 {
-       register int fd, n;
+       register int ch, lastc;
+       register FILE *fp;
 
        (void)sprintf(tbuf, "%s/%s", directory, file_name);
 
        (void)sprintf(tbuf, "%s/%s", directory, file_name);
-       if ((fd = open(tbuf, O_RDONLY, 0)) < 0)
+       if ((fp = fopen(tbuf, "r")) == NULL)
                return(0);
        (void)printf("%s\n", header);
                return(0);
        (void)printf("%s\n", header);
-       (void)fflush(stdout);
-       while ((n = read(fd, tbuf, sizeof(tbuf))) > 0)
-               if (write(1, tbuf, n) != n)
-                       break;
-       (void)close(fd);
+       while ((ch = getc(fp)) != EOF)
+               vputc(lastc = ch);
+       if (lastc != '\n')
+               (void)putchar('\n');
+       (void)fclose(fp);
        return(1);
 }
 
        return(1);
 }
 
-chkmail(pn)
-       PERSON *pn;
+vputc(ch)
+       register int ch;
 {
 {
-       register char *date;
-       struct stat sb;
+       int meta;
 
 
-       /*
-        * build path of user's mail box and get stats; if missing
-        * or empty, no mail.
-        */
-       (void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pn->name);
-       if (stat(tbuf, &sb) < 0 || !sb.st_size) {
-               (void)printf("No unread mail.\n");
-               return;
-       }
-
-       /*
-        * if access time matches the modification time then we know
-        * that new mail was received but we haven't a clue as to when
-        * it was last read.
-        */
-       date = ctime(&sb.st_ctime);
-       if (sb.st_atime == sb.st_ctime)
-               (void)printf("New mail received %16.16s.\n", date);
-       else if (sb.st_atime > sb.st_ctime) {
-               date = ctime(&sb.st_atime);
-               (void)printf("No new mail, last read %16.16s.\n", date);
-       } else {
-               (void)printf("Mail received %16.16s; ", date);
-               date = ctime(&sb.st_atime);
-               (void)printf("last read %16.16s.\n", date);
+       if (!isascii(ch)) {
+               (void)putchar('M');
+               (void)putchar('-');
+               ch = toascii(ch);
+               meta = 1;
+       } else
+               meta = 0;
+       if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
+               (void)putchar(ch);
+       else {
+               (void)putchar('^');
+               (void)putchar(ch == '\177' ? '?' : ch | 0100);
        }
 }
        }
 }