8354 sync regcomp(3C) with upstream (fix make catalog)
[unleashed/tickless.git] / usr / src / lib / libast / common / port / astcopy.c
blobb946b7fd26080dfb4bc5e70061a13b04bf77ee63
1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
20 * *
21 ***********************************************************************/
22 #pragma prototyped
24 * Glenn Fowler
25 * AT&T Bell Laboratories
27 * copy from rfd to wfd (with conditional mmap hacks)
30 #include <ast.h>
31 #include <ast_mmap.h>
33 #if _mmap_worthy > 1
35 #include <ls.h>
37 #define MAPSIZE (1024*256)
39 #endif
41 #undef BUFSIZ
42 #define BUFSIZ 4096
45 * copy n bytes from rfd to wfd
46 * actual byte count returned
47 * if n<=0 then ``good'' size is used
50 off_t
51 astcopy(int rfd, int wfd, off_t n)
53 register off_t c;
54 #ifdef MAPSIZE
55 off_t pos;
56 off_t mapsize;
57 char* mapbuf;
58 struct stat st;
59 #endif
61 static int bufsiz;
62 static char* buf;
64 if (n <= 0 || n >= BUFSIZ * 2)
66 #if MAPSIZE
67 if (!fstat(rfd, &st) && S_ISREG(st.st_mode) && (pos = lseek(rfd, (off_t)0, 1)) != ((off_t)-1))
69 if (pos >= st.st_size) return(0);
70 mapsize = st.st_size - pos;
71 if (mapsize > MAPSIZE) mapsize = (mapsize > n && n > 0) ? n : MAPSIZE;
72 if (mapsize >= BUFSIZ * 2 && (mapbuf = (char*)mmap(NiL, mapsize, PROT_READ, MAP_SHARED, rfd, pos)) != ((caddr_t)-1))
74 if (write(wfd, mapbuf, mapsize) != mapsize || lseek(rfd, mapsize, 1) == ((off_t)-1)) return(-1);
75 munmap((caddr_t)mapbuf, mapsize);
76 return(mapsize);
79 #endif
80 if (n <= 0) n = BUFSIZ;
82 if (n > bufsiz)
84 if (buf) free(buf);
85 bufsiz = roundof(n, BUFSIZ);
86 if (!(buf = newof(0, char, bufsiz, 0))) return(-1);
88 if ((c = read(rfd, buf, (size_t)n)) > 0 && write(wfd, buf, (size_t)c) != c) c = -1;
89 return(c);