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