[net.sources] pipe input to vi

brighton@pixar ("Amazing what a pair of fives can do . . . ") (09/15/86)

/*
I wrote this late one night, to facilitate saving files from rn ( s | svi )
Bevare: Written under 4.2BSD, and has not been tested elsewhere.
Improvements welcome.  Enjoy!

			Bill Carson @ Pixar
			415 499 3600
			uucp: ...!{ucbvax,sun}!pixar!brighton
			arpa: brighton%pixar.uucp@ucbvax.berkeley.edu
*/
/*% cc -o svi svi.c
*/

#include <stdio.h>
#include <sys/file.h>

char editor[99];
char file[1024];
char buf[10240];

char *getenv();

main()
{
	int fd, tty_in, tty_out;


	sprintf (file, "/tmp/svi.%d", getpid());
	
	fd = open (file, O_CREAT|O_RDWR, 0644);
	if (fd < 0) {
		perror (file);
		exit (1);
	}

	while (write(fd,buf,read(0,buf,1024)))
		;
	
	close (fd);

	close (0); close(1);

	tty_in = open ("/dev/tty", O_RDONLY);
	if (tty_in < 0) {
		perror ("tty_in");
		exit (1);
	}

	tty_out = open ("/dev/tty", O_WRONLY);
	if (tty_out < 0) {
		perror ("tty_out");
		exit (1);
	}

	strcpy (editor, getenv ("EDITOR"));
	if (editor[0] == 0) {
		strcpy (editor, getenv ("VISUAL"));
		if (editor[0] == 0) {
/*			fprintf (stderr, "svi: defaulting to vi\n"); */
			strcpy (editor, "/usr/ucb/vi");
		}
	}

	execl (editor, editor, file, 0);	/* should never return */

	fprintf (stderr, "execl() returned!\007\n");
}