kmk: Made chmod build on windows. Some cleanup of the bsdisms.
[kbuild-mirror.git] / src / kash / shfile.c
blob7886345ff52323b2485831b9d02b03992adaa68c
1 /* $Id$ */
2 /** @file
4 * File management.
6 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
9 * This file is part of kBuild.
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.
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.
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
27 #include "shfile.h"
28 #include <stdlib.h>
29 #include <stdio.h>
31 #if K_OS == K_OS_WINDOWS
32 # include <limits.h>
33 # ifndef PIPE_BUF
34 # define PIPE_BUF 512
35 # endif
36 #else
37 # include <unistd.h>
38 # include <fcntl.h>
39 # include <dirent.h>
40 #endif
43 int shfile_open(shfdtab *pfdtab, const char *name, unsigned flags, mode_t mode)
45 #ifdef SH_PURE_STUB_MODE
46 return -1;
47 #elif defined(SH_STUB_MODE)
48 return open(name, flags, mode);
49 #else
50 #endif
53 int shfile_pipe(shfdtab *pfdtab, int fds[2])
55 #ifdef SH_PURE_STUB_MODE
56 return -1;
57 #elif defined(SH_STUB_MODE)
58 # ifdef _MSC_VER
59 return _pipe(fds, PIPE_BUF, O_BINARY);
60 # else
61 return pipe(fds);
62 # endif
63 #else
64 #endif
67 int shfile_dup(shfdtab *pfdtab, int fd)
69 #ifdef SH_PURE_STUB_MODE
70 return -1;
71 #elif defined(SH_STUB_MODE)
72 return dup(fd);
73 #else
74 #endif
77 int shfile_close(shfdtab *pfdtab, unsigned fd)
79 #ifdef SH_PURE_STUB_MODE
80 return -1;
81 #elif defined(SH_STUB_MODE)
82 return close(fd);
83 #else
84 #endif
87 long shfile_read(shfdtab *pfdtab, int fd, void *buf, size_t len)
89 #ifdef SH_PURE_STUB_MODE
90 return -1;
91 #elif defined(SH_STUB_MODE)
92 # ifdef _MSC_VER
93 return read(fd, buf, (unsigned)len);
94 # else
95 return read(fd, buf, len);
96 # endif
97 #else
98 #endif
101 long shfile_write(shfdtab *pfdtab, int fd, const void *buf, size_t len)
103 #ifdef SH_PURE_STUB_MODE
104 return -1;
105 #elif defined(SH_STUB_MODE)
106 # ifdef _MSC_VER
107 return write(fd, buf, (unsigned)len);
108 # else
109 return write(fd, buf, len);
110 # endif
111 #else
112 #endif
115 long shfile_lseek(shfdtab *pfdtab, int fd, long off, int whench)
117 #ifdef SH_PURE_STUB_MODE
118 return -1;
119 #elif defined(SH_STUB_MODE)
120 return lseek(fd, off, whench);
121 #else
122 #endif
125 int shfile_fcntl(shfdtab *pfdtab, int fd, int cmd, int arg)
127 #ifdef SH_PURE_STUB_MODE
128 return -1;
129 #elif defined(SH_STUB_MODE)
130 # ifdef _MSC_VER
131 return -1;
132 # else
133 return fcntl(fd, cmd, arg);
134 # endif
135 #else
136 #endif
139 int shfile_stat(shfdtab *pfdtab, const char *path, struct stat *pst)
141 #ifdef SH_PURE_STUB_MODE
142 return -1;
143 #elif defined(SH_STUB_MODE)
144 return stat(path, pst);
145 #else
146 #endif
149 int shfile_lstat(shfdtab *pfdtab, const char *link, struct stat *pst)
151 #ifdef SH_PURE_STUB_MODE
152 return -1;
153 #elif defined(SH_STUB_MODE)
154 # ifdef _MSC_VER
155 return stat(link, pst);
156 # else
157 return lstat(link, pst);
158 # endif
159 #else
160 #endif
163 int shfile_chdir(shfdtab *pfdtab, const char *path)
165 #ifdef SH_PURE_STUB_MODE
166 return -1;
167 #elif defined(SH_STUB_MODE)
168 # ifdef _MSC_VER //???
169 return chdir(path);
170 # else
171 return chdir(path);
172 # endif
173 #else
174 #endif
177 char *shfile_getcwd(shfdtab *pfdtab, char *buf, int len)
179 #ifdef SH_PURE_STUB_MODE
180 return NULL;
181 #elif defined(SH_STUB_MODE)
182 return getcwd(buf, len);
183 #else
184 #endif
187 int shfile_access(shfdtab *pfdtab, const char *path, int type)
189 #ifdef SH_PURE_STUB_MODE
190 return -1;
191 #elif defined(SH_STUB_MODE)
192 # ifdef _MSC_VER
193 type &= ~X_OK;
194 return access(path, type);
195 # else
196 return access(path, type);
197 # endif
198 #else
199 #endif
202 int shfile_isatty(shfdtab *pfdtab, int fd)
204 #ifdef SH_PURE_STUB_MODE
205 return 0;
206 #elif defined(SH_STUB_MODE)
207 return isatty(fd);
208 #else
209 #endif
213 int shfile_cloexec(shfdtab *pfdtab, int fd, int closeit)
215 #ifdef SH_PURE_STUB_MODE
216 return -1;
217 #elif defined(SH_STUB_MODE)
218 # ifdef _MSC_VER
219 return -1;
220 # else
221 int rc = fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0)
222 | (closeit ? FD_CLOEXEC : 0));
223 //fprintf(stderr, "shfile_cloexec(%d, %d) -> %d\n", fd, closeit, rc);
224 return rc;
225 # endif
226 #else
227 #endif
232 int shfile_ioctl(shfdtab *pfdtab, int fd, unsigned long request, void *buf)
234 #ifdef SH_PURE_STUB_MODE
235 return -1;
236 #elif defined(SH_STUB_MODE)
237 # ifdef _MSC_VER
238 return -1;
239 # else
240 int rc = ioctl(fd, request, buf);
241 //fprintf(stderr, "ioctl(%d, %#x, %p) -> %d\n", fd, request, buf, rc);
242 return rc;
243 # endif
244 #else
245 #endif
249 mode_t shfile_get_umask(shfdtab *pfdtab)
251 #ifdef SH_PURE_STUB_MODE
252 return 022;
253 #elif defined(SH_STUB_MODE)
254 return 022;
255 #else
256 #endif
260 shdir *shfile_opendir(shfdtab *pfdtab, const char *dir)
262 #ifdef SH_PURE_STUB_MODE
263 return NULL;
264 #elif defined(SH_STUB_MODE)
265 # ifdef _MSC_VER
266 return NULL;
267 # else
268 return (shdir *)opendir(dir);
269 # endif
270 #else
271 #endif
274 shdirent *shfile_readdir(struct shdir *pdir)
276 #ifdef SH_PURE_STUB_MODE
277 return NULL;
278 #elif defined(SH_STUB_MODE)
279 # ifdef _MSC_VER
280 return NULL;
281 # else
282 struct dirent *pde = readdir((DIR *)pdir);
283 return pde ? (shdirent *)&pde->d_name[0] : NULL;
284 # endif
285 #else
286 #endif
289 void shfile_closedir(struct shdir *pdir)
291 #ifdef SH_PURE_STUB_MODE
292 return NULL;
293 #elif defined(SH_STUB_MODE)
294 # ifndef _MSC_VER
295 closedir((DIR *)pdir);
296 # endif
297 #else
298 #endif