#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "../global.h"
#include "shared_cs_util.h"   /* for crash() */


/* removes leading and trailing whitespace from string */
void
strtrm (char *string)
{

  char *ptr;
  char *newstring;
  int len;

  len = strlen (string);
  newstring = (char *) malloc ((len + 1) * sizeof (char));
  if (!newstring)
    cs_critical_error (ERR_MALLOC_FAILED, string);

  for (ptr = string; *ptr && isspace (*ptr); ptr++);
  strncpy (newstring, ptr, len + 1);

  if (strlen (newstring))
    {
      ptr = strchr (newstring, '\0');
      for (ptr--; (ptr >= newstring) && isspace (*ptr); ptr--);
      *(ptr + 1) = '\0';
    }

  strncpy (string, newstring, len + 1);
  free (newstring);

}
