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

#include <stdio.h>
#include <strings.h>
#include <lucy/lucy.h>
#include <sysexits.h>
#include <sys/file.h>
#include <sys/errno.h>

/**********************************************************************
 * luc_edit_csp()
 *
 * - runs editor, or, under X, forks editor
 * - returns error code
 **********************************************************************/

long
luc_edit_csp()
{
  char filename[127];
  int pid;
  char call[256];

  /* Get correspondents directory */
  sprintf(filename, "%s/Private", getenv("HOME"));
  if (access(filename, W_OK)) {
    if (mkdir(filename, 0700)) {
      strcpy(luv_context, filename);
      return((long) errno);
    }
  }

  /* Get correspondents file */
  strcat(filename, "/correspondents");

  /* put editor in background if necessary */
  if (luc_shouldfork()) {
    pid = vfork();
    if (pid == -1) return((long) errno);
    if (pid) return(0L);	/* parent process just returns */
    /* XXX pick the right editor more carefully */
    execlp(getenv("EDITOR"), getenv("EDITOR"), filename, 0);
    perror(getenv("EDITOR"));	/* no other diagnostic available */
    _exit(EX_OSERR);
  }
  sprintf(call, "%s %s", getenv("EDITOR"), filename);
  system(call);			/* XXX exit status */
  return(0L);
}
