source: rtems/cpukit/libcsupport/src/fcntl.c @ 2e8737a

4.104.115
Last change on this file since 2e8737a was 2e8737a, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/15/09 at 09:42:46

Add attribute((unused)) to function arguments.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 *   fcntl() - POSIX 1003.1b 6.5.2 - File Control
3 *
4 *  COPYRIGHT (c) 1989-1999.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#if HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <stdarg.h>
19#include <unistd.h>
20#include <fcntl.h>
21#include <errno.h>
22
23#include <rtems.h>
24#include <rtems/libio_.h>
25
26static int vfcntl(
27  int fd,
28  int cmd,
29  va_list ap
30)
31{
32  rtems_libio_t *iop;
33  rtems_libio_t *diop;
34  int            fd2;
35  int            flags;
36  int            mask;
37  int            ret = 0;
38
39  rtems_libio_check_fd( fd );
40  iop = rtems_libio_iop( fd );
41  rtems_libio_check_is_open(iop);
42
43  /*
44   *  Now process the fcntl().
45   */
46
47  /*
48   *  This switch should contain all the cases from POSIX.
49   */
50
51  switch ( cmd ) {
52    case F_DUPFD:        /* dup */
53      fd2 = va_arg( ap, int );
54      if ( fd2 )
55        diop = rtems_libio_iop( fd2 );
56      else {
57        /* allocate a file control block */
58        diop = rtems_libio_allocate();
59        if ( diop == 0 ) {
60          ret = -1;
61          break;
62        }
63      }
64
65      diop->handlers   = iop->handlers;
66      diop->file_info  = iop->file_info;
67      diop->flags      = iop->flags;
68      diop->pathinfo   = iop->pathinfo;
69      ret = (int) (diop - rtems_libio_iops);
70      break;
71
72    case F_GETFD:        /* get f_flags */
73      ret = ((iop->flags & LIBIO_FLAGS_CLOSE_ON_EXEC) != 0);
74      break;
75
76    case F_SETFD:        /* set f_flags */
77      /*
78       *  Interpret the third argument as the "close on exec()" flag.
79       *  If this argument is 1, then the file descriptor is to be closed
80       *  if a new process is exec()'ed.  Since RTEMS does not support
81       *  processes, then we can ignore this one except to make
82       *  F_GETFD work.
83       */
84
85      if ( va_arg( ap, int ) )
86        iop->flags |= LIBIO_FLAGS_CLOSE_ON_EXEC;
87      else
88        iop->flags &= ~LIBIO_FLAGS_CLOSE_ON_EXEC;
89      break;
90
91    case F_GETFL:        /* more flags (cloexec) */
92      ret = rtems_libio_to_fcntl_flags( iop->flags );
93      break;
94
95    case F_SETFL:
96      flags = rtems_libio_fcntl_flags( va_arg( ap, int ) );
97      mask = LIBIO_FLAGS_NO_DELAY | LIBIO_FLAGS_APPEND;
98
99      /*
100       *  XXX If we are turning on append, should we seek to the end?
101       */
102
103      iop->flags = (iop->flags & ~mask) | (flags & mask);
104      break;
105
106    case F_GETLK:
107      errno = ENOTSUP;
108      ret = -1;
109      break;
110
111    case F_SETLK:
112      errno = ENOTSUP;
113      ret = -1;
114      break;
115
116    case F_SETLKW:
117      errno = ENOTSUP;
118      ret = -1;
119      break;
120
121    case F_SETOWN:       /*  for sockets. */
122      errno = ENOTSUP;
123      ret = -1;
124      break;
125
126    case F_GETOWN:       /*  for sockets. */
127      errno = ENOTSUP;
128      ret = -1;
129      break;
130
131    default:
132      errno = EINVAL;
133      ret = -1;
134      break;
135  }
136
137  /*
138   *  If we got this far successfully, then we give the optional
139   *  filesystem specific handler a chance to process this.
140   */
141
142  if (ret >= 0) {
143    if (iop->handlers->fcntl_h) {
144      int err = (*iop->handlers->fcntl_h)( cmd, iop );
145      if (err) {
146        errno = err;
147        ret = -1;
148      }
149    }
150  }
151  return ret;
152}
153
154int fcntl(
155  int fd,
156  int cmd,
157  ...
158)
159{
160  int            ret;
161  va_list        ap;
162  va_start( ap, cmd );
163  ret = vfcntl(fd,cmd,ap);
164  va_end(ap);
165  return ret;
166}
167
168
169/*
170 *  _fcntl_r
171 *
172 *  This is the Newlib dependent reentrant version of fcntl().
173 */
174
175#if defined(RTEMS_NEWLIB)
176
177#include <reent.h>
178
179int _fcntl_r(
180  struct _reent *ptr __attribute__((unused)),
181  int fd,
182  int cmd,
183  int arg
184)
185{
186  return fcntl( fd, cmd, arg );
187}
188#endif
Note: See TracBrowser for help on using the repository browser.