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