/*
 * inode.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id: eweid.c,v 1.13 2001/10/12 13:51:11 root Exp $";

/*
 * $Log: eweid.c,v $
 * Revision 1.13  2001/10/12 13:51:11  root
 * #
 *
 * Revision 1.12  2001/10/04 18:21:16  root
 * #
 *
 * Revision 1.11  2001/01/30 06:16:18  root
 * #
 *
 * Revision 1.10  2000/12/01 04:42:10  root
 * #
 *
 * Revision 1.9  2000/11/23 23:45:19  root
 * #
 *
 * Revision 1.8  2000/10/07 18:30:03  root
 * #
 *
 * Revision 1.7  2000/09/24 01:58:21  root
 * #
 *
 * Revision 1.6  2000/09/24 01:48:12  root
 * #
 *
 * Revision 1.5  2000/09/24 01:44:47  root
 * #
 *
 * Revision 1.4  2000/09/24 01:12:43  root
 * #
 *
 * Revision 1.3  2000/09/19 03:53:27  root
 * #
 *
 * Revision 1.2  2000/09/13 15:13:40  root
 * #
 *
 * Revision 1.1  2000/09/13 14:36:48  root
 * #
 *
 * Revision 1.1  2000/09/10 17:44:49  root
 * #
 *
 */

#include "project.h"

#include <ctype.h>

EXTERNAL Eweid eweid_dup(Eweid e)
{
  Eweid ret;

  if (!e)
    return NULL;

  ret = CREATE_OBJ(Eweid);

  bcopy(e->e, ret->e, sizeof(e->e));

  return ret;
}

EXTERNAL int EWEID_IS_NONE(Eweid e)
{
  int i;

  if (!e)
    return 1;

#if 1
  for (i = 0; i < 8; ++i)
    if (e->e[i])
      return 0;
  return 1;
#else
  return 0;
#endif

}


EXTERNAL int EWEID_CMP(Eweid a, Eweid b)
{
  int i;

  for (i = 0; i < 8; ++i) {
    int j = a->e[i];
    j -= b->e[i];
    if (j)
      return j;
  }

  return 0;

}

EXTERNAL Eweid eweid_combine(Eweid e, long inode)
{
  Eweid ret = CREATE_OBJ(Eweid);


  ret->e[7] = inode & 0xff;
  inode >>= 8;
  ret->e[6] = inode & 0xff;
  inode >>= 8;
  ret->e[5] = inode & 0xff;
  inode >>= 8;
  ret->e[4] = inode & 0xff;

  ret->e[3] = e->e[3];
  ret->e[2] = e->e[2];
  ret->e[1] = e->e[1];
  ret->e[0] = e->e[0];

  return ret;
}


INTERNAL Eweid eweid_dev(Eweid e)
{
  Eweid ret = CREATE_OBJ(Eweid);

  ret->e[3] = e->e[3];
  ret->e[2] = e->e[2];
  ret->e[1] = e->e[1];
  ret->e[0] = e->e[0];

  return ret;
}


EXTERNAL char *eweidtoa(Eweid e)
{

  char *ret = malloc(17);
  if (EWEID_IS_NONE(e)) {
    free(ret);
    return strdup("EWEID_NONE");
  }

  sprintf(ret, "%02x%02x%02x%02x%02x%02x%02x%02x",
          e->e[0], e->e[1], e->e[2], e->e[3],
          e->e[4], e->e[5], e->e[6], e->e[7]);


  return ret;
}

EXTERNAL void satoeweid(Eweid e, char *a)
{

  int i;

  for (i = 0; i < 8; i++) {
    char buf[3] = { a[0], a[1], 0 };
    e->e[i] = strtol(buf, NULL, 16);
    a += 2;
  }


}


INTERNAL int isvalidaeweid(char *a)
{
  int i = 16;

  while (i--) {
    if (!isxdigit(*(a++)))
      return 0;
  }

  return 1;
}


EXTERNAL Eweid atoeweid(char *a)
{
  Eweid e = NULL;

  if (!isvalidaeweid(a))
    return e;

  e = CREATE_OBJ(Eweid);
  satoeweid(e, a);
  return e;
}
