1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #ifndef lint 19 static char sccsid[] = "@(#)lprint.c 5.3 (Berkeley) 05/08/89"; 20 #endif /* not lint */ 21 22 #include <sys/types.h> 23 #include <sys/file.h> 24 #include <sys/stat.h> 25 #include <sys/time.h> 26 #include <tzfile.h> 27 #include <stdio.h> 28 #include "finger.h" 29 #include "pathnames.h" 30 31 #define LINE_LEN 80 32 #define TAB_LEN 8 /* 8 spaces between tabs */ 33 #define _PATH_PLAN ".plan" 34 #define _PATH_PROJECT ".project" 35 36 lflag_print() 37 { 38 extern int pplan; 39 register PERSON *pn; 40 41 for (pn = phead;;) { 42 lprint(pn); 43 if (!pplan) { 44 (void)show_text(pn->dir, _PATH_PROJECT, "Project:"); 45 if (!show_text(pn->dir, _PATH_PLAN, "Plan:")) 46 (void)printf("No Plan.\n"); 47 } 48 if (!(pn = pn->next)) 49 break; 50 putchar('\n'); 51 } 52 } 53 54 lprint(pn) 55 register PERSON *pn; 56 { 57 extern time_t now; 58 register struct tm *delta; 59 register WHERE *w; 60 register int cpr, len, maxlen; 61 int oddfield; 62 time_t time(); 63 char *t, *ctime(); 64 65 /* 66 * long format -- 67 * login name 68 * real name 69 * home directory 70 * shell 71 * office, office phone, home phone if available 72 */ 73 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s", 74 pn->name, pn->realname, pn->dir); 75 (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL); 76 77 /* 78 * try and print office, office phone, and home phone on one line; 79 * if that fails, do line filling so it looks nice. 80 */ 81 #define OFFICE_TAG "Office" 82 #define OFFICE_PHONE_TAG "Office Phone" 83 oddfield = 0; 84 if (pn->office && pn->officephone && 85 strlen(pn->office) + strlen(pn->officephone) + 86 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) { 87 (void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office, 88 pn->officephone); 89 oddfield = demi_print(tbuf, oddfield); 90 } else { 91 if (pn->office) { 92 (void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office); 93 oddfield = demi_print(tbuf, oddfield); 94 } 95 if (pn->officephone) { 96 (void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG, 97 pn->officephone); 98 oddfield = demi_print(tbuf, oddfield); 99 } 100 } 101 if (pn->homephone) { 102 (void)sprintf(tbuf, "%s: %s", "Home Phone", pn->homephone); 103 oddfield = demi_print(tbuf, oddfield); 104 } 105 if (oddfield) 106 putchar('\n'); 107 108 /* 109 * long format con't: * if logged in 110 * terminal 111 * idle time 112 * if messages allowed 113 * where logged in from 114 * if not logged in 115 * when last logged in 116 */ 117 /* find out longest device name for this user for formatting */ 118 for (w = pn->whead; w != NULL; w = w->next) 119 if ((len = strlen(w->tty)) > maxlen) 120 maxlen = len; 121 /* find rest of entries for user */ 122 for (w = pn->whead; w != NULL; w = w->next) { 123 switch (w->info) { 124 case LOGGEDIN: 125 cpr = printf("On since %16.16s on %s", 126 ctime(&w->loginat), w->tty); 127 /* 128 * idle time is tough; if have one, print a comma, 129 * then spaces to pad out the device name, then the 130 * idle time. Follow with a comma if a remote login. 131 */ 132 delta = gmtime(&w->idletime); 133 if (delta->tm_yday || delta->tm_hour || delta->tm_min) { 134 cpr += printf("%-*s idle ", 135 maxlen - strlen(w->tty) + 1, ","); 136 if (delta->tm_yday > 0) { 137 cpr += printf("%d day%s ", 138 delta->tm_yday, 139 delta->tm_yday == 1 ? "" : "s"); 140 } 141 cpr += printf("%d:%02d", 142 delta->tm_hour, delta->tm_min); 143 if (*w->host) { 144 putchar(','); 145 ++cpr; 146 } 147 } 148 if (!w->writable) 149 cpr += printf(" (messages off)"); 150 break; 151 case LASTLOG: 152 if (w->loginat == 0) { 153 (void)printf("Never logged in."); 154 break; 155 } 156 t = ctime(&w->loginat); 157 if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2) 158 cpr = printf("Last login %10.10s, %4.4s on %s", 159 t, t + 20, w->tty); 160 else 161 cpr = printf("Last login %16.16s on %s", 162 t, w->tty); 163 break; 164 } 165 if (*w->host) { 166 if (LINE_LEN < (cpr + 6 + strlen(w->host))) 167 (void)printf("\n "); 168 (void)printf(" from %s", w->host); 169 } 170 putchar('\n'); 171 } 172 /* 173 * long format con't: 174 * mail status 175 */ 176 chkmail(pn); 177 } 178 179 demi_print(str, oddfield) 180 char *str; 181 int oddfield; 182 { 183 static int lenlast; 184 int lenthis, maxlen; 185 186 lenthis = strlen(str); 187 if (oddfield) { 188 /* 189 * We left off on an odd number of fields. If we haven't 190 * crossed the midpoint of the screen, and we have room for 191 * the next field, print it on the same line; otherwise, 192 * print it on a new line. 193 * 194 * Note: we insist on having the right hand fields start 195 * no less than 5 tabs out. 196 */ 197 maxlen = 5 * TAB_LEN; 198 if (maxlen < lenlast) 199 maxlen = lenlast; 200 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) + 201 lenthis) <= LINE_LEN) { 202 while(lenlast < (4 * TAB_LEN)) { 203 putchar('\t'); 204 lenlast += TAB_LEN; 205 } 206 (void)printf("\t%s\n", str); /* force one tab */ 207 } else { 208 (void)printf("\n%s", str); /* go to next line */ 209 oddfield = !oddfield; /* this'll be undone below */ 210 } 211 } else 212 (void)printf("%s", str); 213 oddfield = !oddfield; /* toggle odd/even marker */ 214 lenlast = lenthis; 215 return(oddfield); 216 } 217 218 show_text(directory, file_name, header) 219 char *directory, *file_name, *header; 220 { 221 register int fd, n; 222 223 (void)sprintf(tbuf, "%s/%s", directory, file_name); 224 if ((fd = open(tbuf, O_RDONLY, 0)) < 0) 225 return(0); 226 (void)printf("%s\n", header); 227 (void)fflush(stdout); 228 while ((n = read(fd, tbuf, sizeof(tbuf))) > 0) 229 if (write(1, tbuf, n) != n) 230 break; 231 (void)close(fd); 232 return(1); 233 } 234 235 chkmail(pn) 236 PERSON *pn; 237 { 238 register char *date; 239 struct stat sb; 240 241 /* 242 * build path of user's mail box and get stats; if missing 243 * or empty, no mail. 244 */ 245 (void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pn->name); 246 if (stat(tbuf, &sb) < 0 || !sb.st_size) { 247 (void)printf("No unread mail.\n"); 248 return; 249 } 250 251 /* 252 * if access time matches the modification time then we know 253 * that new mail was received but we haven't a clue as to when 254 * it was last read. 255 */ 256 date = ctime(&sb.st_ctime); 257 if (sb.st_atime == sb.st_ctime) 258 (void)printf("New mail received %16.16s.\n", date); 259 else if (sb.st_atime > sb.st_ctime) { 260 date = ctime(&sb.st_atime); 261 (void)printf("No new mail, last read %16.16s.\n", date); 262 } else { 263 (void)printf("Mail received %16.16s; ", date); 264 date = ctime(&sb.st_atime); 265 (void)printf("last read %16.16s.\n", date); 266 } 267 } 268