source: rtems/cpukit/libcsupport/src/fcntl.c @ 5f2566b

4.104.114.84.95
Last change on this file since 5f2566b was 5f2566b, checked in by Joel Sherrill <joel.sherrill@…>, on 11/02/99 at 16:27:53

Fixed F_SETFL to properly convert between internal
libio flags and external fcntl.h style flags.

  • Property mode set to 100644
File size: 3.1 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  int            mask;
34  int            ret = 0;
35 
36  va_start( ap, cmd );
37
38  rtems_libio_check_fd( fd );
39  iop = rtems_libio_iop( fd );
40  rtems_libio_check_is_open(iop);
41
42  /*
43   *  Now process the fcntl().
44   */
45
46  /*
47   *  This switch should contain all the cases from POSIX.
48   */
49
50  switch ( cmd ) {
51    case F_DUPFD:        /* dup */
52      fd2 = va_arg( ap, int );
53      if ( fd2 )
54        diop = rtems_libio_iop( fd2 );
55      else {
56        /* allocate a file control block */
57        diop = rtems_libio_allocate();
58        if ( diop == 0 ) {
59          ret = -1;
60          break;
61        }
62      }
63
64      diop->handlers   = iop->handlers;
65      diop->file_info  = iop->file_info;
66      diop->flags      = iop->flags;
67      diop->pathinfo   = iop->pathinfo;
68      ret = (int) (diop - rtems_libio_iops);
69      break;
70
71    case F_GETFD:        /* get f_flags */
72      ret = ((iop->flags & LIBIO_FLAGS_CLOSE_ON_EXEC) != 0);
73      break;
74
75    case F_SETFD:        /* set f_flags */
76      /*
77       *  Interpret the third argument as the "close on exec()" flag.
78       *  If this argument is 1, then the file descriptor is to be closed
79       *  if a new process is exec()'ed.  Since RTEMS does not support
80       *  processes, then we can ignore this one except to make
81       *  F_GETFD work.
82       */
83
84      if ( va_arg( ap, int ) )
85        iop->flags |= LIBIO_FLAGS_CLOSE_ON_EXEC;
86      else
87        iop->flags &= ~LIBIO_FLAGS_CLOSE_ON_EXEC;
88      break;
89
90    case F_GETFL:        /* more flags (cloexec) */
91      ret = rtems_libio_to_fcntl_flags( iop->flags );
92      break;
93
94    case F_SETFL:
95      flags = rtems_libio_fcntl_flags( va_arg( ap, int ) );
96      mask = LIBIO_FLAGS_NO_DELAY | LIBIO_FLAGS_APPEND;
97
98      /*
99       *  XXX If we are turning on append, should we seek to the end?
100       */
101
102      iop->flags = (iop->flags & ~mask) | (flags & mask);
103      break;
104
105    case F_GETLK:
106      errno = ENOTSUP;
107      ret = -1;
108      break;
109
110    case F_SETLK:
111      errno = ENOTSUP;
112      ret = -1;
113      break;
114
115    case F_SETLKW:
116      errno = ENOTSUP;
117      ret = -1;
118      break;
119
120    case F_SETOWN:       /*  for sockets. */
121      errno = ENOTSUP;
122      ret = -1;
123      break;
124
125    case F_GETOWN:       /*  for sockets. */
126      errno = ENOTSUP;
127      ret = -1;
128      break;
129
130    default:
131      errno = EINVAL;
132      ret = -1;
133      break;
134  }
135  if ((ret >= 0) && iop->handlers->fcntl) {
136    int err = (*iop->handlers->fcntl)( cmd, iop );
137    if (err) {
138      errno = err;
139      ret = -1;
140    }
141  }
142  return ret;
143}
Note: See TracBrowser for help on using the repository browser.