[BACK]Return to lprint.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / finger

Annotation of src/usr.bin/finger/lprint.c, Revision 1.8

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