source: mainline/uspace/lib/posix/source/fcntl.c@ 0d0b319

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

Flip error constants to positive values, and update libposix for the change.

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[ad28599]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 */
[4cf8ca6]32/** @file File control.
[ad28599]33 */
34
35#define LIBPOSIX_INTERNAL
[fdf97f6]36#define __POSIX_DEF__(x) posix_##x
[ad28599]37
[9b1503e]38#include "internal/common.h"
[a3da2b2]39#include "posix/fcntl.h"
[ec18957a]40
[4ec6820]41#include "libc/vfs/vfs.h"
[0d0b319]42
43#include <errno.h>
[ad28599]44
45/**
[4ec6820]46 * Performs set of operations on the opened files.
47 *
48 * @param fd File descriptor of the opened file.
49 * @param cmd Operation to carry out.
50 * @return Non-negative on success. Might have special meaning corresponding
51 * to the requested operation.
[ad28599]52 */
53int posix_fcntl(int fd, int cmd, ...)
54{
[4ec6820]55 int flags;
56
57 switch (cmd) {
58 case F_DUPFD:
[f00af83]59 case F_DUPFD_CLOEXEC:
60 /* VFS currently does not provide the functionality to duplicate
61 * opened file descriptor. */
62 // FIXME: implement this once dup() is available
63 errno = ENOTSUP;
64 return -1;
[4ec6820]65 case F_GETFD:
66 /* FD_CLOEXEC is not supported. There are no other flags. */
67 return 0;
68 case F_SETFD:
69 /* FD_CLOEXEC is not supported. Ignore arguments and report success. */
70 return 0;
71 case F_GETFL:
72 /* File status flags (i.e. O_APPEND) are currently private to the
73 * VFS server so it cannot be easily retrieved. */
74 flags = 0;
75 /* File access flags are currently not supported for file descriptors.
76 * Provide full access. */
77 flags |= O_RDWR;
78 return flags;
79 case F_SETFL:
80 /* File access flags are currently not supported for file descriptors.
81 * Ignore arguments and report success. */
82 return 0;
83 case F_GETOWN:
84 case F_SETOWN:
85 case F_GETLK:
86 case F_SETLK:
87 case F_SETLKW:
88 /* Signals (SIGURG) and file locks are not supported. */
89 errno = ENOTSUP;
90 return -1;
91 default:
92 /* Unknown command */
93 errno = EINVAL;
94 return -1;
95 }
[ad28599]96}
97
[ca4c6ec1]98/**
99 * Open, possibly create, a file.
100 *
101 * @param pathname Path to the file.
[b19e892]102 * @param posix_flags Access mode flags.
[ca4c6ec1]103 */
[b19e892]104int posix_open(const char *pathname, int posix_flags, ...)
[ca4c6ec1]105{
[8fe46a0]106 posix_mode_t posix_mode = 0;
[b19e892]107 if (posix_flags & O_CREAT) {
[ca4c6ec1]108 va_list args;
[b19e892]109 va_start(args, posix_flags);
[8fe46a0]110 posix_mode = va_arg(args, posix_mode_t);
[ca4c6ec1]111 va_end(args);
[b19e892]112 (void) posix_mode;
[ca4c6ec1]113 }
114
[b19e892]115 if (((posix_flags & (O_RDONLY | O_WRONLY | O_RDWR)) == 0) ||
116 ((posix_flags & (O_RDONLY | O_WRONLY)) == (O_RDONLY | O_WRONLY)) ||
117 ((posix_flags & (O_RDONLY | O_RDWR)) == (O_RDONLY | O_RDWR)) ||
118 ((posix_flags & (O_WRONLY | O_RDWR)) == (O_WRONLY | O_RDWR))) {
119 errno = EINVAL;
120 return -1;
121 }
122
123 int flags = WALK_REGULAR;
124 if (posix_flags & O_CREAT) {
125 if (posix_flags & O_EXCL)
126 flags |= WALK_MUST_CREATE;
127 else
128 flags |= WALK_MAY_CREATE;
129 }
130
131 int mode =
132 ((posix_flags & O_RDWR) ? MODE_READ | MODE_WRITE : 0) |
133 ((posix_flags & O_RDONLY) ? MODE_READ : 0) |
134 ((posix_flags & O_WRONLY) ? MODE_WRITE : 0) |
135 ((posix_flags & O_APPEND) ? MODE_APPEND : 0);
136
[f77c1c9]137 int file;
[0d0b319]138
139 if (failed(vfs_lookup(pathname, flags, &file)))
[b19e892]140 return -1;
141
[0d0b319]142 if (failed(vfs_open(file, mode))) {
[9c4cf0d]143 vfs_put(file);
[b19e892]144 return -1;
145 }
146
147 if (posix_flags & O_TRUNC) {
148 if (posix_flags & (O_RDWR | O_WRONLY)) {
[0d0b319]149 if (failed(vfs_resize(file, 0))) {
[9c4cf0d]150 vfs_put(file);
[b19e892]151 return -1;
152 }
153 }
154 }
155
156 return file;
[ca4c6ec1]157}
158
[ad28599]159/** @}
160 */
[b19e892]161
Note: See TracBrowser for help on using the repository browser.