Back to index

glibc  2.9
ether_aton_r.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996,97,98,99,2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <ctype.h>
00021 #include <stdlib.h>
00022 #include <netinet/ether.h>
00023 #include <netinet/if_ether.h>
00024 
00025 
00026 struct ether_addr *
00027 ether_aton_r (const char *asc, struct ether_addr *addr)
00028 {
00029   size_t cnt;
00030 
00031   for (cnt = 0; cnt < 6; ++cnt)
00032     {
00033       unsigned int number;
00034       char ch;
00035 
00036       ch = _tolower (*asc++);
00037       if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
00038        return NULL;
00039       number = isdigit (ch) ? (ch - '0') : (ch - 'a' + 10);
00040 
00041       ch = _tolower (*asc);
00042       if ((cnt < 5 && ch != ':') || (cnt == 5 && ch != '\0' && !isspace (ch)))
00043        {
00044          ++asc;
00045          if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
00046            return NULL;
00047          number <<= 4;
00048          number += isdigit (ch) ? (ch - '0') : (ch - 'a' + 10);
00049 
00050          ch = *asc;
00051          if (cnt < 5 && ch != ':')
00052            return NULL;
00053        }
00054 
00055       /* Store result.  */
00056       addr->ether_addr_octet[cnt] = (unsigned char) number;
00057 
00058       /* Skip ':'.  */
00059       ++asc;
00060     }
00061 
00062   return addr;
00063 }
00064 libc_hidden_def (ether_aton_r)