Changeset 8cd8bf6 in mainline for uspace/lib/posix/fcntl.c


Ignore:
Timestamp:
2011-07-08T17:25:53Z (13 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
11809eab
Parents:
f5b2522 (diff), ddc63fd (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge libposix changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/fcntl.c

    rf5b2522 r8cd8bf6  
    3838#include "fcntl.h"
    3939
     40#include "libc/unistd.h"
     41#include "libc/vfs/vfs.h"
     42#include "errno.h"
     43
    4044/**
    41  *
    42  * @param fd
    43  * @param cmd
    44  * @param ...
    45  * @return
     45 * Performs set of operations on the opened files.
     46 *
     47 * @param fd File descriptor of the opened file.
     48 * @param cmd Operation to carry out.
     49 * @return Non-negative on success. Might have special meaning corresponding
     50 *     to the requested operation.
    4651 */
    4752int posix_fcntl(int fd, int cmd, ...)
    4853{
    49         // TODO
    50         not_implemented();
     54        int rc;
     55        int flags;
     56
     57        switch (cmd) {
     58        case F_DUPFD:
     59        case F_DUPFD_CLOEXEC: /* FD_CLOEXEC is not supported. */
     60                /* VFS does not provide means to express constraints on the new
     61                 * file descriptor so the third argument is ignored. */
     62
     63                /* Retrieve node triplet corresponding to the file descriptor. */
     64                /* Empty statement after label. */;
     65                fdi_node_t node;
     66                rc = fd_node(fd, &node);
     67                if (rc != EOK) {
     68                        errno = -rc;
     69                        return -1;
     70                }
     71
     72                /* Reopen the node so the fresh file descriptor is generated. */
     73                int newfd = open_node(&node, 0);
     74                if (newfd < 0) {
     75                        errno = -newfd;
     76                        return -1;
     77                }
     78
     79                /* Associate the newly generated descriptor to the file description
     80                 * of the old file descriptor. Just reopened node will be automatically
     81                 * closed. */
     82                rc = dup2(fd, newfd);
     83                if (rc != EOK) {
     84                        errno = -rc;
     85                        return -1;
     86                }
     87
     88                return newfd;
     89        case F_GETFD:
     90                /* FD_CLOEXEC is not supported. There are no other flags. */
     91                return 0;
     92        case F_SETFD:
     93                /* FD_CLOEXEC is not supported. Ignore arguments and report success. */
     94                return 0;
     95        case F_GETFL:
     96                /* File status flags (i.e. O_APPEND) are currently private to the
     97                 * VFS server so it cannot be easily retrieved. */
     98                flags = 0;
     99                /* File access flags are currently not supported for file descriptors.
     100                 * Provide full access. */
     101                flags |= O_RDWR;
     102                return flags;
     103        case F_SETFL:
     104                /* File access flags are currently not supported for file descriptors.
     105                 * Ignore arguments and report success. */
     106                return 0;
     107        case F_GETOWN:
     108        case F_SETOWN:
     109        case F_GETLK:
     110        case F_SETLK:
     111        case F_SETLKW:
     112                /* Signals (SIGURG) and file locks are not supported. */
     113                errno = ENOTSUP;
     114                return -1;
     115        default:
     116                /* Unknown command */
     117                errno = EINVAL;
     118                return -1;
     119        }
    51120}
    52121
Note: See TracChangeset for help on using the changeset viewer.