Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/3d/copy.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1989-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* David Korn <[email protected]> *
19
* Eduardo Krell <[email protected]> *
20
* *
21
***********************************************************************/
22
#pragma prototyped
23
24
#include "3d.h"
25
26
#if _hdr_utime
27
#include <utime.h>
28
#else
29
struct utimbuf
30
{
31
time_t actime;
32
time_t modtime;
33
};
34
#endif
35
36
#define COPYBUF 8096
37
38
#if __sun__ || sun
39
40
#if _lib_mmap
41
42
#define COPYMAP (32*COPYBUF)
43
44
#endif
45
46
#endif
47
48
/*
49
* copy rfd to wfd
50
* return 0 if success, otherwise -1.
51
*/
52
53
int
54
fs3d_copy(int rfd, int wfd, struct stat* st)
55
{
56
register ssize_t n;
57
ssize_t filesize;
58
struct stat stbuf;
59
struct utimbuf ut;
60
char buf[COPYBUF];
61
62
#ifdef COPYMAP
63
off_t offset;
64
size_t munmapsize;
65
size_t mapsize;
66
char* mapbuf;
67
#endif
68
69
if (!st && FSTAT(rfd, st = &stbuf)) return -1;
70
if (!S_ISREG(st->st_mode)) return 0;
71
if ((filesize = st->st_size) <= 0)
72
filesize = 1;
73
#ifdef COPYMAP
74
mapsize = (filesize < COPYMAP) ? filesize : COPYMAP;
75
if ((filesize > COPYBUF) && ((mapbuf = mmap((caddr_t)0, mapsize, PROT_READ, MAP_SHARED, rfd, 0)) != ((caddr_t)-1)))
76
{
77
offset = 0;
78
munmapsize = mapsize;
79
for (;;)
80
{
81
if (write(wfd, mapbuf, mapsize) != mapsize)
82
{
83
munmap(mapbuf, munmapsize);
84
return -1;
85
}
86
filesize -= mapsize;
87
if (filesize <= 0) break;
88
offset += mapsize;
89
if (filesize < mapsize) mapsize = filesize;
90
if (mmap(mapbuf, mapsize, PROT_READ, MAP_SHARED|MAP_FIXED, rfd, offset) == (caddr_t)-1)
91
{
92
munmap(mapbuf, munmapsize);
93
return -1;
94
}
95
}
96
munmap(mapbuf, munmapsize);
97
}
98
#endif
99
if (filesize > 0)
100
{
101
while ((n = read(rfd, buf, COPYBUF)) > 0)
102
if (write(wfd, buf, n) != n)
103
return -1;
104
if (n < 0 || lseek(wfd, (off_t)0, 0)) return -1;
105
}
106
ut.actime = st->st_atime;
107
ut.modtime = st->st_mtime;
108
UTIME(state.path.name, &ut);
109
return 0;
110
}
111
112