[pisa-src] r2516 - trunk/tools/dhcp/mac2ip.c

  • From: Christoph Viethen <christoph.viethen@xxxxxxxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Tue, 5 Apr 2011 02:37:40 +0200

Author: viethen
Date: Tue Apr  5 02:37:39 2011
New Revision: 2516

Log:
clean up string_to_ipv4_t() and make it return a status code
(this conversion _can_ fail)

Modified:
   trunk/tools/dhcp/mac2ip.c

Modified: trunk/tools/dhcp/mac2ip.c
==============================================================================
--- trunk/tools/dhcp/mac2ip.c   Tue Apr  5 02:12:17 2011        (r2515)
+++ trunk/tools/dhcp/mac2ip.c   Tue Apr  5 02:37:39 2011        (r2516)
@@ -6,11 +6,12 @@
  */
 
 #include "mac2ip.h"
+#include <assert.h>
+#include <inttypes.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h>
 #include <time.h>
-#include <stdio.h>
 
 /* mac2ip.conf file */
 const char *CONFIG_FILE_LOCATION = "/etc/mac2ip.conf";
@@ -74,23 +75,13 @@
 }
 
 /* converts mac in hex_string_format to mac_t */
-static void string_to_ipv4_t(const char *string, mac2ip_ipv4_t *ip)
+static int string_to_ipv4_t(const char *string, mac2ip_ipv4_t *ip)
 {
-    int   i;
-    char *byte = NULL, *string_duplicate = NULL;
-
-    string_duplicate = strdup(string);
-
-
-    byte = strtok(string_duplicate, ".");
-
-    for (i = 0; i < sizeof(ip->x); i++) {
-        sscanf(byte, "%hhu", &ip->x[i]);
-
-        byte = strtok(NULL, ".");
+    if (sscanf(string, "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8, &ip->x[0], 
&ip->x[1], &ip->x[2], &ip->x[3]) == 4) {
+        return 0;
+    } else {
+        return -1;
     }
-
-    free(string_duplicate);
 }
 
 static char *readline(FILE *fp, char **buf, size_t *buflen)
-- 
This is the pisa developer mailing list. Please also subscribe to the main pisa 
list at:
//www.freelists.org/list/pisa

Other related posts: