From 072847109936bc0e822f8cf67c31eb62183f0db8 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Mon, 16 Dec 2013 11:12:03 +0800 Subject: lib/util: Move mac_buf from nc code to util library Signed-off-by: Jeremy Kerr --- lib/util/util.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/util/util.c (limited to 'lib/util/util.c') 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; +} -- cgit v1.2.1