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

Annotation of src/usr.bin/finger/sprint.c, Revision 1.4

1.4     ! kstailey    1: /*     $OpenBSD: sprint.c,v 1.3 1997/05/30 23:35:52 kstailey 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.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: #ifndef lint
                     40: /*static char sccsid[] = "from: @(#)sprint.c   5.8 (Berkeley) 12/4/90";*/
1.4     ! kstailey   41: static char rcsid[] = "$OpenBSD: sprint.c,v 1.3 1997/05/30 23:35:52 kstailey Exp $";
1.1       deraadt    42: #endif /* not lint */
                     43:
                     44: #include <sys/types.h>
                     45: #include <sys/time.h>
                     46: #include <tzfile.h>
                     47: #include <stdio.h>
1.3       kstailey   48: #include <stdlib.h>
1.1       deraadt    49: #include "finger.h"
1.3       kstailey   50: #include "extern.h"
1.1       deraadt    51:
1.3       kstailey   52: void
1.1       deraadt    53: sflag_print()
                     54: {
1.3       kstailey   55:        PERSON *pn;
                     56:        WHERE *w;
                     57:        int cnt;
                     58:        char *p;
                     59:        PERSON **list;
1.1       deraadt    60:
                     61:        list = sort();
                     62:        /*
                     63:         * short format --
                     64:         *      login name
                     65:         *      real name
                     66:         *      terminal name (the XX of ttyXX)
                     67:         *      if terminal writeable (add an '*' to the terminal name
                     68:         *              if not)
                     69:         *      if logged in show idle time and day logged in, else
1.3       kstailey   70:         *              show last login date and time.  If > 6 months,
                     71:         *              show year instead of time.  If < 6 days,
                     72:         *              show day name instead of month & day.
                     73:         *      if -h given
                     74:         *              remote host
                     75:         *      else if -o given (overriding -h) (default)
                     76:         *              office location
                     77:         *              office phone
1.1       deraadt    78:         */
                     79: #define        MAXREALNAME     20
1.3       kstailey   80: #define        MAXHOSTNAME     20
                     81:        (void)printf("%-*s %-*s %s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
                     82:            "Name", "Tty  Idle  Login Time  ",
                     83:            (oflag) ? "Office     Office Phone" : "Where");
1.1       deraadt    84:        for (cnt = 0; cnt < entries; ++cnt) {
                     85:                pn = list[cnt];
                     86:                for (w = pn->whead; w != NULL; w = w->next) {
                     87:                        (void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
1.4     ! kstailey   88:                            vs(pn->name), MAXREALNAME, MAXREALNAME,
        !            89:                            pn->realname ? vs(pn->realname) : "");
1.1       deraadt    90:                        if (!w->loginat) {
                     91:                                (void)printf("  *     *  No logins   ");
                     92:                                goto office;
                     93:                        }
                     94:                        (void)putchar(w->info == LOGGEDIN && !w->writable ?
                     95:                            '*' : ' ');
                     96:                        if (*w->tty)
                     97:                                (void)printf("%-2.2s ",
                     98:                                    w->tty[0] != 't' || w->tty[1] != 't' ||
                     99:                                    w->tty[2] != 'y' ? w->tty : w->tty + 3);
                    100:                        else
                    101:                                (void)printf("   ");
                    102:                        if (w->info == LOGGEDIN) {
                    103:                                stimeprint(w);
                    104:                                (void)printf("  ");
                    105:                        } else
                    106:                                (void)printf("    *  ");
                    107:                        p = ctime(&w->loginat);
1.3       kstailey  108:                        if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
                    109:                                (void)printf("   %.3s", p);
                    110:                        else
                    111:                                (void)printf("%.6s", p + 4);
1.1       deraadt   112:                        if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
1.3       kstailey  113:                                (void)printf(" %.4s ", p + 20);
1.1       deraadt   114:                        else
                    115:                                (void)printf(" %.5s", p + 11);
1.3       kstailey  116: office:
                    117:                        putchar(' ');
                    118:                        if (oflag) {
                    119:                                if (pn->office)
1.4     ! kstailey  120:                                        (void)printf("%-10.10s",
        !           121:                                            vs(pn->office));
1.3       kstailey  122:                                else if (pn->officephone)
                    123:                                        (void)printf("%-10.10s", " ");
                    124:                                if (pn->officephone)
                    125:                                        (void)printf(" %-.15s",
1.4     ! kstailey  126:                                            vs(prphone(pn->officephone)));
1.3       kstailey  127:                        } else
                    128:                                (void)printf("%.*s", MAXHOSTNAME, w->host);
1.1       deraadt   129:                        putchar('\n');
                    130:                }
                    131:        }
                    132: }
                    133:
                    134: PERSON **
                    135: sort()
                    136: {
1.3       kstailey  137:        PERSON *pn, **lp;
1.1       deraadt   138:        PERSON **list;
                    139:
                    140:        if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
                    141:                (void)fprintf(stderr, "finger: out of space.\n");
                    142:                exit(1);
                    143:        }
                    144:        for (lp = list, pn = phead; pn != NULL; pn = pn->next)
                    145:                *lp++ = pn;
                    146:        (void)qsort(list, entries, sizeof(PERSON *), psort);
                    147:        return(list);
                    148: }
                    149:
1.3       kstailey  150: int
1.1       deraadt   151: psort(p, t)
1.3       kstailey  152:        const void *p, *t;
1.1       deraadt   153: {
1.3       kstailey  154:        return(strcmp((*(PERSON **)p)->name, (*(PERSON **)t)->name));
1.1       deraadt   155: }
                    156:
1.3       kstailey  157: void
1.1       deraadt   158: stimeprint(w)
                    159:        WHERE *w;
                    160: {
1.3       kstailey  161:        struct tm *delta;
1.1       deraadt   162:
                    163:        delta = gmtime(&w->idletime);
                    164:        if (!delta->tm_yday)
                    165:                if (!delta->tm_hour)
                    166:                        if (!delta->tm_min)
                    167:                                (void)printf("    -");
                    168:                        else
                    169:                                (void)printf("%5d", delta->tm_min);
                    170:                else
                    171:                        (void)printf("%2d:%02d",
                    172:                            delta->tm_hour, delta->tm_min);
                    173:        else
                    174:                (void)printf("%4dd", delta->tm_yday);
                    175: }