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