| 1 | /* $Id: shfile.c 1785 2008-09-14 17:31:16Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * File management.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
|---|
| 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 |
|
|---|
| 44 | int shfile_open(shfdtab *pfdtab, const char *name, unsigned flags, mode_t mode)
|
|---|
| 45 | {
|
|---|
| 46 | int fd;
|
|---|
| 47 |
|
|---|
| 48 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 49 | fd = -1;
|
|---|
| 50 | #elif defined(SH_STUB_MODE)
|
|---|
| 51 | fd = open(name, flags, mode);
|
|---|
| 52 | #else
|
|---|
| 53 | #endif
|
|---|
| 54 |
|
|---|
| 55 | TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
|
|---|
| 56 | return fd;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | int shfile_pipe(shfdtab *pfdtab, int fds[2])
|
|---|
| 60 | {
|
|---|
| 61 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 62 | return -1;
|
|---|
| 63 | #elif defined(SH_STUB_MODE)
|
|---|
| 64 | # ifdef _MSC_VER
|
|---|
| 65 | return _pipe(fds, PIPE_BUF, O_BINARY);
|
|---|
| 66 | # else
|
|---|
| 67 | return pipe(fds);
|
|---|
| 68 | # endif
|
|---|
| 69 | #else
|
|---|
| 70 | #endif
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | int shfile_dup(shfdtab *pfdtab, int fd)
|
|---|
| 74 | {
|
|---|
| 75 | int rc;
|
|---|
| 76 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 77 | rc = -1;
|
|---|
| 78 | #elif defined(SH_STUB_MODE)
|
|---|
| 79 | rc = dup(fd);
|
|---|
| 80 | #else
|
|---|
| 81 | #endif
|
|---|
| 82 |
|
|---|
| 83 | TRACE2((NULL, "shfile_dup(%d) -> %d [%d]\n", fd, rc, errno));
|
|---|
| 84 | return rc;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | int shfile_close(shfdtab *pfdtab, unsigned fd)
|
|---|
| 88 | {
|
|---|
| 89 | int rc;
|
|---|
| 90 |
|
|---|
| 91 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 92 | rc = -1;
|
|---|
| 93 | #elif defined(SH_STUB_MODE)
|
|---|
| 94 | rc = close(fd);
|
|---|
| 95 | #else
|
|---|
| 96 | #endif
|
|---|
| 97 |
|
|---|
| 98 | TRACE2((NULL, "shfile_close(%d) -> %d [%d]\n", fd, rc, errno));
|
|---|
| 99 | return rc;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | long shfile_read(shfdtab *pfdtab, int fd, void *buf, size_t len)
|
|---|
| 103 | {
|
|---|
| 104 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 105 | return -1;
|
|---|
| 106 | #elif defined(SH_STUB_MODE)
|
|---|
| 107 | # ifdef _MSC_VER
|
|---|
| 108 | return read(fd, buf, (unsigned)len);
|
|---|
| 109 | # else
|
|---|
| 110 | return read(fd, buf, len);
|
|---|
| 111 | # endif
|
|---|
| 112 | #else
|
|---|
| 113 | #endif
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | long shfile_write(shfdtab *pfdtab, int fd, const void *buf, size_t len)
|
|---|
| 117 | {
|
|---|
| 118 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 119 | return -1;
|
|---|
| 120 | #elif defined(SH_STUB_MODE)
|
|---|
| 121 | # ifdef _MSC_VER
|
|---|
| 122 | return write(fd, buf, (unsigned)len);
|
|---|
| 123 | # else
|
|---|
| 124 | return write(fd, buf, len);
|
|---|
| 125 | # endif
|
|---|
| 126 | #else
|
|---|
| 127 | #endif
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | long shfile_lseek(shfdtab *pfdtab, int fd, long off, int whench)
|
|---|
| 131 | {
|
|---|
| 132 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 133 | return -1;
|
|---|
| 134 | #elif defined(SH_STUB_MODE)
|
|---|
| 135 | return lseek(fd, off, whench);
|
|---|
| 136 | #else
|
|---|
| 137 | #endif
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | int shfile_fcntl(shfdtab *pfdtab, int fd, int cmd, int arg)
|
|---|
| 141 | {
|
|---|
| 142 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 143 | return -1;
|
|---|
| 144 | #elif defined(SH_STUB_MODE)
|
|---|
| 145 | # ifdef _MSC_VER
|
|---|
| 146 | return -1;
|
|---|
| 147 | # else
|
|---|
| 148 | return fcntl(fd, cmd, arg);
|
|---|
| 149 | # endif
|
|---|
| 150 | #else
|
|---|
| 151 | #endif
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | int shfile_stat(shfdtab *pfdtab, const char *path, struct stat *pst)
|
|---|
| 155 | {
|
|---|
| 156 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 157 | return -1;
|
|---|
| 158 | #elif defined(SH_STUB_MODE)
|
|---|
| 159 | return stat(path, pst);
|
|---|
| 160 | #else
|
|---|
| 161 | #endif
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | int shfile_lstat(shfdtab *pfdtab, const char *link, struct stat *pst)
|
|---|
| 165 | {
|
|---|
| 166 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 167 | return -1;
|
|---|
| 168 | #elif defined(SH_STUB_MODE)
|
|---|
| 169 | # ifdef _MSC_VER
|
|---|
| 170 | return stat(link, pst);
|
|---|
| 171 | # else
|
|---|
| 172 | return lstat(link, pst);
|
|---|
| 173 | # endif
|
|---|
| 174 | #else
|
|---|
| 175 | #endif
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | int shfile_chdir(shfdtab *pfdtab, const char *path)
|
|---|
| 179 | {
|
|---|
| 180 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 181 | return -1;
|
|---|
| 182 | #elif defined(SH_STUB_MODE)
|
|---|
| 183 | # ifdef _MSC_VER //???
|
|---|
| 184 | return chdir(path);
|
|---|
| 185 | # else
|
|---|
| 186 | return chdir(path);
|
|---|
| 187 | # endif
|
|---|
| 188 | #else
|
|---|
| 189 | #endif
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | char *shfile_getcwd(shfdtab *pfdtab, char *buf, int len)
|
|---|
| 193 | {
|
|---|
| 194 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 195 | return NULL;
|
|---|
| 196 | #elif defined(SH_STUB_MODE)
|
|---|
| 197 | return getcwd(buf, len);
|
|---|
| 198 | #else
|
|---|
| 199 | #endif
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | int shfile_access(shfdtab *pfdtab, const char *path, int type)
|
|---|
| 203 | {
|
|---|
| 204 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 205 | return -1;
|
|---|
| 206 | #elif defined(SH_STUB_MODE)
|
|---|
| 207 | # ifdef _MSC_VER
|
|---|
| 208 | type &= ~X_OK;
|
|---|
| 209 | return access(path, type);
|
|---|
| 210 | # else
|
|---|
| 211 | return access(path, type);
|
|---|
| 212 | # endif
|
|---|
| 213 | #else
|
|---|
| 214 | #endif
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | int shfile_isatty(shfdtab *pfdtab, int fd)
|
|---|
| 218 | {
|
|---|
| 219 | int rc;
|
|---|
| 220 |
|
|---|
| 221 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 222 | rc = 0;
|
|---|
| 223 | #elif defined(SH_STUB_MODE)
|
|---|
| 224 | rc = isatty(fd);
|
|---|
| 225 | #else
|
|---|
| 226 | #endif
|
|---|
| 227 |
|
|---|
| 228 | TRACE2((NULL, "isatty(%d) -> %d [%d]\n", fd, rc, errno));
|
|---|
| 229 | return rc;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 | int shfile_cloexec(shfdtab *pfdtab, int fd, int closeit)
|
|---|
| 234 | {
|
|---|
| 235 | int rc;
|
|---|
| 236 |
|
|---|
| 237 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 238 | rc = -1;
|
|---|
| 239 | #elif defined(SH_STUB_MODE)
|
|---|
| 240 | # ifdef _MSC_VER
|
|---|
| 241 | rc = -1;
|
|---|
| 242 | # else
|
|---|
| 243 | rc = fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0)
|
|---|
| 244 | | (closeit ? FD_CLOEXEC : 0));
|
|---|
| 245 | # endif
|
|---|
| 246 | #else
|
|---|
| 247 | #endif
|
|---|
| 248 |
|
|---|
| 249 | TRACE2((NULL, "shfile_cloexec(%d, %d) -> %d [%d]\n", fd, closeit, rc, errno));
|
|---|
| 250 | return rc;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 | int shfile_ioctl(shfdtab *pfdtab, int fd, unsigned long request, void *buf)
|
|---|
| 255 | {
|
|---|
| 256 | int rc;
|
|---|
| 257 |
|
|---|
| 258 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 259 | rc = -1;
|
|---|
| 260 | #elif defined(SH_STUB_MODE)
|
|---|
| 261 | # ifdef _MSC_VER
|
|---|
| 262 | rc = -1;
|
|---|
| 263 | # else
|
|---|
| 264 | rc = ioctl(fd, request, buf);
|
|---|
| 265 | # endif
|
|---|
| 266 | #else
|
|---|
| 267 | #endif
|
|---|
| 268 |
|
|---|
| 269 | TRACE2((NULL, "ioctl(%d, %#x, %p) -> %d\n", fd, request, buf, rc));
|
|---|
| 270 | return rc;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 | mode_t shfile_get_umask(shfdtab *pfdtab)
|
|---|
| 275 | {
|
|---|
| 276 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 277 | return 022;
|
|---|
| 278 | #elif defined(SH_STUB_MODE)
|
|---|
| 279 | return 022;
|
|---|
| 280 | #else
|
|---|
| 281 | #endif
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 | shdir *shfile_opendir(shfdtab *pfdtab, const char *dir)
|
|---|
| 286 | {
|
|---|
| 287 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 288 | return NULL;
|
|---|
| 289 | #elif defined(SH_STUB_MODE)
|
|---|
| 290 | # ifdef _MSC_VER
|
|---|
| 291 | return NULL;
|
|---|
| 292 | # else
|
|---|
| 293 | return (shdir *)opendir(dir);
|
|---|
| 294 | # endif
|
|---|
| 295 | #else
|
|---|
| 296 | #endif
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | shdirent *shfile_readdir(struct shdir *pdir)
|
|---|
| 300 | {
|
|---|
| 301 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 302 | return NULL;
|
|---|
| 303 | #elif defined(SH_STUB_MODE)
|
|---|
| 304 | # ifdef _MSC_VER
|
|---|
| 305 | return NULL;
|
|---|
| 306 | # else
|
|---|
| 307 | struct dirent *pde = readdir((DIR *)pdir);
|
|---|
| 308 | return pde ? (shdirent *)&pde->d_name[0] : NULL;
|
|---|
| 309 | # endif
|
|---|
| 310 | #else
|
|---|
| 311 | #endif
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | void shfile_closedir(struct shdir *pdir)
|
|---|
| 315 | {
|
|---|
| 316 | #ifdef SH_PURE_STUB_MODE
|
|---|
| 317 | return NULL;
|
|---|
| 318 | #elif defined(SH_STUB_MODE)
|
|---|
| 319 | # ifndef _MSC_VER
|
|---|
| 320 | closedir((DIR *)pdir);
|
|---|
| 321 | # endif
|
|---|
| 322 | #else
|
|---|
| 323 | #endif
|
|---|
| 324 | }
|
|---|