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