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

Last change on this file was 9b8be79, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

libposix: Change header organization and remove passthrough headers

Posix headers now function like an overlay. The system include directories
are searched after posix directories. The headers don't need to be patched
for export now. libposix files now include headers using bracket notation
instead of quoted notation.

  • 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 <fcntl.h>
37#include <vfs/vfs.h>
38#include <errno.h>
39
40/**
41 * Performs set of operations on the opened files.
42 *
43 * @param fd File descriptor of the opened file.
44 * @param cmd Operation to carry out.
45 * @return Non-negative on success. Might have special meaning corresponding
46 * to the requested operation.
47 */
48int fcntl(int fd, int cmd, ...)
49{
50 int flags;
51
52 switch (cmd) {
53 case F_DUPFD:
54 case F_DUPFD_CLOEXEC:
55 /*
56 * VFS currently does not provide the functionality to duplicate
57 * opened file descriptor.
58 */
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 /*
70 * File status flags (i.e. O_APPEND) are currently private to the
71 * VFS server so it cannot be easily retrieved.
72 */
73 flags = 0;
74 /*
75 * File access flags are currently not supported for file descriptors.
76 * Provide full access.
77 */
78 flags |= O_RDWR;
79 return flags;
80 case F_SETFL:
81 /*
82 * File access flags are currently not supported for file descriptors.
83 * Ignore arguments and report success.
84 */
85 return 0;
86 case F_GETOWN:
87 case F_SETOWN:
88 case F_GETLK:
89 case F_SETLK:
90 case F_SETLKW:
91 /* Signals (SIGURG) and file locks are not supported. */
92 errno = ENOTSUP;
93 return -1;
94 default:
95 /* Unknown command */
96 errno = EINVAL;
97 return -1;
98 }
99}
100
101/**
102 * Open, possibly create, a file.
103 *
104 * @param pathname Path to the file.
105 * @param posix_flags Access mode flags.
106 */
107int open(const char *pathname, int posix_flags, ...)
108{
109 mode_t posix_mode = 0;
110 if (posix_flags & O_CREAT) {
111 va_list args;
112 va_start(args, posix_flags);
113 posix_mode = va_arg(args, mode_t);
114 va_end(args);
115 (void) posix_mode;
116 }
117
118 if (((posix_flags & (O_RDONLY | O_WRONLY | O_RDWR)) == 0) ||
119 ((posix_flags & (O_RDONLY | O_WRONLY)) == (O_RDONLY | O_WRONLY)) ||
120 ((posix_flags & (O_RDONLY | O_RDWR)) == (O_RDONLY | O_RDWR)) ||
121 ((posix_flags & (O_WRONLY | O_RDWR)) == (O_WRONLY | O_RDWR))) {
122 errno = EINVAL;
123 return -1;
124 }
125
126 int flags = WALK_REGULAR;
127 if (posix_flags & O_CREAT) {
128 if (posix_flags & O_EXCL)
129 flags |= WALK_MUST_CREATE;
130 else
131 flags |= WALK_MAY_CREATE;
132 }
133
134 int mode =
135 ((posix_flags & O_RDWR) ? MODE_READ | MODE_WRITE : 0) |
136 ((posix_flags & O_RDONLY) ? MODE_READ : 0) |
137 ((posix_flags & O_WRONLY) ? MODE_WRITE : 0) |
138 ((posix_flags & O_APPEND) ? MODE_APPEND : 0);
139
140 int file;
141
142 if (failed(vfs_lookup(pathname, flags, &file)))
143 return -1;
144
145 if (failed(vfs_open(file, mode))) {
146 vfs_put(file);
147 return -1;
148 }
149
150 if (posix_flags & O_TRUNC) {
151 if (posix_flags & (O_RDWR | O_WRONLY)) {
152 if (failed(vfs_resize(file, 0))) {
153 vfs_put(file);
154 return -1;
155 }
156 }
157 }
158
159 return file;
160}
161
162/** @}
163 */
Note: See TracBrowser for help on using the repository browser.