(void) snprintf(tname, sizeof tname, "/tmp/#%ld.sup", (long)thispid); tof = open(tname, (O_WRONLY|O_CREAT|O_TRUNC), 0600);
(void) snprintf(tname, sizeof tname, "/tmp/#%ld.sup.XXXXXXXXXX", (long)thispid); tof = mkstemp(tname);
copyfile(to, from)	char *to;	char *from;		/* 0 if reading from network */{	int fromf, tof, istemp, x;	char dpart[STRINGLENGTH], fpart[STRINGLENGTH];	char tname[STRINGLENGTH];	static int true = 1;	static pid_t thispid = 0;	/* process id # */	if (from) {			/* reading file */		fromf = open(from, O_RDONLY, 0);		if (fromf < 0) {			notify("SUP: Can't open %s to copy to %s: %s\n",			    from, to, errmsg(-1));			return (TRUE);		}	} else				/* reading network */		fromf = -1;	istemp = TRUE;			/* try to create temp file */	lockout(TRUE);			/* block interrupts */	if (thispid == 0)		thispid = getpid();	/* Now try hard to find a temp file name.  Try VERY hard. */	for (;;) {	/* try destination directory */		path(to, dpart, sizeof dpart, fpart, sizeof fpart);		(void) snprintf(tname, sizeof tname, "%s/#%ld.sup",		    dpart, (long)thispid);		tof = open(tname, (O_WRONLY|O_CREAT|O_TRUNC), 0600);		if (tof >= 0)			break;	/* try sup directory */		if (thisC->Cprefix)			(void) chdir (thisC->Cbase);		(void) snprintf(tname, sizeof tname, "sup/#%ld.sup",		    (long)thispid);		tof = open(tname, (O_WRONLY|O_CREAT|O_TRUNC), 0600);		if (tof >= 0) {			if (thisC->Cprefix)				(void) chdir(thisC->Cprefix);			break;		}	/* try base directory */		(void) snprintf(tname, sizeof tname, "#%ld.sup",		    (long)thispid);		tof = open (tname,(O_WRONLY|O_CREAT|O_TRUNC),0600);		if (thisC->Cprefix)			(void) chdir(thisC->Cprefix);		if (tof >= 0)			break;#ifdef	VAR_TMP	/* try /var/tmp */		(void) snprintf(tname, sizeof tname, "/var/tmp/#%ld.sup",		    (long)thispid);		tof = open(tname, (O_WRONLY|O_CREAT|O_TRUNC), 0600);		if (tof >= 0)			break;#else	/* try /usr/tmp */		(void) snprintf(tname, sizeof tname, "/usr/tmp/#%ld.sup",			(long)thispid);		tof = open(tname, (O_WRONLY|O_CREAT|O_TRUNC), 0600);		if (tof >= 0)			break;#endif	/* try /tmp */		(void) snprintf(tname, sizeof tname, "/tmp/#%ld.sup",		    (long)thispid);		tof = open(tname, (O_WRONLY|O_CREAT|O_TRUNC), 0600);		if (tof >= 0)			break;		istemp = FALSE;	/* give up: try to create output file */		if (!docompress)			tof = open(to, (O_WRONLY|O_CREAT|O_TRUNC), 0600);		if (tof >= 0)			break;	/* no luck */		notify("SUP: Can't create %s or temp file for it\n", to);		lockout (FALSE);		if (fromf >= 0)			(void) close (fromf);		else {			x = readskip();			if (x != SCMOK)				goaway("Can't skip file transfer");		}		if (true)			return (TRUE);	}	if (fromf >= 0) {		/* read file */		x = filecopy(fromf, tof);		(void) close(fromf);		(void) close(tof);		if (x < 0) {			notify("SUP: Error in copying %s to %s\n", from, to);			if (istemp)				(void) unlink(tname);			lockout(FALSE);			return (TRUE);		}	} else {			/* read network */#if	MACH		if (!rpauseflag) {			int fsize;			struct fsparam fsp;			x = prereadcount(&fsize);			if (x != SCMOK) {				if (istemp)					(void) unlink(tname);				lockout(FALSE);				x = readskip();				if (x != SCMOK)					goaway("Can't skip file transfer");				goaway("Error in server space check");				logquit(1, "Error in server space check");			}			errno = 0;			if (ioctl(tof, FIOCFSPARAM, (char *)&fsp) < 0 &&			    errno != EINVAL) {				if (istemp)					(void) unlink(tname);				lockout(FALSE);				x = readskip();				if (x != SCMOK)					goaway("Can't skip file transfer");				goaway("Error in disk space check");				logquit(1, "Error in disk space check");			}			if (errno == 0) {				fsize = (fsize + 1023) / 1024;				x = fsp.fsp_size * MAX (fsp.fsp_minfree, 1) / 100;				fsp.fsp_free -= x;				if (fsize > MAX (fsp.fsp_free, 0)) {					if (istemp)						(void) unlink(tname);					lockout(FALSE);					x = readskip();					if (x != SCMOK)						goaway("Can't skip file transfer");					goaway("No disk space for file %s", to);					logquit (1, "No disk space for file %s",					    to);				}			}		}#endif	/* MACH */		x = readfile(tof);		(void) close(tof);		if (x != SCMOK) {			if (istemp)				(void) unlink(tname);			lockout(FALSE);			goaway("Error in receiving %s\n", to);		}	}	if (!istemp) {			/* no temp file used */		lockout(FALSE);		return (FALSE);	}	/*	 * If the file is compressed, uncompress it in place.  We open the	 * temp file for reading, unlink the file, and then open the same	 * file again for writing.  Then we pipe through gzip.  When 	 * finished the temp file contains the uncompressed version and we	 * can continue as before.	 *	 * Since sup prefers to write close to the original file the	 * benefits of atomic updates probably outweigh the cost of the	 * extra filecopy which occurs when the temp file is on a different	 * filesystem from the original.	 */	if (docompress) {		char *av[4];		int   ac = 0;		int   infd = -1;		int   outfd = -1;		av[ac++] = "gzip";		av[ac++] = "-d";		av[ac++] = NULL;		if ((infd = open(tname, O_RDONLY, 0)) == -1 ||		     unlink(tname) == -1 ||		     (outfd = open(tname, O_WRONLY|O_CREAT|O_TRUNC, 0600)) == -1 ||		     runiofd(av, infd, outfd, 2) != 0 ) {			notify("SUP: Error in uncompressing file %s (%s)\n",				to, tname);			(void) unlink(tname);			if (infd != -1)				(void) close(infd);			if (outfd != -1)				(void) close(outfd);			lockout(FALSE);			return(TRUE);		}		(void) close(infd);		(void) close(outfd);	}	/* move to destination */	if (rename(tname, to) == 0) {		(void) unlink(tname);		lockout(FALSE);		return(FALSE);	}	fromf = open(tname, O_RDONLY, 0);	if (fromf < 0) {		notify ("SUP: Error in moving temp file to %s: %s\n",			to, errmsg (-1));		(void) unlink(tname);		lockout(FALSE);		return (TRUE);	}	tof = open(to, O_WRONLY|O_CREAT|O_TRUNC, 0600);	if (tof < 0) {		(void) close(fromf);		notify("SUP: Can't create %s from temp file: %s\n",			to, errmsg(-1));		(void) unlink(tname);		lockout(FALSE);		return (TRUE);	}	x = filecopy(fromf, tof);	(void) close(fromf);	(void) close(tof);	(void) unlink(tname);	lockout(FALSE);	if (x < 0) {		notify("SUP: Error in storing data in %s\n", to);		return (TRUE);	}	return (FALSE);}