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

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