1/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <unistd.h>
18#include <fcntl.h>
19#include <stdarg.h>
20#include <fcntl_portable.h>
21
22
23static inline int x86_change_flags(int flags)
24{
25    int x86flags = flags & O_ACCMODE_PORTABLE;
26    if (flags & O_CREAT_PORTABLE)
27        x86flags |= O_CREAT;
28    if (flags & O_EXCL_PORTABLE)
29        x86flags |= O_EXCL;
30    if (flags & O_NOCTTY_PORTABLE)
31        x86flags |= O_NOCTTY;
32    if (flags & O_TRUNC_PORTABLE)
33        x86flags |= O_TRUNC;
34    if (flags & O_APPEND_PORTABLE)
35        x86flags |= O_APPEND;
36    if (flags & O_NONBLOCK_PORTABLE)
37        x86flags |= O_NONBLOCK;
38    if (flags & O_SYNC_PORTABLE)
39        x86flags |= O_SYNC;
40    if (flags & FASYNC_PORTABLE)
41        x86flags |= FASYNC;
42    if (flags & O_DIRECT_PORTABLE)
43        x86flags |= O_DIRECT;
44    if (flags & O_LARGEFILE_PORTABLE)
45        x86flags |= O_LARGEFILE;
46    if (flags & O_DIRECTORY_PORTABLE)
47        x86flags |= O_DIRECTORY;
48    if (flags & O_NOFOLLOW_PORTABLE)
49        x86flags |= O_NOFOLLOW;
50    if (flags & O_NOATIME_PORTABLE)
51        x86flags |= O_NOATIME;
52    if (flags & O_NDELAY_PORTABLE)
53        x86flags |= O_NDELAY;
54
55    return x86flags;
56}
57
58extern int  __open(const char*, int, int);
59int open_portable(const char *pathname, int flags, ...)
60{
61    mode_t  mode = 0;
62    flags |= O_LARGEFILE;
63
64    if (flags & O_CREAT)
65    {
66        va_list  args;
67
68        va_start(args, flags);
69        mode = (mode_t) va_arg(args, int);
70        va_end(args);
71    }
72
73    return __open(pathname, x86_change_flags(flags), mode);
74}
75