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