137660Sbostic /* 237660Sbostic * Copyright (c) 1989 The Regents of the University of California. 337660Sbostic * All rights reserved. 437660Sbostic * 5*40027Sbostic * This code is derived from software contributed to Berkeley by 6*40027Sbostic * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 7*40027Sbostic * 837660Sbostic * Redistribution and use in source and binary forms are permitted 937660Sbostic * provided that the above copyright notice and this paragraph are 1037660Sbostic * duplicated in all such forms and that any documentation, 1137660Sbostic * advertising materials, and other materials related to such 1237660Sbostic * distribution and use acknowledge that the software was developed 1337660Sbostic * by the University of California, Berkeley. The name of the 1437660Sbostic * University may not be used to endorse or promote products derived 1537660Sbostic * from this software without specific prior written permission. 1637660Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1737660Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1837660Sbostic * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1937660Sbostic */ 2037660Sbostic 2137660Sbostic #ifndef lint 22*40027Sbostic static char sccsid[] = "@(#)lprint.c 5.9 (Berkeley) 02/07/90"; 2337660Sbostic #endif /* not lint */ 2437660Sbostic 2537660Sbostic #include <sys/types.h> 2637660Sbostic #include <sys/file.h> 2737660Sbostic #include <sys/stat.h> 2837660Sbostic #include <sys/time.h> 2937660Sbostic #include <tzfile.h> 3037660Sbostic #include <stdio.h> 3138901Sbostic #include <ctype.h> 3237660Sbostic #include "finger.h" 3337660Sbostic #include "pathnames.h" 3437660Sbostic 3537660Sbostic #define LINE_LEN 80 3637660Sbostic #define TAB_LEN 8 /* 8 spaces between tabs */ 3737660Sbostic #define _PATH_PLAN ".plan" 3837660Sbostic #define _PATH_PROJECT ".project" 3937660Sbostic 4037660Sbostic lflag_print() 4137660Sbostic { 4237660Sbostic extern int pplan; 4337660Sbostic register PERSON *pn; 4437660Sbostic 4537664Sedward for (pn = phead;;) { 4637660Sbostic lprint(pn); 4737660Sbostic if (!pplan) { 4837664Sedward (void)show_text(pn->dir, _PATH_PROJECT, "Project:"); 4937660Sbostic if (!show_text(pn->dir, _PATH_PLAN, "Plan:")) 5037660Sbostic (void)printf("No Plan.\n"); 5137660Sbostic } 5237660Sbostic if (!(pn = pn->next)) 5337660Sbostic break; 5437660Sbostic putchar('\n'); 5537660Sbostic } 5637660Sbostic } 5737660Sbostic 5837660Sbostic lprint(pn) 5937660Sbostic register PERSON *pn; 6037660Sbostic { 6137660Sbostic extern time_t now; 6237660Sbostic register struct tm *delta; 6337664Sedward register WHERE *w; 6437660Sbostic register int cpr, len, maxlen; 6537660Sbostic int oddfield; 6637660Sbostic time_t time(); 6738052Sbostic char *t, *ctime(), *prphone(); 6837660Sbostic 6937660Sbostic /* 7037660Sbostic * long format -- 7137660Sbostic * login name 7237660Sbostic * real name 7337660Sbostic * home directory 7437660Sbostic * shell 7537660Sbostic * office, office phone, home phone if available 7637660Sbostic */ 7737660Sbostic (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s", 7837660Sbostic pn->name, pn->realname, pn->dir); 7937660Sbostic (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL); 8037660Sbostic 8137660Sbostic /* 8237660Sbostic * try and print office, office phone, and home phone on one line; 8337660Sbostic * if that fails, do line filling so it looks nice. 8437660Sbostic */ 8537660Sbostic #define OFFICE_TAG "Office" 8637660Sbostic #define OFFICE_PHONE_TAG "Office Phone" 8737660Sbostic oddfield = 0; 8837660Sbostic if (pn->office && pn->officephone && 8937660Sbostic strlen(pn->office) + strlen(pn->officephone) + 9037660Sbostic sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) { 9137660Sbostic (void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office, 9238052Sbostic prphone(pn->officephone)); 9337660Sbostic oddfield = demi_print(tbuf, oddfield); 9437660Sbostic } else { 9537660Sbostic if (pn->office) { 9637660Sbostic (void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office); 9737660Sbostic oddfield = demi_print(tbuf, oddfield); 9837660Sbostic } 9937660Sbostic if (pn->officephone) { 10037660Sbostic (void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG, 10138052Sbostic prphone(pn->officephone)); 10237660Sbostic oddfield = demi_print(tbuf, oddfield); 10337660Sbostic } 10437660Sbostic } 10537660Sbostic if (pn->homephone) { 10638052Sbostic (void)sprintf(tbuf, "%s: %s", "Home Phone", 10738052Sbostic prphone(pn->homephone)); 10837660Sbostic oddfield = demi_print(tbuf, oddfield); 10937660Sbostic } 11037660Sbostic if (oddfield) 11137660Sbostic putchar('\n'); 11237660Sbostic 11337660Sbostic /* 11437664Sedward * long format con't: * if logged in 11537660Sbostic * terminal 11637660Sbostic * idle time 11737660Sbostic * if messages allowed 11837660Sbostic * where logged in from 11937664Sedward * if not logged in 12037664Sedward * when last logged in 12137660Sbostic */ 12237664Sedward /* find out longest device name for this user for formatting */ 12337775Sbostic for (w = pn->whead, maxlen = -1; w != NULL; w = w->next) 12437664Sedward if ((len = strlen(w->tty)) > maxlen) 12537664Sedward maxlen = len; 12637664Sedward /* find rest of entries for user */ 12737664Sedward for (w = pn->whead; w != NULL; w = w->next) { 12837664Sedward switch (w->info) { 12937664Sedward case LOGGEDIN: 13037660Sbostic cpr = printf("On since %16.16s on %s", 13137664Sedward ctime(&w->loginat), w->tty); 13237660Sbostic /* 13337660Sbostic * idle time is tough; if have one, print a comma, 13437660Sbostic * then spaces to pad out the device name, then the 13537660Sbostic * idle time. Follow with a comma if a remote login. 13637660Sbostic */ 13737664Sedward delta = gmtime(&w->idletime); 13837660Sbostic if (delta->tm_yday || delta->tm_hour || delta->tm_min) { 13937660Sbostic cpr += printf("%-*s idle ", 14037664Sedward maxlen - strlen(w->tty) + 1, ","); 14137660Sbostic if (delta->tm_yday > 0) { 14237660Sbostic cpr += printf("%d day%s ", 14337660Sbostic delta->tm_yday, 14437660Sbostic delta->tm_yday == 1 ? "" : "s"); 14537660Sbostic } 14637660Sbostic cpr += printf("%d:%02d", 14737660Sbostic delta->tm_hour, delta->tm_min); 14837664Sedward if (*w->host) { 14937660Sbostic putchar(','); 15037660Sbostic ++cpr; 15137660Sbostic } 15237660Sbostic } 15337664Sedward if (!w->writable) 15437660Sbostic cpr += printf(" (messages off)"); 15537664Sedward break; 15637664Sedward case LASTLOG: 15737664Sedward if (w->loginat == 0) { 15837664Sedward (void)printf("Never logged in."); 15937664Sedward break; 16037660Sbostic } 16137664Sedward t = ctime(&w->loginat); 16237664Sedward if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2) 16337664Sedward cpr = printf("Last login %10.10s, %4.4s on %s", 16437664Sedward t, t + 20, w->tty); 16537664Sedward else 16637664Sedward cpr = printf("Last login %16.16s on %s", 16737664Sedward t, w->tty); 16837664Sedward break; 16937660Sbostic } 17037664Sedward if (*w->host) { 17137664Sedward if (LINE_LEN < (cpr + 6 + strlen(w->host))) 17237660Sbostic (void)printf("\n "); 17337664Sedward (void)printf(" from %s", w->host); 17437660Sbostic } 17537660Sbostic putchar('\n'); 17637660Sbostic } 17737660Sbostic } 17837660Sbostic 17937660Sbostic demi_print(str, oddfield) 18037660Sbostic char *str; 18137660Sbostic int oddfield; 18237660Sbostic { 18337660Sbostic static int lenlast; 18437660Sbostic int lenthis, maxlen; 18537660Sbostic 18637660Sbostic lenthis = strlen(str); 18737660Sbostic if (oddfield) { 18837660Sbostic /* 18937660Sbostic * We left off on an odd number of fields. If we haven't 19037660Sbostic * crossed the midpoint of the screen, and we have room for 19137660Sbostic * the next field, print it on the same line; otherwise, 19237660Sbostic * print it on a new line. 19337660Sbostic * 19437660Sbostic * Note: we insist on having the right hand fields start 19537660Sbostic * no less than 5 tabs out. 19637660Sbostic */ 19737660Sbostic maxlen = 5 * TAB_LEN; 19837660Sbostic if (maxlen < lenlast) 19937660Sbostic maxlen = lenlast; 20037660Sbostic if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) + 20137660Sbostic lenthis) <= LINE_LEN) { 20237660Sbostic while(lenlast < (4 * TAB_LEN)) { 20337660Sbostic putchar('\t'); 20437660Sbostic lenlast += TAB_LEN; 20537660Sbostic } 20637660Sbostic (void)printf("\t%s\n", str); /* force one tab */ 20737660Sbostic } else { 20837660Sbostic (void)printf("\n%s", str); /* go to next line */ 20937660Sbostic oddfield = !oddfield; /* this'll be undone below */ 21037660Sbostic } 21137660Sbostic } else 21237660Sbostic (void)printf("%s", str); 21337660Sbostic oddfield = !oddfield; /* toggle odd/even marker */ 21437660Sbostic lenlast = lenthis; 21537660Sbostic return(oddfield); 21637660Sbostic } 21737660Sbostic 21837660Sbostic show_text(directory, file_name, header) 21937660Sbostic char *directory, *file_name, *header; 22037660Sbostic { 22139241Sedward register int ch, lastc; 22239241Sedward register FILE *fp; 22337660Sbostic 22437660Sbostic (void)sprintf(tbuf, "%s/%s", directory, file_name); 22539241Sedward if ((fp = fopen(tbuf, "r")) == NULL) 22637660Sbostic return(0); 22737660Sbostic (void)printf("%s\n", header); 22839241Sedward while ((ch = getc(fp)) != EOF) 22939241Sedward vputc(lastc = ch); 23039241Sedward if (lastc != '\n') 23138901Sbostic (void)putchar('\n'); 23239241Sedward (void)fclose(fp); 23337660Sbostic return(1); 23437660Sbostic } 23538901Sbostic 23638901Sbostic vputc(ch) 23738901Sbostic register int ch; 23838901Sbostic { 23938901Sbostic int meta; 24038901Sbostic 24138901Sbostic if (!isascii(ch)) { 24238901Sbostic (void)putchar('M'); 24338901Sbostic (void)putchar('-'); 24438901Sbostic ch = toascii(ch); 24538901Sbostic meta = 1; 24638901Sbostic } else 24738901Sbostic meta = 0; 24838912Sbostic if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n')) 24938901Sbostic (void)putchar(ch); 25038901Sbostic else { 25138901Sbostic (void)putchar('^'); 25238901Sbostic (void)putchar(ch == '\177' ? '?' : ch | 0100); 25338901Sbostic } 25438901Sbostic } 255