From: Davidlohr Bueso Date: Fri, 22 Oct 2010 15:24:37 +0000 (-0300) Subject: col: use xalloc for memory allocation X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04faca4a1325fe21b90b761e5d3ef37c4a267ecc;p=util-linux col: use xalloc for memory allocation Differentiate between malloc and realloc (not done so far) and get rid of local warn() Signed-off-by: Davidlohr Bueso --- diff --git a/text-utils/col.c b/text-utils/col.c index 3b81a891..580a84c7 100644 --- a/text-utils/col.c +++ b/text-utils/col.c @@ -50,8 +50,9 @@ #include #include #include -#include "nls.h" +#include "nls.h" +#include "xalloc.h" #include "widechar.h" #define BS '\b' /* backspace */ @@ -94,12 +95,10 @@ struct line_str { void usage(void); void wrerr(void); -void warn(int); void free_line(LINE *l); void flush_line(LINE *l); void flush_lines(int); void flush_blanks(void); -void *xmalloc(void *p, size_t size); LINE *alloc_line(void); CSET last_set; /* char_set of last char printed */ @@ -275,7 +274,9 @@ int main(int argc, char **argv) } } else { if (!warned++) - warn(cur_line); + fprintf(stderr, + _("col: warning: can't back up %s.\n"), cur_line < 0 ? + _("past first line") : _("-- line already flushed")); cur_line -= nmove; } } @@ -302,8 +303,8 @@ int main(int argc, char **argv) int need; need = l->l_lsize ? l->l_lsize * 2 : 90; - l->l_line = (CHAR *)xmalloc((void *) l->l_line, - (unsigned) need * sizeof(CHAR)); + l->l_line = (CHAR *)xrealloc((void *) l->l_line, + (unsigned) need * sizeof(CHAR)); l->l_lsize = need; } c = &l->l_line[l->l_line_len++]; @@ -415,12 +416,12 @@ void flush_line(LINE *l) */ if (l->l_lsize > sorted_size) { sorted_size = l->l_lsize; - sorted = (CHAR *)xmalloc((void *)sorted, - (unsigned)sizeof(CHAR) * sorted_size); + sorted = (CHAR *)xrealloc((void *)sorted, + (unsigned)sizeof(CHAR) * sorted_size); } if (l->l_max_col >= count_size) { count_size = l->l_max_col + 1; - count = (int *)xmalloc((void *)count, + count = (int *)xrealloc((void *)count, (unsigned)sizeof(int) * count_size); } memset(count, 0, sizeof(int) * l->l_max_col + 1); @@ -510,7 +511,7 @@ alloc_line() int i; if (!line_freelist) { - l = (LINE *)xmalloc((void *)NULL, sizeof(LINE) * NALLOC); + l = xmalloc(sizeof(LINE) * NALLOC); line_freelist = l; for (i = 1; i < NALLOC; i++, l++) l->l_next = l + 1; @@ -529,16 +530,6 @@ void free_line(LINE *l) line_freelist = l; } -void * -xmalloc(void *p, size_t size) -{ - if (!(p = (void *)realloc(p, size))) { - (void)fprintf(stderr, "col: %s.\n", strerror(ENOMEM)); - exit(1); - } - return(p); -} - void usage() { (void)fprintf(stderr, _("usage: col [-bfpx] [-l nline]\n")); @@ -550,10 +541,3 @@ void wrerr() (void)fprintf(stderr, _("col: write error.\n")); exit(1); } - -void warn(int line) -{ - (void)fprintf(stderr, - _("col: warning: can't back up %s.\n"), line < 0 ? - _("past first line") : _("-- line already flushed")); -}