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.13 (Berkeley) 10/31/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 %.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 = 160 printf("Last login %.16s %.4s (%s) on %s", 161 t, t + 20, tzn, w->tty); 162 else 163 cpr = printf("Last login %.16s (%s) on %s", 164 t, tzn, w->tty); 165 break; 166 } 167 if (*w->host) { 168 if (LINE_LEN < (cpr + 6 + strlen(w->host))) 169 (void)printf("\n "); 170 (void)printf(" from %s", w->host); 171 } 172 putchar('\n'); 173 } 174 } 175 176 demi_print(str, oddfield) 177 char *str; 178 int oddfield; 179 { 180 static int lenlast; 181 int lenthis, maxlen; 182 183 lenthis = strlen(str); 184 if (oddfield) { 185 /* 186 * We left off on an odd number of fields. If we haven't 187 * crossed the midpoint of the screen, and we have room for 188 * the next field, print it on the same line; otherwise, 189 * print it on a new line. 190 * 191 * Note: we insist on having the right hand fields start 192 * no less than 5 tabs out. 193 */ 194 maxlen = 5 * TAB_LEN; 195 if (maxlen < lenlast) 196 maxlen = lenlast; 197 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) + 198 lenthis) <= LINE_LEN) { 199 while(lenlast < (4 * TAB_LEN)) { 200 putchar('\t'); 201 lenlast += TAB_LEN; 202 } 203 (void)printf("\t%s\n", str); /* force one tab */ 204 } else { 205 (void)printf("\n%s", str); /* go to next line */ 206 oddfield = !oddfield; /* this'll be undone below */ 207 } 208 } else 209 (void)printf("%s", str); 210 oddfield = !oddfield; /* toggle odd/even marker */ 211 lenlast = lenthis; 212 return(oddfield); 213 } 214 215 show_text(directory, file_name, header) 216 char *directory, *file_name, *header; 217 { 218 register int ch, lastc; 219 register FILE *fp; 220 221 (void)sprintf(tbuf, "%s/%s", directory, file_name); 222 if ((fp = fopen(tbuf, "r")) == NULL) 223 return(0); 224 (void)printf("%s\n", header); 225 while ((ch = getc(fp)) != EOF) 226 vputc(lastc = ch); 227 if (lastc != '\n') 228 (void)putchar('\n'); 229 (void)fclose(fp); 230 return(1); 231 } 232 233 vputc(ch) 234 register int ch; 235 { 236 int meta; 237 238 if (!isascii(ch)) { 239 (void)putchar('M'); 240 (void)putchar('-'); 241 ch = toascii(ch); 242 meta = 1; 243 } else 244 meta = 0; 245 if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n')) 246 (void)putchar(ch); 247 else { 248 (void)putchar('^'); 249 (void)putchar(ch == '\177' ? '?' : ch | 0100); 250 } 251 } 252