#include <stdio.h>
#include <ctype.h>
#include <syslog.h>

char *srealloc(ptr, size)
   char	*ptr;
   unsigned	size;
{
     extern int	debug;	/* defined in ftpd.c */
     char	*t;

     if ((t = (char *) realloc(ptr, size))==NULL && debug)
	  syslog(LOG_DEBUG, "alloc_vector: realloc failed\n");
     return t;
}

char *smalloc(size)
   unsigned	size;
{
     extern int	debug;  /* defined in ftpd.c */
     char	*t;

     if ((t = (char *) malloc(size))==NULL && debug)
	  syslog(LOG_DEBUG, "alloc_vector: malloc failed\n");
     return t;
}

int alloc_vector(s, vect, c)
   char	*s, ***vect;
   int	*c;
{
     char	**v, *t, *index();
     int	size;

     v = (char **) malloc(0);
     size = 0;
     for (t = s; isspace(*s); s++, t++) ;

     /* Get everthing but the last one */
     while (t = index(s, ' ')) {
	  v = (char **) srealloc(v, (size+1)*sizeof(char *));
	  v[size] = (char *) malloc((int) (t-s) + 1);
	  strncpy(v[size], s, (int) (t-s));
	  v[size][(int) (t-s)] = '\0';

	  for (s = t; isspace(*s); s++) ; 

	  ++size;
     }

     /* Now get the last one */
     t = s;
     v = (char **) srealloc(v, (size+1)*sizeof(char *));
     v[size] = (char *) malloc(strlen(s) + 1);
     strcpy(v[size], s);
     ++size;

     /* Make the vector null-terminated */
     v = (char **) srealloc(v, (size+1)*sizeof(char *));
     v[size] = NULL;
     ++size;
     
     *vect = v;
     *c = size;
     return(size);
}

int free_vector(v, s)
   char	**v;
   int	s;
{
     int	c;

     for (c=0; c<s; c++)
	  free(v[c]);
     free(v);
}

int in_vector(s, v)
   char	*s, **v;
{
     while (*v) {
	  if (strcmp(s, *v)==0) return 1;
	  ++v;
     }

     return 0;
}
