[PATCH 4/7] col: use xalloc for memory allocation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Davidlohr Bueso <dave@xxxxxxx>
Date: Fri, 22 Oct 2010 12:24:37 -0300
Subject: [PATCH 4/7] 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 <dave@xxxxxxx>
---
 text-utils/col.c |   38 +++++++++++---------------------------
 1 files changed, 11 insertions(+), 27 deletions(-)

diff --git a/text-utils/col.c b/text-utils/col.c
index 3b81a89..580a84c 100644
--- a/text-utils/col.c
+++ b/text-utils/col.c
@@ -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"));
-}
-- 
1.7.0.4


--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux