1 | /* $Id: shfile.c 2281 2009-02-24 04:08:14Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * File management.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2007-2009 knut st. osmundsen <[email protected]>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This file is part of kBuild.
|
---|
10 | *
|
---|
11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kBuild is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kBuild; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 | #include "shfile.h"
|
---|
28 | #include "shinstance.h" /* TRACE2 */
|
---|
29 | #include <stdlib.h>
|
---|
30 | #include <stdio.h>
|
---|
31 |
|
---|
32 | #if K_OS == K_OS_WINDOWS
|
---|
33 | # include <limits.h>
|
---|
34 | # ifndef PIPE_BUF
|
---|
35 | # define PIPE_BUF 512
|
---|
36 | # endif
|
---|
37 | #else
|
---|
38 | # include <unistd.h>
|
---|
39 | # include <fcntl.h>
|
---|
40 | # include <dirent.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #ifdef DEBUG
|
---|
44 | extern FILE *tracefile;
|
---|
45 | #endif
|
---|
46 |
|
---|
47 |
|
---|
48 | int shfile_open(shfdtab *pfdtab, const char *name, unsigned flags, mode_t mode)
|
---|
49 | {
|
---|
50 | int fd;
|
---|
51 |
|
---|
52 | #ifdef SH_PURE_STUB_MODE
|
---|
53 | fd = -1;
|
---|
54 | #elif defined(SH_STUB_MODE)
|
---|
55 | fd = open(name, flags, mode);
|
---|
56 | #else
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | #ifdef DEBUG
|
---|
60 | if (tracefile)
|
---|
61 | TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
|
---|
62 | #endif
|
---|
63 | return fd;
|
---|
64 | }
|
---|
65 |
|
---|
66 | int shfile_pipe(shfdtab *pfdtab, int fds[2])
|
---|
67 | {
|
---|
68 | #ifdef SH_PURE_STUB_MODE
|
---|
69 | return -1;
|
---|
70 | #elif defined(SH_STUB_MODE)
|
---|
71 | # ifdef _MSC_VER
|
---|
72 | return _pipe(fds, PIPE_BUF, O_BINARY);
|
---|
73 | # else
|
---|
74 | return pipe(fds);
|
---|
75 | # endif
|
---|
76 | #else
|
---|
77 | #endif
|
---|
78 | }
|
---|
79 |
|
---|
80 | int shfile_dup(shfdtab *pfdtab, int fd)
|
---|
81 | {
|
---|
82 | int rc;
|
---|
83 | #ifdef SH_PURE_STUB_MODE
|
---|
84 | rc = -1;
|
---|
85 | #elif defined(SH_STUB_MODE)
|
---|
86 | rc = dup(fd);
|
---|
87 | #else
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | TRACE2((NULL, "shfile_dup(%d) -> %d [%d]\n", fd, rc, errno));
|
---|
91 | return rc;
|
---|
92 | }
|
---|
93 |
|
---|
94 | int shfile_close(shfdtab *pfdtab, unsigned fd)
|
---|
95 | {
|
---|
96 | int rc;
|
---|
97 |
|
---|
98 | #ifdef SH_PURE_STUB_MODE
|
---|
99 | rc = -1;
|
---|
100 | #elif defined(SH_STUB_MODE)
|
---|
101 | rc = close(fd);
|
---|
102 | #else
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | TRACE2((NULL, "shfile_close(%d) -> %d [%d]\n", fd, rc, errno));
|
---|
106 | return rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 | long shfile_read(shfdtab *pfdtab, int fd, void *buf, size_t len)
|
---|
110 | {
|
---|
111 | #ifdef SH_PURE_STUB_MODE
|
---|
112 | return -1;
|
---|
113 | #elif defined(SH_STUB_MODE)
|
---|
114 | # ifdef _MSC_VER
|
---|
115 | return read(fd, buf, (unsigned)len);
|
---|
116 | # else
|
---|
117 | return read(fd, buf, len);
|
---|
118 | # endif
|
---|
119 | #else
|
---|
120 | #endif
|
---|
121 | }
|
---|
122 |
|
---|
123 | long shfile_write(shfdtab *pfdtab, int fd, const void *buf, size_t len)
|
---|
124 | {
|
---|
125 | #ifdef SH_PURE_STUB_MODE
|
---|
126 | return -1;
|
---|
127 | #elif defined(SH_STUB_MODE)
|
---|
128 | # ifdef _MSC_VER
|
---|
129 | return write(fd, buf, (unsigned)len);
|
---|
130 | # else
|
---|
131 | return write(fd, buf, len);
|
---|
132 | # endif
|
---|
133 | #else
|
---|
134 | #endif
|
---|
135 | }
|
---|
136 |
|
---|
137 | long shfile_lseek(shfdtab *pfdtab, int fd, long off, int whench)
|
---|
138 | {
|
---|
139 | #ifdef SH_PURE_STUB_MODE
|
---|
140 | return -1;
|
---|
141 | #elif defined(SH_STUB_MODE)
|
---|
142 | return lseek(fd, off, whench);
|
---|
143 | #else
|
---|
144 | #endif
|
---|
145 | }
|
---|
146 |
|
---|
147 | int shfile_fcntl(shfdtab *pfdtab, int fd, int cmd, int arg)
|
---|
148 | {
|
---|
149 | #ifdef SH_PURE_STUB_MODE
|
---|
150 | return -1;
|
---|
151 | #elif defined(SH_STUB_MODE)
|
---|
152 | # ifdef _MSC_VER
|
---|
153 | return -1;
|
---|
154 | # else
|
---|
155 | return fcntl(fd, cmd, arg);
|
---|
156 | # endif
|
---|
157 | #else
|
---|
158 | #endif
|
---|
159 | }
|
---|
160 |
|
---|
161 | int shfile_stat(shfdtab *pfdtab, const char *path, struct stat *pst)
|
---|
162 | {
|
---|
163 | #ifdef SH_PURE_STUB_MODE
|
---|
164 | return -1;
|
---|
165 | #elif defined(SH_STUB_MODE)
|
---|
166 | return stat(path, pst);
|
---|
167 | #else
|
---|
168 | #endif
|
---|
169 | }
|
---|
170 |
|
---|
171 | int shfile_lstat(shfdtab *pfdtab, const char *link, struct stat *pst)
|
---|
172 | {
|
---|
173 | #ifdef SH_PURE_STUB_MODE
|
---|
174 | return -1;
|
---|
175 | #elif defined(SH_STUB_MODE)
|
---|
176 | # ifdef _MSC_VER
|
---|
177 | return stat(link, pst);
|
---|
178 | # else
|
---|
179 | return lstat(link, pst);
|
---|
180 | # endif
|
---|
181 | #else
|
---|
182 | #endif
|
---|
183 | }
|
---|
184 |
|
---|
185 | int shfile_chdir(shfdtab *pfdtab, const char *path)
|
---|
186 | {
|
---|
187 | #ifdef SH_PURE_STUB_MODE
|
---|
188 | return -1;
|
---|
189 | #elif defined(SH_STUB_MODE)
|
---|
190 | # ifdef _MSC_VER //???
|
---|
191 | return chdir(path);
|
---|
192 | # else
|
---|
193 | return chdir(path);
|
---|
194 | # endif
|
---|
195 | #else
|
---|
196 | #endif
|
---|
197 | }
|
---|
198 |
|
---|
199 | char *shfile_getcwd(shfdtab *pfdtab, char *buf, int len)
|
---|
200 | {
|
---|
201 | #ifdef SH_PURE_STUB_MODE
|
---|
202 | return NULL;
|
---|
203 | #elif defined(SH_STUB_MODE)
|
---|
204 | return getcwd(buf, len);
|
---|
205 | #else
|
---|
206 | #endif
|
---|
207 | }
|
---|
208 |
|
---|
209 | int shfile_access(shfdtab *pfdtab, const char *path, int type)
|
---|
210 | {
|
---|
211 | #ifdef SH_PURE_STUB_MODE
|
---|
212 | return -1;
|
---|
213 | #elif defined(SH_STUB_MODE)
|
---|
214 | # ifdef _MSC_VER
|
---|
215 | type &= ~X_OK;
|
---|
216 | return access(path, type);
|
---|
217 | # else
|
---|
218 | return access(path, type);
|
---|
219 | # endif
|
---|
220 | #else
|
---|
221 | #endif
|
---|
222 | }
|
---|
223 |
|
---|
224 | int shfile_isatty(shfdtab *pfdtab, int fd)
|
---|
225 | {
|
---|
226 | int rc;
|
---|
227 |
|
---|
228 | #ifdef SH_PURE_STUB_MODE
|
---|
229 | rc = 0;
|
---|
230 | #elif defined(SH_STUB_MODE)
|
---|
231 | rc = isatty(fd);
|
---|
232 | #else
|
---|
233 | #endif
|
---|
234 |
|
---|
235 | TRACE2((NULL, "isatty(%d) -> %d [%d]\n", fd, rc, errno));
|
---|
236 | return rc;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | int shfile_cloexec(shfdtab *pfdtab, int fd, int closeit)
|
---|
241 | {
|
---|
242 | int rc;
|
---|
243 |
|
---|
244 | #ifdef SH_PURE_STUB_MODE
|
---|
245 | rc = -1;
|
---|
246 | #elif defined(SH_STUB_MODE)
|
---|
247 | # ifdef _MSC_VER
|
---|
248 | rc = -1;
|
---|
249 | # else
|
---|
250 | rc = fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0)
|
---|
251 | | (closeit ? FD_CLOEXEC : 0));
|
---|
252 | # endif
|
---|
253 | #else
|
---|
254 | #endif
|
---|
255 |
|
---|
256 | TRACE2((NULL, "shfile_cloexec(%d, %d) -> %d [%d]\n", fd, closeit, rc, errno));
|
---|
257 | return rc;
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | int shfile_ioctl(shfdtab *pfdtab, int fd, unsigned long request, void *buf)
|
---|
262 | {
|
---|
263 | int rc;
|
---|
264 |
|
---|
265 | #ifdef SH_PURE_STUB_MODE
|
---|
266 | rc = -1;
|
---|
267 | #elif defined(SH_STUB_MODE)
|
---|
268 | # ifdef _MSC_VER
|
---|
269 | rc = -1;
|
---|
270 | # else
|
---|
271 | rc = ioctl(fd, request, buf);
|
---|
272 | # endif
|
---|
273 | #else
|
---|
274 | #endif
|
---|
275 |
|
---|
276 | TRACE2((NULL, "ioctl(%d, %#x, %p) -> %d\n", fd, request, buf, rc));
|
---|
277 | return rc;
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | mode_t shfile_get_umask(shfdtab *pfdtab)
|
---|
282 | {
|
---|
283 | #ifdef SH_PURE_STUB_MODE
|
---|
284 | return 022;
|
---|
285 | #elif defined(SH_STUB_MODE)
|
---|
286 | return 022;
|
---|
287 | #else
|
---|
288 | #endif
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 | shdir *shfile_opendir(shfdtab *pfdtab, const char *dir)
|
---|
293 | {
|
---|
294 | #ifdef SH_PURE_STUB_MODE
|
---|
295 | return NULL;
|
---|
296 | #elif defined(SH_STUB_MODE)
|
---|
297 | # ifdef _MSC_VER
|
---|
298 | return NULL;
|
---|
299 | # else
|
---|
300 | return (shdir *)opendir(dir);
|
---|
301 | # endif
|
---|
302 | #else
|
---|
303 | #endif
|
---|
304 | }
|
---|
305 |
|
---|
306 | shdirent *shfile_readdir(struct shdir *pdir)
|
---|
307 | {
|
---|
308 | #ifdef SH_PURE_STUB_MODE
|
---|
309 | return NULL;
|
---|
310 | #elif defined(SH_STUB_MODE)
|
---|
311 | # ifdef _MSC_VER
|
---|
312 | return NULL;
|
---|
313 | # else
|
---|
314 | struct dirent *pde = readdir((DIR *)pdir);
|
---|
315 | return pde ? (shdirent *)&pde->d_name[0] : NULL;
|
---|
316 | # endif
|
---|
317 | #else
|
---|
318 | #endif
|
---|
319 | }
|
---|
320 |
|
---|
321 | void shfile_closedir(struct shdir *pdir)
|
---|
322 | {
|
---|
323 | #ifdef SH_PURE_STUB_MODE
|
---|
324 | return NULL;
|
---|
325 | #elif defined(SH_STUB_MODE)
|
---|
326 | # ifndef _MSC_VER
|
---|
327 | closedir((DIR *)pdir);
|
---|
328 | # endif
|
---|
329 | #else
|
---|
330 | #endif
|
---|
331 | }
|
---|