From 59f8c787e7cdc9714603e2b2e70e702d5071923f Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 17 Sep 2010 10:35:35 +0200 Subject: [PATCH] ul: use atexit() to deallocate buffer, print errors by err() Signed-off-by: Karel Zak --- text-utils/ul.c | 60 +++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/text-utils/ul.c b/text-utils/ul.c index b409c7cd..4601e3c7 100644 --- a/text-utils/ul.c +++ b/text-utils/ul.c @@ -47,6 +47,8 @@ #include /* for getenv() */ #include /* for INT_MAX */ #include /* for signal() */ +#include +#include #include "nls.h" #include "widechar.h" @@ -144,9 +146,9 @@ int main(int argc, char **argv) default: fprintf(stderr, - _("usage: %s [ -i ] [ -tTerm ] file...\n"), - argv[0]); - exit(1); + _("Usage: %s [ -i ] [ -tTerm ] file...\n"), + program_invocation_short_name); + return EXIT_FAILURE; } setupterm(termtype, 1, &ret); switch(ret) { @@ -155,7 +157,7 @@ int main(int argc, char **argv) break; default: - fprintf(stderr,_("trouble reading terminfo")); + warnx(_("trouble reading terminfo")); /* fall through to ... */ case 0: @@ -167,24 +169,20 @@ int main(int argc, char **argv) if ( (tigetflag("os") && ENTER_BOLD==NULL ) || (tigetflag("ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL)) must_overstrike = 1; + atexit(exitbuf); initbuf(); if (optind == argc) filter(stdin); else for (; optind= obuflen, expand obuf until obuflen > col. */ while (col >= obuflen) { /* Paranoid check for obuflen == INT_MAX. */ - if (obuflen == INT_MAX) { - fprintf(stderr, - _("Input line too long.\n")); - exit(1); - } + if (obuflen == INT_MAX) + errx(EXIT_FAILURE, _("Input line too long.")); /* Similar paranoia: double only up to INT_MAX. */ obuflen = ((INT_MAX / 2) < obuflen) @@ -604,21 +597,18 @@ needcol(int col) { /* Now we can try to expand obuf. */ obuf = realloc(obuf, sizeof(struct CHAR) * obuflen); - if (obuf == NULL) { - fprintf(stderr, - _("Out of memory when growing buffer.\n")); - exit(1); - } + if (obuf == NULL) + err(EXIT_FAILURE, _("growing buffer failed")); } } static void sig_handler(int signo) { - exitbuf(); exit(EXIT_SUCCESS); } static void exitbuf(void) { free(obuf); + obuf = NULL; } -- 2.39.5