VirtualBox

source: kBuild/trunk/src/kash/shfile.c@ 2392

Last change on this file since 2392 was 2376, checked in by bird, 15 years ago

kash: Fixed some file handle inheritance issues on windows. piping to the native sort.exe works now.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 41.3 KB
Line 
1/* $Id: shfile.c 2376 2010-01-13 01:45:49Z 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/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include "shfile.h"
31#include "shinstance.h" /* TRACE2 */
32#include <stdlib.h>
33#include <stdio.h>
34#include <assert.h>
35
36#if K_OS == K_OS_WINDOWS
37# include <limits.h>
38# ifndef PIPE_BUF
39# define PIPE_BUF 512
40# endif
41# include <Windows.h>
42#else
43# include <unistd.h>
44# include <fcntl.h>
45# include <dirent.h>
46#endif
47
48
49/*******************************************************************************
50* Defined Constants And Macros *
51*******************************************************************************/
52/** @def SHFILE_IN_USE
53 * Whether the file descriptor table stuff is actually in use or not.
54 */
55#if K_OS == K_OS_WINDOWS \
56 || !defined(SH_FORKED_MODE)
57# define SHFILE_IN_USE
58#endif
59/** The max file table size. */
60#define SHFILE_MAX 1024
61/** The file table growth rate. */
62#define SHFILE_GROW 64
63/** The min native unix file descriptor. */
64#define SHFILE_UNIX_MIN_FD 32
65/** The path buffer size we use. */
66#define SHFILE_MAX_PATH 4096
67
68/** Set errno and return. Doing a trace in debug build. */
69#define RETURN_ERROR(rc, err, msg) \
70 do { \
71 TRACE2((NULL, "%s: " ## msg ## " - returning %d / %d\n", __FUNCTION__, (rc), (err))); \
72 errno = (err); \
73 return (rc); \
74 } while (0)
75
76#if K_OS == K_OS_WINDOWS
77 /* See msdos.h for description. */
78# define FOPEN 0x01
79# define FEOFLAG 0x02
80# define FCRLF 0x04
81# define FPIPE 0x08
82# define FNOINHERIT 0x10
83# define FAPPEND 0x20
84# define FDEV 0x40
85# define FTEXT 0x80
86
87# define MY_ObjectBasicInformation 0
88typedef LONG (NTAPI * PFN_NtQueryObject)(HANDLE, int, void *, size_t, size_t *);
89
90typedef struct MY_OBJECT_BASIC_INFORMATION
91{
92 ULONG Attributes;
93 ACCESS_MASK GrantedAccess;
94 ULONG HandleCount;
95 ULONG PointerCount;
96 ULONG PagedPoolUsage;
97 ULONG NonPagedPoolUsage;
98 ULONG Reserved[3];
99 ULONG NameInformationLength;
100 ULONG TypeInformationLength;
101 ULONG SecurityDescriptorLength;
102 LARGE_INTEGER CreateTime;
103} MY_OBJECT_BASIC_INFORMATION;
104
105#endif /* K_OS_WINDOWS */
106
107
108#ifdef SHFILE_IN_USE
109
110/**
111 * Close the specified native handle.
112 *
113 * @param native The native file handle.
114 * @param flags The flags in case they might come in handy later.
115 */
116static void shfile_native_close(intptr_t native, unsigned flags)
117{
118#if K_OS == K_OS_WINDOWS
119 BOOL fRc = CloseHandle((HANDLE)native);
120 assert(fRc); (void)fRc;
121#else
122 int s = errno;
123 close(native);
124 errno = s;
125#endif
126 (void)flags;
127}
128
129/**
130 * Inserts the file into the descriptor table.
131 *
132 * If we're out of memory and cannot extend the table, we'll close the
133 * file, set errno to EMFILE and return -1.
134 *
135 * @returns The file descriptor number. -1 and errno on failure.
136 * @param pfdtab The file descriptor table.
137 * @param native The native file handle.
138 * @param oflags The flags the it was opened/created with.
139 * @param shflags The shell file flags.
140 * @param fdMin The minimum file descriptor number, pass -1 if any is ok.
141 * @param who Who we're doing this for (for logging purposes).
142 */
143static int shfile_insert(shfdtab *pfdtab, intptr_t native, unsigned oflags, unsigned shflags, int fdMin, const char *who)
144{
145 shmtxtmp tmp;
146 int fd;
147 int i;
148
149 /*
150 * Fend of bad stuff.
151 */
152 if (fdMin >= SHFILE_MAX)
153 {
154 errno = EMFILE;
155 return -1;
156 }
157
158 shmtx_enter(&pfdtab->mtx, &tmp);
159
160 /*
161 * Search for a fitting unused location.
162 */
163 fd = -1;
164 for (i = 0; (unsigned)i < pfdtab->size; i++)
165 if ( i >= fdMin
166 && pfdtab->tab[i].fd == -1)
167 {
168 fd = i;
169 break;
170 }
171 if (fd == -1)
172 {
173 /*
174 * Grow the descriptor table.
175 */
176 shfile *new_tab;
177 int new_size = pfdtab->size + SHFILE_GROW;
178 while (new_size < fdMin)
179 new_size += SHFILE_GROW;
180 new_tab = sh_realloc(shthread_get_shell(), pfdtab->tab, new_size * sizeof(shfile));
181 if (new_tab)
182 {
183 for (i = pfdtab->size; i < new_size; i++)
184 {
185 new_tab[i].fd = -1;
186 new_tab[i].oflags = 0;
187 new_tab[i].shflags = 0;
188 new_tab[i].native = -1;
189 }
190
191 fd = pfdtab->size;
192 if (fd < fdMin)
193 fd = fdMin;
194
195 pfdtab->tab = new_tab;
196 pfdtab->size = new_size;
197 }
198 }
199
200 /*
201 * Fill in the entry if we've found one.
202 */
203 if (fd != -1)
204 {
205 pfdtab->tab[fd].fd = fd;
206 pfdtab->tab[fd].oflags = oflags;
207 pfdtab->tab[fd].shflags = shflags;
208 pfdtab->tab[fd].native = native;
209 }
210 else
211 shfile_native_close(native, oflags);
212
213 shmtx_leave(&pfdtab->mtx, &tmp);
214
215 if (fd == -1)
216 errno = EMFILE;
217 (void)who;
218 return fd;
219}
220
221/**
222 * Gets a file descriptor and lock the file descriptor table.
223 *
224 * @returns Pointer to the file and table ownership on success,
225 * NULL and errno set to EBADF on failure.
226 * @param pfdtab The file descriptor table.
227 * @param fd The file descriptor number.
228 * @param ptmp See shmtx_enter.
229 */
230static shfile *shfile_get(shfdtab *pfdtab, int fd, shmtxtmp *ptmp)
231{
232 shfile *file = NULL;
233 if ( fd >= 0
234 && (unsigned)fd < pfdtab->size)
235 {
236 shmtx_enter(&pfdtab->mtx, ptmp);
237 if ((unsigned)fd < pfdtab->size
238 && pfdtab->tab[fd].fd != -1)
239 file = &pfdtab->tab[fd];
240 else
241 shmtx_leave(&pfdtab->mtx, ptmp);
242 }
243 if (!file)
244 errno = EBADF;
245 return file;
246}
247
248/**
249 * Puts back a file descriptor and releases the table ownership.
250 *
251 * @param pfdtab The file descriptor table.
252 * @param file The file.
253 * @param ptmp See shmtx_leave.
254 */
255static void shfile_put(shfdtab *pfdtab, shfile *file, shmtxtmp *ptmp)
256{
257 shmtx_leave(&pfdtab->mtx, ptmp);
258 assert(file);
259 (void)file;
260}
261
262/**
263 * Constructs a path from the current directory and the passed in path.
264 *
265 * @returns 0 on success, -1 and errno set to ENAMETOOLONG or EINVAL on failure.
266 *
267 * @param pfdtab The file descriptor table
268 * @param path The path the caller supplied.
269 * @param buf Where to put the path. This is assumed to be SHFILE_MAX_PATH
270 * chars in size.
271 */
272int shfile_make_path(shfdtab *pfdtab, const char *path, char *buf)
273{
274 size_t path_len = strlen(path);
275 if (path_len == 0)
276 {
277 errno = EINVAL;
278 return -1;
279 }
280 if (path_len >= SHFILE_MAX_PATH)
281 {
282 errno = ENAMETOOLONG;
283 return -1;
284 }
285 if ( *path == '/'
286#if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
287 || *path == '\\'
288 || ( *path
289 && path[1] == ':'
290 && ( (*path >= 'A' && *path <= 'Z')
291 || (*path >= 'a' && *path <= 'z')))
292#endif
293 )
294 {
295 memcpy(buf, path, path_len + 1);
296 }
297 else
298 {
299 size_t cwd_len;
300 shmtxtmp tmp;
301
302 shmtx_enter(&pfdtab->mtx, &tmp);
303
304 cwd_len = strlen(pfdtab->cwd);
305 memcpy(buf, pfdtab->cwd, cwd_len);
306
307 shmtx_leave(&pfdtab->mtx, &tmp);
308
309 if (cwd_len + path_len + 1 >= SHFILE_MAX_PATH)
310 {
311 errno = ENAMETOOLONG;
312 return -1;
313 }
314 if ( !cwd_len
315 || buf[cwd_len - 1] != '/')
316 buf[cwd_len++] = '/';
317 memcpy(buf + cwd_len, path, path_len + 1);
318 }
319
320#if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
321 if (!strcmp(buf, "/dev/null"))
322 strcpy(buf, "NUL");
323#endif
324 return 0;
325}
326
327# if K_OS == K_OS_WINDOWS
328
329/**
330 * Converts a DOS(/Windows) error code to errno,
331 * assigning it to errno.
332 *
333 * @returns -1
334 * @param err The DOS error.
335 */
336static int shfile_dos2errno(int err)
337{
338 switch (err)
339 {
340 case ERROR_BAD_ENVIRONMENT: errno = E2BIG; break;
341 case ERROR_ACCESS_DENIED: errno = EACCES; break;
342 case ERROR_CURRENT_DIRECTORY: errno = EACCES; break;
343 case ERROR_LOCK_VIOLATION: errno = EACCES; break;
344 case ERROR_NETWORK_ACCESS_DENIED: errno = EACCES; break;
345 case ERROR_CANNOT_MAKE: errno = EACCES; break;
346 case ERROR_FAIL_I24: errno = EACCES; break;
347 case ERROR_DRIVE_LOCKED: errno = EACCES; break;
348 case ERROR_SEEK_ON_DEVICE: errno = EACCES; break;
349 case ERROR_NOT_LOCKED: errno = EACCES; break;
350 case ERROR_LOCK_FAILED: errno = EACCES; break;
351 case ERROR_NO_PROC_SLOTS: errno = EAGAIN; break;
352 case ERROR_MAX_THRDS_REACHED: errno = EAGAIN; break;
353 case ERROR_NESTING_NOT_ALLOWED: errno = EAGAIN; break;
354 case ERROR_INVALID_HANDLE: errno = EBADF; break;
355 case ERROR_INVALID_TARGET_HANDLE: errno = EBADF; break;
356 case ERROR_DIRECT_ACCESS_HANDLE: errno = EBADF; break;
357 case ERROR_WAIT_NO_CHILDREN: errno = ECHILD; break;
358 case ERROR_CHILD_NOT_COMPLETE: errno = ECHILD; break;
359 case ERROR_FILE_EXISTS: errno = EEXIST; break;
360 case ERROR_ALREADY_EXISTS: errno = EEXIST; break;
361 case ERROR_INVALID_FUNCTION: errno = EINVAL; break;
362 case ERROR_INVALID_ACCESS: errno = EINVAL; break;
363 case ERROR_INVALID_DATA: errno = EINVAL; break;
364 case ERROR_INVALID_PARAMETER: errno = EINVAL; break;
365 case ERROR_NEGATIVE_SEEK: errno = EINVAL; break;
366 case ERROR_TOO_MANY_OPEN_FILES: errno = EMFILE; break;
367 case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
368 case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
369 case ERROR_INVALID_DRIVE: errno = ENOENT; break;
370 case ERROR_NO_MORE_FILES: errno = ENOENT; break;
371 case ERROR_BAD_NETPATH: errno = ENOENT; break;
372 case ERROR_BAD_NET_NAME: errno = ENOENT; break;
373 case ERROR_BAD_PATHNAME: errno = ENOENT; break;
374 case ERROR_FILENAME_EXCED_RANGE: errno = ENOENT; break;
375 case ERROR_BAD_FORMAT: errno = ENOEXEC; break;
376 case ERROR_ARENA_TRASHED: errno = ENOMEM; break;
377 case ERROR_NOT_ENOUGH_MEMORY: errno = ENOMEM; break;
378 case ERROR_INVALID_BLOCK: errno = ENOMEM; break;
379 case ERROR_NOT_ENOUGH_QUOTA: errno = ENOMEM; break;
380 case ERROR_DISK_FULL: errno = ENOSPC; break;
381 case ERROR_DIR_NOT_EMPTY: errno = ENOTEMPTY; break;
382 case ERROR_BROKEN_PIPE: errno = EPIPE; break;
383 case ERROR_NOT_SAME_DEVICE: errno = EXDEV; break;
384 default: errno = EINVAL; break;
385 }
386 return -1;
387}
388
389DWORD shfile_query_handle_access_mask(HANDLE h, PACCESS_MASK pMask)
390{
391 static PFN_NtQueryObject s_pfnNtQueryObject = NULL;
392 MY_OBJECT_BASIC_INFORMATION BasicInfo;
393 LONG Status;
394
395 if (!s_pfnNtQueryObject)
396 {
397 s_pfnNtQueryObject = (PFN_NtQueryObject)GetProcAddress(GetModuleHandle("NTDLL"), "NtQueryObject");
398 if (!s_pfnNtQueryObject)
399 return ERROR_NOT_SUPPORTED;
400 }
401
402 Status = s_pfnNtQueryObject(h, MY_ObjectBasicInformation, &BasicInfo, sizeof(BasicInfo), NULL);
403 if (Status >= 0)
404 {
405 *pMask = BasicInfo.GrantedAccess;
406 return NO_ERROR;
407 }
408 if (Status != STATUS_INVALID_HANDLE)
409 return ERROR_GEN_FAILURE;
410 return ERROR_INVALID_HANDLE;
411}
412
413# endif /* K_OS == K_OS_WINDOWS */
414
415#endif /* SHFILE_IN_USE */
416
417/**
418 * Initializes a file descriptor table.
419 *
420 * @returns 0 on success, -1 and errno on failure.
421 * @param pfdtab The table to initialize.
422 * @param inherit File descriptor table to inherit from. If not specified
423 * we will inherit from the current process as it were.
424 */
425int shfile_init(shfdtab *pfdtab, shfdtab *inherit)
426{
427 int rc;
428
429 pfdtab->cwd = NULL;
430 pfdtab->size = 0;
431 pfdtab->tab = NULL;
432 rc = shmtx_init(&pfdtab->mtx);
433 if (!rc)
434 {
435#ifdef SHFILE_IN_USE
436 char buf[SHFILE_MAX_PATH];
437 if (getcwd(buf, sizeof(buf)))
438 {
439 pfdtab->cwd = sh_strdup(NULL, buf);
440 if (!inherit)
441 {
442# if K_OS == K_OS_WINDOWS
443 static const struct
444 {
445 DWORD dwStdHandle;
446 unsigned fFlags;
447 } aStdHandles[3] =
448 {
449 { STD_INPUT_HANDLE, _O_RDONLY },
450 { STD_OUTPUT_HANDLE, _O_WRONLY },
451 { STD_ERROR_HANDLE, _O_WRONLY }
452 };
453 int i;
454 STARTUPINFO Info;
455 ACCESS_MASK Mask;
456 DWORD dwErr;
457
458 rc = 0;
459
460 /* Try pick up the Visual C++ CRT file descriptor info. */
461 __try {
462 GetStartupInfo(&Info);
463 } __except (EXCEPTION_EXECUTE_HANDLER) {
464 memset(&Info, 0, sizeof(Info));
465 }
466
467 if ( Info.cbReserved2 > sizeof(int)
468 && (uintptr_t)Info.lpReserved2 >= 0x1000
469 && (i = *(int *)Info.lpReserved2) >= 1
470 && i <= 2048
471 && ( Info.cbReserved2 == i * 5 + 4
472 //|| Info.cbReserved2 == i * 5 + 1 - check the cygwin sources.
473 || Info.cbReserved2 == i * 9 + 4))
474 {
475 uint8_t *paf = (uint8_t *)Info.lpReserved2 + sizeof(int);
476 int dwPerH = 1 + (Info.cbReserved2 == i * 9 + 4);
477 DWORD *ph = (DWORD *)(paf + i) + dwPerH * i;
478 HANDLE aStdHandles2[3];
479 int j;
480
481 //if (Info.cbReserved2 == i * 5 + 1) - check the cygwin sources.
482 // i--;
483
484 for (j = 0; j < 3; j++)
485 aStdHandles2[j] = GetStdHandle(aStdHandles[j].dwStdHandle);
486
487 while (i-- > 0)
488 {
489 ph -= dwPerH;
490
491 if ( (paf[i] & (FOPEN | FNOINHERIT)) == FOPEN
492 && *ph != (uint32_t)INVALID_HANDLE_VALUE)
493 {
494 HANDLE h = (HANDLE)(intptr_t)*ph;
495 int fd2;
496 int fFlags;
497 int fFlags2;
498
499 if ( h == aStdHandles2[j = 0]
500 || h == aStdHandles2[j = 1]
501 || h == aStdHandles2[j = 2])
502 fFlags = aStdHandles[j].fFlags;
503 else
504 {
505 dwErr = shfile_query_handle_access_mask(h, &Mask);
506 if (dwErr == ERROR_INVALID_HANDLE)
507 continue;
508 else if (dwErr == NO_ERROR)
509 {
510 fFlags = 0;
511 if ( (Mask & (GENERIC_READ | FILE_READ_DATA))
512 && (Mask & (GENERIC_WRITE | FILE_WRITE_DATA | FILE_APPEND_DATA)))
513 fFlags |= O_RDWR;
514 else if (Mask & (GENERIC_READ | FILE_READ_DATA))
515 fFlags |= O_RDONLY;
516 else if (Mask & (GENERIC_WRITE | FILE_WRITE_DATA | FILE_APPEND_DATA))
517 fFlags |= O_WRONLY;
518 else
519 fFlags |= O_RDWR;
520 if ((Mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) == FILE_APPEND_DATA)
521 fFlags |= O_APPEND;
522 }
523 else
524 fFlags = O_RDWR;
525 }
526
527 if (paf[i] & FPIPE)
528 fFlags2 = SHFILE_FLAGS_PIPE;
529 else if (paf[i] & FDEV)
530 fFlags2 = SHFILE_FLAGS_TTY;
531 else
532 fFlags2 = 0;
533
534 fd2 = shfile_insert(pfdtab, (intptr_t)h, fFlags, fFlags2, i, "shtab_init");
535 assert(fd2 == i); (void)fd2;
536 if (fd2 != i)
537 rc = -1;
538 }
539 }
540 }
541
542 /* Check the three standard handles. */
543 for (i = 0; i < 3; i++)
544 if ( (unsigned)i >= pfdtab->size
545 || pfdtab->tab[i].fd == -1)
546 {
547 HANDLE hFile = GetStdHandle(aStdHandles[i].dwStdHandle);
548 if (hFile != INVALID_HANDLE_VALUE)
549 {
550 DWORD dwType = GetFileType(hFile);
551 unsigned fFlags = aStdHandles[i].fFlags;
552 unsigned fFlags2;
553 int fd2;
554 if (dwType == FILE_TYPE_CHAR)
555 fFlags2 = SHFILE_FLAGS_TTY;
556 else if (dwType == FILE_TYPE_PIPE)
557 fFlags2 = SHFILE_FLAGS_PIPE;
558 else
559 fFlags2 = SHFILE_FLAGS_FILE;
560 fd2 = shfile_insert(pfdtab, (intptr_t)hFile, fFlags, fFlags2, i, "shtab_init");
561 assert(fd2 == i); (void)fd2;
562 if (fd2 != i)
563 rc = -1;
564 }
565 }
566# else
567# endif
568 }
569 else
570 {
571 /** @todo */
572 errno = ENOSYS;
573 rc = -1;
574 }
575 }
576 else
577 rc = -1;
578#endif
579 }
580 return rc;
581}
582
583#if K_OS == K_OS_WINDOWS && defined(SHFILE_IN_USE)
584
585/**
586 * Helper for shfork.
587 *
588 * @param pfdtab The file descriptor table.
589 * @param set Whether to make all handles inheritable (1) or
590 * to restore them to the rigth state (0).
591 * @param hndls Where to store the three standard handles.
592 */
593void shfile_fork_win(shfdtab *pfdtab, int set, intptr_t *hndls)
594{
595 shmtxtmp tmp;
596 unsigned i;
597 DWORD fFlag = set ? HANDLE_FLAG_INHERIT : 0;
598
599 shmtx_enter(&pfdtab->mtx, &tmp);
600 TRACE2((NULL, "shfile_fork_win: set=%d\n", set));
601
602 i = pfdtab->size;
603 while (i-- > 0)
604 {
605 if (pfdtab->tab[i].fd == i)
606 {
607 HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
608 if (set)
609 TRACE2((NULL, " #%d: native=%#x oflags=%#x shflags=%#x\n",
610 i, hFile, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
611 if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, fFlag))
612 {
613 DWORD err = GetLastError();
614 assert(0);
615 }
616 }
617 }
618
619 if (hndls)
620 {
621 for (i = 0; i < 3; i++)
622 {
623 if ( pfdtab->size > i
624 && pfdtab->tab[i].fd == i)
625 hndls[i] = pfdtab->tab[i].native;
626 else
627 hndls[i] = (intptr_t)INVALID_HANDLE_VALUE;
628 TRACE2((NULL, "shfile_fork_win: i=%d size=%d fd=%d native=%d hndls[%d]=%p\n",
629 i, pfdtab->size, pfdtab->tab[i].fd, pfdtab->tab[i].native, i, hndls[i]));
630 }
631 }
632
633 shmtx_leave(&pfdtab->mtx, &tmp);
634}
635
636/**
637 * Helper for sh_execve.
638 *
639 * This is called before and after CreateProcess. On the first call it
640 * will mark the non-close-on-exec handles as inheritable and produce
641 * the startup info for the CRT. On the second call, after CreateProcess,
642 * it will restore the handle inheritability properties.
643 *
644 * @returns Pointer to CRT data if prepare is 1, NULL if prepare is 0.
645 * @param pfdtab The file descriptor table.
646 * @param prepare Which call, 1 if before and 0 if after.
647 * @param sizep Where to store the size of the returned data.
648 * @param hndls Where to store the three standard handles.
649 */
650void *shfile_exec_win(shfdtab *pfdtab, int prepare, unsigned short *sizep, intptr_t *hndls)
651{
652 void *pvRet;
653 shmtxtmp tmp;
654 unsigned count;
655 unsigned i;
656
657 shmtx_enter(&pfdtab->mtx, &tmp);
658 TRACE2((NULL, "shfile_exec_win: prepare=%p\n", prepare));
659
660 count = pfdtab->size < (0x10000-4) / (1 + sizeof(HANDLE))
661 ? pfdtab->size
662 : (0x10000-4) / (1 + sizeof(HANDLE));
663 while (count > 3 && pfdtab->tab[count - 1].fd == -1)
664 count--;
665
666 if (prepare)
667 {
668 size_t cbData = sizeof(int) + count * (1 + sizeof(HANDLE));
669 uint8_t *pbData = sh_malloc(shthread_get_shell(), cbData);
670 uint8_t *paf = pbData + sizeof(int);
671 HANDLE *pah = (HANDLE *)(paf + count);
672
673 *(int *)pbData = count;
674
675 i = count;
676 while (i-- > 0)
677 {
678 if ( pfdtab->tab[i].fd == i
679 && !(pfdtab->tab[i].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
680 {
681 HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
682 TRACE2((NULL, " #%d: native=%#x oflags=%#x shflags=%#x\n",
683 i, hFile, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
684
685 if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
686 {
687 DWORD err = GetLastError();
688 assert(0);
689 }
690 paf[i] = FOPEN;
691 if (pfdtab->tab[i].oflags & _O_APPEND)
692 paf[i] |= FAPPEND;
693 if (pfdtab->tab[i].oflags & _O_TEXT)
694 paf[i] |= FTEXT;
695 switch (pfdtab->tab[i].shflags & SHFILE_FLAGS_TYPE_MASK)
696 {
697 case SHFILE_FLAGS_TTY: paf[i] |= FDEV; break;
698 case SHFILE_FLAGS_PIPE: paf[i] |= FPIPE; break;
699 }
700 pah[i] = hFile;
701 }
702 else
703 {
704 paf[i] = 0;
705 pah[i] = INVALID_HANDLE_VALUE;
706 }
707 }
708
709 for (i = 0; i < 3; i++)
710 {
711 if ( i < count
712 && pfdtab->tab[i].fd == i)
713 hndls[i] = pfdtab->tab[i].native;
714 else
715 hndls[i] = (intptr_t)INVALID_HANDLE_VALUE;
716 TRACE2((NULL, "shfile_exec_win: i=%d count=%d fd=%d native=%d hndls[%d]=\n",
717 i, count, pfdtab->tab[i].fd, pfdtab->tab[i].native, i, hndls[i]));
718 }
719
720 *sizep = (unsigned short)cbData;
721 pvRet = pbData;
722 }
723 else
724 {
725 assert(!hndls);
726 assert(!sizep);
727 i = count;
728 while (i-- > 0)
729 {
730 if ( pfdtab->tab[i].fd == i
731 && !(pfdtab->tab[i].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
732 {
733 HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
734 if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, 0))
735 {
736 DWORD err = GetLastError();
737 assert(0);
738 }
739 }
740 }
741 pvRet = NULL;
742 }
743
744 shmtx_leave(&pfdtab->mtx, &tmp);
745 return pvRet;
746}
747
748#endif /* K_OS_WINDOWS */
749
750
751/**
752 * open().
753 */
754int shfile_open(shfdtab *pfdtab, const char *name, unsigned flags, mode_t mode)
755{
756 int fd;
757#ifdef SHFILE_IN_USE
758 char absname[SHFILE_MAX_PATH];
759# if K_OS == K_OS_WINDOWS
760 HANDLE hFile;
761 DWORD dwDesiredAccess;
762 DWORD dwShareMode;
763 DWORD dwCreationDisposition;
764 DWORD dwFlagsAndAttributes;
765 SECURITY_ATTRIBUTES SecurityAttributes;
766
767# ifndef _O_ACCMODE
768# define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
769# endif
770 switch (flags & (_O_ACCMODE | _O_APPEND))
771 {
772 case _O_RDONLY: dwDesiredAccess = GENERIC_READ; break;
773 case _O_RDONLY | _O_APPEND: dwDesiredAccess = GENERIC_READ; break;
774 case _O_WRONLY: dwDesiredAccess = GENERIC_WRITE; break;
775 case _O_WRONLY | _O_APPEND: dwDesiredAccess = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); break;
776 case _O_RDWR: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; break;
777 case _O_RDWR | _O_APPEND: dwDesiredAccess = GENERIC_READ | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); break;
778
779 default:
780 RETURN_ERROR(-1, EINVAL, "invalid mode");
781 }
782
783 dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
784
785 SecurityAttributes.nLength = sizeof(SecurityAttributes);
786 SecurityAttributes.lpSecurityDescriptor = NULL;
787 SecurityAttributes.bInheritHandle = FALSE;
788
789 if (flags & _O_CREAT)
790 {
791 if ((flags & (_O_EXCL | _O_TRUNC)) == (_O_EXCL | _O_TRUNC))
792 RETURN_ERROR(-1, EINVAL, "_O_EXCL | _O_TRUNC");
793
794 if (flags & _O_TRUNC)
795 dwCreationDisposition = CREATE_ALWAYS; /* not 100%, but close enough */
796 else if (flags & _O_EXCL)
797 dwCreationDisposition = CREATE_NEW;
798 else
799 dwCreationDisposition = OPEN_ALWAYS;
800 }
801 else if (flags & _O_TRUNC)
802 dwCreationDisposition = TRUNCATE_EXISTING;
803 else
804 dwCreationDisposition = OPEN_EXISTING;
805
806 if (!(flags & _O_CREAT) || (mode & 0222))
807 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
808 else
809 dwFlagsAndAttributes = FILE_ATTRIBUTE_READONLY;
810
811 fd = shfile_make_path(pfdtab, name, &absname[0]);
812 if (!fd)
813 {
814 SetLastError(0);
815 hFile = CreateFileA(absname,
816 dwDesiredAccess,
817 dwShareMode,
818 &SecurityAttributes,
819 dwCreationDisposition,
820 dwFlagsAndAttributes,
821 NULL /* hTemplateFile */);
822 if (hFile != INVALID_HANDLE_VALUE)
823 fd = shfile_insert(pfdtab, (intptr_t)hFile, flags, 0, -1, "shfile_open");
824 else
825 fd = shfile_dos2errno(GetLastError());
826 }
827
828# else /* K_OS != K_OS_WINDOWS */
829 fd = shfile_make_path(pfdtab, name, &absname[0]);
830 if (!fd)
831 {
832 fd = open(absname, flag, mode);
833 if (fd != -1)
834 {
835 int native = fcntl(fd, F_DUPFD, SHFILE_UNIX_MIN_FD);
836 int s = errno;
837 close(fd);
838 errno = s;
839 if (native != -1)
840 fd = shfile_insert(pfdtab, native, flags, 0, -1, "shfile_open");
841 else
842 fd = -1;
843 }
844 }
845
846# endif /* K_OS != K_OS_WINDOWS */
847
848#else
849 fd = open(name, flags, mode);
850#endif
851
852 TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
853 return fd;
854}
855
856int shfile_pipe(shfdtab *pfdtab, int fds[2])
857{
858 int rc;
859#ifdef SHFILE_IN_USE
860# if K_OS == K_OS_WINDOWS
861 HANDLE hRead = INVALID_HANDLE_VALUE;
862 HANDLE hWrite = INVALID_HANDLE_VALUE;
863 SECURITY_ATTRIBUTES SecurityAttributes;
864
865 SecurityAttributes.nLength = sizeof(SecurityAttributes);
866 SecurityAttributes.lpSecurityDescriptor = NULL;
867 SecurityAttributes.bInheritHandle = FALSE;
868
869 fds[1] = fds[0] = -1;
870 if (CreatePipe(&hRead, &hWrite, &SecurityAttributes, 4096))
871 {
872 fds[0] = shfile_insert(pfdtab, (intptr_t)hRead, O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
873 if (fds[0] != -1)
874 {
875 fds[1] = shfile_insert(pfdtab, (intptr_t)hWrite, O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
876 if (fds[1] != -1)
877 rc = 0;
878 }
879
880# else
881 int native_fds[2];
882
883 fds[1] = fds[0] = -1;
884 if (!pipe(native_fds))
885 {
886 fds[0] = shfile_insert(pfdtab, native_fds[0], O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
887 if (fds[0] != -1)
888 {
889 fds[1] = shfile_insert(pfdtab, native_fds[1], O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
890 if (fds[1] != -1)
891 rc = 0;
892 }
893# endif
894 if (fds[1] == -1)
895 {
896 int s = errno;
897 if (fds[0] != -1)
898 {
899 shmtxtmp tmp;
900 shmtx_enter(&pfdtab->mtx, &tmp);
901 rc = fds[0];
902 pfdtab->tab[rc].fd = -1;
903 pfdtab->tab[rc].oflags = 0;
904 pfdtab->tab[rc].shflags = 0;
905 pfdtab->tab[rc].native = -1;
906 shmtx_leave(&pfdtab->mtx, &tmp);
907 }
908
909# if K_OS == K_OS_WINDOWS
910 CloseHandle(hRead);
911 CloseHandle(hWrite);
912# else
913 close(native_fds[0]);
914 close(native_fds[1]);
915# endif
916 fds[0] = fds[1] = -1;
917 errno = s;
918 rc = -1;
919 }
920 }
921 else
922 {
923# if K_OS == K_OS_WINDOWS
924 errno = shfile_dos2errno(GetLastError());
925# endif
926 rc = -1;
927 }
928
929#else
930 rc = pipe(fds);
931#endif
932
933 TRACE2((NULL, "shfile_pipe() -> %d{%d,%d} [%d]\n", rc, fds[0], fds[1], errno));
934 return rc;
935}
936
937/**
938 * dup().
939 */
940int shfile_dup(shfdtab *pfdtab, int fd)
941{
942 return shfile_fcntl(pfdtab,fd, F_DUPFD, 0);
943}
944
945/**
946 * close().
947 */
948int shfile_close(shfdtab *pfdtab, unsigned fd)
949{
950 int rc;
951#ifdef SHFILE_IN_USE
952 shmtxtmp tmp;
953 shfile *file = shfile_get(pfdtab, fd, &tmp);
954 if (file)
955 {
956 shfile_native_close(file->native, file->oflags);
957
958 file->fd = -1;
959 file->oflags = 0;
960 file->shflags = 0;
961 file->native = -1;
962
963 shfile_put(pfdtab, file, &tmp);
964 rc = 0;
965 }
966 else
967 rc = -1;
968
969#else
970 rc = close(fd);
971#endif
972
973 TRACE2((NULL, "shfile_close(%d) -> %d [%d]\n", fd, rc, errno));
974 return rc;
975}
976
977/**
978 * read().
979 */
980long shfile_read(shfdtab *pfdtab, int fd, void *buf, size_t len)
981{
982 long rc;
983#ifdef SHFILE_IN_USE
984 shmtxtmp tmp;
985 shfile *file = shfile_get(pfdtab, fd, &tmp);
986 if (file)
987 {
988# if K_OS == K_OS_WINDOWS
989 DWORD dwRead = 0;
990 if (ReadFile((HANDLE)file->native, buf, (DWORD)len, &dwRead, NULL))
991 rc = dwRead;
992 else
993 rc = shfile_dos2errno(GetLastError());
994# else
995 rc = read(file->native, buf, len);
996# endif
997
998 shfile_put(pfdtab, file, &tmp);
999 }
1000 else
1001 rc = -1;
1002
1003#else
1004 rc = read(fd, buf, len);
1005#endif
1006 return rc;
1007}
1008
1009/**
1010 * write().
1011 */
1012long shfile_write(shfdtab *pfdtab, int fd, const void *buf, size_t len)
1013{
1014 long rc;
1015#ifdef SHFILE_IN_USE
1016 shmtxtmp tmp;
1017 shfile *file = shfile_get(pfdtab, fd, &tmp);
1018 if (file)
1019 {
1020# if K_OS == K_OS_WINDOWS
1021 DWORD dwWritten = 0;
1022 if (WriteFile((HANDLE)file->native, buf, (DWORD)len, &dwWritten, NULL))
1023 rc = dwWritten;
1024 else
1025 rc = shfile_dos2errno(GetLastError());
1026# else
1027 rc = write(file->native, buf, len);
1028# endif
1029
1030 shfile_put(pfdtab, file, &tmp);
1031 }
1032 else
1033 rc = -1;
1034
1035#else
1036 rc = write(fd, buf, len);
1037#endif
1038 return rc;
1039}
1040
1041/**
1042 * lseek().
1043 */
1044long shfile_lseek(shfdtab *pfdtab, int fd, long off, int whench)
1045{
1046 long rc;
1047#ifdef SHFILE_IN_USE
1048 shmtxtmp tmp;
1049 shfile *file = shfile_get(pfdtab, fd, &tmp);
1050 if (file)
1051 {
1052# if K_OS == K_OS_WINDOWS
1053 assert(SEEK_SET == FILE_BEGIN);
1054 assert(SEEK_CUR == FILE_CURRENT);
1055 assert(SEEK_END == FILE_END);
1056 rc = SetFilePointer((HANDLE)file->native, off, NULL, whench);
1057 if (rc == INVALID_SET_FILE_POINTER)
1058 rc = shfile_dos2errno(GetLastError());
1059# else
1060 rc = lseek(file->native, off, whench);
1061# endif
1062
1063 shfile_put(pfdtab, file, &tmp);
1064 }
1065 else
1066 rc = -1;
1067
1068#else
1069 rc = lseek(fd, off, whench);
1070#endif
1071
1072 return rc;
1073}
1074
1075int shfile_fcntl(shfdtab *pfdtab, int fd, int cmd, int arg)
1076{
1077 int rc;
1078#ifdef SHFILE_IN_USE
1079 shmtxtmp tmp;
1080 shfile *file = shfile_get(pfdtab, fd, &tmp);
1081 if (file)
1082 {
1083 switch (cmd)
1084 {
1085 case F_GETFL:
1086 rc = file->oflags;
1087 break;
1088
1089 case F_SETFL:
1090 {
1091 unsigned mask = O_NONBLOCK | O_APPEND | O_BINARY | O_TEXT;
1092# ifdef O_DIRECT
1093 mask |= O_DIRECT;
1094# endif
1095# ifdef O_ASYNC
1096 mask |= O_ASYNC;
1097# endif
1098# ifdef O_SYNC
1099 mask |= O_SYNC;
1100# endif
1101 if ((file->oflags & mask) == (arg & mask))
1102 rc = 0;
1103 else
1104 {
1105# if K_OS == K_OS_WINDOWS
1106 assert(0);
1107 errno = EINVAL;
1108 rc = -1;
1109# else
1110 rc = fcntl(file->native, F_SETFL, arg);
1111 if (rc != -1)
1112 file->flags = (file->flags & ~mask) | (arg & mask);
1113# endif
1114 }
1115 break;
1116 }
1117
1118 case F_DUPFD:
1119 {
1120# if K_OS == K_OS_WINDOWS
1121 HANDLE hNew = INVALID_HANDLE_VALUE;
1122 if (DuplicateHandle(GetCurrentProcess(),
1123 (HANDLE)file->native,
1124 GetCurrentProcess(),
1125 &hNew,
1126 0,
1127 FALSE /* bInheritHandle */,
1128 DUPLICATE_SAME_ACCESS))
1129 rc = shfile_insert(pfdtab, (intptr_t)hNew, file->oflags, file->shflags, arg, "shfile_fcntl");
1130 else
1131 rc = shfile_dos2errno(GetLastError());
1132# else
1133 int nativeNew = fcntl(file->native F_DUPFD, SHFILE_UNIX_MIN_FD);
1134 if (nativeNew != -1)
1135 rc = shfile_insert(pfdtab, nativeNew, file->oflags, file->shflags, arg, "shfile_fcntl");
1136# endif
1137 break;
1138 }
1139
1140 default:
1141 errno = -EINVAL;
1142 rc = -1;
1143 break;
1144 }
1145
1146 shfile_put(pfdtab, file, &tmp);
1147 }
1148 else
1149 rc = -1;
1150
1151#else
1152 rc = fcntl(fd, cmd, arg);
1153#endif
1154
1155 switch (cmd)
1156 {
1157 case F_GETFL: TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno)); break;
1158 case F_SETFL: TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno)); break;
1159 case F_DUPFD: TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno)); break;
1160 default: TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno)); break;
1161 }
1162 return rc;
1163}
1164
1165int shfile_stat(shfdtab *pfdtab, const char *path, struct stat *pst)
1166{
1167#ifdef SHFILE_IN_USE
1168 char abspath[SHFILE_MAX_PATH];
1169 int rc;
1170 rc = shfile_make_path(pfdtab, path, &abspath[0]);
1171 if (!rc)
1172 {
1173# if K_OS == K_OS_WINDOWS
1174 rc = stat(abspath, pst); /** @todo re-implement stat. */
1175# else
1176 rc = stat(abspath, pst);
1177# endif
1178 }
1179 TRACE2((NULL, "shfile_stat(,%s,) -> %d [%d]\n", path, rc, errno));
1180 return rc;
1181#else
1182 return stat(path, pst);
1183#endif
1184}
1185
1186int shfile_lstat(shfdtab *pfdtab, const char *path, struct stat *pst)
1187{
1188 int rc;
1189#ifdef SHFILE_IN_USE
1190 char abspath[SHFILE_MAX_PATH];
1191
1192 rc = shfile_make_path(pfdtab, path, &abspath[0]);
1193 if (!rc)
1194 {
1195# if K_OS == K_OS_WINDOWS
1196 rc = stat(abspath, pst); /** @todo implement lstat. */
1197# else
1198 rc = lstat(abspath, pst);
1199# endif
1200 }
1201#else
1202 rc = stat(path, pst);
1203#endif
1204 TRACE2((NULL, "shfile_stat(,%s,) -> %d [%d]\n", path, rc, errno));
1205 return rc;
1206}
1207
1208/**
1209 * chdir().
1210 */
1211int shfile_chdir(shfdtab *pfdtab, const char *path)
1212{
1213 shinstance *psh = shthread_get_shell();
1214 int rc;
1215#ifdef SHFILE_IN_USE
1216 char abspath[SHFILE_MAX_PATH];
1217
1218 rc = shfile_make_path(pfdtab, path, &abspath[0]);
1219 if (!rc)
1220 {
1221 char *abspath_copy = sh_strdup(psh, abspath);
1222 char *free_me = abspath_copy;
1223 rc = chdir(path);
1224 if (!rc)
1225 {
1226 shmtxtmp tmp;
1227 shmtx_enter(&pfdtab->mtx, &tmp);
1228
1229 free_me = pfdtab->cwd;
1230 pfdtab->cwd = abspath_copy;
1231
1232 shmtx_leave(&pfdtab->mtx, &tmp);
1233 }
1234 sh_free(psh, free_me);
1235 }
1236 else
1237 rc = -1;
1238#else
1239 rc = chdir(path);
1240#endif
1241
1242 TRACE2((psh, "shfile_chdir(,%s) -> %d [%d]\n", path, rc, errno));
1243 return rc;
1244}
1245
1246/**
1247 * getcwd().
1248 */
1249char *shfile_getcwd(shfdtab *pfdtab, char *buf, int size)
1250{
1251 char *ret;
1252#ifdef SHFILE_IN_USE
1253
1254 ret = NULL;
1255 if (buf && !size)
1256 errno = -EINVAL;
1257 else
1258 {
1259 size_t cwd_size;
1260 shmtxtmp tmp;
1261 shmtx_enter(&pfdtab->mtx, &tmp);
1262
1263 cwd_size = strlen(pfdtab->cwd) + 1;
1264 if (buf)
1265 {
1266 if (cwd_size <= (size_t)size)
1267 ret = memcpy(buf, pfdtab->cwd, cwd_size);
1268 else
1269 errno = ERANGE;
1270 }
1271 else
1272 {
1273 if (size < cwd_size)
1274 size = (int)cwd_size;
1275 ret = sh_malloc(shthread_get_shell(), size);
1276 if (ret)
1277 ret = memcpy(ret, pfdtab->cwd, cwd_size);
1278 else
1279 errno = ENOMEM;
1280 }
1281
1282 shmtx_leave(&pfdtab->mtx, &tmp);
1283 }
1284#else
1285 ret = getcwd(buf, size);
1286#endif
1287
1288 TRACE2((NULL, "shfile_getcwd(,%p,%d) -> %s [%d]\n", buf, size, ret, errno));
1289 return ret;
1290}
1291
1292/**
1293 * access().
1294 */
1295int shfile_access(shfdtab *pfdtab, const char *path, int type)
1296{
1297 int rc;
1298#ifdef SHFILE_IN_USE
1299 char abspath[SHFILE_MAX_PATH];
1300
1301 rc = shfile_make_path(pfdtab, path, &abspath[0]);
1302 if (!rc)
1303 {
1304# ifdef _MSC_VER
1305 if (type & X_OK)
1306 type = (type & ~X_OK) | R_OK;
1307# endif
1308 rc = access(abspath, type);
1309 }
1310#else
1311# ifdef _MSC_VER
1312 if (type & X_OK)
1313 type = (type & ~X_OK) | R_OK;
1314# endif
1315 rc = access(path, type);
1316#endif
1317
1318 TRACE2((NULL, "shfile_access(,%s,%#x) -> %d [%d]\n", path, type, rc, errno));
1319 return rc;
1320}
1321
1322/**
1323 * isatty()
1324 */
1325int shfile_isatty(shfdtab *pfdtab, int fd)
1326{
1327 int rc;
1328#ifdef SHFILE_IN_USE
1329 shmtxtmp tmp;
1330 shfile *file = shfile_get(pfdtab, fd, &tmp);
1331 if (file)
1332 {
1333# if K_OS == K_OS_WINDOWS
1334 rc = (file->shflags & SHFILE_FLAGS_TYPE_MASK) == SHFILE_FLAGS_TTY;
1335# else
1336 rc = isatty(file->native);
1337# endif
1338 shfile_put(pfdtab, file, &tmp);
1339 }
1340 else
1341 rc = 0;
1342#else
1343 rc = isatty(fd);
1344#endif
1345
1346 TRACE2((NULL, "isatty(%d) -> %d [%d]\n", fd, rc, errno));
1347 return rc;
1348}
1349
1350/**
1351 * fcntl F_SETFD / FD_CLOEXEC.
1352 */
1353int shfile_cloexec(shfdtab *pfdtab, int fd, int closeit)
1354{
1355 int rc;
1356#ifdef SHFILE_IN_USE
1357 shmtxtmp tmp;
1358 shfile *file = shfile_get(pfdtab, fd, &tmp);
1359 if (file)
1360 {
1361 if (closeit)
1362 file->shflags |= SHFILE_FLAGS_CLOSE_ON_EXEC;
1363 else
1364 file->shflags &= ~SHFILE_FLAGS_CLOSE_ON_EXEC;
1365 shfile_put(pfdtab, file, &tmp);
1366 }
1367 else
1368 rc = -1;
1369#else
1370 rc = fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0)
1371 | (closeit ? FD_CLOEXEC : 0));
1372#endif
1373
1374 TRACE2((NULL, "shfile_cloexec(%d, %d) -> %d [%d]\n", fd, closeit, rc, errno));
1375 return rc;
1376}
1377
1378
1379int shfile_ioctl(shfdtab *pfdtab, int fd, unsigned long request, void *buf)
1380{
1381 int rc;
1382#ifdef SHFILE_IN_USE
1383 shmtxtmp tmp;
1384 shfile *file = shfile_get(pfdtab, fd, &tmp);
1385 if (file)
1386 {
1387# if K_OS == K_OS_WINDOWS
1388 rc = -1;
1389 errno = ENOSYS;
1390# else
1391 rc = ioctl(file->native, request, buf);
1392# endif
1393 shfile_put(pfdtab, file, &tmp);
1394 }
1395 else
1396 rc = -1;
1397#else
1398 rc = ioctl(fd, request, buf);
1399#endif
1400
1401 TRACE2((NULL, "ioctl(%d, %#x, %p) -> %d\n", fd, request, buf, rc));
1402 return rc;
1403}
1404
1405
1406mode_t shfile_get_umask(shfdtab *pfdtab)
1407{
1408 /** @todo */
1409 return 022;
1410}
1411
1412void shfile_set_umask(shfdtab *pfdtab, mode_t mask)
1413{
1414 /** @todo */
1415 (void)mask;
1416}
1417
1418
1419shdir *shfile_opendir(shfdtab *pfdtab, const char *dir)
1420{
1421#ifdef SHFILE_IN_USE
1422 errno = ENOSYS;
1423 return NULL;
1424#else
1425 return (shdir *)opendir(dir);
1426#endif
1427}
1428
1429shdirent *shfile_readdir(struct shdir *pdir)
1430{
1431#ifdef SHFILE_IN_USE
1432 errno = ENOSYS;
1433 return NULL;
1434#else
1435 struct dirent *pde = readdir((DIR *)pdir);
1436 return pde ? (shdirent *)&pde->d_name[0] : NULL;
1437#endif
1438}
1439
1440void shfile_closedir(struct shdir *pdir)
1441{
1442#ifdef SHFILE_IN_USE
1443 errno = ENOSYS;
1444#else
1445 closedir((DIR *)pdir);
1446#endif
1447}
1448
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette