From: Jeremy Kerr Date: Mon, 16 Dec 2013 03:12:03 +0000 (+0800) Subject: lib/util: Move mac_buf from nc code to util library X-Git-Tag: v1.0.0~286 X-Git-Url: https://git.ozlabs.org/?p=petitboot;a=commitdiff_plain;h=072847109936bc0e822f8cf67c31eb62183f0db8 lib/util: Move mac_buf from nc code to util library Signed-off-by: Jeremy Kerr --- diff --git a/lib/Makefile.am b/lib/Makefile.am index b492d69..e3ec2dc 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -43,6 +43,7 @@ libpbcore_la_SOURCES = \ system/system.h \ url/url.c \ url/url.h \ + util/util.c \ util/util.h MAINTAINERCLEANFILES = Makefile.in diff --git a/lib/util/util.c b/lib/util/util.c new file mode 100644 index 0000000..75b1c23 --- /dev/null +++ b/lib/util/util.c @@ -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 +#include +#include + +#include + +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; +} diff --git a/lib/util/util.h b/lib/util/util.h index ba5ea4f..39966d0 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -18,6 +18,8 @@ #ifndef UTIL_H #define UTIL_H +#include + #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) +void mac_str(uint8_t *mac, unsigned int maclen, char *buf, unsigned int buflen); + #endif /* UTIL_H */ diff --git a/ui/ncurses/nc-sysinfo.c b/ui/ncurses/nc-sysinfo.c index 1d7bd1e..142c705 100644 --- a/ui/ncurses/nc-sysinfo.c +++ b/ui/ncurses/nc-sysinfo.c @@ -127,28 +127,10 @@ static __attribute__((format(printf, 2, 3))) void sysinfo_screen_append_line( 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, @@ -190,7 +172,7 @@ static void sysinfo_screen_populate(struct sysinfo_screen *screen, 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);