lprint.c revision 9987
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 38static char sccsid[] = "@(#)lprint.c 8.1 (Berkeley) 6/6/93"; 39#endif /* not lint */ 40 41#include <sys/types.h> 42#include <sys/stat.h> 43#include <sys/time.h> 44#include <fcntl.h> 45#include <time.h> 46#include <db.h> 47#include <pwd.h> 48#include <utmp.h> 49#include <errno.h> 50#include <unistd.h> 51#include <stdio.h> 52#include <ctype.h> 53#include <string.h> 54#include <paths.h> 55#include "finger.h" 56 57#define LINE_LEN 80 58#define TAB_LEN 8 /* 8 spaces between tabs */ 59#define _PATH_FORWARD ".forward" 60#define _PATH_PLAN ".plan" 61#define _PATH_PROJECT ".project" 62 63static int demi_print __P((char *, int)); 64static void lprint __P((PERSON *)); 65static int show_text __P((char *, char *, char *)); 66static void vputc __P((int)); 67 68void 69lflag_print() 70{ 71 extern int pplan; 72 register PERSON *pn; 73 register int sflag, r; 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("db seq: %s", strerror(errno)); 80 if (r == 1) 81 break; 82 pn = *(PERSON **)data.data; 83 if (sflag != R_FIRST) 84 putchar('\n'); 85 lprint(pn); 86 if (!pplan) { 87 (void)show_text(pn->dir, 88 _PATH_FORWARD, "Mail forwarded to"); 89 (void)show_text(pn->dir, _PATH_PROJECT, "Project"); 90 if (!show_text(pn->dir, _PATH_PLAN, "Plan")) 91 (void)printf("No Plan.\n"); 92 } 93 } 94} 95 96static void 97lprint(pn) 98 register PERSON *pn; 99{ 100 extern time_t now; 101 register struct tm *delta; 102 register WHERE *w; 103 register int cpr, len, maxlen; 104 struct tm *tp; 105 int oddfield; 106 char *t, *tzn; 107 108 /* 109 * long format -- 110 * login name 111 * real name 112 * home directory 113 * shell 114 * office, office phone, home phone if available 115 * mail status 116 */ 117 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s", 118 pn->name, pn->realname, pn->dir); 119 (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL); 120 121 /* 122 * try and print office, office phone, and home phone on one line; 123 * if that fails, do line filling so it looks nice. 124 */ 125#define OFFICE_TAG "Office" 126#define OFFICE_PHONE_TAG "Office Phone" 127 oddfield = 0; 128 if (pn->office && pn->officephone && 129 strlen(pn->office) + strlen(pn->officephone) + 130 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) { 131 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s", 132 OFFICE_TAG, pn->office, prphone(pn->officephone)); 133 oddfield = demi_print(tbuf, oddfield); 134 } else { 135 if (pn->office) { 136 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", 137 OFFICE_TAG, pn->office); 138 oddfield = demi_print(tbuf, oddfield); 139 } 140 if (pn->officephone) { 141 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", 142 OFFICE_PHONE_TAG, prphone(pn->officephone)); 143 oddfield = demi_print(tbuf, oddfield); 144 } 145 } 146 if (pn->homephone) { 147 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone", 148 prphone(pn->homephone)); 149 oddfield = demi_print(tbuf, oddfield); 150 } 151 if (oddfield) 152 putchar('\n'); 153 154 /* 155 * long format con't: 156 * if logged in 157 * terminal 158 * idle time 159 * if messages allowed 160 * where logged in from 161 * if not logged in 162 * when last logged in 163 */ 164 /* find out longest device name for this user for formatting */ 165 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next) 166 if ((len = strlen(w->tty)) > maxlen) 167 maxlen = len; 168 /* find rest of entries for user */ 169 for (w = pn->whead; w != NULL; w = w->next) { 170 switch (w->info) { 171 case LOGGEDIN: 172 tp = localtime(&w->loginat); 173 t = asctime(tp); 174 tzn = tp->tm_zone; 175 cpr = printf("On since %.16s (%s) on %s", 176 t, tzn, w->tty); 177 /* 178 * idle time is tough; if have one, print a comma, 179 * then spaces to pad out the device name, then the 180 * idle time. Follow with a comma if a remote login. 181 */ 182 delta = gmtime(&w->idletime); 183 if (delta->tm_yday || delta->tm_hour || delta->tm_min) { 184 cpr += printf("%-*s idle ", 185 maxlen - strlen(w->tty) + 1, ","); 186 if (delta->tm_yday > 0) { 187 cpr += printf("%d day%s ", 188 delta->tm_yday, 189 delta->tm_yday == 1 ? "" : "s"); 190 } 191 cpr += printf("%d:%02d", 192 delta->tm_hour, delta->tm_min); 193 if (*w->host) { 194 putchar(','); 195 ++cpr; 196 } 197 } 198 if (!w->writable) 199 cpr += printf(" (messages off)"); 200 break; 201 case LASTLOG: 202 if (w->loginat == 0) { 203 (void)printf("Never logged in."); 204 break; 205 } 206 tp = localtime(&w->loginat); 207 t = asctime(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 break; 217 } 218 if (*w->host) { 219 if (LINE_LEN < (cpr + 6 + strlen(w->host))) 220 (void)printf("\n "); 221 (void)printf(" from %s", w->host); 222 } 223 putchar('\n'); 224 } 225 if (pn->mailrecv == -1) 226 printf("No Mail.\n"); 227 else if (pn->mailrecv > pn->mailread) { 228 tp = localtime(&pn->mailrecv); 229 t = asctime(tp); 230 tzn = tp->tm_zone; 231 printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn); 232 tp = localtime(&pn->mailread); 233 t = asctime(tp); 234 tzn = tp->tm_zone; 235 printf(" Unread since %.16s %.4s (%s)\n", t, t + 20, tzn); 236 } else { 237 tp = localtime(&pn->mailread); 238 t = asctime(tp); 239 tzn = tp->tm_zone; 240 printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn); 241 } 242} 243 244static int 245demi_print(str, oddfield) 246 char *str; 247 int oddfield; 248{ 249 static int lenlast; 250 int lenthis, maxlen; 251 252 lenthis = strlen(str); 253 if (oddfield) { 254 /* 255 * We left off on an odd number of fields. If we haven't 256 * crossed the midpoint of the screen, and we have room for 257 * the next field, print it on the same line; otherwise, 258 * print it on a new line. 259 * 260 * Note: we insist on having the right hand fields start 261 * no less than 5 tabs out. 262 */ 263 maxlen = 5 * TAB_LEN; 264 if (maxlen < lenlast) 265 maxlen = lenlast; 266 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) + 267 lenthis) <= LINE_LEN) { 268 while(lenlast < (4 * TAB_LEN)) { 269 putchar('\t'); 270 lenlast += TAB_LEN; 271 } 272 (void)printf("\t%s\n", str); /* force one tab */ 273 } else { 274 (void)printf("\n%s", str); /* go to next line */ 275 oddfield = !oddfield; /* this'll be undone below */ 276 } 277 } else 278 (void)printf("%s", str); 279 oddfield = !oddfield; /* toggle odd/even marker */ 280 lenlast = lenthis; 281 return(oddfield); 282} 283 284static int 285show_text(directory, file_name, header) 286 char *directory, *file_name, *header; 287{ 288 struct stat sb; 289 register FILE *fp; 290 register int ch, cnt, lastc; 291 register char *p; 292 int fd, nr; 293 294 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name); 295 if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) || 296 sb.st_size == 0) 297 return(0); 298 299 /* If short enough, and no newlines, show it on a single line.*/ 300 if (sb.st_size <= LINE_LEN - strlen(header) - 5) { 301 nr = read(fd, tbuf, sizeof(tbuf)); 302 if (nr <= 0) { 303 (void)close(fd); 304 return(0); 305 } 306 for (p = tbuf, cnt = nr; cnt--; ++p) 307 if (*p == '\n') 308 break; 309 if (cnt <= 1) { 310 (void)printf("%s: ", header); 311 for (p = tbuf, cnt = nr; cnt--; ++p) 312 vputc(lastc = *p); 313 if (lastc != '\n') 314 (void)putchar('\n'); 315 (void)close(fd); 316 return(1); 317 } 318 else 319 (void)lseek(fd, 0L, SEEK_SET); 320 } 321 if ((fp = fdopen(fd, "r")) == NULL) 322 return(0); 323 (void)printf("%s:\n", header); 324 while ((ch = getc(fp)) != EOF) 325 vputc(lastc = ch); 326 if (lastc != '\n') 327 (void)putchar('\n'); 328 (void)fclose(fp); 329 return(1); 330} 331 332static void 333vputc(ch) 334 register int ch; 335{ 336 int meta; 337 338 if (!isascii(ch)) { 339 (void)putchar('M'); 340 (void)putchar('-'); 341 ch = toascii(ch); 342 meta = 1; 343 } else 344 meta = 0; 345 if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n')) 346 (void)putchar(ch); 347 else { 348 (void)putchar('^'); 349 (void)putchar(ch == '\177' ? '?' : ch | 0100); 350 } 351} 352