Changeset 4ec6820 in mainline


Ignore:
Timestamp:
2011-06-29T23:16:31Z (13 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
87ba48cb
Parents:
8b5fb5e
Message:

Implemented fcntl.

Location:
uspace/lib/posix
Files:
2 edited

Legend:

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

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

    r8b5fb5e r4ec6820  
    3636#define POSIX_FCNTL_H_
    3737
     38#include "sys/types.h"
    3839#include "libc/fcntl.h"
     40
     41/* Mask for file access modes. */
     42#undef O_ACCMODE
     43#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
    3944
    4045/* fcntl commands */
    4146#undef F_DUPFD
     47#undef F_DUPFD_CLOEXEC
    4248#undef F_GETFD
    4349#undef F_SETFD
     
    4652#undef F_GETOWN
    4753#undef F_SETOWN
    48 #define F_DUPFD         0       /* Duplicate file descriptor. */
    49 #define F_GETFD         1       /* Get file descriptor flags. */
    50 #define F_SETFD         2       /* Set file descriptor flags. */
    51 #define F_GETFL         3       /* Get file status flags. */
    52 #define F_SETFL         4       /* Set file status flags. */
    53 #define F_GETOWN        5       /* Get owner. */
    54 #define F_SETOWN        6       /* Set owner. */
     54#undef F_GETLK
     55#undef F_SETLK
     56#undef F_SETLKW
     57#define F_DUPFD            0 /* Duplicate file descriptor. */
     58#define F_DUPFD_CLOEXEC    1 /* Same as F_DUPFD but with FD_CLOEXEC flag set. */
     59#define F_GETFD            2 /* Get file descriptor flags. */
     60#define F_SETFD            3 /* Set file descriptor flags. */
     61#define F_GETFL            4 /* Get file status and access flags. */
     62#define F_SETFL            5 /* Set file status flags. */
     63#define F_GETOWN           6 /* Get socket owner. */
     64#define F_SETOWN           7 /* Set socket owner. */
     65#define F_GETLK            8 /* Get locking information. */
     66#define F_SETLK            9 /* Set locking information. */
     67#define F_SETLKW          10 /* Set locking information; wait if blocked. */
    5568
    5669/* File descriptor flags used with F_GETFD and F_SETFD. */
    5770#undef FD_CLOEXEC
    58 #define FD_CLOEXEC      1       /* Close on exec. */
     71#define FD_CLOEXEC         1 /* Close on exec. */
    5972
    6073extern int posix_fcntl(int fd, int cmd, ...);
Note: See TracChangeset for help on using the changeset viewer.