ether_addr.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. libc_hidden_proto(ether_aton_r)
  29. libc_hidden_proto(ether_ntoa_r)
  30. libc_hidden_proto(sprintf)
  31. #ifdef __UCLIBC_HAS_XLOCALE__
  32. libc_hidden_proto(__ctype_b_loc)
  33. libc_hidden_proto(__ctype_tolower_loc)
  34. #elif __UCLIBC_HAS_CTYPE_TABLES__
  35. libc_hidden_proto(__ctype_b)
  36. libc_hidden_proto(__ctype_tolower)
  37. #endif
  38. struct ether_addr *ether_aton_r(const char *asc, struct ether_addr *addr)
  39. {
  40. size_t cnt;
  41. for (cnt = 0; cnt < 6; ++cnt) {
  42. unsigned int number;
  43. char ch;
  44. ch = _tolower(*asc++);
  45. if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
  46. return NULL;
  47. number = isdigit(ch) ? (ch - '0') : (ch - 'a' + 10);
  48. ch = _tolower(*asc);
  49. if ((cnt < 5 && ch != ':')
  50. || (cnt == 5 && ch != '\0' && !isspace(ch))) {
  51. ++asc;
  52. if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
  53. return NULL;
  54. number <<= 4;
  55. number += isdigit(ch) ? (ch - '0') : (ch - 'a' + 10);
  56. ch = *asc;
  57. if (cnt < 5 && ch != ':')
  58. return NULL;
  59. }
  60. /* Store result. */
  61. addr->ether_addr_octet[cnt] = (unsigned char) number;
  62. /* Skip ':'. */
  63. ++asc;
  64. }
  65. return addr;
  66. }
  67. libc_hidden_def(ether_aton_r)
  68. struct ether_addr *ether_aton(const char *asc)
  69. {
  70. static struct ether_addr result;
  71. return ether_aton_r(asc, &result);
  72. }
  73. char *ether_ntoa_r(const struct ether_addr *addr, char *buf)
  74. {
  75. sprintf(buf, "%x:%x:%x:%x:%x:%x",
  76. addr->ether_addr_octet[0], addr->ether_addr_octet[1],
  77. addr->ether_addr_octet[2], addr->ether_addr_octet[3],
  78. addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
  79. return buf;
  80. }
  81. libc_hidden_def(ether_ntoa_r)
  82. char *ether_ntoa(const struct ether_addr *addr)
  83. {
  84. static char asc[18];
  85. return ether_ntoa_r(addr, asc);
  86. }