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