source: rtems/c/src/exec/libcsupport/src/fcntl.c @ 1bb17020

4.104.114.84.9
Last change on this file since 1bb17020 was 1bb17020, checked in by Joel Sherrill <joel.sherrill@…>, on Mar 8, 1999 at 9:38:56 PM

Added support for F_GETFL and F_SETFL.

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 *   fcntl() - POSIX 1003.1b 6.5.2 - File Control
3 *
4 *  COPYRIGHT (c) 1989-1998.
5 *  On-Line Applications Research Corporation (OAR).
6 *  Copyright assigned to U.S. Government, 1994.
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.OARcorp.com/rtems/license.html.
11 *
12 *  $Id$
13 */
14
15#include <unistd.h>
16#include <fcntl.h>
17#include <errno.h>
18
19#include <rtems.h>
20#include "libio_.h"
21
22int fcntl(
23  int fd,
24  int cmd,
25  ...
26)
27{
28  va_list        ap;
29  rtems_libio_t *iop;
30  rtems_libio_t *diop;
31  int            fd2;
32  int            flags;
33 
34  va_start( ap, cmd );
35
36  rtems_libio_check_fd( fd );
37  iop = rtems_libio_iop( fd );
38  rtems_libio_check_is_open(iop);
39
40  /*
41   *  Now process the fcntl().
42   */
43
44  /*
45   *  This switch should contain all the cases from POSIX.
46   */
47
48  switch ( cmd ) {
49    case F_DUPFD:        /* dup */
50      fd2 = va_arg( ap, int );
51      if ( fd2 )
52        diop = rtems_libio_iop( fd2 );
53      else {
54        /* allocate a file control block */
55        diop = rtems_libio_allocate();
56        if ( diop == 0 )
57          return -1;
58      }
59
60      diop->handlers   = iop->handlers;
61      diop->file_info  = iop->file_info;
62      diop->flags      = iop->flags;
63      diop->pathinfo   = iop->pathinfo;
64     
65      return 0;
66
67    case F_GETFD:        /* get f_flags */
68      if ( iop->flags & LIBIO_FLAGS_CLOSE_ON_EXEC )
69        return 1;
70      return 0;
71
72    case F_SETFD:        /* set f_flags */
73      /*
74       *  Interpret the third argument as the "close on exec()" flag.
75       *  If this argument is 1, then the file descriptor is to be closed
76       *  if a new process is exec()'ed.  Since RTEMS does not support
77       *  processes, then we can ignore this one except to make
78       *  F_GETFD work.
79       */
80
81      if ( va_arg( ap, int ) )
82        iop->flags |= LIBIO_FLAGS_CLOSE_ON_EXEC;
83      else
84        iop->flags &= ~LIBIO_FLAGS_CLOSE_ON_EXEC;
85      return 0;
86
87    case F_GETFL:        /* more flags (cloexec) */
88      return rtems_libio_to_fcntl_flags( iop->flags );
89
90    case F_SETFL:
91      flags = rtems_libio_fcntl_flags( va_arg( ap, int ) );
92
93      /*
94       *  XXX Double check this in the POSIX spec.  According to the Linux
95       *  XXX man page, only these flags can be added.
96       */
97
98      flags = (iop->flags & ~(O_APPEND|O_NONBLOCK)) |
99                   (flags & (O_APPEND|O_NONBLOCK));
100
101      /*
102       *  XXX If we are turning on append, should we seek to the end?
103       */
104
105      iop->flags = flags;
106      return 0;
107
108    case F_GETLK:
109      return -1;
110
111    case F_SETLK:
112      return -1;
113
114    case F_SETLKW:
115      return -1;
116
117    case F_SETOWN:       /*  for sockets. */
118      return -1;
119
120    case F_GETOWN:       /*  for sockets. */
121      return -1;
122
123    default:
124      break;
125  }
126  return -1;
127}
Note: See TracBrowser for help on using the repository browser.