1 | /* $Id: shfile.c 1233 2007-10-09 23:24:41Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * File management.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2007 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 <stdlib.h>
|
---|
29 |
|
---|
30 | #ifdef KBUILD_OS_WINDOWS
|
---|
31 | # include <limits.h>
|
---|
32 | # ifndef PIPE_BUF
|
---|
33 | # define PIPE_BUF 512
|
---|
34 | # endif
|
---|
35 | #else
|
---|
36 | # include <unistd.h>
|
---|
37 | # include <fcntl.h>
|
---|
38 | # include <dirent.h>
|
---|
39 | #endif
|
---|
40 |
|
---|
41 |
|
---|
42 | int shfile_open(shfdtab *pfdtab, const char *name, unsigned flags, mode_t mode)
|
---|
43 | {
|
---|
44 | #ifdef SH_PURE_STUB_MODE
|
---|
45 | return -1;
|
---|
46 | #elif defined(SH_STUB_MODE)
|
---|
47 | return open(name, flags, mode);
|
---|
48 | #else
|
---|
49 | #endif
|
---|
50 | }
|
---|
51 |
|
---|
52 | int shfile_pipe(shfdtab *pfdtab, int fds[2])
|
---|
53 | {
|
---|
54 | #ifdef SH_PURE_STUB_MODE
|
---|
55 | return -1;
|
---|
56 | #elif defined(SH_STUB_MODE)
|
---|
57 | # ifdef _MSC_VER
|
---|
58 | return _pipe(fds, PIPE_BUF, O_BINARY);
|
---|
59 | # else
|
---|
60 | return pipe(fds);
|
---|
61 | # endif
|
---|
62 | #else
|
---|
63 | #endif
|
---|
64 | }
|
---|
65 |
|
---|
66 | int shfile_dup(shfdtab *pfdtab, int fd)
|
---|
67 | {
|
---|
68 | #ifdef SH_PURE_STUB_MODE
|
---|
69 | return -1;
|
---|
70 | #elif defined(SH_STUB_MODE)
|
---|
71 | return dup(fd);
|
---|
72 | #else
|
---|
73 | #endif
|
---|
74 | }
|
---|
75 |
|
---|
76 | int shfile_close(shfdtab *pfdtab, unsigned fd)
|
---|
77 | {
|
---|
78 | #ifdef SH_PURE_STUB_MODE
|
---|
79 | return -1;
|
---|
80 | #elif defined(SH_STUB_MODE)
|
---|
81 | return close(fd);
|
---|
82 | #else
|
---|
83 | #endif
|
---|
84 | }
|
---|
85 |
|
---|
86 | long shfile_read(shfdtab *pfdtab, int fd, void *buf, size_t len)
|
---|
87 | {
|
---|
88 | #ifdef SH_PURE_STUB_MODE
|
---|
89 | return -1;
|
---|
90 | #elif defined(SH_STUB_MODE)
|
---|
91 | # ifdef _MSC_VER
|
---|
92 | return read(fd, buf, (unsigned)len);
|
---|
93 | # else
|
---|
94 | return read(fd, buf, len);
|
---|
95 | # endif
|
---|
96 | #else
|
---|
97 | #endif
|
---|
98 | }
|
---|
99 |
|
---|
100 | long shfile_write(shfdtab *pfdtab, int fd, const void *buf, size_t len)
|
---|
101 | {
|
---|
102 | #ifdef SH_PURE_STUB_MODE
|
---|
103 | return -1;
|
---|
104 | #elif defined(SH_STUB_MODE)
|
---|
105 | # ifdef _MSC_VER
|
---|
106 | return write(fd, buf, (unsigned)len);
|
---|
107 | # else
|
---|
108 | return write(fd, buf, len);
|
---|
109 | # endif
|
---|
110 | #else
|
---|
111 | #endif
|
---|
112 | }
|
---|
113 |
|
---|
114 | long shfile_lseek(shfdtab *pfdtab, int fd, long off, int whench)
|
---|
115 | {
|
---|
116 | #ifdef SH_PURE_STUB_MODE
|
---|
117 | return -1;
|
---|
118 | #elif defined(SH_STUB_MODE)
|
---|
119 | return lseek(fd, off, whench);
|
---|
120 | #else
|
---|
121 | #endif
|
---|
122 | }
|
---|
123 |
|
---|
124 | int shfile_fcntl(shfdtab *pfdtab, int fd, int cmd, int arg)
|
---|
125 | {
|
---|
126 | #ifdef SH_PURE_STUB_MODE
|
---|
127 | return -1;
|
---|
128 | #elif defined(SH_STUB_MODE)
|
---|
129 | # ifdef _MSC_VER
|
---|
130 | return -1;
|
---|
131 | # else
|
---|
132 | return fcntl(fd, cmd, arg);
|
---|
133 | # endif
|
---|
134 | #else
|
---|
135 | #endif
|
---|
136 | }
|
---|
137 |
|
---|
138 | int shfile_stat(shfdtab *pfdtab, const char *path, struct stat *pst)
|
---|
139 | {
|
---|
140 | #ifdef SH_PURE_STUB_MODE
|
---|
141 | return -1;
|
---|
142 | #elif defined(SH_STUB_MODE)
|
---|
143 | return stat(path, pst);
|
---|
144 | #else
|
---|
145 | #endif
|
---|
146 | }
|
---|
147 |
|
---|
148 | int shfile_lstat(shfdtab *pfdtab, const char *link, struct stat *pst)
|
---|
149 | {
|
---|
150 | #ifdef SH_PURE_STUB_MODE
|
---|
151 | return -1;
|
---|
152 | #elif defined(SH_STUB_MODE)
|
---|
153 | # ifdef _MSC_VER
|
---|
154 | return stat(link, pst);
|
---|
155 | # else
|
---|
156 | return lstat(link, pst);
|
---|
157 | # endif
|
---|
158 | #else
|
---|
159 | #endif
|
---|
160 | }
|
---|
161 |
|
---|
162 | int shfile_chdir(shfdtab *pfdtab, const char *path)
|
---|
163 | {
|
---|
164 | #ifdef SH_PURE_STUB_MODE
|
---|
165 | return -1;
|
---|
166 | #elif defined(SH_STUB_MODE)
|
---|
167 | # ifdef _MSC_VER //???
|
---|
168 | return chdir(path);
|
---|
169 | # else
|
---|
170 | return chdir(path);
|
---|
171 | # endif
|
---|
172 | #else
|
---|
173 | #endif
|
---|
174 | }
|
---|
175 |
|
---|
176 | char *shfile_getcwd(shfdtab *pfdtab, char *buf, int len)
|
---|
177 | {
|
---|
178 | #ifdef SH_PURE_STUB_MODE
|
---|
179 | return NULL;
|
---|
180 | #elif defined(SH_STUB_MODE)
|
---|
181 | return getcwd(buf, len);
|
---|
182 | #else
|
---|
183 | #endif
|
---|
184 | }
|
---|
185 |
|
---|
186 | int shfile_access(shfdtab *pfdtab, const char *path, int type)
|
---|
187 | {
|
---|
188 | #ifdef SH_PURE_STUB_MODE
|
---|
189 | return -1;
|
---|
190 | #elif defined(SH_STUB_MODE)
|
---|
191 | # ifdef _MSC_VER
|
---|
192 | type &= ~X_OK;
|
---|
193 | return access(path, type);
|
---|
194 | # else
|
---|
195 | return access(path, type);
|
---|
196 | # endif
|
---|
197 | #else
|
---|
198 | #endif
|
---|
199 | }
|
---|
200 |
|
---|
201 | int shfile_isatty(shfdtab *pfdtab, int fd)
|
---|
202 | {
|
---|
203 | #ifdef SH_PURE_STUB_MODE
|
---|
204 | return 0;
|
---|
205 | #elif defined(SH_STUB_MODE)
|
---|
206 | return isatty(fd);
|
---|
207 | #else
|
---|
208 | #endif
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | int shfile_ioctl(shfdtab *pfdtab, int fd, unsigned long request, void *buf)
|
---|
213 | {
|
---|
214 | #ifdef SH_PURE_STUB_MODE
|
---|
215 | return -1;
|
---|
216 | #elif defined(SH_STUB_MODE)
|
---|
217 | # ifdef _MSC_VER
|
---|
218 | return -1;
|
---|
219 | # else
|
---|
220 | return ioctl(fd, request, buf);
|
---|
221 | # endif
|
---|
222 | #else
|
---|
223 | #endif
|
---|
224 | }
|
---|
225 |
|
---|
226 | mode_t shfile_get_umask(shfdtab *pfdtab)
|
---|
227 | {
|
---|
228 | #ifdef SH_PURE_STUB_MODE
|
---|
229 | return 022;
|
---|
230 | #elif defined(SH_STUB_MODE)
|
---|
231 | return 022;
|
---|
232 | #else
|
---|
233 | #endif
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | shdir *shfile_opendir(shfdtab *pfdtab, const char *dir)
|
---|
238 | {
|
---|
239 | #ifdef SH_PURE_STUB_MODE
|
---|
240 | return NULL;
|
---|
241 | #elif defined(SH_STUB_MODE)
|
---|
242 | # ifdef _MSC_VER
|
---|
243 | return NULL;
|
---|
244 | # else
|
---|
245 | return (shdir *)opendir(dir);
|
---|
246 | # endif
|
---|
247 | #else
|
---|
248 | #endif
|
---|
249 | }
|
---|
250 |
|
---|
251 | shdirent *shfile_readdir(struct shdir *pdir)
|
---|
252 | {
|
---|
253 | #ifdef SH_PURE_STUB_MODE
|
---|
254 | return NULL;
|
---|
255 | #elif defined(SH_STUB_MODE)
|
---|
256 | # ifdef _MSC_VER
|
---|
257 | return NULL;
|
---|
258 | # else
|
---|
259 | struct dirent *pde = readdir((DIR *)pdir);
|
---|
260 | return pde ? (shdirent *)&pde->d_name[0] : NULL;
|
---|
261 | # endif
|
---|
262 | #else
|
---|
263 | #endif
|
---|
264 | }
|
---|
265 |
|
---|
266 | void shfile_closedir(struct shdir *pdir)
|
---|
267 | {
|
---|
268 | #ifdef SH_PURE_STUB_MODE
|
---|
269 | return NULL;
|
---|
270 | #elif defined(SH_STUB_MODE)
|
---|
271 | # ifndef _MSC_VER
|
---|
272 | closedir((DIR *)pdir);
|
---|
273 | # endif
|
---|
274 | #else
|
---|
275 | #endif
|
---|
276 | }
|
---|