]> err.no Git - util-linux/commitdiff
col: use xalloc for memory allocation
authorDavidlohr Bueso <dave@gnu.org>
Fri, 22 Oct 2010 15:24:37 +0000 (12:24 -0300)
committerKarel Zak <kzak@redhat.com>
Mon, 1 Nov 2010 13:32:48 +0000 (14:32 +0100)
Differentiate between malloc and realloc (not done so far) and get rid of local warn()

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
text-utils/col.c

index 3b81a891ae9b010377286a19238e10126f11f070..580a84c7534628622db9439b63c4888dafc4ec30 100644 (file)
@@ -50,8 +50,9 @@
 #include <string.h>
 #include <stdio.h>
 #include <unistd.h>
-#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"));
-}