137660Sbostic /* 237660Sbostic * Copyright (c) 1989 The Regents of the University of California. 337660Sbostic * All rights reserved. 437660Sbostic * 540027Sbostic * This code is derived from software contributed to Berkeley by 640027Sbostic * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 740027Sbostic * 842731Sbostic * %sccs.include.redist.c% 937660Sbostic */ 1037660Sbostic 1137660Sbostic #ifndef lint 12*42919Sbostic static char sccsid[] = "@(#)lprint.c 5.11 (Berkeley) 06/05/90"; 1337660Sbostic #endif /* not lint */ 1437660Sbostic 1537660Sbostic #include <sys/types.h> 1637660Sbostic #include <sys/file.h> 1737660Sbostic #include <sys/stat.h> 1837660Sbostic #include <sys/time.h> 1937660Sbostic #include <tzfile.h> 2037660Sbostic #include <stdio.h> 2138901Sbostic #include <ctype.h> 2237660Sbostic #include "finger.h" 2337660Sbostic #include "pathnames.h" 2437660Sbostic 2537660Sbostic #define LINE_LEN 80 2637660Sbostic #define TAB_LEN 8 /* 8 spaces between tabs */ 2737660Sbostic #define _PATH_PLAN ".plan" 2837660Sbostic #define _PATH_PROJECT ".project" 2937660Sbostic 3037660Sbostic lflag_print() 3137660Sbostic { 3237660Sbostic extern int pplan; 3337660Sbostic register PERSON *pn; 3437660Sbostic 3537664Sedward for (pn = phead;;) { 3637660Sbostic lprint(pn); 3737660Sbostic if (!pplan) { 3837664Sedward (void)show_text(pn->dir, _PATH_PROJECT, "Project:"); 3937660Sbostic if (!show_text(pn->dir, _PATH_PLAN, "Plan:")) 4037660Sbostic (void)printf("No Plan.\n"); 4137660Sbostic } 4237660Sbostic if (!(pn = pn->next)) 4337660Sbostic break; 4437660Sbostic putchar('\n'); 4537660Sbostic } 4637660Sbostic } 4737660Sbostic 4837660Sbostic lprint(pn) 4937660Sbostic register PERSON *pn; 5037660Sbostic { 5137660Sbostic extern time_t now; 5237660Sbostic register struct tm *delta; 5337664Sedward register WHERE *w; 5437660Sbostic register int cpr, len, maxlen; 55*42919Sbostic struct tm *tp; 5637660Sbostic int oddfield; 5737660Sbostic time_t time(); 58*42919Sbostic char *t, *tzn, *prphone(); 5937660Sbostic 6037660Sbostic /* 6137660Sbostic * long format -- 6237660Sbostic * login name 6337660Sbostic * real name 6437660Sbostic * home directory 6537660Sbostic * shell 6637660Sbostic * office, office phone, home phone if available 6737660Sbostic */ 6837660Sbostic (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s", 6937660Sbostic pn->name, pn->realname, pn->dir); 7037660Sbostic (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL); 7137660Sbostic 7237660Sbostic /* 7337660Sbostic * try and print office, office phone, and home phone on one line; 7437660Sbostic * if that fails, do line filling so it looks nice. 7537660Sbostic */ 7637660Sbostic #define OFFICE_TAG "Office" 7737660Sbostic #define OFFICE_PHONE_TAG "Office Phone" 7837660Sbostic oddfield = 0; 7937660Sbostic if (pn->office && pn->officephone && 8037660Sbostic strlen(pn->office) + strlen(pn->officephone) + 8137660Sbostic sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) { 8237660Sbostic (void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office, 8338052Sbostic prphone(pn->officephone)); 8437660Sbostic oddfield = demi_print(tbuf, oddfield); 8537660Sbostic } else { 8637660Sbostic if (pn->office) { 8737660Sbostic (void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office); 8837660Sbostic oddfield = demi_print(tbuf, oddfield); 8937660Sbostic } 9037660Sbostic if (pn->officephone) { 9137660Sbostic (void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG, 9238052Sbostic prphone(pn->officephone)); 9337660Sbostic oddfield = demi_print(tbuf, oddfield); 9437660Sbostic } 9537660Sbostic } 9637660Sbostic if (pn->homephone) { 9738052Sbostic (void)sprintf(tbuf, "%s: %s", "Home Phone", 9838052Sbostic prphone(pn->homephone)); 9937660Sbostic oddfield = demi_print(tbuf, oddfield); 10037660Sbostic } 10137660Sbostic if (oddfield) 10237660Sbostic putchar('\n'); 10337660Sbostic 10437660Sbostic /* 10537664Sedward * long format con't: * if logged in 10637660Sbostic * terminal 10737660Sbostic * idle time 10837660Sbostic * if messages allowed 10937660Sbostic * where logged in from 11037664Sedward * if not logged in 11137664Sedward * when last logged in 11237660Sbostic */ 11337664Sedward /* find out longest device name for this user for formatting */ 11437775Sbostic for (w = pn->whead, maxlen = -1; w != NULL; w = w->next) 11537664Sedward if ((len = strlen(w->tty)) > maxlen) 11637664Sedward maxlen = len; 11737664Sedward /* find rest of entries for user */ 11837664Sedward for (w = pn->whead; w != NULL; w = w->next) { 11937664Sedward switch (w->info) { 12037664Sedward case LOGGEDIN: 121*42919Sbostic tp = localtime(&w->loginat); 122*42919Sbostic t = asctime(tp); 123*42919Sbostic tzn = tp->tm_zone; 124*42919Sbostic cpr = printf("On since %16.16s (%s) on %s", 125*42919Sbostic t, tzn, w->tty); 12637660Sbostic /* 12737660Sbostic * idle time is tough; if have one, print a comma, 12837660Sbostic * then spaces to pad out the device name, then the 12937660Sbostic * idle time. Follow with a comma if a remote login. 13037660Sbostic */ 13137664Sedward delta = gmtime(&w->idletime); 13237660Sbostic if (delta->tm_yday || delta->tm_hour || delta->tm_min) { 13337660Sbostic cpr += printf("%-*s idle ", 13437664Sedward maxlen - strlen(w->tty) + 1, ","); 13537660Sbostic if (delta->tm_yday > 0) { 13637660Sbostic cpr += printf("%d day%s ", 13737660Sbostic delta->tm_yday, 13837660Sbostic delta->tm_yday == 1 ? "" : "s"); 13937660Sbostic } 14037660Sbostic cpr += printf("%d:%02d", 14137660Sbostic delta->tm_hour, delta->tm_min); 14237664Sedward if (*w->host) { 14337660Sbostic putchar(','); 14437660Sbostic ++cpr; 14537660Sbostic } 14637660Sbostic } 14737664Sedward if (!w->writable) 14837660Sbostic cpr += printf(" (messages off)"); 14937664Sedward break; 15037664Sedward case LASTLOG: 15137664Sedward if (w->loginat == 0) { 15237664Sedward (void)printf("Never logged in."); 15337664Sedward break; 15437660Sbostic } 155*42919Sbostic tp = localtime(&w->loginat); 156*42919Sbostic t = asctime(tp); 157*42919Sbostic tzn = tp->tm_zone; 15837664Sedward if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2) 159*42919Sbostic cpr = printf("Last login %10.10s (%s), %4.4s on %s", 160*42919Sbostic t, t + 20, tzn, w->tty); 16137664Sedward else 162*42919Sbostic cpr = printf("Last login %16.16s (%s) on %s", 163*42919Sbostic t, tzn, w->tty); 16437664Sedward break; 16537660Sbostic } 16637664Sedward if (*w->host) { 16737664Sedward if (LINE_LEN < (cpr + 6 + strlen(w->host))) 16837660Sbostic (void)printf("\n "); 16937664Sedward (void)printf(" from %s", w->host); 17037660Sbostic } 17137660Sbostic putchar('\n'); 17237660Sbostic } 17337660Sbostic } 17437660Sbostic 17537660Sbostic demi_print(str, oddfield) 17637660Sbostic char *str; 17737660Sbostic int oddfield; 17837660Sbostic { 17937660Sbostic static int lenlast; 18037660Sbostic int lenthis, maxlen; 18137660Sbostic 18237660Sbostic lenthis = strlen(str); 18337660Sbostic if (oddfield) { 18437660Sbostic /* 18537660Sbostic * We left off on an odd number of fields. If we haven't 18637660Sbostic * crossed the midpoint of the screen, and we have room for 18737660Sbostic * the next field, print it on the same line; otherwise, 18837660Sbostic * print it on a new line. 18937660Sbostic * 19037660Sbostic * Note: we insist on having the right hand fields start 19137660Sbostic * no less than 5 tabs out. 19237660Sbostic */ 19337660Sbostic maxlen = 5 * TAB_LEN; 19437660Sbostic if (maxlen < lenlast) 19537660Sbostic maxlen = lenlast; 19637660Sbostic if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) + 19737660Sbostic lenthis) <= LINE_LEN) { 19837660Sbostic while(lenlast < (4 * TAB_LEN)) { 19937660Sbostic putchar('\t'); 20037660Sbostic lenlast += TAB_LEN; 20137660Sbostic } 20237660Sbostic (void)printf("\t%s\n", str); /* force one tab */ 20337660Sbostic } else { 20437660Sbostic (void)printf("\n%s", str); /* go to next line */ 20537660Sbostic oddfield = !oddfield; /* this'll be undone below */ 20637660Sbostic } 20737660Sbostic } else 20837660Sbostic (void)printf("%s", str); 20937660Sbostic oddfield = !oddfield; /* toggle odd/even marker */ 21037660Sbostic lenlast = lenthis; 21137660Sbostic return(oddfield); 21237660Sbostic } 21337660Sbostic 21437660Sbostic show_text(directory, file_name, header) 21537660Sbostic char *directory, *file_name, *header; 21637660Sbostic { 21739241Sedward register int ch, lastc; 21839241Sedward register FILE *fp; 21937660Sbostic 22037660Sbostic (void)sprintf(tbuf, "%s/%s", directory, file_name); 22139241Sedward if ((fp = fopen(tbuf, "r")) == NULL) 22237660Sbostic return(0); 22337660Sbostic (void)printf("%s\n", header); 22439241Sedward while ((ch = getc(fp)) != EOF) 22539241Sedward vputc(lastc = ch); 22639241Sedward if (lastc != '\n') 22738901Sbostic (void)putchar('\n'); 22839241Sedward (void)fclose(fp); 22937660Sbostic return(1); 23037660Sbostic } 23138901Sbostic 23238901Sbostic vputc(ch) 23338901Sbostic register int ch; 23438901Sbostic { 23538901Sbostic int meta; 23638901Sbostic 23738901Sbostic if (!isascii(ch)) { 23838901Sbostic (void)putchar('M'); 23938901Sbostic (void)putchar('-'); 24038901Sbostic ch = toascii(ch); 24138901Sbostic meta = 1; 24238901Sbostic } else 24338901Sbostic meta = 0; 24438912Sbostic if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n')) 24538901Sbostic (void)putchar(ch); 24638901Sbostic else { 24738901Sbostic (void)putchar('^'); 24838901Sbostic (void)putchar(ch == '\177' ? '?' : ch | 0100); 24938901Sbostic } 25038901Sbostic } 251