source: rtems/c/src/exec/libcsupport/src/fcntl.c @ e6babd7

4.104.114.84.95
Last change on this file since e6babd7 was e6babd7, checked in by Joel Sherrill <joel.sherrill@…>, on 10/18/00 at 13:08:22

2000-10-18 Joel Sherrill <joel@…>

  • libc/fcntl.c: Do not require every filesystem to have an fcntl() handler. Most fcntl() operations can be performed with no filesystem support.
  • ChangeLog?: Corrected comment.
  • Property mode set to 100644
File size: 3.2 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.OARcorp.com/rtems/license.html.
10 *
11 *  $Id$
12 */
13
14#include <unistd.h>
15#include <fcntl.h>
16#include <errno.h>
17
18#include <rtems.h>
19#include "libio_.h"
20
21int fcntl(
22  int fd,
23  int cmd,
24  ...
25)
26{
27  va_list        ap;
28  rtems_libio_t *iop;
29  rtems_libio_t *diop;
30  int            fd2;
31  int            flags;
32  int            mask;
33  int            ret = 0;
34 
35  va_start( ap, cmd );
36
37  rtems_libio_check_fd( fd );
38  iop = rtems_libio_iop( fd );
39  rtems_libio_check_is_open(iop);
40
41  /*
42   *  Now process the fcntl().
43   */
44
45  /*
46   *  This switch should contain all the cases from POSIX.
47   */
48
49  switch ( cmd ) {
50    case F_DUPFD:        /* dup */
51      fd2 = va_arg( ap, int );
52      if ( fd2 )
53        diop = rtems_libio_iop( fd2 );
54      else {
55        /* allocate a file control block */
56        diop = rtems_libio_allocate();
57        if ( diop == 0 ) {
58          ret = -1;
59          break;
60        }
61      }
62
63      diop->handlers   = iop->handlers;
64      diop->file_info  = iop->file_info;
65      diop->flags      = iop->flags;
66      diop->pathinfo   = iop->pathinfo;
67      ret = (int) (diop - rtems_libio_iops);
68      break;
69
70    case F_GETFD:        /* get f_flags */
71      ret = ((iop->flags & LIBIO_FLAGS_CLOSE_ON_EXEC) != 0);
72      break;
73
74    case F_SETFD:        /* set f_flags */
75      /*
76       *  Interpret the third argument as the "close on exec()" flag.
77       *  If this argument is 1, then the file descriptor is to be closed
78       *  if a new process is exec()'ed.  Since RTEMS does not support
79       *  processes, then we can ignore this one except to make
80       *  F_GETFD work.
81       */
82
83      if ( va_arg( ap, int ) )
84        iop->flags |= LIBIO_FLAGS_CLOSE_ON_EXEC;
85      else
86        iop->flags &= ~LIBIO_FLAGS_CLOSE_ON_EXEC;
87      break;
88
89    case F_GETFL:        /* more flags (cloexec) */
90      ret = rtems_libio_to_fcntl_flags( iop->flags );
91      break;
92
93    case F_SETFL:
94      flags = rtems_libio_fcntl_flags( va_arg( ap, int ) );
95      mask = LIBIO_FLAGS_NO_DELAY | LIBIO_FLAGS_APPEND;
96
97      /*
98       *  XXX If we are turning on append, should we seek to the end?
99       */
100
101      iop->flags = (iop->flags & ~mask) | (flags & mask);
102      break;
103
104    case F_GETLK:
105      errno = ENOTSUP;
106      ret = -1;
107      break;
108
109    case F_SETLK:
110      errno = ENOTSUP;
111      ret = -1;
112      break;
113
114    case F_SETLKW:
115      errno = ENOTSUP;
116      ret = -1;
117      break;
118
119    case F_SETOWN:       /*  for sockets. */
120      errno = ENOTSUP;
121      ret = -1;
122      break;
123
124    case F_GETOWN:       /*  for sockets. */
125      errno = ENOTSUP;
126      ret = -1;
127      break;
128
129    default:
130      errno = EINVAL;
131      ret = -1;
132      break;
133  }
134
135  /*
136   *  If we got this far successfully, then we give the optional
137   *  filesystem specific handler a chance to process this. 
138   */
139
140  if (ret >= 0) {   
141    if (iop->handlers->fcntl_h) {
142      int err = (*iop->handlers->fcntl_h)( cmd, iop );
143      if (err) {
144        errno = err;
145        ret = -1;
146      }
147    }
148  }
149  return ret;
150}
Note: See TracBrowser for help on using the repository browser.