/**********************************************************************
 * luc_edit_file.c -- edit a file
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_edit_file.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_edit_file.c,v 1.2 91/09/24 14:33:57 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_file_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_edit_file.c,v 1.2 91/09/24 14:33:57 brlewis Exp Locker: brlewis $";
#endif /* lint */

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

/**********************************************************************
 * luc_edit_file(fname, bground)
 *	caller sets fname to file to be edited
 *	caller sets bground nonzero if edit is to be done in background
 *
 * - runs editor
 * - returns error code
 **********************************************************************/

long
luc_edit_file(fname, bground)
     char *fname;
     int bground;
{
  int pid;
#ifdef POSIX
  int status;
#else
  union wait status;
#endif

  pid = vfork();

  /* handle error */
  if (pid == -1) {
    *luv_context = '\0';	/* luv_context = "" */
    return((long) errno);
  }

  if (pid) {
    /* handle parent process */
    /* wait for child to exit if not backgrounded */
    if (!bground) while(wait(&status) != pid);
    return(0L);
  } else {
    /* handle child process */
    execlp(luv_editor, luv_editor, fname, 0);
    perror(luv_editor);	/* no other way to handle error */
    _exit(EX_OSERR);
  }
}
