source: mainline/uspace/lib/posix/src/fcntl.c@ a35b458

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a35b458 was 7f9df7b9, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Remove unnecessary symbol renaming from libposix.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 * Copyright (c) 2011 Petr Koupy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libposix
30 * @{
31 */
32/** @file File control.
33 */
34
35#include "internal/common.h"
36#include "posix/fcntl.h"
37
38#include "libc/vfs/vfs.h"
39
40#include <errno.h>
41
42/**
43 * Performs set of operations on the opened files.
44 *
45 * @param fd File descriptor of the opened file.
46 * @param cmd Operation to carry out.
47 * @return Non-negative on success. Might have special meaning corresponding
48 * to the requested operation.
49 */
50int fcntl(int fd, int cmd, ...)
51{
52 int flags;
53
54 switch (cmd) {
55 case F_DUPFD:
56 case F_DUPFD_CLOEXEC:
57 /* VFS currently does not provide the functionality to duplicate
58 * opened file descriptor. */
59 // FIXME: implement this once dup() is available
60 errno = ENOTSUP;
61 return -1;
62 case F_GETFD:
63 /* FD_CLOEXEC is not supported. There are no other flags. */
64 return 0;
65 case F_SETFD:
66 /* FD_CLOEXEC is not supported. Ignore arguments and report success. */
67 return 0;
68 case F_GETFL:
69 /* File status flags (i.e. O_APPEND) are currently private to the
70 * VFS server so it cannot be easily retrieved. */
71 flags = 0;
72 /* File access flags are currently not supported for file descriptors.
73 * Provide full access. */
74 flags |= O_RDWR;
75 return flags;
76 case F_SETFL:
77 /* File access flags are currently not supported for file descriptors.
78 * Ignore arguments and report success. */
79 return 0;
80 case F_GETOWN:
81 case F_SETOWN:
82 case F_GETLK:
83 case F_SETLK:
84 case F_SETLKW:
85 /* Signals (SIGURG) and file locks are not supported. */
86 errno = ENOTSUP;
87 return -1;
88 default:
89 /* Unknown command */
90 errno = EINVAL;
91 return -1;
92 }
93}
94
95/**
96 * Open, possibly create, a file.
97 *
98 * @param pathname Path to the file.
99 * @param posix_flags Access mode flags.
100 */
101int open(const char *pathname, int posix_flags, ...)
102{
103 mode_t posix_mode = 0;
104 if (posix_flags & O_CREAT) {
105 va_list args;
106 va_start(args, posix_flags);
107 posix_mode = va_arg(args, mode_t);
108 va_end(args);
109 (void) posix_mode;
110 }
111
112 if (((posix_flags & (O_RDONLY | O_WRONLY | O_RDWR)) == 0) ||
113 ((posix_flags & (O_RDONLY | O_WRONLY)) == (O_RDONLY | O_WRONLY)) ||
114 ((posix_flags & (O_RDONLY | O_RDWR)) == (O_RDONLY | O_RDWR)) ||
115 ((posix_flags & (O_WRONLY | O_RDWR)) == (O_WRONLY | O_RDWR))) {
116 errno = EINVAL;
117 return -1;
118 }
119
120 int flags = WALK_REGULAR;
121 if (posix_flags & O_CREAT) {
122 if (posix_flags & O_EXCL)
123 flags |= WALK_MUST_CREATE;
124 else
125 flags |= WALK_MAY_CREATE;
126 }
127
128 int mode =
129 ((posix_flags & O_RDWR) ? MODE_READ | MODE_WRITE : 0) |
130 ((posix_flags & O_RDONLY) ? MODE_READ : 0) |
131 ((posix_flags & O_WRONLY) ? MODE_WRITE : 0) |
132 ((posix_flags & O_APPEND) ? MODE_APPEND : 0);
133
134 int file;
135
136 if (failed(vfs_lookup(pathname, flags, &file)))
137 return -1;
138
139 if (failed(vfs_open(file, mode))) {
140 vfs_put(file);
141 return -1;
142 }
143
144 if (posix_flags & O_TRUNC) {
145 if (posix_flags & (O_RDWR | O_WRONLY)) {
146 if (failed(vfs_resize(file, 0))) {
147 vfs_put(file);
148 return -1;
149 }
150 }
151 }
152
153 return file;
154}
155
156/** @}
157 */
158
Note: See TracBrowser for help on using the repository browser.