/**********************************************************************
 * ti library's routine for miscellaneous stuff
 *
 * $Author: brlewis $
 * $Source: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/auth.c,v $
 * $Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/auth.c,v 0.2 92/04/01 16:19:54 brlewis Exp $
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/

#include <mit-copyright.h>

#ifndef lint
static char rcsid_ti_auth_c[] = "$Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/auth.c,v 0.2 92/04/01 16:19:54 brlewis Exp $";
#endif /* lint */

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <ti/memory.h>
#include <ti/ti.h>

/**********************************************************************
 * HSTR ti_dupstring(LPSTR s)
 *
 * allocates new memory for a string, copies it into that space.
 *
 **********************************************************************/

HSTR ti_dupstring(LPSTR s)
{
  HSTR hnew;
  LPSTR lpnew;

  hnew = New(lstrlen(s) + 1);
  if (hnew)
    {
      lpnew = TI_DEREFERENCE(hnew);
      lstrcpy(lpnew, s);
      TI_REMOVE_DEREFERENCE(hnew, lpnew);
    }
  return hnew;
}

/**********************************************************************
 * HSTR ti_dupstring(LPSTR s)
 *
 * allocates new memory for several strings,
 * and packs them into that space.
 *
 **********************************************************************/

HSTR ti_packstrings(int n, int *offsets, ...)
{
  va_list pvar;
  LPSTR currstring;
  int totalsize = 0;
  int i = 0;

  HSTR hnew;
  LPSTR lpnew;

  va_start(pvar, offsets);
  while (i < n)
    {
      offsets[i++] = totalsize;
      currstring = va_arg(pvar, LPSTR);
      totalsize += lstrlen(currstring) + 1;
    }
  va_end(pvar);

  hnew = New(totalsize);
  if (hnew)
    {
      lpnew = TI_DEREFERENCE(hnew);
      while (i < n)
	{
	  currstring = va_arg(pvar, LPSTR);
	  lstrcpy(lpnew + offsets[i++], currstring);
	}
      TI_REMOVE_DEREFERENCE(hnew, lpnew);
    }
  return hnew;
}
