correct printing of \n at the end of .plan; and use fopen() not freopen()
[unix-history] / usr / src / usr.bin / finger / sprint.c
CommitLineData
6b8910b8
KB
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
6b8910b8
KB
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#ifndef lint
f156ea40 19static char sccsid[] = "@(#)sprint.c 5.5 (Berkeley) %G%";
6b8910b8
KB
20#endif /* not lint */
21
22#include <sys/types.h>
23#include <sys/time.h>
24#include <tzfile.h>
25#include <stdio.h>
26#include "finger.h"
27
28extern int entries;
29
30sflag_print()
31{
32 extern time_t now;
33 register PERSON *pn;
7619ff47 34 register WHERE *w;
6b8910b8
KB
35 register int cnt;
36 register char *p;
37 PERSON **list, **sort();
38 time_t time();
f156ea40 39 char *ctime(), *prphone();
6b8910b8
KB
40
41 list = sort();
42 /*
43 * short format --
44 * login name
45 * real name
46 * terminal name (the XX of ttyXX)
47 * if terminal writeable (add an '*' to the terminal name
48 * if not)
49 * if logged in show idle time and day logged in, else
50 * show last login date and time. If > 6 moths,
51 * show year instead of time.
52 * office location
53 * office phone
54 */
55#define MAXREALNAME 20
56 (void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
f156ea40 57 "Name", "Tty Idle Login Office Office Phone");
6b8910b8
KB
58 for (cnt = 0; cnt < entries; ++cnt) {
59 pn = list[cnt];
7619ff47
EW
60 for (w = pn->whead; w != NULL; w = w->next) {
61 (void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
62 pn->name, MAXREALNAME, MAXREALNAME,
63 pn->realname ? pn->realname : "");
64 if (!w->loginat) {
65 (void)printf(" * * No logins ");
66 goto office;
67 }
68 (void)putchar(w->info == LOGGEDIN && !w->writable ?
69 '*' : ' ');
70 if (*w->tty)
71 (void)printf("%-2.2s ",
72 w->tty[0] != 't' || w->tty[1] != 't' ||
73 w->tty[2] != 'y' ? w->tty : w->tty + 3);
74 else
75 (void)printf(" ");
76 if (w->info == LOGGEDIN) {
77 stimeprint(w);
78 (void)printf(" ");
79 } else
80 (void)printf(" * ");
81 p = ctime(&w->loginat);
82 (void)printf("%.6s", p + 4);
83 if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
84 (void)printf(" %.4s", p + 20);
85 else
86 (void)printf(" %.5s", p + 11);
87office: if (pn->office)
f156ea40 88 (void)printf(" %-10.10s", pn->office);
7619ff47 89 else if (pn->officephone)
f156ea40 90 (void)printf(" %-10.10s", " ");
7619ff47 91 if (pn->officephone)
f156ea40
KB
92 (void)printf(" %-.15s",
93 prphone(pn->officephone));
7619ff47 94 putchar('\n');
6b8910b8 95 }
6b8910b8
KB
96 }
97}
98
99PERSON **
100sort()
101{
7619ff47 102 register PERSON *pn, **lp;
6b8910b8
KB
103 PERSON **list;
104 int psort();
105 char *malloc();
106
107 if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
108 (void)fprintf(stderr, "finger: out of space.\n");
109 exit(1);
110 }
7619ff47
EW
111 for (lp = list, pn = phead; pn != NULL; pn = pn->next)
112 *lp++ = pn;
6b8910b8
KB
113 (void)qsort(list, entries, sizeof(PERSON *), psort);
114 return(list);
115}
116
117psort(p, t)
118 PERSON **p, **t;
119{
120 return(strcmp((*p)->name, (*t)->name));
121}
122
7619ff47
EW
123stimeprint(w)
124 WHERE *w;
6b8910b8
KB
125{
126 register struct tm *delta;
127
7619ff47 128 delta = gmtime(&w->idletime);
6b8910b8
KB
129 if (!delta->tm_yday)
130 if (!delta->tm_hour)
131 if (!delta->tm_min)
132 (void)printf(" ");
133 else
134 (void)printf("%5d", delta->tm_min);
135 else
136 (void)printf("%2d:%02d",
137 delta->tm_hour, delta->tm_min);
138 else
139 (void)printf("%4dd", delta->tm_yday);
140}