source: mainline/uspace/lib/posix/source/fcntl.c@ 67632f7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 67632f7 was fdf97f6, checked in by Vojtech Horky <vojtechhorky@…>, 12 years ago

Libposix functions are without posix_ prefix

Prior this commit, libposix headers declared all functions as posix_*
and used macros to rename e.g. strncpy to posix_strncpy in all (ported)
sources.

After this change, libposix headers look as normal POSIX compliant headers
(well, almost) and no renaming is done in the source codei (of the ported
applications). Instead, the renaming is done at object files level to
bypass weird problems that are bound to happen if you use macros.

The scheme is following. libposix headers use special macro to declare
the names. When included from outside, the functions have their normal
(standard) names. When included from the libposix sources, posix_ prefix
is added. Thus, when libposix is compiled and linked, it contains the
posix_* naming while compiling of ported software uses the normal
non-prefixed versions. This way the posix_* can use HelenOS libc without
any problem. Before linking, the posix_* prefix is removed from all
symbols and special prefix helenos_libc_ is added to all functions
that exists in our (HelenOS) libc and its name clashes with the POSIX
one.

The following happens, for example, to the open() function that exists in
both libposix and in libc.

  • Headers and sources of libc are left intact.
  • Copy of libc.a is made and to all clashing functions is added the helenos_libc prefix. This library is called libc4posix.a.
  • POSIX_DEF(open)(const char *) is used in libposix headers. This macro expands to plain open when included from the "outside world". But it expands to posix_open when included from libposix sources.
  • Libposix is compiled and linked, containing posix_open() that internally calls open() [the original one from libc].
  • Libposix is transformed - all open() are replaced with prefix variant: helenos_libc_open() and all posix_open() are replaced with open(). The transformed library is stored as libposixaslibc.a

Binutils and PCC are then linked with libc4posix and libposixaslibc
libraries instead of libc and libposix as was done previously.

WARNING: it looks that binutils, PCC and MSIM still works but not all
architectures were tested.

  • Property mode set to 100644
File size: 3.5 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#define LIBPOSIX_INTERNAL
36#define __POSIX_DEF__(x) posix_##x
37
38#include "internal/common.h"
39#include "posix/fcntl.h"
40
41#include "libc/unistd.h"
42#include "libc/vfs/vfs.h"
43#include "posix/errno.h"
44
45/**
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.
52 */
53int posix_fcntl(int fd, int cmd, ...)
54{
55 int flags;
56
57 switch (cmd) {
58 case F_DUPFD:
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;
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 }
96}
97
98/**
99 * Open, possibly create, a file.
100 *
101 * @param pathname Path to the file.
102 * @param flags Access mode flags.
103 */
104int posix_open(const char *pathname, int flags, ...)
105{
106 mode_t mode = 0;
107 if ((flags & O_CREAT) > 0) {
108 va_list args;
109 va_start(args, flags);
110 mode = va_arg(args, mode_t);
111 va_end(args);
112 }
113
114 int rc = open(pathname, flags, mode);
115 if (rc < 0) {
116 errno = -rc;
117 rc = -1;
118 }
119
120 return rc;
121}
122
123/** @}
124 */
Note: See TracBrowser for help on using the repository browser.