From 724276498d22ec1f418cdad128e4211648a134e6 Mon Sep 17 00:00:00 2001 From: Marc Espie Date: Tue, 23 Aug 2005 13:43:54 +0000 Subject: use vis() consistently in field printing. Replace vs() with a version that logs its memory usage, so that it can be cleaned up after each entry. okay millert@, delegated by deraadt@ --- usr.bin/finger/extern.h | 7 +++++-- usr.bin/finger/lprint.c | 18 ++++++++++-------- usr.bin/finger/sprint.c | 15 +++++++++------ usr.bin/finger/util.c | 30 ++++++++++++++++++++++++++---- 4 files changed, 50 insertions(+), 20 deletions(-) (limited to 'usr.bin/finger') diff --git a/usr.bin/finger/extern.h b/usr.bin/finger/extern.h index 8c185963f46..2201c4aaf99 100644 --- a/usr.bin/finger/extern.h +++ b/usr.bin/finger/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.6 2003/06/03 02:56:08 millert Exp $ */ +/* $OpenBSD: extern.h,v 1.7 2005/08/23 13:43:53 espie Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -38,6 +38,8 @@ extern int lflag; extern int oflag; extern int pplan; +struct storage; + int demi_print(char *, int); void enter_lastlog(PERSON *); PERSON *enter_person(struct passwd *); @@ -57,6 +59,7 @@ void sflag_print(void); int show_text(char *, char *, char *); PERSON **sort(void); void stimeprint(WHERE *); -char *vs(char *); +char *vs(struct storage **, char *); void userlist(int, char **); void vputc(int); +void free_storage(struct storage *); diff --git a/usr.bin/finger/lprint.c b/usr.bin/finger/lprint.c index c70e27c8f42..de80f3d2825 100644 --- a/usr.bin/finger/lprint.c +++ b/usr.bin/finger/lprint.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lprint.c,v 1.8 2004/03/15 02:50:29 tedu Exp $ */ +/* $OpenBSD: lprint.c,v 1.9 2005/08/23 13:43:53 espie Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. @@ -34,7 +34,7 @@ #ifndef lint /*static char sccsid[] = "from: @(#)lprint.c 5.13 (Berkeley) 10/31/90";*/ -static const char rcsid[] = "$OpenBSD: lprint.c,v 1.8 2004/03/15 02:50:29 tedu Exp $"; +static const char rcsid[] = "$OpenBSD: lprint.c,v 1.9 2005/08/23 13:43:53 espie Exp $"; #endif /* not lint */ #include @@ -83,6 +83,7 @@ lprint(PERSON *pn) struct tm *tp; int oddfield; char *t, *tzn; + struct storage *mem = NULL; cpr = 0; /* @@ -95,7 +96,7 @@ lprint(PERSON *pn) * mail status */ (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s", - pn->name, pn->realname, pn->dir); + vs(&mem, pn->name), vs(&mem, pn->realname), pn->dir); (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL); /* @@ -109,27 +110,28 @@ lprint(PERSON *pn) strlen(pn->office) + strlen(pn->officephone) + sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) { (void)snprintf(tbuf, sizeof(tbuf), - "%s: %s, %s", OFFICE_TAG, pn->office, - prphone(pn->officephone)); + "%s: %s, %s", OFFICE_TAG, vs(&mem, pn->office), + vs(&mem, prphone(pn->officephone))); oddfield = demi_print(tbuf, oddfield); } else { if (pn->office) { (void)snprintf(tbuf, sizeof(tbuf), - "%s: %s", OFFICE_TAG, pn->office); + "%s: %s", OFFICE_TAG, vs(&mem, pn->office)); oddfield = demi_print(tbuf, oddfield); } if (pn->officephone) { (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", OFFICE_PHONE_TAG, - prphone(pn->officephone)); + vs(&mem, prphone(pn->officephone))); oddfield = demi_print(tbuf, oddfield); } } if (pn->homephone) { (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone", - prphone(pn->homephone)); + vs(&mem, prphone(pn->homephone))); oddfield = demi_print(tbuf, oddfield); } + free_storage(mem); if (oddfield) putchar('\n'); diff --git a/usr.bin/finger/sprint.c b/usr.bin/finger/sprint.c index 39c010cda61..c7dd9feaaf8 100644 --- a/usr.bin/finger/sprint.c +++ b/usr.bin/finger/sprint.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sprint.c,v 1.10 2004/03/15 02:50:29 tedu Exp $ */ +/* $OpenBSD: sprint.c,v 1.11 2005/08/23 13:43:53 espie Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. @@ -34,7 +34,7 @@ #ifndef lint /*static char sccsid[] = "from: @(#)sprint.c 5.8 (Berkeley) 12/4/90";*/ -static const char rcsid[] = "$OpenBSD: sprint.c,v 1.10 2004/03/15 02:50:29 tedu Exp $"; +static const char rcsid[] = "$OpenBSD: sprint.c,v 1.11 2005/08/23 13:43:53 espie Exp $"; #endif /* not lint */ #include @@ -55,6 +55,7 @@ sflag_print(void) int cnt; char *p; PERSON **list; + struct storage *mem; list = sort(); /* @@ -84,10 +85,11 @@ sflag_print(void) for (cnt = 0; cnt < entries; ++cnt) { pn = list[cnt]; for (w = pn->whead; w != NULL; w = w->next) { + mem = NULL; (void)printf("%-*.*s %-*.*s ", - NAME_WIDTH, UT_NAMESIZE, vs(pn->name), + NAME_WIDTH, UT_NAMESIZE, vs(&mem, pn->name), MAXREALNAME, MAXREALNAME, - pn->realname ? vs(pn->realname) : ""); + pn->realname ? vs(&mem, pn->realname) : ""); if (!w->loginat) { (void)printf(" * * No logins "); goto office; @@ -119,15 +121,16 @@ office: if (oflag) { if (pn->office) (void)printf("%-10.10s", - vs(pn->office)); + vs(&mem, pn->office)); else if (pn->officephone) (void)printf("%-10.10s", " "); if (pn->officephone) (void)printf(" %-.15s", - vs(prphone(pn->officephone))); + vs(&mem, prphone(pn->officephone))); } else (void)printf("%.*s", MAXHOSTNAME, w->host); putchar('\n'); + free_storage(mem); } } } diff --git a/usr.bin/finger/util.c b/usr.bin/finger/util.c index f8728ce2d22..67bfa46b742 100644 --- a/usr.bin/finger/util.c +++ b/usr.bin/finger/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.21 2005/03/15 12:28:48 niallo Exp $ */ +/* $OpenBSD: util.c,v 1.22 2005/08/23 13:43:53 espie Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. @@ -35,7 +35,7 @@ #ifndef lint /*static char sccsid[] = "from: @(#)util.c 5.14 (Berkeley) 1/17/91";*/ -static const char rcsid[] = "$OpenBSD: util.c,v 1.21 2005/03/15 12:28:48 niallo Exp $"; +static const char rcsid[] = "$OpenBSD: util.c,v 1.22 2005/08/23 13:43:53 espie Exp $"; #endif /* not lint */ #include @@ -61,6 +61,23 @@ WHERE *walloc(PERSON *pn); void find_idle_and_ttywrite(WHERE *); void userinfo(PERSON *, struct passwd *); +struct storage { + struct storage *next; + char a[1]; +}; + +void +free_storage(struct storage *st) +{ + struct storage *nx; + + while (st != NULL) { + nx = st->next; + free(st); + st = nx; + } +} + void find_idle_and_ttywrite(WHERE *w) { @@ -376,12 +393,17 @@ prphone(char *num) * The caller is responsible for free()'ing the returned string. */ char * -vs(char *src) +vs(struct storage **exist, char *src) { char *dst; + struct storage *n; - if ((dst = malloc((4 * strlen(src)) + 1)) == NULL) + if ((n = malloc(sizeof(struct storage) + 4 * strlen(src))) == NULL) err(1, "malloc failed"); + n->next = *exist; + *exist = n; + + dst = n->a; strvis(dst, src, VIS_SAFE|VIS_NOSLASH); return (dst); -- cgit v1.2.3