]> git.ozlabs.org Git - petitboot/commitdiff
lib/util: Move mac_buf from nc code to util library
authorJeremy Kerr <jk@ozlabs.org>
Mon, 16 Dec 2013 03:12:03 +0000 (11:12 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Wed, 18 Dec 2013 01:52:42 +0000 (09:52 +0800)
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
lib/Makefile.am
lib/util/util.c [new file with mode: 0644]
lib/util/util.h
ui/ncurses/nc-sysinfo.c

index b492d69fa5a48f60d1a856d827788bf045501b01..e3ec2dc3b869296b1f69fb3434a9fdfd3da91243 100644 (file)
@@ -43,6 +43,7 @@ libpbcore_la_SOURCES = \
        system/system.h \
        url/url.c \
        url/url.h \
        system/system.h \
        url/url.c \
        url/url.h \
+       util/util.c \
        util/util.h
 
 MAINTAINERCLEANFILES = Makefile.in
        util/util.h
 
 MAINTAINERCLEANFILES = Makefile.in
diff --git a/lib/util/util.c b/lib/util/util.c
new file mode 100644 (file)
index 0000000..75b1c23
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ *  Copyright (C) 2013 IBM Corporation
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; version 2 of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include <util/util.h>
+
+void mac_str(uint8_t *mac, unsigned int maclen, char *buf, unsigned int buflen)
+{
+       unsigned int i;
+       char *pos;
+
+       assert(buflen > sizeof("unknown"));
+
+       if (!maclen || maclen * 3 + 1 > buflen) {
+               strcpy(buf, "unknown");
+               return;
+       }
+
+       pos = buf;
+
+       for (i = 0; i < maclen; i++) {
+               snprintf(pos, 4, "%02x:", mac[i]);
+               pos += 3;
+       }
+
+       *(pos - 1) = '\0';
+
+       return;
+}
index ba5ea4f677776fbc0b9c48c38003c21203033848..39966d0b7b0a2586b7ce8d857871c5379d4ecb31 100644 (file)
@@ -18,6 +18,8 @@
 #ifndef UTIL_H
 #define UTIL_H
 
 #ifndef UTIL_H
 #define UTIL_H
 
+#include <stdint.h>
+
 #ifndef container_of
 #define container_of(_ptr, _type, _member) ({ \
        const typeof( ((_type *)0)->_member ) *__mptr = (_ptr); \
 #ifndef container_of
 #define container_of(_ptr, _type, _member) ({ \
        const typeof( ((_type *)0)->_member ) *__mptr = (_ptr); \
@@ -47,5 +49,7 @@
 #define build_assert(x) \
        do { (void)sizeof(char[(x)?1:-1]); } while (0)
 
 #define build_assert(x) \
        do { (void)sizeof(char[(x)?1:-1]); } while (0)
 
+void mac_str(uint8_t *mac, unsigned int maclen, char *buf, unsigned int buflen);
+
 #endif /* UTIL_H */
 
 #endif /* UTIL_H */
 
index 1d7bd1e2834cf8b499517a8320be44dcb68529f0..142c70506aa8e833a42ec8ad4e71be1b1c3e351b 100644 (file)
@@ -127,28 +127,10 @@ static __attribute__((format(printf, 2, 3))) void sysinfo_screen_append_line(
        screen->n_lines++;
 }
 
        screen->n_lines++;
 }
 
-static void mac_str(struct interface_info *info, char *buf, unsigned int buflen)
+static void if_info_mac_str(struct interface_info *info,
+               char *buf, unsigned int buflen)
 {
 {
-       unsigned int i;
-       char *pos;
-
-       assert(buflen > sizeof("unknown"));
-
-       if (!info->hwaddr_size || info->hwaddr_size * 3 + 1 > buflen) {
-               strcpy(buf, "unknown");
-               return;
-       }
-
-       pos = buf;
-
-       for (i = 0; i < info->hwaddr_size; i++) {
-               snprintf(pos, 4, "%02x:", info->hwaddr[i]);
-               pos += 3;
-       }
-
-       *(pos - 1) = '\0';
-
-       return;
+       return mac_str(info->hwaddr, info->hwaddr_size, buf, buflen);
 }
 
 static void sysinfo_screen_populate(struct sysinfo_screen *screen,
 }
 
 static void sysinfo_screen_populate(struct sysinfo_screen *screen,
@@ -190,7 +172,7 @@ static void sysinfo_screen_populate(struct sysinfo_screen *screen,
                struct interface_info *info = sysinfo->interfaces[i];
                char macbuf[32];
 
                struct interface_info *info = sysinfo->interfaces[i];
                char macbuf[32];
 
-               mac_str(info, macbuf, sizeof(macbuf));
+               if_info_mac_str(info, macbuf, sizeof(macbuf));
 
                line("%s:", info->name);
                line(" MAC: %s", macbuf);
 
                line("%s:", info->name);
                line(" MAC: %s", macbuf);