home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- *******************************************************************************
-
- Site: Western Michigan University Academic Computer Center
-
- System: Directory/File System Maintenance
-
- Program: maint
-
- Version=01 Level=00 01/24/92 Leonard J. Peirce
-
- Purpose: Call an editor for a file. The EDITOR variable is first
- examined; if it's not defined, DEFAULT_EDITOR is used.
-
- Arguments: See individual routine(s).
-
- External variables: None
-
- Maint external functions:
-
- Defined: edit
-
- Called: None
-
- Files accessed: File to be edited, passed through parameter list
-
- Return codes: See individual routine(s).
-
- Compiling instructions: See Makefile
-
- Linking instructions: See Makefile
-
- Other information: (C) Copyright 1992, Leonard J. Peirce
-
- ********************************************************************************
- *******************************************************************************/
-
- /******************************************************************************/
- /* */
- /* # I N C L U D E F I L E S */
- /* */
- /******************************************************************************/
-
- #include <stdio.h>
- #include <string.h>
- #if !defined(SYSV) || defined(sun)
- #include <sys/wait.h>
- #endif
- #include "maint.h"
-
- /******************************************************************************/
- /* */
- /* # D E F I N E S */
- /* */
- /******************************************************************************/
-
- /******************************************************************************/
- /* */
- /* S T R U C T U R E S , U N I O N S , T Y P E D E F S */
- /* */
- /******************************************************************************/
-
- /******************************************************************************/
- /* */
- /* E X T E R N A L D E F I N I T I O N S & D E C L A R A T I O N S */
- /* */
- /******************************************************************************/
-
- extern char *getenv();
-
- extern int access(),
- wait(),
- fork(),
- execlp();
-
- int edit();
-
- /******************************************************************************/
- /* */
- /* S T A T I C D E F I N I T I O N S & D E C L A R A T I O N S */
- /* */
- /******************************************************************************/
-
- /*******************************************************************************
- ********************************************************************************
-
- Function: edit
-
- Purpose: Call an editor for a file. If the user has an EDITOR
- environment variable, use it. Otherwise, the default is
- DEFAULT_EDITOR.
-
- Global variables:
-
- Name Examine/Modify/Use/Read/Write
- ---- -----------------------------
- none
-
- Return Codes:
-
- Code Reason
- ---- ------
- SUCCESS
- FAILURE
-
- ********************************************************************************
- *******************************************************************************/
-
- int edit(filename)
- /******* FORMAL PARAMETERS *******/
- char *filename; /* name of file to be edited */
-
- { /*** edit ***/
- /******** LOCAL VARIABLES ********/
- char *editor, /* editor to use */
- *tptr; /* pointer to last level of command */
- int child, /* pid of child process */
- i; /* loop and return value */
- #if !defined(SYSV) || defined(sun)
- union wait status; /* exit status for child process */
- #else
- int status;
- #endif
-
-
- /* see if the user has an EDITOR */
-
- editor = getenv("EDITOR");
-
- if(editor == NULL || *editor == '\0')
- editor = DEFAULT_EDITOR; /* user doesn't have an EDITOR */
-
- tptr = strrchr(editor,'/'); /* get last level to pass to execlp */
-
- if(tptr == NULL)
- tptr = editor;
- else
- tptr++;
-
- if((child = vfork()) == 0)
- {
- /* we're in the child */
-
- endwin();
- execlp(editor,tptr,filename,NULL);
- return(FAILURE); /* execlp failed if we get here */
- }
- else if(child > 0)
- {
- /* we're in the parent; wait for the child to finish */
-
- while(((i = wait(&status)) != child) && i > 0)
- ;
- }
- else
- return(FAILURE);
-
- return(SUCCESS);
-
- } /*** edit ***/
-