Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / libraries / nacl_io / syscalls / fdopen.c
1 /* Copyright (c) 2013 The Chromium Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file. */
4
5 #ifndef _GNU_SOURCE
6 #define _GNU_SOURCE
7 #endif
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 #if defined(__native_client__) && defined(__GLIBC__)
13
14 static ssize_t fdopen_read(void *cookie, char *buf, size_t size) {
15   return read((int) cookie, buf, size);
16 }
17
18 static ssize_t fdopen_write(void *cookie, const char *buf, size_t size) {
19   return write((int) cookie, buf, size);
20 }
21
22 static int fdopen_seek(void *cookie, off_t *offset, int whence) {
23   off_t ret = lseek((int) cookie, *offset, whence);
24   if (ret < 0) {
25     return -1;
26   }
27   *offset = ret;
28   return 0;
29 }
30
31 static int fdopen_close(void *cookie) {
32   return close((int) cookie);
33 }
34
35 static cookie_io_functions_t fdopen_functions = {
36   fdopen_read,
37   fdopen_write,
38   fdopen_seek,
39   fdopen_close,
40 };
41
42 /*
43  * TODO(bradnelson): Remove this glibc is fixed.
44  * See: https://code.google.com/p/nativeclient/issues/detail?id=3362
45  * Workaround fdopen in glibc failing due to it vetting the provided file
46  * handles with fcntl which is not currently interceptable.
47  */
48 FILE *fdopen(int fildes, const char *mode) {
49   return fopencookie((void *) fildes, mode, fdopen_functions);
50 }
51
52 #endif  /* defined(__native_client__) && defined(__GLIBC__) */