ether_addr.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. */
  17. /*
  18. * 2002-12-24 Nick Fedchik <nick@fedchik.org.ua>
  19. * - initial uClibc port
  20. */
  21. #define __FORCE_GLIBC
  22. #include <features.h>
  23. #include <ctype.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <netinet/ether.h>
  27. #include <netinet/if_ether.h>
  28. struct ether_addr attribute_hidden *__ether_aton_r(const char *asc, struct ether_addr *addr)
  29. {
  30. size_t cnt;
  31. for (cnt = 0; cnt < 6; ++cnt) {
  32. unsigned int number;
  33. char ch;
  34. ch = _tolower(*asc++);
  35. if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
  36. return NULL;
  37. number = isdigit(ch) ? (ch - '0') : (ch - 'a' + 10);
  38. ch = _tolower(*asc);
  39. if ((cnt < 5 && ch != ':')
  40. || (cnt == 5 && ch != '\0' && !isspace(ch))) {
  41. ++asc;
  42. if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
  43. return NULL;
  44. number <<= 4;
  45. number += isdigit(ch) ? (ch - '0') : (ch - 'a' + 10);
  46. ch = *asc;
  47. if (cnt < 5 && ch != ':')
  48. return NULL;
  49. }
  50. /* Store result. */
  51. addr->ether_addr_octet[cnt] = (unsigned char) number;
  52. /* Skip ':'. */
  53. ++asc;
  54. }
  55. return addr;
  56. }
  57. strong_alias(__ether_aton_r,ether_aton_r)
  58. struct ether_addr *ether_aton(const char *asc)
  59. {
  60. static struct ether_addr result;
  61. return __ether_aton_r(asc, &result);
  62. }
  63. char attribute_hidden *__ether_ntoa_r(const struct ether_addr *addr, char *buf)
  64. {
  65. __sprintf(buf, "%x:%x:%x:%x:%x:%x",
  66. addr->ether_addr_octet[0], addr->ether_addr_octet[1],
  67. addr->ether_addr_octet[2], addr->ether_addr_octet[3],
  68. addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
  69. return buf;
  70. }
  71. strong_alias(__ether_ntoa_r,ether_ntoa_r)
  72. char *ether_ntoa(const struct ether_addr *addr)
  73. {
  74. static char asc[18];
  75. return __ether_ntoa_r(addr, asc);
  76. }