[PATCH] sys-utils: make use xalloc wrappers

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

 



Pretty straight forward transition, all regression tests passed.

Thanks,
Davidlohr

From: Davidlohr Bueso <dave@xxxxxxx>
Date: Wed, 27 Oct 2010 06:12:52 -0300
Subject: [PATCH] sys-utils: make use xalloc wrappers

Signed-off-by: Davidlohr Bueso <dave@xxxxxxx>
---
 sys-utils/cytune.c      |    9 +++------
 sys-utils/readprofile.c |   25 ++++---------------------
 sys-utils/rtcwake.c     |    7 ++-----
 sys-utils/tunelp.c      |   34 +++++++++++++---------------------
 4 files changed, 22 insertions(+), 53 deletions(-)

diff --git a/sys-utils/cytune.c b/sys-utils/cytune.c
index 10a874d..47b3729 100644
--- a/sys-utils/cytune.c
+++ b/sys-utils/cytune.c
@@ -63,6 +63,7 @@
 #endif
 #endif
 
+#include "xalloc.h"
 #include "nls.h"
 				/* Until it gets put in the kernel,
 				   toggle by hand. */
@@ -315,12 +316,8 @@ int main(int argc, char *argv[]) {
 
   /* query stuff after this line */
   
-  cmon = (struct cyclades_control *) malloc(sizeof (struct cyclades_control)
-					    * numfiles);
-  if(!cmon) {
-    perror(_("malloc failed"));
-    exit(1);
-  }
+  cmon = xmalloc(sizeof(struct cyclades_control) * numfiles);
+
   if(signal(SIGINT, summary)||
      signal(SIGQUIT, summary)||
      signal(SIGTERM, summary)) {
diff --git a/sys-utils/readprofile.c b/sys-utils/readprofile.c
index 52f5d8c..004c4e3 100644
--- a/sys-utils/readprofile.c
+++ b/sys-utils/readprofile.c
@@ -49,6 +49,8 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/utsname.h>
+
+#include "xalloc.h"
 #include "nls.h"
 
 #define S_LEN 128
@@ -60,22 +62,6 @@ static char defaultmap[]="/boot/System.map";
 static char defaultpro[]="/proc/profile";
 static char optstring[]="M:m:np:itvarVbs";
 
-static void *
-xmalloc (size_t size) {
-	void *t;
-
-	if (size == 0)
-		return NULL;
-
-	t = malloc (size);
-	if (t == NULL) {
-		fprintf(stderr, _("out of memory"));
-		exit(1);
-	}
-
-	return t;
-}
-
 static FILE *
 myopen(char *name, char *mode, int *flag) {
 	int len = strlen(name);
@@ -242,11 +228,8 @@ main(int argc, char **argv) {
 		exit(1);
 	}
 
-	if (!(buf=malloc(len))) {
-		fprintf(stderr,"%s: malloc(): %s\n", prgname, strerror(errno));
-		exit(1);
-	}
-
+	buf = xmalloc(len);
+	
 	if (read(proFd,buf,len) != len) {
 		fprintf(stderr,"%s: %s: %s\n",prgname,proFile,strerror(errno));
 		exit(1);
diff --git a/sys-utils/rtcwake.c b/sys-utils/rtcwake.c
index 2651130..f7ecb16 100644
--- a/sys-utils/rtcwake.c
+++ b/sys-utils/rtcwake.c
@@ -36,6 +36,7 @@
 #include <linux/rtc.h>
 
 #include "nls.h"
+#include "xalloc.h"
 #include "pathnames.h"
 #include "usleep.h"
 
@@ -423,11 +424,7 @@ int main(int argc, char **argv)
 	if (strncmp(devname, "/dev/", strlen("/dev/")) != 0) {
 		char *new_devname;
 
-		new_devname = malloc(strlen(devname) + strlen("/dev/") + 1);
-		if (!new_devname) {
-			perror(_("malloc() failed"));
-			exit(EXIT_FAILURE);
-		}
+		new_devname = xmalloc(strlen(devname) + strlen("/dev/") + 1);
 
 		strcpy(new_devname, "/dev/");
 		strcat(new_devname, devname);
diff --git a/sys-utils/tunelp.c b/sys-utils/tunelp.c
index 00ad5b7..0f22fd8 100644
--- a/sys-utils/tunelp.c
+++ b/sys-utils/tunelp.c
@@ -60,7 +60,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+
 #include "lp.h"
+#include "xalloc.h"
 #include "nls.h"
 
 struct command {
@@ -84,16 +86,6 @@ print_version(char *progname) {
   printf(_("%s (%s)\n"), progname, PACKAGE_STRING);
 }
 
-static void *
-mylloc(long size) {
-  void *ptr;
-  if(!(ptr = (void*)malloc(size))) {
-    perror(_("malloc error"));
-    exit(2);
-  }
-  return ptr;
-}
-
 static char *progname;
 
 static long
@@ -131,7 +123,7 @@ main (int argc, char ** argv) {
 
   if (argc < 2) print_usage(progname);
 
-  cmdst = cmds = mylloc(sizeof(struct command));
+  cmdst = cmds = xmalloc(sizeof(struct command));
   cmds->next = 0;
 
   show_irq = 1;
@@ -143,31 +135,31 @@ main (int argc, char ** argv) {
     case 'i':
       cmds->op = LPSETIRQ;
       cmds->val = get_val(optarg);
-      cmds->next = mylloc(sizeof(struct command));
+      cmds->next = xmalloc(sizeof(struct command));
       cmds = cmds->next; cmds->next = 0;
       break;
     case 't':
       cmds->op = LPTIME;
       cmds->val = get_val(optarg);
-      cmds->next = mylloc(sizeof(struct command));
+      cmds->next = xmalloc(sizeof(struct command));
       cmds = cmds->next; cmds->next = 0;
       break;
     case 'c':
       cmds->op = LPCHAR;
       cmds->val = get_val(optarg);
-      cmds->next = mylloc(sizeof(struct command));
+      cmds->next = xmalloc(sizeof(struct command));
       cmds = cmds->next; cmds->next = 0;
       break;
     case 'w':
       cmds->op = LPWAIT;
       cmds->val = get_val(optarg);
-      cmds->next = mylloc(sizeof(struct command));
+      cmds->next = xmalloc(sizeof(struct command));
       cmds = cmds->next; cmds->next = 0;
       break;
     case 'a':
       cmds->op = LPABORT;
       cmds->val = get_onoff(optarg);
-      cmds->next = mylloc(sizeof(struct command));
+      cmds->next = xmalloc(sizeof(struct command));
       cmds = cmds->next; cmds->next = 0;
       break;
     case 'q':
@@ -180,20 +172,20 @@ main (int argc, char ** argv) {
     case 'o':
       cmds->op = LPABORTOPEN;
       cmds->val = get_onoff(optarg);
-      cmds->next = mylloc(sizeof(struct command));
+      cmds->next = xmalloc(sizeof(struct command));
       cmds = cmds->next; cmds->next = 0;
       break;
     case 'C':
       cmds->op = LPCAREFUL;
       cmds->val = get_onoff(optarg);
-      cmds->next = mylloc(sizeof(struct command));
+      cmds->next = xmalloc(sizeof(struct command));
       cmds = cmds->next; cmds->next = 0;
       break;
     case 's':
       show_irq = 0;
       cmds->op = LPGETSTATUS;
       cmds->val = 0;
-      cmds->next = mylloc(sizeof(struct command));
+      cmds->next = xmalloc(sizeof(struct command));
       cmds = cmds->next; cmds->next = 0;
       break;
 #endif
@@ -201,7 +193,7 @@ main (int argc, char ** argv) {
     case 'r':
       cmds->op = LPRESET;
       cmds->val = 0;
-      cmds->next = mylloc(sizeof(struct command));
+      cmds->next = xmalloc(sizeof(struct command));
       cmds = cmds->next; cmds->next = 0;
       break;
 #endif
@@ -210,7 +202,7 @@ main (int argc, char ** argv) {
       /* Note: this will do the wrong thing on 2.0.36 when compiled under 2.2.x */
       cmds->op = LPTRUSTIRQ;
       cmds->val = get_onoff(optarg);
-      cmds->next = mylloc(sizeof(struct command));
+      cmds->next = xmalloc(sizeof(struct command));
       cmds = cmds->next; cmds->next = 0;
       break;
 #endif
-- 
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