xref: /onnv-gate/usr/src/cmd/vi/port/expreserve.c (revision 802:73b56fb6544b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*802Scf46844  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /* Copyright (c) 1981 Regents of the University of California */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/stat.h>
380Sstevel@tonic-gate #include <sys/fcntl.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate #include <dirent.h>
410Sstevel@tonic-gate #include <pwd.h>
420Sstevel@tonic-gate #include <locale.h>
430Sstevel@tonic-gate #include <limits.h>
44*802Scf46844 #include <unistd.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #define	BUFSIZE	(LINE_MAX*2)	/* This should agree with what's in ex.h */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include "ex_tune.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	FTYPE(A)	(A.st_mode)
510Sstevel@tonic-gate #define	FMODE(A)	(A.st_mode)
520Sstevel@tonic-gate #define	IDENTICAL(A, B)	(A.st_dev == B.st_dev && A.st_ino == B.st_ino)
530Sstevel@tonic-gate #define	ISBLK(A)	((A.st_mode & S_IFMT) == S_IFBLK)
540Sstevel@tonic-gate #define	ISCHR(A)	((A.st_mode & S_IFMT) == S_IFCHR)
550Sstevel@tonic-gate #define	ISDIR(A)	((A.st_mode & S_IFMT) == S_IFDIR)
560Sstevel@tonic-gate #define	ISFIFO(A)	((A.st_mode & S_IFMT) == S_IFIFO)
570Sstevel@tonic-gate #define	ISREG(A)	((A.st_mode & S_IFMT) == S_IFREG)
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * Expreserve - preserve a file in usrpath(preserve)
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  * This routine is very naive - it doesn't remove anything from
630Sstevel@tonic-gate  * usrpath(preserve)... this may mean that we  * stuff there...
640Sstevel@tonic-gate  * the danger in doing anything with usrpath(preserve)
650Sstevel@tonic-gate  * is that the clock may be messed up and we may get confused.
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  * We are called in two ways - first from the editor with no arguments
680Sstevel@tonic-gate  * and the standard input open on the temp file. Second with an argument
690Sstevel@tonic-gate  * to preserve the entire contents of /var/tmp (root only).
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  * BUG: should do something about preserving Rx... (register contents)
720Sstevel@tonic-gate  *      temporaries.
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate 
750Sstevel@tonic-gate struct 	header {
760Sstevel@tonic-gate 	time_t	Time;			/* Time temp file last updated */
770Sstevel@tonic-gate 	int	Uid;			/* This user's identity */
780Sstevel@tonic-gate #ifndef VMUNIX
790Sstevel@tonic-gate 	short	Flines;			/* Number of lines in file */
800Sstevel@tonic-gate #else
810Sstevel@tonic-gate 	int	Flines;
820Sstevel@tonic-gate #endif
830Sstevel@tonic-gate 	unsigned char	Savedfile[FNSIZE];	/* The current file name */
840Sstevel@tonic-gate 	short	Blocks[LBLKS];		/* Blocks where line pointers stashed */
850Sstevel@tonic-gate 	short	encrypted;		/* Encrypted temp file flag */
860Sstevel@tonic-gate } H;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	eq(a, b) strcmp(a, b) == 0
890Sstevel@tonic-gate 
90*802Scf46844 void notify(int, unsigned char *, int, int);
91*802Scf46844 void mkdigits(unsigned char *);
92*802Scf46844 
93*802Scf46844 int
main(argc)940Sstevel@tonic-gate main(argc)
950Sstevel@tonic-gate 	int argc;
960Sstevel@tonic-gate {
97*802Scf46844 	DIR *tf;
980Sstevel@tonic-gate 	struct dirent64 *direntry;
990Sstevel@tonic-gate 	unsigned char *filname;
1000Sstevel@tonic-gate 	struct stat64 stbuf;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1030Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1040Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1050Sstevel@tonic-gate #endif
1060Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1070Sstevel@tonic-gate 	/*
1080Sstevel@tonic-gate 	 * If only one argument, then preserve the standard input.
1090Sstevel@tonic-gate 	 */
1100Sstevel@tonic-gate 	if (argc == 1) {
111*802Scf46844 		if (copyout((unsigned char *) 0))
112*802Scf46844 			return (1);
113*802Scf46844 		return (0);
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	/*
1170Sstevel@tonic-gate 	 * If not super user, then can only preserve standard input.
1180Sstevel@tonic-gate 	 */
1190Sstevel@tonic-gate 	if (getuid()) {
1200Sstevel@tonic-gate 		fprintf(stderr, gettext("NOT super user\n"));
121*802Scf46844 		return (1);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	/*
1250Sstevel@tonic-gate 	 * ... else preserve all the stuff in /var/tmp, removing
1260Sstevel@tonic-gate 	 * it as we go.
1270Sstevel@tonic-gate 	 */
1280Sstevel@tonic-gate 	if (chdir(TMPDIR) < 0) {
1290Sstevel@tonic-gate 		perror(TMPDIR);
130*802Scf46844 		return (1);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if ((tf = opendir(".")) == NULL)
1340Sstevel@tonic-gate 	{
1350Sstevel@tonic-gate 		perror(TMPDIR);
136*802Scf46844 		return (1);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	while ((direntry = readdir64(tf)) != NULL)
1390Sstevel@tonic-gate 	{
1400Sstevel@tonic-gate 		filname = (unsigned char *)direntry->d_name;
1410Sstevel@tonic-gate 		/*
1420Sstevel@tonic-gate 		 * Ex temporaries must begin with Ex;
1430Sstevel@tonic-gate 		 * we check that the 12th character of the name is null
1440Sstevel@tonic-gate 		 * so we won't have to worry about non-null terminated names
1450Sstevel@tonic-gate 		 * later on.
1460Sstevel@tonic-gate 		 */
1470Sstevel@tonic-gate 		if (filname[0] != 'E' || filname[1] != 'x' || filname[12])
1480Sstevel@tonic-gate 			continue;
1490Sstevel@tonic-gate 		if (stat64((char *)filname, &stbuf))
1500Sstevel@tonic-gate 			continue;
1510Sstevel@tonic-gate 		if (!ISREG(stbuf))
1520Sstevel@tonic-gate 			continue;
1530Sstevel@tonic-gate 		/*
1540Sstevel@tonic-gate 		 * Save the file.
1550Sstevel@tonic-gate 		 */
1560Sstevel@tonic-gate 		(void) copyout(filname);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 	closedir(tf);
159*802Scf46844 	return (0);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate unsigned char	mydir[] =	USRPRESERVE;
1630Sstevel@tonic-gate unsigned char	pattern[] =	"/Exaa`XXXXXXXXXX";
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * Copy file name into usrpath(preserve)/...
1670Sstevel@tonic-gate  * If name is (char *) 0, then do the standard input.
1680Sstevel@tonic-gate  * We make some checks on the input to make sure it is
1690Sstevel@tonic-gate  * really an editor temporary, generate a name for the
1700Sstevel@tonic-gate  * file (this is the slowest thing since we must stat
1710Sstevel@tonic-gate  * to find a unique name), and finally copy the file.
1720Sstevel@tonic-gate  */
173*802Scf46844 int
copyout(unsigned char * name)174*802Scf46844 copyout(unsigned char *name)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	int i;
1770Sstevel@tonic-gate 	static int reenter;
1780Sstevel@tonic-gate 	unsigned char buf[BUFSIZE];
1790Sstevel@tonic-gate 	unsigned char	savdir[PATH_MAX+1];
1800Sstevel@tonic-gate 	unsigned char	savfil[PATH_MAX+1];
1810Sstevel@tonic-gate 	struct passwd *pp;
1820Sstevel@tonic-gate 	struct stat64	stbuf;
1830Sstevel@tonic-gate 	int savfild;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * The first time we put in the digits of our
1870Sstevel@tonic-gate 	 * process number at the end of the pattern.
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	if (reenter == 0) {
1900Sstevel@tonic-gate 		mkdigits(pattern);
1910Sstevel@tonic-gate 		reenter++;
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	/*
1950Sstevel@tonic-gate 	 * If a file name was given, make it the standard
1960Sstevel@tonic-gate 	 * input if possible.
1970Sstevel@tonic-gate 	 */
1980Sstevel@tonic-gate 	if (name != 0) {
1990Sstevel@tonic-gate 		(void) close(0);
2000Sstevel@tonic-gate 		/*
2010Sstevel@tonic-gate 		 * Need read/write access for arcane reasons
2020Sstevel@tonic-gate 		 * (see below).
2030Sstevel@tonic-gate 		 */
2040Sstevel@tonic-gate 		if (open(name, O_RDWR) < 0)
2050Sstevel@tonic-gate 			return (-1);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/*
2090Sstevel@tonic-gate 	 * Get the header block.
2100Sstevel@tonic-gate 	 */
2110Sstevel@tonic-gate 	(void) lseek(0, 0l, 0);
2120Sstevel@tonic-gate 	if (read(0, (char *)&H, sizeof (H)) != sizeof (H)) {
2130Sstevel@tonic-gate format:
2140Sstevel@tonic-gate 		if (name == 0)
2150Sstevel@tonic-gate 			fprintf(stderr, gettext("Buffer format error\t"));
2160Sstevel@tonic-gate 		else {
2170Sstevel@tonic-gate 			/*
2180Sstevel@tonic-gate 			 * avoid having a bunch of NULL Ex* files
2190Sstevel@tonic-gate 			 * hanging around
2200Sstevel@tonic-gate 			 */
2210Sstevel@tonic-gate 			struct stat64 stbuf;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 			if (stat64((char *)name, &stbuf) == 0)
2240Sstevel@tonic-gate 			if (stbuf.st_size == 0)
225*802Scf46844 				(void) unlink((char *)name);
2260Sstevel@tonic-gate 		}
2270Sstevel@tonic-gate 		return (-1);
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/*
2310Sstevel@tonic-gate 	 * Consistency checks so we don't copy out garbage.
2320Sstevel@tonic-gate 	 */
2330Sstevel@tonic-gate 	if (H.Flines < 0) {
2340Sstevel@tonic-gate #ifdef DEBUG
2350Sstevel@tonic-gate 		fprintf(stderr, "Negative number of lines\n");
2360Sstevel@tonic-gate #endif
2370Sstevel@tonic-gate 		goto format;
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 	if (H.Blocks[0] != HBLKS || H.Blocks[1] != HBLKS+1) {
2400Sstevel@tonic-gate #ifdef DEBUG
2410Sstevel@tonic-gate 		fprintf(stderr, "Blocks %d %d\n", H.Blocks[0], H.Blocks[1]);
2420Sstevel@tonic-gate #endif
2430Sstevel@tonic-gate 		goto format;
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 	if (name == 0 && H.Uid != getuid()) {
2460Sstevel@tonic-gate #ifdef DEBUG
2470Sstevel@tonic-gate 		fprintf(stderr, "Wrong user-id\n");
2480Sstevel@tonic-gate #endif
2490Sstevel@tonic-gate 		goto format;
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 	if (lseek(0, 0l, 0)) {
2520Sstevel@tonic-gate #ifdef DEBUG
2530Sstevel@tonic-gate 		fprintf(stderr, gettext("Negative number of lines\n"));
2540Sstevel@tonic-gate #endif
2550Sstevel@tonic-gate 		goto format;
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	/*
2590Sstevel@tonic-gate 	 * If no name was assigned to the file, then give it the name
2600Sstevel@tonic-gate 	 * LOST, by putting this in the header.
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	if (H.Savedfile[0] == 0) {
2630Sstevel@tonic-gate 		(void) strcpy(H.Savedfile, "LOST");
2640Sstevel@tonic-gate 		(void) write(0, (char *) &H, sizeof (H));
2650Sstevel@tonic-gate 		H.Savedfile[0] = 0;
2660Sstevel@tonic-gate 		(void) lseek(0, 0l, 0);
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/*
2700Sstevel@tonic-gate 	 * See if preservation directory for user exists.
2710Sstevel@tonic-gate 	 */
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	strcpy(savdir, mydir);
2740Sstevel@tonic-gate 	pp = getpwuid(H.Uid);
2750Sstevel@tonic-gate 	if (pp)
2760Sstevel@tonic-gate 		strcat(savdir, pp->pw_name);
2770Sstevel@tonic-gate 	else {
2780Sstevel@tonic-gate 		fprintf(stderr, gettext("Unable to get uid for user.\n"));
2790Sstevel@tonic-gate 		return (-1);
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 	if (lstat64((char *)savdir, &stbuf) < 0 || !S_ISDIR(stbuf.st_mode)) {
2820Sstevel@tonic-gate 		/* It doesn't exist or it isn't a directory, safe to unlink */
283*802Scf46844 		(void) unlink((char *)savdir);
2840Sstevel@tonic-gate 		if (mkdir((char *)savdir, 0700) < 0) {
2850Sstevel@tonic-gate 			fprintf(stderr,
2860Sstevel@tonic-gate 				gettext("Unable to create directory \"%s\"\n"),
2870Sstevel@tonic-gate 				savdir);
2880Sstevel@tonic-gate 			perror("");
2890Sstevel@tonic-gate 			return (-1);
2900Sstevel@tonic-gate 		}
2910Sstevel@tonic-gate 		(void) chmod((char *)savdir, 0700);
292*802Scf46844 		(void) chown((char *)savdir, H.Uid, 2);
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/*
2960Sstevel@tonic-gate 	 * File is good.  Get a name and create a file for the copy.
2970Sstevel@tonic-gate 	 */
2980Sstevel@tonic-gate 	(void) close(1);
2990Sstevel@tonic-gate 	if ((savfild = mknext(savdir, pattern)) < 0) {
3000Sstevel@tonic-gate 		if (name == 0)
3010Sstevel@tonic-gate 			perror((char *)savfil);
3020Sstevel@tonic-gate 		return	(1);
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 	strcpy(savfil, savdir);
3050Sstevel@tonic-gate 	strcat(savfil, pattern);
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * Make target owned by user.
3080Sstevel@tonic-gate 	 */
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	(void) fchown(savfild, H.Uid, 2);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	/*
3130Sstevel@tonic-gate 	 * Copy the file.
3140Sstevel@tonic-gate 	 */
3150Sstevel@tonic-gate 	for (;;) {
3160Sstevel@tonic-gate 		i = read(0, buf, BUFSIZE);
3170Sstevel@tonic-gate 		if (i < 0) {
3180Sstevel@tonic-gate 			if (name)
3190Sstevel@tonic-gate 				perror(gettext("Buffer read error"));
320*802Scf46844 			(void) unlink((char *)savfil);
3210Sstevel@tonic-gate 			return (-1);
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 		if (i == 0) {
3240Sstevel@tonic-gate 			if (name)
325*802Scf46844 				(void) unlink((char *)name);
3260Sstevel@tonic-gate 			notify(H.Uid, H.Savedfile, (int) name, H.encrypted);
3270Sstevel@tonic-gate 			return (0);
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 		if (write(savfild, buf, i) != i) {
3300Sstevel@tonic-gate 			if (name == 0)
3310Sstevel@tonic-gate 				perror((char *)savfil);
332*802Scf46844 			(void) unlink((char *)savfil);
3330Sstevel@tonic-gate 			return (-1);
3340Sstevel@tonic-gate 		}
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate /*
3390Sstevel@tonic-gate  * Blast the last 5 characters of cp to be the process number.
3400Sstevel@tonic-gate  */
341*802Scf46844 void
mkdigits(unsigned char * cp)342*802Scf46844 mkdigits(unsigned char *cp)
3430Sstevel@tonic-gate {
344*802Scf46844 	pid_t i;
345*802Scf46844 	int j;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	for (i = getpid(), j = 10, cp += strlen(cp); j > 0; i /= 10, j--)
3480Sstevel@tonic-gate 		*--cp = i % 10 | '0';
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate /*
3520Sstevel@tonic-gate  * Make the name in cp be unique by clobbering up to
3530Sstevel@tonic-gate  * three alphabetic characters into a sequence of the form 'aab', 'aac', etc.
3540Sstevel@tonic-gate  * Mktemp gets weird names too quickly to be useful here.
3550Sstevel@tonic-gate  */
356*802Scf46844 int
mknext(unsigned char * dir,unsigned char * cp)357*802Scf46844 mknext(unsigned char *dir, unsigned char *cp)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	unsigned char *dcp;
3600Sstevel@tonic-gate 	struct stat stb;
3610Sstevel@tonic-gate 	unsigned char path[PATH_MAX+1];
3620Sstevel@tonic-gate 	int fd;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	strcpy(path, dir);
3650Sstevel@tonic-gate 	strcat(path, cp);
3660Sstevel@tonic-gate 	dcp = path + strlen(path) - 1;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	while (isdigit(*dcp))
3690Sstevel@tonic-gate 		dcp--;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	do {
3720Sstevel@tonic-gate 		if (dcp[0] == 'z') {
3730Sstevel@tonic-gate 			dcp[0] = 'a';
3740Sstevel@tonic-gate 			if (dcp[-1] == 'z') {
3750Sstevel@tonic-gate 				dcp[-1] = 'a';
3760Sstevel@tonic-gate 				if (dcp[-2] == 'z') {
3770Sstevel@tonic-gate 					fprintf(stderr,
3780Sstevel@tonic-gate 						gettext("Can't find a name\t"));
3790Sstevel@tonic-gate 						return (-1);
3800Sstevel@tonic-gate 				}
3810Sstevel@tonic-gate 				dcp[-2]++;
3820Sstevel@tonic-gate 			} else
3830Sstevel@tonic-gate 				dcp[-1]++;
3840Sstevel@tonic-gate 		} else
3850Sstevel@tonic-gate 			dcp[0]++;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	} while (((fd = open(path, O_CREAT|O_EXCL|O_WRONLY, 0600)) < 0) &&
3880Sstevel@tonic-gate 		errno == EEXIST);
3890Sstevel@tonic-gate 	/* copy out patern */
3900Sstevel@tonic-gate 	strcpy(cp, path + strlen(dir));
3910Sstevel@tonic-gate 	return (fd);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate  * Notify user uid that his file fname has been saved.
3960Sstevel@tonic-gate  */
397*802Scf46844 void
notify(int uid,unsigned char * fname,int flag,int cryflag)398*802Scf46844 notify(int uid, unsigned char *fname, int flag, int cryflag)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate #define MAXHOSTNAMELEN 256
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	struct passwd *pp = getpwuid(uid);
404*802Scf46844 	FILE *mf;
4050Sstevel@tonic-gate 	unsigned char cmd[BUFSIZE];
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	char hostname[MAXHOSTNAMELEN];
4080Sstevel@tonic-gate 	int namelen = MAXHOSTNAMELEN ;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if (gethostname((char *)hostname, namelen) == -1)
4110Sstevel@tonic-gate 	  return;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	if (pp == NULL)
4140Sstevel@tonic-gate 		return;
4150Sstevel@tonic-gate 	sprintf((char *)cmd, "/usr/bin/mail %s", pp->pw_name);
4160Sstevel@tonic-gate 	mf = popen((char *)cmd, "w");
4170Sstevel@tonic-gate 	if (mf == NULL)
4180Sstevel@tonic-gate 		return;
4190Sstevel@tonic-gate 	setbuf(mf, (char *)cmd);
4200Sstevel@tonic-gate 	if (fname[0] == 0) {
4210Sstevel@tonic-gate 		fprintf(mf, flag ?
4220Sstevel@tonic-gate "A copy of an editor buffer of yours was saved on %s when the system went down.\n" :
4230Sstevel@tonic-gate "A copy of an editor buffer of yours was saved on %s when the editor was killed\nor was unable to save your changes.\n", hostname);
4240Sstevel@tonic-gate 		fprintf(mf,
4250Sstevel@tonic-gate "No name was associated with this buffer so it has been named \"LOST\".\n");
4260Sstevel@tonic-gate 	} else
4270Sstevel@tonic-gate 		fprintf(mf, flag ?
4280Sstevel@tonic-gate "A copy of an editor buffer of your file \"%s\"%s was saved on %s\nwhen the system \
4290Sstevel@tonic-gate went down.\n" :
4300Sstevel@tonic-gate "A copy of an editor buffer of your file \"%s\"%s was saved on %s\nwhen the editor \
4310Sstevel@tonic-gate was killed or was unable to save your changes.\n", fname, (cryflag) ? "[ENCRYPTED]" : "", hostname);
4320Sstevel@tonic-gate 		/*
4330Sstevel@tonic-gate 		 * "the editor was killed" is perhaps still not an ideal
4340Sstevel@tonic-gate 		 * error message.  Usually, either it was forceably terminated
4350Sstevel@tonic-gate 		 * or the phone was hung up, but we don't know which.
4360Sstevel@tonic-gate 		 */
4370Sstevel@tonic-gate 	fprintf(mf,
4380Sstevel@tonic-gate "This buffer can be retrieved using the \"recover\" command of the editor.\n");
4390Sstevel@tonic-gate 	fprintf(mf,
4400Sstevel@tonic-gate "An easy way to do this is to give the command \"vi -r %s\".\n",
4410Sstevel@tonic-gate 		(fname[0] == 0) ? "LOST" : (char *) fname);
4420Sstevel@tonic-gate 	fprintf(mf, "This works for \"edit\" and \"ex\" also.\n");
4430Sstevel@tonic-gate 	(void) pclose(mf);
4440Sstevel@tonic-gate }
445