[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.14

1.14    ! millert     1: /*     $OpenBSD: sprint.c,v 1.13 2009/10/27 23:59:38 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.7       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: #include <sys/types.h>
                     36: #include <sys/time.h>
                     37: #include <tzfile.h>
                     38: #include <stdio.h>
1.3       kstailey   39: #include <stdlib.h>
1.9       david      40: #include <string.h>
1.5       mickey     41: #include <err.h>
1.1       deraadt    42: #include "finger.h"
1.3       kstailey   43: #include "extern.h"
1.1       deraadt    44:
1.3       kstailey   45: void
1.8       deraadt    46: sflag_print(void)
1.1       deraadt    47: {
1.3       kstailey   48:        PERSON *pn;
                     49:        WHERE *w;
                     50:        int cnt;
                     51:        char *p;
                     52:        PERSON **list;
1.1       deraadt    53:
                     54:        list = sort();
                     55:        /*
                     56:         * short format --
                     57:         *      login name
                     58:         *      real name
                     59:         *      terminal name (the XX of ttyXX)
                     60:         *      if terminal writeable (add an '*' to the terminal name
                     61:         *              if not)
                     62:         *      if logged in show idle time and day logged in, else
1.3       kstailey   63:         *              show last login date and time.  If > 6 months,
                     64:         *              show year instead of time.  If < 6 days,
                     65:         *              show day name instead of month & day.
                     66:         *      if -h given
                     67:         *              remote host
                     68:         *      else if -o given (overriding -h) (default)
                     69:         *              office location
                     70:         *              office phone
1.1       deraadt    71:         */
1.6       deraadt    72: #define NAME_WIDTH     8
1.1       deraadt    73: #define        MAXREALNAME     20
1.3       kstailey   74: #define        MAXHOSTNAME     20
1.6       deraadt    75:        (void)printf("%-*.*s %-*s %s %s\n",
                     76:            NAME_WIDTH, UT_NAMESIZE, "Login", MAXREALNAME,
1.3       kstailey   77:            "Name", "Tty  Idle  Login Time  ",
                     78:            (oflag) ? "Office     Office Phone" : "Where");
1.1       deraadt    79:        for (cnt = 0; cnt < entries; ++cnt) {
                     80:                pn = list[cnt];
                     81:                for (w = pn->whead; w != NULL; w = w->next) {
1.6       deraadt    82:                        (void)printf("%-*.*s %-*.*s ",
1.14    ! millert    83:                            NAME_WIDTH, UT_NAMESIZE, pn->name,
1.6       deraadt    84:                            MAXREALNAME, MAXREALNAME,
1.14    ! millert    85:                            pn->realname ? pn->realname : "");
1.1       deraadt    86:                        if (!w->loginat) {
                     87:                                (void)printf("  *     *  No logins   ");
                     88:                                goto office;
                     89:                        }
                     90:                        (void)putchar(w->info == LOGGEDIN && !w->writable ?
                     91:                            '*' : ' ');
                     92:                        if (*w->tty)
                     93:                                (void)printf("%-2.2s ",
                     94:                                    w->tty[0] != 't' || w->tty[1] != 't' ||
                     95:                                    w->tty[2] != 'y' ? w->tty : w->tty + 3);
                     96:                        else
                     97:                                (void)printf("   ");
                     98:                        if (w->info == LOGGEDIN) {
                     99:                                stimeprint(w);
                    100:                                (void)printf("  ");
                    101:                        } else
                    102:                                (void)printf("    *  ");
                    103:                        p = ctime(&w->loginat);
1.3       kstailey  104:                        if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
                    105:                                (void)printf("   %.3s", p);
                    106:                        else
                    107:                                (void)printf("%.6s", p + 4);
1.1       deraadt   108:                        if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
1.3       kstailey  109:                                (void)printf(" %.4s ", p + 20);
1.1       deraadt   110:                        else
                    111:                                (void)printf(" %.5s", p + 11);
1.3       kstailey  112: office:
                    113:                        putchar(' ');
                    114:                        if (oflag) {
                    115:                                if (pn->office)
1.14    ! millert   116:                                        (void)printf("%-10.10s", pn->office);
1.3       kstailey  117:                                else if (pn->officephone)
                    118:                                        (void)printf("%-10.10s", " ");
                    119:                                if (pn->officephone)
                    120:                                        (void)printf(" %-.15s",
1.14    ! millert   121:                                            prphone(pn->officephone));
1.3       kstailey  122:                        } else
                    123:                                (void)printf("%.*s", MAXHOSTNAME, w->host);
1.1       deraadt   124:                        putchar('\n');
                    125:                }
                    126:        }
                    127: }
                    128:
                    129: PERSON **
1.8       deraadt   130: sort(void)
1.1       deraadt   131: {
1.3       kstailey  132:        PERSON *pn, **lp;
1.1       deraadt   133:        PERSON **list;
                    134:
1.12      deraadt   135:        if (!(list = (PERSON **)calloc((u_int)entries, sizeof(PERSON *))))
1.5       mickey    136:                err(1, "malloc");
1.1       deraadt   137:        for (lp = list, pn = phead; pn != NULL; pn = pn->next)
                    138:                *lp++ = pn;
                    139:        (void)qsort(list, entries, sizeof(PERSON *), psort);
1.10      tedu      140:        return (list);
1.1       deraadt   141: }
                    142:
1.3       kstailey  143: int
1.8       deraadt   144: psort(const void *p, const void *t)
1.1       deraadt   145: {
1.10      tedu      146:        return (strcmp((*(PERSON **)p)->name, (*(PERSON **)t)->name));
1.1       deraadt   147: }
                    148:
1.3       kstailey  149: void
1.8       deraadt   150: stimeprint(WHERE *w)
1.1       deraadt   151: {
1.3       kstailey  152:        struct tm *delta;
1.1       deraadt   153:
                    154:        delta = gmtime(&w->idletime);
1.10      tedu      155:        if (!delta->tm_yday) {
                    156:                if (!delta->tm_hour) {
1.1       deraadt   157:                        if (!delta->tm_min)
                    158:                                (void)printf("    -");
                    159:                        else
                    160:                                (void)printf("%5d", delta->tm_min);
1.10      tedu      161:                 } else {
1.1       deraadt   162:                        (void)printf("%2d:%02d",
                    163:                            delta->tm_hour, delta->tm_min);
1.10      tedu      164:                 }
                    165:        } else
1.1       deraadt   166:                (void)printf("%4dd", delta->tm_yday);
                    167: }