/**********************************************************************
 * luc_csp.c -- retrieve list of correspondents
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_csp.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_csp.c,v 1.1 91/08/01 14:29:25 brlewis Exp Locker: brlewis $
 *
 * 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_luc_csp_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_csp.c,v 1.1 91/08/01 14:29:25 brlewis Exp Locker: brlewis $";
#endif /* lint */

#include <stdio.h>
#include <strings.h>
#include <ctype.h>
#include <lucy/lucy.h>
#include <lucy/memory.h>
#include <sys/errno.h>

/**********************************************************************
 * luc_csp(char ***arrayp)
 *
 * - points arrayp at char **(correspondent list)
 * - returns error code
 **********************************************************************/

long
luc_csp(arrayp)
     char ***arrayp;
{
  register char *s;
  char filename[127];
  char buf[256];
  FILE *fp;
  int i=0;

  *arrayp = NewArray(char *, 1);
  if (!(*arrayp)) {
    *luv_context = '\0';
    return((long) errno);
  }
  **arrayp = NULL;		/* start with zero-length list */

  /* Get correspondents filename */
  sprintf(filename, "%s/Private/correspondents", getenv("HOME"));
  fp = fopen(filename, "r");
  if (!fp) {
    if (errno == ENOENT) return(0L); /* no correspondents */
    free(*arrayp);
    strcpy(luv_context, filename);
    return((long) errno);
  }

  /* main loop to read correspondents */
  while(fgets(buf, 255, fp)) {

    /* remove extra whitespace */
    for (s = buf; isspace(*s); s++);
    if (s != buf) strcpy(buf, s); /* remove leading blanks */
    if (*buf == '#') continue; /* skip comments */
    while (*s != '\0') s++;
    while (isspace(*(--s))) *s = '\0'; /* remove trailing blanks */
    if (*buf == '\0') continue; /* skip blank lines */

    /* Allocate space for new correspodent */
    *arrayp = BiggerArray(char *, *arrayp, ++i);
    if (!(*arrayp)) {
      *luv_context = '\0';
      return((long) errno);
    }
    (*arrayp)[i-1] = NewArray(char, strlen(buf)+1);
    if (!((*arrayp)[i-1])) {
      luc_free_csp(arrayp);
      *luv_context = '\0';
      return((long) errno);
    }
    strcpy((*arrayp)[i-1], buf);
  }
  (void) fclose(fp);

  *arrayp = BiggerArray(char *, *arrayp, ++i);
  if (!(*arrayp)) {
    *luv_context = '\0';
    return((long) errno);
  }
  (*arrayp)[i-1] = NULL;	/* mark end of list */
  return(0L);
}
