usr.bin/xinstall/xinstall.c
void
copy(int from_fd, const char *from_name, int to_fd,
     const char *to_name, off_t size)
{
	int nr, nw;
	int serrno;
	char *p, buf[MAXBSIZE];
	int done_copy;

	/* Rewind file descriptors. */
	if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
		err(EX_OSERR, "lseek: %s", from_name);
	if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
		err(EX_OSERR, "lseek: %s", to_name);

	/*
	 * Mmap and write if less than 8M (the limit is so we don't totally
	 * trash memory on big files.  This is really a minor hack, but it
	 * wins some CPU back.
	 */
	done_copy = 0;
	if (size <= 8 * 1048576 && trymmap(from_fd) &&
	    (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
		    from_fd, (off_t)0)) != (char *)MAP_FAILED) {
		if ((nw = write(to_fd, p, size)) != size) {
			serrno = errno;
			unlink(to_name);
			errno = nw > 0 ? EIO : serrno;
			err(EX_OSERR, "%s", to_name);
		}
		done_copy = 1;
	}
	if (!done_copy) {
		while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
			if ((nw = write(to_fd, buf, nr)) != nr) {
				serrno = errno;
				unlink(to_name);
				errno = nw > 0 ? EIO : serrno;
				err(EX_OSERR, "%s", to_name);
			}
		if (nr != 0) {
			serrno = errno;
			unlink(to_name);
			errno = serrno;
			err(EX_OSERR, "%s", from_name);
		}
	}
}
/*
 * copy --
 *	copy from one file to another
 */
copy
copy from one file to another
copy copy from one file to another
void
copy(int from_fd, const char *from_name, int to_fd,
     const char *to_name, off_t size)
{
	int nr, nw;
	int serrno;
	char *p, buf[MAXBSIZE];
	int done_copy;
	if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
		err(EX_OSERR, "lseek: %s", from_name);
	if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
		err(EX_OSERR, "lseek: %s", to_name);
	done_copy = 0;
	if (size <= 8 * 1048576 && trymmap(from_fd) &&
	    (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
		    from_fd, (off_t)0)) != (char *)MAP_FAILED) {
		if ((nw = write(to_fd, p, size)) != size) {
			serrno = errno;
			unlink(to_name);
			errno = nw > 0 ? EIO : serrno;
			err(EX_OSERR, "%s", to_name);
		}
		done_copy = 1;
	}
	if (!done_copy) {
		while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
			if ((nw = write(to_fd, buf, nr)) != nr) {
				serrno = errno;
				unlink(to_name);
				errno = nw > 0 ? EIO : serrno;
				err(EX_OSERR, "%s", to_name);
			}
		if (nr != 0) {
			serrno = errno;
			unlink(to_name);
			errno = serrno;
			err(EX_OSERR, "%s", from_name);
		}
	}
}
void copy ( int from_fd , const char * from_name , int to_fd , const char * to_name , off_t size ) { int nr , nw ; int serrno ; char * p , buf [ MAXBSIZE ] ; int done_copy ; if ( lseek ( from_fd , ( off_t ) 0 , SEEK_SET ) == ( off_t ) - 1 ) err ( EX_OSERR , " " , from_name ) ; if ( lseek ( to_fd , ( off_t ) 0 , SEEK_SET ) == ( off_t ) - 1 ) err ( EX_OSERR , " " , to_name ) ; done_copy = 0 ; if ( size <= 8 * 1048576 && trymmap ( from_fd ) && ( p = mmap ( NULL , ( size_t ) size , PROT_READ , MAP_SHARED , from_fd , ( off_t ) 0 ) ) != ( char * ) MAP_FAILED ) { if ( ( nw = write ( to_fd , p , size ) ) != size ) { serrno = errno ; unlink ( to_name ) ; errno = nw > 0 ? EIO : serrno ; err ( EX_OSERR , " " , to_name ) ; } done_copy = 1 ; } if ( ! done_copy ) { while ( ( nr = read ( from_fd , buf , sizeof ( buf ) ) ) > 0 ) if ( ( nw = write ( to_fd , buf , nr ) ) != nr ) { serrno = errno ; unlink ( to_name ) ; errno = nw > 0 ? EIO : serrno ; err ( EX_OSERR , " " , to_name ) ; } if ( nr != 0 ) { serrno = errno ; unlink ( to_name ) ; errno = serrno ; err ( EX_OSERR , " " , from_name ) ; } } }
copy
      copy from one file to another
copy copy from one file to another
/* Rewind file descriptors. */ /*
	 * Mmap and write if less than 8M (the limit is so we don't totally
	 * trash memory on big files.  This is really a minor hack, but it
	 * wins some CPU back.
	 */