lprint.c revision 72109
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38#if 0 39static char sccsid[] = "@(#)lprint.c 8.3 (Berkeley) 4/28/95"; 40#endif 41static const char rcsid[] = 42 "$FreeBSD: head/usr.bin/finger/lprint.c 72109 2001-02-06 20:13:48Z charnier $"; 43#endif /* not lint */ 44 45#include <sys/stat.h> 46#include <ctype.h> 47#include <db.h> 48#include <err.h> 49#include <fcntl.h> 50#include <paths.h> 51#include <pwd.h> 52#include <stdio.h> 53#include <string.h> 54#include <unistd.h> 55#include <utmp.h> 56#include "finger.h" 57#include "pathnames.h" 58#include "extern.h" 59 60#define LINE_LEN 80 61#define TAB_LEN 8 /* 8 spaces between tabs */ 62 63static int demi_print __P((char *, int)); 64static void lprint __P((PERSON *)); 65static void vputc __P((unsigned char)); 66 67void 68lflag_print() 69{ 70 extern int pplan; 71 register PERSON *pn; 72 register int sflag, r; 73 PERSON *tmp; 74 DBT data, key; 75 76 for (sflag = R_FIRST;; sflag = R_NEXT) { 77 r = (*db->seq)(db, &key, &data, sflag); 78 if (r == -1) 79 err(1, "db seq"); 80 if (r == 1) 81 break; 82 memmove(&tmp, data.data, sizeof tmp); 83 pn = tmp; 84 if (sflag != R_FIRST) 85 putchar('\n'); 86 lprint(pn); 87 if (!pplan) { 88 (void)show_text(pn->dir, 89 _PATH_FORWARD, "Mail forwarded to"); 90 (void)show_text(pn->dir, _PATH_PROJECT, "Project"); 91 if (!show_text(pn->dir, _PATH_PLAN, "Plan")) 92 (void)printf("No Plan.\n"); 93 (void)show_text(pn->dir, 94 _PATH_PUBKEY, "Public key"); 95 } 96 } 97} 98 99static void 100lprint(pn) 101 register PERSON *pn; 102{ 103 extern time_t now; 104 register struct tm *delta; 105 register WHERE *w; 106 register int cpr, len, maxlen; 107 struct tm *tp; 108 int oddfield; 109 char *tzn; 110 char t[80]; 111 112 /* 113 * long format -- 114 * login name 115 * real name 116 * home directory 117 * shell 118 * office, office phone, home phone if available 119 * mail status 120 */ 121 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s", 122 pn->name, pn->realname, pn->dir); 123 (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL); 124 125 /* 126 * try and print office, office phone, and home phone on one line; 127 * if that fails, do line filling so it looks nice. 128 */ 129#define OFFICE_TAG "Office" 130#define OFFICE_PHONE_TAG "Office Phone" 131 oddfield = 0; 132 if (pn->office && pn->officephone && 133 strlen(pn->office) + strlen(pn->officephone) + 134 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) { 135 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s", 136 OFFICE_TAG, pn->office, prphone(pn->officephone)); 137 oddfield = demi_print(tbuf, oddfield); 138 } else { 139 if (pn->office) { 140 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", 141 OFFICE_TAG, pn->office); 142 oddfield = demi_print(tbuf, oddfield); 143 } 144 if (pn->officephone) { 145 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", 146 OFFICE_PHONE_TAG, prphone(pn->officephone)); 147 oddfield = demi_print(tbuf, oddfield); 148 } 149 } 150 if (pn->homephone) { 151 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone", 152 prphone(pn->homephone)); 153 oddfield = demi_print(tbuf, oddfield); 154 } 155 if (oddfield) 156 putchar('\n'); 157 158 /* 159 * long format con't: 160 * if logged in 161 * terminal 162 * idle time 163 * if messages allowed 164 * where logged in from 165 * if not logged in 166 * when last logged in 167 */ 168 /* find out longest device name for this user for formatting */ 169 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next) 170 if ((len = strlen(w->tty)) > maxlen) 171 maxlen = len; 172 /* find rest of entries for user */ 173 for (w = pn->whead; w != NULL; w = w->next) { 174 if (w->info == LOGGEDIN) { 175 tp = localtime(&w->loginat); 176 strftime(t, sizeof(t), "%c", tp); 177 tzn = tp->tm_zone; 178 cpr = printf("On since %.16s (%s) on %s", 179 t, tzn, w->tty); 180 /* 181 * idle time is tough; if have one, print a comma, 182 * then spaces to pad out the device name, then the 183 * idle time. Follow with a comma if a remote login. 184 */ 185 delta = gmtime(&w->idletime); 186 if (delta->tm_yday || delta->tm_hour || delta->tm_min) { 187 cpr += printf("%-*s idle ", 188 maxlen - strlen(w->tty) + 1, ","); 189 if (delta->tm_yday > 0) { 190 cpr += printf("%d day%s ", 191 delta->tm_yday, 192 delta->tm_yday == 1 ? "" : "s"); 193 } 194 cpr += printf("%d:%02d", 195 delta->tm_hour, delta->tm_min); 196 if (*w->host) { 197 putchar(','); 198 ++cpr; 199 } 200 } 201 if (!w->writable) 202 cpr += printf(" (messages off)"); 203 } else if (w->loginat == 0) { 204 cpr = printf("Never logged in."); 205 } else { 206 tp = localtime(&w->loginat); 207 strftime(t, sizeof(t), "%c", tp); 208 tzn = tp->tm_zone; 209 if (now - w->loginat > 86400 * 365 / 2) 210 cpr = 211 printf("Last login %.16s %.4s (%s) on %s", 212 t, t + 20, tzn, w->tty); 213 else 214 cpr = printf("Last login %.16s (%s) on %s", 215 t, tzn, w->tty); 216 } 217 if (*w->host) { 218 if (LINE_LEN < (cpr + 6 + strlen(w->host))) 219 (void)printf("\n "); 220 (void)printf(" from %s", w->host); 221 } 222 putchar('\n'); 223 } 224 if (pn->mailrecv == -1) 225 printf("No Mail.\n"); 226 else if (pn->mailrecv > pn->mailread) { 227 tp = localtime(&pn->mailrecv); 228 strftime(t, sizeof(t), "%c", tp); 229 tzn = tp->tm_zone; 230 printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn); 231 tp = localtime(&pn->mailread); 232 strftime(t, sizeof(t), "%c", tp); 233 tzn = tp->tm_zone; 234 printf(" Unread since %.16s %.4s (%s)\n", t, t + 20, tzn); 235 } else { 236 tp = localtime(&pn->mailread); 237 strftime(t, sizeof(t), "%c", tp); 238 tzn = tp->tm_zone; 239 printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn); 240 } 241} 242 243static int 244demi_print(str, oddfield) 245 char *str; 246 int oddfield; 247{ 248 static int lenlast; 249 int lenthis, maxlen; 250 251 lenthis = strlen(str); 252 if (oddfield) { 253 /* 254 * We left off on an odd number of fields. If we haven't 255 * crossed the midpoint of the screen, and we have room for 256 * the next field, print it on the same line; otherwise, 257 * print it on a new line. 258 * 259 * Note: we insist on having the right hand fields start 260 * no less than 5 tabs out. 261 */ 262 maxlen = 5 * TAB_LEN; 263 if (maxlen < lenlast) 264 maxlen = lenlast; 265 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) + 266 lenthis) <= LINE_LEN) { 267 while(lenlast < (4 * TAB_LEN)) { 268 putchar('\t'); 269 lenlast += TAB_LEN; 270 } 271 (void)printf("\t%s\n", str); /* force one tab */ 272 } else { 273 (void)printf("\n%s", str); /* go to next line */ 274 oddfield = !oddfield; /* this'll be undone below */ 275 } 276 } else 277 (void)printf("%s", str); 278 oddfield = !oddfield; /* toggle odd/even marker */ 279 lenlast = lenthis; 280 return(oddfield); 281} 282 283int 284show_text(directory, file_name, header) 285 char *directory, *file_name, *header; 286{ 287 struct stat sb; 288 register FILE *fp; 289 register int ch, cnt; 290 register char *p, lastc; 291 int fd, nr; 292 293 lastc = '\0'; 294 295 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name); 296 if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) || 297 sb.st_size == 0) 298 return(0); 299 300 /* If short enough, and no newlines, show it on a single line.*/ 301 if (sb.st_size <= LINE_LEN - strlen(header) - 5) { 302 nr = read(fd, tbuf, sizeof(tbuf)); 303 if (nr <= 0) { 304 (void)close(fd); 305 return(0); 306 } 307 for (p = tbuf, cnt = nr; cnt--; ++p) 308 if (*p == '\n') 309 break; 310 if (cnt <= 1) { 311 if (*header != '\0') 312 (void)printf("%s: ", header); 313 for (p = tbuf, cnt = nr; cnt--; ++p) 314 if (*p != '\r') 315 vputc(lastc = *p); 316 if (lastc != '\n') 317 (void)putchar('\n'); 318 (void)close(fd); 319 return(1); 320 } 321 else 322 (void)lseek(fd, 0L, SEEK_SET); 323 } 324 if ((fp = fdopen(fd, "r")) == NULL) 325 return(0); 326 if (*header != '\0') 327 (void)printf("%s:\n", header); 328 while ((ch = getc(fp)) != EOF) 329 if (ch != '\r') 330 vputc(lastc = ch); 331 if (lastc != '\n') 332 (void)putchar('\n'); 333 (void)fclose(fp); 334 return(1); 335} 336 337static void 338vputc(ch) 339 register unsigned char ch; 340{ 341 int meta; 342 343 if (!isprint(ch) && !isascii(ch)) { 344 (void)putchar('M'); 345 (void)putchar('-'); 346 ch = toascii(ch); 347 meta = 1; 348 } else 349 meta = 0; 350 if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n'))) 351 (void)putchar(ch); 352 else { 353 (void)putchar('^'); 354 (void)putchar(ch == '\177' ? '?' : ch | 0100); 355 } 356} 357