/*
 * $Id: util.c,v 1.4 1999/03/11 20:27:52 labovit Exp $
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <netdb.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>

#include <mrt.h>
      


/* r_inet_ntoa
 * A thread safe (and IPNG) version of inet_ntoa
 * takes allocated buffer and buffer size
 */
char *r_inet_ntoa (char *buf, int n, u_char *l, int len)
{
  memset (buf, 0, n);

  /*ASSERT ( (len >= 0) && (len < 255));*/

   if (len > 24)
      sprintf(buf, "%d.%d.%d.%d/%d", l[0], l[1], l[2], l[3], len);
   else if (len > 16)
      sprintf(buf, "%d.%d.%d/%d", l[0], l[1], l[2], len);
   else if (len > 8)
      sprintf(buf, "%d.%d/%d", l[0], l[1], len);
   else 
      sprintf(buf, "%d/%d", l[0], len);

   return (buf);
}

/* 
 * A thread safe (and IPNG) version of inet_ntoa
 * takes allocated buffer and buffer size
 * this doesn't append /length
 */
char *r_inet_ntoa2 (char *buf, int n, u_char *l)
{
   sprintf(buf, "%d.%d.%d.%d", l[0], l[1], l[2], l[3]);
   return (buf);
}

/* r_inet_ntoa
 * A thread safe (and IPNG) version of inet_ntoa
 * takes allocated buffer and buffer size
 */
char *rinet_ntoa (char *buf, int n, prefix_t *prefix)
{
  int len = prefix->bitlen;
  u_char *l = (u_char *) prefix_tochar (prefix);
  memset (buf, 0, n);

  /*ASSERT ( (len >= 0) && (len < 255));*/

   if (len > 24)
      sprintf(buf, "%d.%d.%d.%d/%d", l[0], l[1], l[2], l[3], len);
   else if (len > 16)
      sprintf(buf, "%d.%d.%d/%d", l[0], l[1], l[2], len);
   else if (len > 8)
      sprintf(buf, "%d.%d/%d", l[0], l[1], len);
   else 
      sprintf(buf, "%d/%d", l[0], len);

   return (buf);
}

char *my_inet_ntoa_simple (u_char *l, int len)
{
   static char tmp[100];
   memset (tmp, 0, 100);

   /*   ASSERT ( (len >= 0) && (len < 255));*/

   if (len > 24)
      sprintf(tmp, "%d.%d.%d.%d", l[0], l[1], l[2], l[3]);
   else if (len > 16)
      sprintf(tmp, "%d.%d.%d", l[0], l[1], l[2]);
   else if (len > 8)
      sprintf(tmp, "%d.%d", l[0], l[1]);
   else 
      sprintf(tmp, "%d", l[0]);

   return (tmp);
}


/*-----------------------------------------------------------
 *  Name: 	my_atoi
 *  Created:	Wed Oct 26 00:04:36 1994
 *  Author: 	Laurent Joncheray <lpj@merit.edu>
 *  DESCR:  	
 */

int my_atoi(str, i)
        char *str;
        int *i;
{
        for(*i = 0; *str; str++) {
	  if (!isdigit((int) *str))
                        return(0);
                *i = *i * 10 + ((int)*str - 0x30);
        }
	if ( (*i < 0) || (*i > 255))
	   return (0);
        return(1);
}



/*-----------------------------------------------------------
 *  Name: 	atox
 *  Created:	Tue Nov 26 20:32:23 EST 1996
 *  Author: 	Masaki Hirabaru <masaki@merit.edu>
 *  DESCR:  	Hex string into binary
 */

int atox(str)
        char *str;
{
	int x = 0;

        for(; *str; str++) {
	  int c = toupper ((int) *str);

                if (isdigit(c)) {
			x = x * 16 + c - '0';
		}
		else if (c >= 'A' && c <= 'F') {
			x = x * 16 + c - 'A' + 10;
		}
		else {
                        return(x);
		}
        }
        return(x);
}



  
/*-----------------------------------------------------------
 *  Name: 	my_strftime
 *  Created:	Mon Dec 19 14:15:42 1994
 *  Author: 	Craig Labovitz   <labovit@snoopy.merit.net>
 *  DESCR:  	Given a time long and format, return string. 
 *		If time <=0, use current time of day
 */

char *my_strftime(long in_time, char *fmt)
{
   char *tmp = NewArray (char, MAXLINE);
   time_t t;
   struct tm *tm;

   if (in_time <= 0)
      t = time (NULL);
   else
      t = (time_t) in_time;

   tm = localtime((time_t *) &t);

   strftime (tmp, MAXLINE, fmt, tm);
   return (tmp);
}



buffer_t *
New_Buffer (int len)
{
   buffer_t *buffer = New (buffer_t);

   buffer->data = NewArray (u_char, len);
   buffer->len = len;

   return (buffer);
}


void
Delete_Buffer (buffer_t *buffer)
{
   Delete (buffer->data);
   Delete (buffer);
}


/*
 * returns pointer to a token
 * line, a pointer of pointer to the string will be updated to the next position
 * word is a strage for the token
 * if word == NULL, it will be dynamically allocated by malloc
 */

char *
uii_parse_line2 (char **line, char *word)
{
    char *cp = *line, *start;
    int len;

    /* skip spaces */
    while (*cp && isspace ((int) *cp))
	cp++;

    start = cp;
    while (!isspace ((int) *cp) && (*cp != '\0') && (*cp != '\n'))
	cp++;

    if ((len = cp - start) > 0) {
	if (word == NULL) {
	    word = NewArray (char, len + 1);
	    assert (word);
	}
	memcpy (word, start, len);
	word[len] = '\0';
	*line = cp;
	return (word);
    }

    return (NULL);
}

