1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * Glenn Fowler
1N/A * AT&T Bell Laboratories
1N/A *
1N/A * copy from rfd to wfd (with conditional mmap hacks)
1N/A */
1N/A
1N/A#include <ast.h>
1N/A#include <ast_mmap.h>
1N/A
1N/A#if _mmap_worthy > 1
1N/A
1N/A#include <ls.h>
1N/A
1N/A#define MAPSIZE (1024*256)
1N/A
1N/A#endif
1N/A
1N/A#undef BUFSIZ
1N/A#define BUFSIZ 4096
1N/A
1N/A/*
1N/A * copy n bytes from rfd to wfd
1N/A * actual byte count returned
1N/A * if n<=0 then ``good'' size is used
1N/A */
1N/A
1N/Aoff_t
1N/Aastcopy(int rfd, int wfd, off_t n)
1N/A{
1N/A register off_t c;
1N/A#ifdef MAPSIZE
1N/A off_t pos;
1N/A off_t mapsize;
1N/A char* mapbuf;
1N/A struct stat st;
1N/A#endif
1N/A
1N/A static int bufsiz;
1N/A static char* buf;
1N/A
1N/A if (n <= 0 || n >= BUFSIZ * 2)
1N/A {
1N/A#if MAPSIZE
1N/A if (!fstat(rfd, &st) && S_ISREG(st.st_mode) && (pos = lseek(rfd, (off_t)0, 1)) != ((off_t)-1))
1N/A {
1N/A if (pos >= st.st_size) return(0);
1N/A mapsize = st.st_size - pos;
1N/A if (mapsize > MAPSIZE) mapsize = (mapsize > n && n > 0) ? n : MAPSIZE;
1N/A if (mapsize >= BUFSIZ * 2 && (mapbuf = (char*)mmap(NiL, mapsize, PROT_READ, MAP_SHARED, rfd, pos)) != ((caddr_t)-1))
1N/A {
1N/A if (write(wfd, mapbuf, mapsize) != mapsize || lseek(rfd, mapsize, 1) == ((off_t)-1)) return(-1);
1N/A munmap((caddr_t)mapbuf, mapsize);
1N/A return(mapsize);
1N/A }
1N/A }
1N/A#endif
1N/A if (n <= 0) n = BUFSIZ;
1N/A }
1N/A if (n > bufsiz)
1N/A {
1N/A if (buf) free(buf);
1N/A bufsiz = roundof(n, BUFSIZ);
1N/A if (!(buf = newof(0, char, bufsiz, 0))) return(-1);
1N/A }
1N/A if ((c = read(rfd, buf, (size_t)n)) > 0 && write(wfd, buf, (size_t)c) != c) c = -1;
1N/A return(c);
1N/A}