source: rtems/c/src/lib/libc/fcntl.c @ 5c27c80

4.104.114.84.95
Last change on this file since 5c27c80 was 9c49db4, checked in by Joel Sherrill <joel.sherrill@…>, on 01/08/01 at 18:26:44

2001-01-08 Ralf Corsepius <corsepiu@…>

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