home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume20 / etherlib / part01 / src / ethera2e.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-24  |  995 b   |  57 lines

  1. /* $Id: ethera2e.c,v 2.1 89/10/23 15:42:22 dupuy Exp $ */
  2.  
  3. #include <stdio.h>            /* sscanf */
  4.  
  5. #include "libether.h"
  6.  
  7. #define EBYTES 6
  8.  
  9. ether_addr *
  10. ether_a2e (estring, addr)
  11. char *estring;
  12. ether_addr *addr;
  13. {
  14.     unsigned bytes[EBYTES];
  15.     int allocated = 0;
  16.     int i;
  17.  
  18.     if (addr == NULL)
  19.     {
  20.     addr = (ether_addr *) malloc (sizeof (ether_addr));
  21.     allocated++;
  22.     }
  23.  
  24.     if (addr != NULL)
  25.     if (sscanf (estring, " %2x:%2x:%2x:%2x:%2x:%2x", &bytes[0], &bytes[1],
  26.             &bytes[2], &bytes[3], &bytes[4], &bytes[5])
  27.         == EBYTES ||
  28.         sscanf (estring, " %2x-%2x-%2x-%2x-%2x-%2x", &bytes[0], &bytes[1],
  29.             &bytes[2], &bytes[3], &bytes[4], &bytes[5])
  30.         == EBYTES)
  31.     {
  32.         for (i = 0; i < EBYTES; i++)
  33.         addr->bytes[i] = bytes[i];
  34.     }
  35.     else
  36.     {
  37.         if (allocated)
  38.         (void) free ((char *) addr);
  39.         addr = NULL;
  40.     }
  41.  
  42.     return (addr);
  43. }
  44.  
  45. #ifndef ETHERDB
  46.  
  47. ether_addr *
  48. ether_aton (estring)
  49. char *estring;
  50. {
  51.     static ether_addr addr;
  52.  
  53.     return (ether_a2e (estring, &addr));
  54. }
  55.  
  56. #endif
  57.