Print this page
1150 libcmdutils has superfluous #define
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
Reviewed by: Andy Stormont <astormont@racktopsystems.com>
Reviewed by: Marcel Telka <marcel@telka.sk>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libcmdutils/common/writefile.c
+++ new/usr/src/lib/libcmdutils/common/writefile.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 28 /* All Rights Reserved */
29 29
↓ open down ↓ |
29 lines elided |
↑ open up ↑ |
30 30 /*
31 31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 32 * The Regents of the University of California
33 33 * All Rights Reserved
34 34 *
35 35 * University Acknowledgment- Portions of this document are derived from
36 36 * software developed by the University of California, Berkeley, and its
37 37 * contributors.
38 38 */
39 39
40 -#pragma ident "%Z%%M% %I% %E% SMI"
41 -
42 40 #include "libcmdutils.h"
43 41
44 42
45 43 int
46 44 writefile(int fi, int fo, char *infile, char *outfile, char *asfile,
47 45 char *atfile, struct stat *s1p, struct stat *s2p)
48 46 {
49 47 int mapsize, munmapsize;
50 48 caddr_t cp;
51 49 off_t filesize = s1p->st_size;
52 50 off_t offset;
53 51 int nbytes;
54 52 int remains;
55 53 int n;
56 54 size_t src_size;
57 55 size_t targ_size;
58 56 char *srcbuf;
59 57 char *targbuf;
60 58
61 59 if (asfile != NULL) {
62 60 src_size = strlen(infile) + strlen(asfile) +
63 61 strlen(dgettext(TEXT_DOMAIN, " attribute ")) + 1;
64 62 } else {
65 63 src_size = strlen(infile) + 1;
66 64 }
67 65 srcbuf = malloc(src_size);
68 66 if (srcbuf == NULL) {
69 67 (void) fprintf(stderr,
70 68 dgettext(TEXT_DOMAIN, "could not allocate memory"
71 69 " for path buffer: "));
72 70 return (1);
73 71 }
74 72 if (asfile != NULL) {
75 73 (void) snprintf(srcbuf, src_size, "%s%s%s",
76 74 infile, dgettext(TEXT_DOMAIN, " attribute "), asfile);
77 75 } else {
78 76 (void) snprintf(srcbuf, src_size, "%s", infile);
79 77 }
80 78
81 79 if (atfile != NULL) {
82 80 targ_size = strlen(outfile) + strlen(atfile) +
83 81 strlen(dgettext(TEXT_DOMAIN, " attribute ")) + 1;
84 82 } else {
85 83 targ_size = strlen(outfile) + 1;
86 84 }
87 85 targbuf = malloc(targ_size);
88 86 if (targbuf == NULL) {
89 87 (void) fprintf(stderr,
90 88 dgettext(TEXT_DOMAIN, "could not allocate memory"
↓ open down ↓ |
39 lines elided |
↑ open up ↑ |
91 89 " for path buffer: "));
92 90 return (1);
93 91 }
94 92 if (atfile != NULL) {
95 93 (void) snprintf(targbuf, targ_size, "%s%s%s",
96 94 outfile, dgettext(TEXT_DOMAIN, " attribute "), atfile);
97 95 } else {
98 96 (void) snprintf(targbuf, targ_size, "%s", outfile);
99 97 }
100 98
101 - if (ISREG(*s1p) && s1p->st_size > SMALLFILESIZE) {
99 + if (S_ISREG(s1p->st_mode) && s1p->st_size > SMALLFILESIZE) {
102 100 /*
103 101 * Determine size of initial mapping. This will determine the
104 102 * size of the address space chunk we work with. This initial
105 103 * mapping size will be used to perform munmap() in the future.
106 104 */
107 105 mapsize = MAXMAPSIZE;
108 106 if (s1p->st_size < mapsize) mapsize = s1p->st_size;
109 107 munmapsize = mapsize;
110 108
111 109 /*
112 110 * Mmap time!
113 111 */
114 112 if ((cp = mmap((caddr_t)NULL, mapsize, PROT_READ,
115 113 MAP_SHARED, fi, (off_t)0)) == MAP_FAILED)
116 114 mapsize = 0; /* can't mmap today */
117 115 } else
118 116 mapsize = 0;
119 117
120 118 if (mapsize != 0) {
121 119 offset = 0;
122 120
123 121 for (;;) {
124 122 nbytes = write(fo, cp, mapsize);
125 123 /*
126 124 * if we write less than the mmaped size it's due to a
127 125 * media error on the input file or out of space on
128 126 * the output file. So, try again, and look for errno.
129 127 */
130 128 if ((nbytes >= 0) && (nbytes != (int)mapsize)) {
131 129 remains = mapsize - nbytes;
132 130 while (remains > 0) {
↓ open down ↓ |
21 lines elided |
↑ open up ↑ |
133 131 nbytes = write(fo,
134 132 cp + mapsize - remains, remains);
135 133 if (nbytes < 0) {
136 134 if (errno == ENOSPC)
137 135 perror(targbuf);
138 136 else
139 137 perror(srcbuf);
140 138 (void) close(fi);
141 139 (void) close(fo);
142 140 (void) munmap(cp, munmapsize);
143 - if (ISREG(*s2p))
141 + if (S_ISREG(s2p->st_mode))
144 142 (void) unlink(targbuf);
145 143 return (1);
146 144 }
147 145 remains -= nbytes;
148 146 if (remains == 0)
149 147 nbytes = mapsize;
150 148 }
151 149 }
152 150 /*
153 151 * although the write manual page doesn't specify this
154 152 * as a possible errno, it is set when the nfs read
155 153 * via the mmap'ed file is accessed, so report the
156 154 * problem as a source access problem, not a target file
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
157 155 * problem
158 156 */
159 157 if (nbytes < 0) {
160 158 if (errno == EACCES)
161 159 perror(srcbuf);
162 160 else
163 161 perror(targbuf);
164 162 (void) close(fi);
165 163 (void) close(fo);
166 164 (void) munmap(cp, munmapsize);
167 - if (ISREG(*s2p))
165 + if (S_ISREG(s2p->st_mode))
168 166 (void) unlink(targbuf);
169 167 if (srcbuf != NULL)
170 168 free(srcbuf);
171 169 if (targbuf != NULL)
172 170 free(targbuf);
173 171 return (1);
174 172 }
175 173 filesize -= nbytes;
176 174 if (filesize == 0)
177 175 break;
178 176 offset += nbytes;
179 177 if (filesize < mapsize)
180 178 mapsize = filesize;
181 179 if (mmap(cp, mapsize, PROT_READ, MAP_SHARED |
182 180 MAP_FIXED, fi, offset) == MAP_FAILED) {
183 181 perror(srcbuf);
184 182 (void) close(fi);
185 183 (void) close(fo);
186 184 (void) munmap(cp, munmapsize);
187 - if (ISREG(*s2p))
185 + if (S_ISREG(s2p->st_mode))
188 186 (void) unlink(targbuf);
189 187 if (srcbuf != NULL)
190 188 free(srcbuf);
191 189 if (targbuf != NULL)
192 190 free(targbuf);
193 191 return (1);
194 192 }
195 193 }
196 194 (void) munmap(cp, munmapsize);
197 195 } else {
198 196 char buf[SMALLFILESIZE];
199 197 for (;;) {
200 198 n = read(fi, buf, sizeof (buf));
201 199 if (n == 0) {
202 200 return (0);
203 201 } else if (n < 0) {
204 202 (void) close(fi);
205 203 (void) close(fo);
206 - if (ISREG(*s2p))
204 + if (S_ISREG(s2p->st_mode))
207 205 (void) unlink(targbuf);
208 206 if (srcbuf != NULL)
209 207 free(srcbuf);
210 208 if (targbuf != NULL)
211 209 free(targbuf);
212 210 return (1);
213 211 } else if (write(fo, buf, n) != n) {
214 212 (void) close(fi);
215 213 (void) close(fo);
216 - if (ISREG(*s2p))
214 + if (S_ISREG(s2p->st_mode))
217 215 (void) unlink(targbuf);
218 216 if (srcbuf != NULL)
219 217 free(srcbuf);
220 218 if (targbuf != NULL)
221 219 free(targbuf);
222 220 return (1);
223 221 }
224 222 }
225 223 }
226 224 if (srcbuf != NULL)
227 225 free(srcbuf);
228 226 if (targbuf != NULL)
229 227 free(targbuf);
230 228 return (0);
231 229 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX