ether_addr.c 2.5 KB

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