1 | /* $Id: shfile.c 2288 2009-02-25 06:21:10Z 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 |
|
---|
71 | #elif defined(SH_STUB_MODE)
|
---|
72 | # ifdef _MSC_VER
|
---|
73 | return _pipe(fds, PIPE_BUF, O_BINARY);
|
---|
74 | # else
|
---|
75 | return pipe(fds);
|
---|
76 | # endif
|
---|
77 |
|
---|
78 | #else
|
---|
79 | #endif
|
---|
80 | }
|
---|
81 |
|
---|
82 | int shfile_dup(shfdtab *pfdtab, int fd)
|
---|
83 | {
|
---|
84 | int rc;
|
---|
85 | #ifdef SH_PURE_STUB_MODE
|
---|
86 | rc = -1;
|
---|
87 |
|
---|
88 | #elif defined(SH_STUB_MODE)
|
---|
89 | rc = dup(fd);
|
---|
90 |
|
---|
91 | #else
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | TRACE2((NULL, "shfile_dup(%d) -> %d [%d]\n", fd, rc, errno));
|
---|
95 | return rc;
|
---|
96 | }
|
---|
97 |
|
---|
98 | int shfile_close(shfdtab *pfdtab, unsigned fd)
|
---|
99 | {
|
---|
100 | int rc;
|
---|
101 |
|
---|
102 | #ifdef SH_PURE_STUB_MODE
|
---|
103 | rc = -1;
|
---|
104 |
|
---|
105 | #elif defined(SH_STUB_MODE)
|
---|
106 | rc = close(fd);
|
---|
107 |
|
---|
108 | #else
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | TRACE2((NULL, "shfile_close(%d) -> %d [%d]\n", fd, rc, errno));
|
---|
112 | return rc;
|
---|
113 | }
|
---|
114 |
|
---|
115 | long shfile_read(shfdtab *pfdtab, int fd, void *buf, size_t len)
|
---|
116 | {
|
---|
117 | #ifdef SH_PURE_STUB_MODE
|
---|
118 | return -1;
|
---|
119 |
|
---|
120 | #elif defined(SH_STUB_MODE)
|
---|
121 | # ifdef _MSC_VER
|
---|
122 | return read(fd, buf, (unsigned)len);
|
---|
123 | # else
|
---|
124 | return read(fd, buf, len);
|
---|
125 | # endif
|
---|
126 |
|
---|
127 | #else
|
---|
128 | #endif
|
---|
129 | }
|
---|
130 |
|
---|
131 | long shfile_write(shfdtab *pfdtab, int fd, const void *buf, size_t len)
|
---|
132 | {
|
---|
133 | #ifdef SH_PURE_STUB_MODE
|
---|
134 | return -1;
|
---|
135 |
|
---|
136 | #elif defined(SH_STUB_MODE)
|
---|
137 | # ifdef _MSC_VER
|
---|
138 | return write(fd, buf, (unsigned)len);
|
---|
139 | # else
|
---|
140 | return write(fd, buf, len);
|
---|
141 | # endif
|
---|
142 |
|
---|
143 | #else
|
---|
144 | #endif
|
---|
145 | }
|
---|
146 |
|
---|
147 | long shfile_lseek(shfdtab *pfdtab, int fd, long off, int whench)
|
---|
148 | {
|
---|
149 | #ifdef SH_PURE_STUB_MODE
|
---|
150 | return -1;
|
---|
151 |
|
---|
152 | #elif defined(SH_STUB_MODE)
|
---|
153 | return lseek(fd, off, whench);
|
---|
154 |
|
---|
155 | #else
|
---|
156 | #endif
|
---|
157 | }
|
---|
158 |
|
---|
159 | int shfile_fcntl(shfdtab *pfdtab, int fd, int cmd, int arg)
|
---|
160 | {
|
---|
161 | int rc;
|
---|
162 | #ifdef SH_PURE_STUB_MODE
|
---|
163 | rc = -1;
|
---|
164 |
|
---|
165 | #elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
|
---|
166 | # ifdef _MSC_VER
|
---|
167 | switch (cmd)
|
---|
168 | {
|
---|
169 | /* Just enough F_GETFL/F_SETFL to get along with. */
|
---|
170 | case F_GETFL:
|
---|
171 | errno = 0;
|
---|
172 | rc = _isatty(fd);
|
---|
173 | if (errno == EBADF)
|
---|
174 | rc = -1;
|
---|
175 | break;
|
---|
176 |
|
---|
177 | case F_SETFL:
|
---|
178 | errno = 0;
|
---|
179 | rc = _isatty(fd);
|
---|
180 | if (errno != EBADF)
|
---|
181 | {
|
---|
182 | if (!arg)
|
---|
183 | rc = 0;
|
---|
184 | else
|
---|
185 | {
|
---|
186 | errno = EINVAL;
|
---|
187 | rc = -1;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | else
|
---|
191 | rc = -1;
|
---|
192 | break;
|
---|
193 |
|
---|
194 | case F_DUPFD:
|
---|
195 | {
|
---|
196 | # if 1
|
---|
197 | /* the brute force approach. */
|
---|
198 | int i = 0;
|
---|
199 | int fds[256];
|
---|
200 | for (i = 0; i < 256; i++)
|
---|
201 | {
|
---|
202 | fds[i] = -1;
|
---|
203 | rc = _dup(fd);
|
---|
204 | if (rc >= arg)
|
---|
205 | break;
|
---|
206 | fds[i] = rc;
|
---|
207 | }
|
---|
208 | while (i-- > 0)
|
---|
209 | close(fds[i]);
|
---|
210 | if (rc < arg)
|
---|
211 | {
|
---|
212 | errno = EMFILE;
|
---|
213 | rc = -1;
|
---|
214 | }
|
---|
215 | # else
|
---|
216 | /* A much quick approach which is spoiled by crash validation in the CRT. */
|
---|
217 | int fdnew = arg;
|
---|
218 | rc = -2;
|
---|
219 | for (fdnew = arg; fdnew < 1024; fdnew++)
|
---|
220 | {
|
---|
221 | /* is the file open? */
|
---|
222 | errno = 0;
|
---|
223 | _isatty(fdnew);
|
---|
224 | if (errno == EBADF)
|
---|
225 | {
|
---|
226 | rc = _dup2(fd, fdnew);
|
---|
227 | break;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | if (rc == -2)
|
---|
231 | {
|
---|
232 | errno = EMFILE;
|
---|
233 | rc = -1;
|
---|
234 | }
|
---|
235 | # endif
|
---|
236 | break;
|
---|
237 | }
|
---|
238 | }
|
---|
239 | # else
|
---|
240 | rc = fcntl(fd, cmd, arg);
|
---|
241 | # endif
|
---|
242 |
|
---|
243 | #else
|
---|
244 | #endif
|
---|
245 |
|
---|
246 | #ifdef DEBUG
|
---|
247 | if (tracefile)
|
---|
248 | switch (cmd)
|
---|
249 | {
|
---|
250 | case F_GETFL:
|
---|
251 | TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno));
|
---|
252 | break;
|
---|
253 | case F_SETFL:
|
---|
254 | TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno));
|
---|
255 | break;
|
---|
256 | case F_DUPFD:
|
---|
257 | TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno));
|
---|
258 | break;
|
---|
259 | default:
|
---|
260 | TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno));
|
---|
261 | break;
|
---|
262 | }
|
---|
263 | #endif
|
---|
264 | return rc;
|
---|
265 | }
|
---|
266 |
|
---|
267 | int shfile_stat(shfdtab *pfdtab, const char *path, struct stat *pst)
|
---|
268 | {
|
---|
269 | #ifdef SH_PURE_STUB_MODE
|
---|
270 | return -1;
|
---|
271 |
|
---|
272 | #elif defined(SH_STUB_MODE)
|
---|
273 | return stat(path, pst);
|
---|
274 |
|
---|
275 | #else
|
---|
276 | #endif
|
---|
277 | }
|
---|
278 |
|
---|
279 | int shfile_lstat(shfdtab *pfdtab, const char *link, struct stat *pst)
|
---|
280 | {
|
---|
281 | #ifdef SH_PURE_STUB_MODE
|
---|
282 | return -1;
|
---|
283 |
|
---|
284 | #elif defined(SH_STUB_MODE)
|
---|
285 | # ifdef _MSC_VER
|
---|
286 | return stat(link, pst);
|
---|
287 | # else
|
---|
288 | return lstat(link, pst);
|
---|
289 | # endif
|
---|
290 |
|
---|
291 | #else
|
---|
292 | #endif
|
---|
293 | }
|
---|
294 |
|
---|
295 | int shfile_chdir(shfdtab *pfdtab, const char *path)
|
---|
296 | {
|
---|
297 | #ifdef SH_PURE_STUB_MODE
|
---|
298 | return -1;
|
---|
299 |
|
---|
300 | #elif defined(SH_STUB_MODE)
|
---|
301 | # ifdef _MSC_VER //???
|
---|
302 | return chdir(path);
|
---|
303 | # else
|
---|
304 | return chdir(path);
|
---|
305 | # endif
|
---|
306 |
|
---|
307 | #else
|
---|
308 | #endif
|
---|
309 | }
|
---|
310 |
|
---|
311 | char *shfile_getcwd(shfdtab *pfdtab, char *buf, int len)
|
---|
312 | {
|
---|
313 | #ifdef SH_PURE_STUB_MODE
|
---|
314 | return NULL;
|
---|
315 |
|
---|
316 | #elif defined(SH_STUB_MODE)
|
---|
317 | return getcwd(buf, len);
|
---|
318 |
|
---|
319 | #else
|
---|
320 | #endif
|
---|
321 | }
|
---|
322 |
|
---|
323 | int shfile_access(shfdtab *pfdtab, const char *path, int type)
|
---|
324 | {
|
---|
325 | #ifdef SH_PURE_STUB_MODE
|
---|
326 | return -1;
|
---|
327 |
|
---|
328 | #elif defined(SH_STUB_MODE)
|
---|
329 | # ifdef _MSC_VER
|
---|
330 | type &= ~X_OK;
|
---|
331 | return access(path, type);
|
---|
332 | # else
|
---|
333 | return access(path, type);
|
---|
334 | # endif
|
---|
335 |
|
---|
336 | #else
|
---|
337 | #endif
|
---|
338 | }
|
---|
339 |
|
---|
340 | int shfile_isatty(shfdtab *pfdtab, int fd)
|
---|
341 | {
|
---|
342 | int rc;
|
---|
343 |
|
---|
344 | #ifdef SH_PURE_STUB_MODE
|
---|
345 | rc = 0;
|
---|
346 | #elif defined(SH_STUB_MODE)
|
---|
347 | rc = isatty(fd);
|
---|
348 | #else
|
---|
349 | #endif
|
---|
350 |
|
---|
351 | TRACE2((NULL, "isatty(%d) -> %d [%d]\n", fd, rc, errno));
|
---|
352 | return rc;
|
---|
353 | }
|
---|
354 |
|
---|
355 |
|
---|
356 | int shfile_cloexec(shfdtab *pfdtab, int fd, int closeit)
|
---|
357 | {
|
---|
358 | int rc;
|
---|
359 |
|
---|
360 | #ifdef SH_PURE_STUB_MODE
|
---|
361 | rc = -1;
|
---|
362 |
|
---|
363 | #elif defined(SH_STUB_MODE)
|
---|
364 | # ifdef _MSC_VER
|
---|
365 | errno = ENOSYS;
|
---|
366 | rc = -1;
|
---|
367 | # else
|
---|
368 | rc = fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0)
|
---|
369 | | (closeit ? FD_CLOEXEC : 0));
|
---|
370 | # endif
|
---|
371 |
|
---|
372 | #else
|
---|
373 | #endif
|
---|
374 |
|
---|
375 | TRACE2((NULL, "shfile_cloexec(%d, %d) -> %d [%d]\n", fd, closeit, rc, errno));
|
---|
376 | return rc;
|
---|
377 | }
|
---|
378 |
|
---|
379 |
|
---|
380 | int shfile_ioctl(shfdtab *pfdtab, int fd, unsigned long request, void *buf)
|
---|
381 | {
|
---|
382 | int rc;
|
---|
383 |
|
---|
384 | #ifdef SH_PURE_STUB_MODE
|
---|
385 | rc = -1;
|
---|
386 |
|
---|
387 | #elif defined(SH_STUB_MODE)
|
---|
388 | # ifdef _MSC_VER
|
---|
389 | errno = ENOSYS;
|
---|
390 | rc = -1;
|
---|
391 | # else
|
---|
392 | rc = ioctl(fd, request, buf);
|
---|
393 | # endif
|
---|
394 |
|
---|
395 | #else
|
---|
396 | #endif
|
---|
397 |
|
---|
398 | TRACE2((NULL, "ioctl(%d, %#x, %p) -> %d\n", fd, request, buf, rc));
|
---|
399 | return rc;
|
---|
400 | }
|
---|
401 |
|
---|
402 |
|
---|
403 | mode_t shfile_get_umask(shfdtab *pfdtab)
|
---|
404 | {
|
---|
405 | #ifdef SH_PURE_STUB_MODE
|
---|
406 | return 022;
|
---|
407 |
|
---|
408 | #elif defined(SH_STUB_MODE)
|
---|
409 | return 022;
|
---|
410 |
|
---|
411 | #else
|
---|
412 | #endif
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | shdir *shfile_opendir(shfdtab *pfdtab, const char *dir)
|
---|
417 | {
|
---|
418 | #ifdef SH_PURE_STUB_MODE
|
---|
419 | return NULL;
|
---|
420 |
|
---|
421 | #elif defined(SH_STUB_MODE)
|
---|
422 | # ifdef _MSC_VER
|
---|
423 | errno = ENOSYS;
|
---|
424 | return NULL;
|
---|
425 | # else
|
---|
426 | return (shdir *)opendir(dir);
|
---|
427 | # endif
|
---|
428 |
|
---|
429 | #else
|
---|
430 | #endif
|
---|
431 | }
|
---|
432 |
|
---|
433 | shdirent *shfile_readdir(struct shdir *pdir)
|
---|
434 | {
|
---|
435 | #ifdef SH_PURE_STUB_MODE
|
---|
436 | return NULL;
|
---|
437 |
|
---|
438 | #elif defined(SH_STUB_MODE)
|
---|
439 | # ifdef _MSC_VER
|
---|
440 | errno = ENOSYS;
|
---|
441 | return NULL;
|
---|
442 | # else
|
---|
443 | struct dirent *pde = readdir((DIR *)pdir);
|
---|
444 | return pde ? (shdirent *)&pde->d_name[0] : NULL;
|
---|
445 | # endif
|
---|
446 |
|
---|
447 | #else
|
---|
448 | #endif
|
---|
449 | }
|
---|
450 |
|
---|
451 | void shfile_closedir(struct shdir *pdir)
|
---|
452 | {
|
---|
453 | #ifdef SH_PURE_STUB_MODE
|
---|
454 | return NULL;
|
---|
455 |
|
---|
456 | #elif defined(SH_STUB_MODE)
|
---|
457 | # ifdef _MSC_VER
|
---|
458 | errno = ENOSYS;
|
---|
459 | # else
|
---|
460 | closedir((DIR *)pdir);
|
---|
461 | # endif
|
---|
462 |
|
---|
463 | #else
|
---|
464 | #endif
|
---|
465 | }
|
---|