/*
 * This is the statistic file.
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Tom Coppeto
 * MIT Network Services
 * 8 August 1990
 *
 *    $Source: /afs/net.mit.edu/tools/src/xinterface/RCS/utils.c,v $
 *    $Author: tom $
 *    $Locker:  $
 *    $Log:	utils.c,v $
 * Revision 1.2  91/02/21  13:48:33  tom
 * fixed allocation problem
 * 
 * Revision 1.1  90/10/29  09:19:21  tom
 * Initial revision
 * 
 * Revision 1.1  90/08/15  01:13:12  tom
 * Initial revision
 * 
 */

#ifndef lint
static char *rcsid = "$Header: /afs/net.mit.edu/tools/src/xinterface/RCS/utils.c,v 1.2 91/02/21 13:48:33 tom Exp $";
#endif

#include "xport.h"
#include <mit-copyright.h>


char *buf = (char *) NULL;
static int bufsize = 0;


int
make_buf(size)
     int size;
{
  char *ptr;

  if(size < bufsize)
    return(0);
  if(buf == (char *) NULL)
    {
      if((ptr = (char *) malloc((size + 1) * sizeof(char))) == (char *) NULL)
	return(-1);
    }
  else
    if((ptr = (char *) realloc(buf, (size + 1) * sizeof(char))) == (char *) NULL)
      return(-1);
  bufsize = size + 1;
  buf = ptr;
  return(0);
}



char *
make_phys_address(v)
     objval *v;
{
  char cbuf[BUFSIZ];
  char c[16];
  int j;

  bzero(cbuf, sizeof(cbuf));
  if(v)
    for(j = 0; j < v->value.str.len; j++)
      {
	sprintf(c, "%2x ", v->value.str.str[j] & 0xff);
	strcat(cbuf, c, sizeof(cbuf) - strlen(cbuf) - 1);
      }
  return(cbuf);
}


int
isnumber(n)
     char *n;
{
  while(*n && (*n == ' '))
    ++n;

  while(*n && (*n != ' '))
    if(!isdigit(*n++))
      return(0);
      
  return(1);
}




strip_word(s, w)
     char *s;
     char *w;
{
  int l = strlen(w);

  while(*s)
    {
      if(strncasecmp(s, w, l) == 0) 
	strcpy(s, s+strlen(w));
      ++s;
    }
}


char *
elapsed_time(t)
     unsigned int t;
{
  static char tbuf[BUFSIZ];
  int days = 0;
  int hrs  = 0;
  int min  = 0;
  int sec  = 0;
  int j;
  
  j = t/86400;
  days = j;

  t = t%86400;
  j = t/3600;
  hrs = j;

  t = t%3600;
  j = t/60;
  min = j;

  t = t%60;
  sec = t;

  j = 0;

  if(days > 0)
    {
      sprintf(tbuf, "%d day%s ", days, days == 1 ? "" : "s");
      j = strlen(tbuf);
    }

  sprintf(tbuf+j, "%2.2d:%2.2d:%2.2d", hrs, min, sec);
  return(tbuf);
}
  

char *
format_number(n)
     unsigned int n;
{
  static char string[32];
  char   buf[32];
  int    i = 0;
  int    j = 0;
  int    len;
  int    pos = 0;

  if(n == -1)
    strcpy(string, "unsupported");
  else
    {
      (void) sprintf(buf, "%u", n);
      len = strlen(buf);
      len = len % 3;
      
      if(len == 2)
	pos = 1;
      if(len == 1)
	pos = 2;
      if(len == 0)
	pos = 0;
      
      while(buf[i] != '\0')
	{
	  string[j] = buf[i];
	  if((pos == 2) && (buf[i+1] != '\0'))
	    {
	      ++j;
	      string[j] = ',';
	      pos = -1;
	    }
	  ++j;
	  ++i;
	  ++pos;
	}
      string[j] = '\0';
    }
  return(string);
}


