[net.emacs] less hostile unlink-file in gemacs

gallaher@topaz.ARPA (Mike Gallaher) (03/14/85)

>> Perhaps delete-buffer shouldn't complete on buffer
>> names; that would avoid the dangerous behavior that brought this to my
>> attention.  

>While we're at it, perhaps unlink-file shouldn't do name completion
>either.  I know some people that have been burned by this a few times.

Here's a quick way to fix that problem.  If the following version of
unlink-file is invoked interactively, it prints the name of the file it is
about to delete and asks for confirmation.  This code is from Unipress
V2.01, so some things are different from 264 and before, but it should be
obvious how to make this work in your version.  In particular, the behavior
of error() and message() is different, and the global error flag err is now
called Emacs_err.  
---------------------------------------------------------------------------
#define FALSE		0
#define TRUE		1
#define	SUCCEED		0
#define FAIL		-1
#define PATHLEN		200	/* max absolute pathname length, as you like */
#define MAXDSPLINELEN	200	/* max display line length */

/* UnlinkFile()
 *
 * Deletes a file using help facility and file completion.
 * There are five cases:
 *  1> no file-name given -> raises error.
 *  2> file not found interactively -> raises error.
 *  3> file deleted   interactively -> prints message.
 *  4> file not found called by MLisp proc -> returns FAIL.
 *  5> file deleted  called by MLisp proc -> returns SUCCEED.
 */
hidden int
UnlinkFile()
{
char *fn;
char  scratch[MAXDSPLINELEN + 1];
char  filename[PATHLEN + 1];
int   flag = TRUE;

  if ((fn = GetFileName(": unlink-file ")) != NULL && ! Emacs_Err) {
    (void) strcpy(filename, fn);
    if (interactive) {
      (void) sprintf(scratch, "Delete \"%s\"? ", filename);
      flag = yes(TRUE, scratch);
    }
    if (! Emacs_Err && flag) {		/* If we should go ahead and rm it */
      if (unlink(filename) == FAIL)
        if (interactive) {			/* it didn't work */
          (void) sprintf(Err_buf, Err[Err_nofile], filename);
          error(Err_see_errbuf);
	} else {				/* it did work */
	    MLvalue->exp_type = IsInteger;
	    MLvalue->exp_int  = FAIL;
	}
    else
       if (interactive)
         message(FALSE, Msg_filedeleted);
       else 
         MLvalue->exp_type = IsInteger;
         MLvalue->exp_int  = SUCCEED;
    }
  }
  else
    error(Err_emptyfilename);
  return(NULL);
}
/* ------  end of less hostile unlink-file -------- */