From 8b7805e44e9aeaf8fd798a813f000e087eac63b9 Mon Sep 17 00:00:00 2001 From: Boris Popov Date: Mon, 13 Dec 1999 15:57:11 +0000 Subject: [PATCH] Allow ifunit() routine to understand names like ed0f2. Also fix a bug caused by using bcmp() instead of strcmp(). Reviewed by: Garrett Wollman --- sys/net/if.c | 57 ++++++++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/sys/net/if.c b/sys/net/if.c index e244ce9ab819..5329c6d7ecba 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -597,46 +597,41 @@ if_slowtimo(arg) * interface structure pointer. */ struct ifnet * -ifunit(name) - register char *name; +ifunit(char *name) { char namebuf[IFNAMSIZ + 1]; - register char *cp, *cp2; - char *end; - register struct ifnet *ifp; + char *cp; + struct ifnet *ifp; int unit; - unsigned len; - register char c = '\0'; + unsigned len, m; + char c; - /* - * Look for a non numeric part - */ - end = name + IFNAMSIZ; - cp2 = namebuf; - cp = name; - while ((cp < end) && (c = *cp)) { - if (c >= '0' && c <= '9') - break; - *cp2++ = c; - cp++; - } - if ((cp == end) || (c == '\0') || (cp == name)) - return ((struct ifnet *)0); - *cp2 = '\0'; - /* - * check we have a legal number (limit to 7 digits?) - */ + len = strlen(name); + if (len < 2 || len > IFNAMSIZ) + return NULL; + cp = name + len - 1; + c = *cp; + if (c < '0' || c > '9') + return NULL; /* trailing garbage */ + unit = 0; + m = 1; + do { + if (cp == name) + return NULL; /* no interface name */ + unit += (c - '0') * m; + if (unit > 1000000) + return NULL; /* number is unreasonable */ + m *= 10; + c = *--cp; + } while (c >= '0' && c <= '9'); len = cp - name + 1; - for (unit = 0; - ((c = *cp) >= '0') && (c <= '9') && (unit < 1000000); cp++ ) - unit = (unit * 10) + (c - '0'); - if (*cp != '\0') - return 0; /* no trailing garbage allowed */ + bcopy(name, namebuf, len); + namebuf[len] = '\0'; /* * Now search all the interfaces for this name/number */ for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { - if (bcmp(ifp->if_name, namebuf, len)) + if (strcmp(ifp->if_name, namebuf)) continue; if (unit == ifp->if_unit) break;