- add sources.
[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 #define _GNU_SOURCE
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 #if defined(__native_client__) && defined(__GLIBC__)
11
12 static ssize_t fdopen_read(void *cookie, char *buf, size_t size) {
13   return read((int) cookie, buf, size);
14 }
15
16 static ssize_t fdopen_write(void *cookie, const char *buf, size_t size) {
17   return write((int) cookie, buf, size);
18 }
19
20 static int fdopen_seek(void *cookie, off_t *offset, int whence) {
21   off_t ret = lseek((int) cookie, *offset, whence);
22   if (ret < 0) {
23     return -1;
24   }
25   *offset = ret;
26   return 0;
27 }
28
29 static int fdopen_close(void *cookie) {
30   return close((int) cookie);
31 }
32
33 static cookie_io_functions_t fdopen_functions = {
34   fdopen_read,
35   fdopen_write,
36   fdopen_seek,
37   fdopen_close,
38 };
39
40 /*
41  * TODO(bradnelson): Remove this glibc is fixed.
42  * See: https://code.google.com/p/nativeclient/issues/detail?id=3362
43  * Workaround fdopen in glibc failing due to it vetting the provided file
44  * handles with fcntl which is not currently interceptable.
45  */
46 FILE *fdopen(int fildes, const char *mode) {
47   return fopencookie((void *) fildes, mode, fdopen_functions);
48 }
49
50 #endif  /* defined(__native_client__) && defined(__GLIBC__) */