1 /* $NetBSD: lprint.c,v 1.12 1998/12/19 16:00:33 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)lprint.c 8.3 (Berkeley) 4/28/95"; 43 #else 44 __RCSID( "$NetBSD: lprint.c,v 1.12 1998/12/19 16:00:33 christos Exp $"); 45 #endif 46 #endif /* not lint */ 47 48 #include <sys/types.h> 49 #include <sys/stat.h> 50 #include <sys/time.h> 51 #include <fcntl.h> 52 #include <time.h> 53 #include <tzfile.h> 54 #include <db.h> 55 #include <err.h> 56 #include <pwd.h> 57 #include <utmp.h> 58 #include <errno.h> 59 #include <unistd.h> 60 #include <stdio.h> 61 #include <string.h> 62 #include <time.h> 63 #include <ctype.h> 64 #include <string.h> 65 #include <paths.h> 66 #include <vis.h> 67 68 #include "finger.h" 69 #include "extern.h" 70 71 #define LINE_LEN 80 72 #define TAB_LEN 8 /* 8 spaces between tabs */ 73 #define _PATH_FORWARD ".forward" 74 #define _PATH_PLAN ".plan" 75 #define _PATH_PROJECT ".project" 76 77 static int demi_print __P((char *, int)); 78 static void lprint __P((PERSON *)); 79 static int show_text __P((char *, char *, char *)); 80 static void vputc __P((int)); 81 82 #ifdef __SVR4 83 #define TIMEZONE(a) tzname[0] 84 #else 85 #define TIMEZONE(a) (a)->tm_zone 86 #endif 87 88 void 89 lflag_print() 90 { 91 PERSON *pn; 92 int sflag, r; 93 PERSON *tmp; 94 DBT data, key; 95 96 for (sflag = R_FIRST;; sflag = R_NEXT) { 97 r = (*db->seq)(db, &key, &data, sflag); 98 if (r == -1) 99 err(1, "db seq"); 100 if (r == 1) 101 break; 102 memmove(&tmp, data.data, sizeof tmp); 103 pn = tmp; 104 if (sflag != R_FIRST) 105 putchar('\n'); 106 lprint(pn); 107 if (!pplan) { 108 (void)show_text(pn->dir, 109 _PATH_FORWARD, "Mail forwarded to"); 110 (void)show_text(pn->dir, _PATH_PROJECT, "Project"); 111 if (!show_text(pn->dir, _PATH_PLAN, "Plan")) 112 (void)printf("No Plan.\n"); 113 } 114 } 115 } 116 117 static void 118 lprint(pn) 119 PERSON *pn; 120 { 121 struct tm *delta; 122 WHERE *w; 123 int cpr, len, maxlen; 124 struct tm *tp; 125 int oddfield; 126 char *t; 127 const char *tzn; 128 129 cpr = 0; 130 /* 131 * long format -- 132 * login name 133 * real name 134 * home directory 135 * shell 136 * office, office phone, home phone if available 137 * mail status 138 */ 139 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s", 140 pn->name, pn->realname, pn->dir); 141 (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL); 142 143 if (gflag) 144 goto no_gecos; 145 /* 146 * try and print office, office phone, and home phone on one line; 147 * if that fails, do line filling so it looks nice. 148 */ 149 #define OFFICE_TAG "Office" 150 #define OFFICE_PHONE_TAG "Office Phone" 151 oddfield = 0; 152 if (pn->office && pn->officephone && 153 strlen(pn->office) + strlen(pn->officephone) + 154 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) { 155 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s", 156 OFFICE_TAG, pn->office, prphone(pn->officephone)); 157 oddfield = demi_print(tbuf, oddfield); 158 } else { 159 if (pn->office) { 160 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", 161 OFFICE_TAG, pn->office); 162 oddfield = demi_print(tbuf, oddfield); 163 } 164 if (pn->officephone) { 165 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", 166 OFFICE_PHONE_TAG, prphone(pn->officephone)); 167 oddfield = demi_print(tbuf, oddfield); 168 } 169 } 170 if (pn->homephone) { 171 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone", 172 prphone(pn->homephone)); 173 oddfield = demi_print(tbuf, oddfield); 174 } 175 if (oddfield) 176 putchar('\n'); 177 178 no_gecos: 179 /* 180 * long format con't: 181 * if logged in 182 * terminal 183 * idle time 184 * if messages allowed 185 * where logged in from 186 * if not logged in 187 * when last logged in 188 */ 189 /* find out longest device name for this user for formatting */ 190 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next) 191 if ((len = strlen(w->tty)) > maxlen) 192 maxlen = len; 193 /* find rest of entries for user */ 194 for (w = pn->whead; w != NULL; w = w->next) { 195 switch (w->info) { 196 case LOGGEDIN: 197 tp = localtime(&w->loginat); 198 t = asctime(tp); 199 tzn = TIMEZONE(tp); 200 cpr = printf("On since %.16s (%s) on %s", 201 t, tzn, w->tty); 202 /* 203 * idle time is tough; if have one, print a comma, 204 * then spaces to pad out the device name, then the 205 * idle time. Follow with a comma if a remote login. 206 */ 207 delta = gmtime(&w->idletime); 208 if (delta->tm_yday || delta->tm_hour || delta->tm_min) { 209 cpr += printf("%-*s idle ", 210 (int)(maxlen - strlen(w->tty) + 1), ","); 211 if (delta->tm_yday > 0) { 212 cpr += printf("%d day%s ", 213 delta->tm_yday, 214 delta->tm_yday == 1 ? "" : "s"); 215 } 216 cpr += printf("%d:%02d", 217 delta->tm_hour, delta->tm_min); 218 if (*w->host) { 219 putchar(','); 220 ++cpr; 221 } 222 } 223 if (!w->writable) 224 cpr += printf(" (messages off)"); 225 break; 226 case LASTLOG: 227 if (w->loginat == 0) { 228 (void)printf("Never logged in."); 229 break; 230 } 231 tp = localtime(&w->loginat); 232 t = asctime(tp); 233 tzn = TIMEZONE(tp); 234 if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2) 235 cpr = 236 printf("Last login %.16s %.4s (%s) on %s", 237 t, t + 20, tzn, w->tty); 238 else 239 cpr = printf("Last login %.16s (%s) on %s", 240 t, tzn, w->tty); 241 break; 242 } 243 if (*w->host) { 244 if (LINE_LEN < (cpr + 6 + strlen(w->host))) 245 (void)printf("\n "); 246 (void)printf(" from %s", w->host); 247 } 248 putchar('\n'); 249 } 250 if (pn->mailrecv == -1) 251 printf("No Mail.\n"); 252 else if (pn->mailrecv > pn->mailread) { 253 tp = localtime(&pn->mailrecv); 254 t = asctime(tp); 255 tzn = TIMEZONE(tp); 256 printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn); 257 tp = localtime(&pn->mailread); 258 t = asctime(tp); 259 tzn = TIMEZONE(tp); 260 printf(" Unread since %.16s %.4s (%s)\n", t, t + 20, tzn); 261 } else { 262 tp = localtime(&pn->mailread); 263 t = asctime(tp); 264 tzn = TIMEZONE(tp); 265 printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn); 266 } 267 } 268 269 static int 270 demi_print(str, oddfield) 271 char *str; 272 int oddfield; 273 { 274 static int lenlast; 275 int lenthis, maxlen; 276 277 lenthis = strlen(str); 278 if (oddfield) { 279 /* 280 * We left off on an odd number of fields. If we haven't 281 * crossed the midpoint of the screen, and we have room for 282 * the next field, print it on the same line; otherwise, 283 * print it on a new line. 284 * 285 * Note: we insist on having the right hand fields start 286 * no less than 5 tabs out. 287 */ 288 maxlen = 5 * TAB_LEN; 289 if (maxlen < lenlast) 290 maxlen = lenlast; 291 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) + 292 lenthis) <= LINE_LEN) { 293 while(lenlast < (4 * TAB_LEN)) { 294 putchar('\t'); 295 lenlast += TAB_LEN; 296 } 297 (void)printf("\t%s\n", str); /* force one tab */ 298 } else { 299 (void)printf("\n%s", str); /* go to next line */ 300 oddfield = !oddfield; /* this'll be undone below */ 301 } 302 } else 303 (void)printf("%s", str); 304 oddfield = !oddfield; /* toggle odd/even marker */ 305 lenlast = lenthis; 306 return(oddfield); 307 } 308 309 static int 310 show_text(directory, file_name, header) 311 char *directory, *file_name, *header; 312 { 313 struct stat sb; 314 FILE *fp; 315 int ch, cnt, lastc; 316 char *p; 317 int fd, nr; 318 319 lastc = 0; 320 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name); 321 if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) || 322 sb.st_size == 0) 323 return(0); 324 325 /* If short enough, and no newlines, show it on a single line.*/ 326 if (sb.st_size <= LINE_LEN - strlen(header) - 5) { 327 nr = read(fd, tbuf, sizeof(tbuf)); 328 if (nr <= 0) { 329 (void)close(fd); 330 return(0); 331 } 332 for (p = tbuf, cnt = nr; cnt--; ++p) 333 if (*p == '\n') 334 break; 335 if (cnt <= 1) { 336 (void)printf("%s: ", header); 337 for (p = tbuf, cnt = nr; cnt--; ++p) 338 vputc(lastc = *p); 339 if (lastc != '\n') 340 (void)putchar('\n'); 341 (void)close(fd); 342 return(1); 343 } 344 else 345 (void)lseek(fd, 0L, SEEK_SET); 346 } 347 if ((fp = fdopen(fd, "r")) == NULL) 348 return(0); 349 (void)printf("%s:\n", header); 350 while ((ch = getc(fp)) != EOF) 351 vputc(lastc = ch); 352 if (lastc != '\n') 353 (void)putchar('\n'); 354 (void)fclose(fp); 355 return(1); 356 } 357 358 static void 359 vputc(ch) 360 int ch; 361 { 362 char visout[5], *s2; 363 364 ch = toascii(ch); 365 vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0); 366 for (s2 = visout; *s2; s2++) 367 (void)putchar(*s2); 368 } 369