source: rtems/c/src/lib/libc/open.c @ 72650fcb

4.104.114.84.95
Last change on this file since 72650fcb was 0c40eb3, checked in by Joel Sherrill <joel.sherrill@…>, on 10/19/01 at 17:37:24

2001-10-18 Till Straumann <strauman@…>

  • libc/open.c, libc/close.c: Moved freenode from open to close.
  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 *  open() - POSIX 1003.1 5.3.1 - Open a File
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 <rtems/libio_.h>
19
20#include <unistd.h>
21
22/*
23 *  Returns file descriptor on success or -1 and errno set to one of the
24 *  following:
25 *
26 *    EACCESS  - Seach permission is denied on a component of the path prefix,
27 *               or the file exists and the permissions specified by the
28 *               flags are denied, or the file does not exist and write
29 *               permission is denied for the parent directory of the file
30 *               to be created, or O_TRUNC is specified and write permission
31 *               is denied.
32 *    EEXIST   - O_CREAT and O_EXCL are set and the named file exists.
33 *    EINTR    - The open( operation was interrupted by a signal.
34 *    EINVAL   - This implementation does not support synchronized IO for this
35 *               file.
36 *    EISDIR   - The named file is a directory and the flags argument
37 *               specified write or read/write access.
38 *    EMFILE   - Too many file descriptors are in used by this process.
39 *    ENAMETOOLONG -
40 *               The length of the path exceeds PATH_MAX or a pathname
41 *               component is longer than NAME_MAX while POSIX_NO_TRUNC
42 *               is in effect.
43 *    ENFILE   - Too many files are open in the system.
44 *    ENOENT   - O_CREAT is not set and and the anmed file does not exist,
45 *               or O_CREAT is set and eitehr the path prefix does not exist
46 *               or the path argument points to an empty string.
47 *    ENOSPC   - The directory or file system that would contain the new file
48 *               cannot be extended.
49 *    ENOTDIR  - A component of the path prefix is not a directory.
50 *    ENXIO    - O_NONBLOCK is set, the named file is a FIFO, O_WRONLY is
51 *               set, and no process has the file open for reading.
52 *    EROFS    - The named file resides on a read-only file system and either
53 *               O_WRONLY, O_RDWR, O_CREAT (if the file does not exist), or
54 *               O_TRUNC is set in the flags argument.
55 */
56
57int open(
58  const char   *pathname,
59  int           flags,
60  ...
61)
62{
63  va_list                             ap;
64  int                                 mode;
65  int                                 rc;
66  rtems_libio_t                      *iop = 0;
67  int                                 status;
68  rtems_filesystem_location_info_t    loc;
69  rtems_filesystem_location_info_t   *loc_to_free = NULL;
70  int                                 eval_flags;
71
72
73  /*
74   * Set the Evaluation flags
75   */
76
77  eval_flags = 0;
78  status = flags + 1;
79  if ( ( status & _FREAD ) == _FREAD )
80    eval_flags |= RTEMS_LIBIO_PERMS_READ;
81  if ( ( status & _FWRITE ) == _FWRITE )
82    eval_flags |= RTEMS_LIBIO_PERMS_WRITE;
83
84 
85  va_start(ap, flags);
86
87  mode = va_arg( ap, int );
88
89  /*
90   * NOTE: This comment is OBSOLETE.  The proper way to do this now
91   *       would be to support a magic mounted file system.
92   *
93   *             Additional external I/O handlers would be supported by adding
94   *             code to pick apart the pathname appropriately. The networking
95   *             code does not require changes here since network file
96   *             descriptors are obtained using socket(), not open().
97   */
98
99  /* allocate a file control block */
100  iop = rtems_libio_allocate();
101  if ( iop == 0 ) {
102    rc = ENFILE;
103    goto done;
104  }
105
106  /*
107   *  See if the file exists.
108   */
109
110  status = rtems_filesystem_evaluate_path(
111     pathname, eval_flags, &loc, TRUE );
112
113  if ( status == -1 ) {
114    if ( errno != ENOENT ) {
115      rc = errno;
116      goto done;
117    }
118
119    /* If the file does not exist and we are not trying to create it--> error */
120    if ( !(flags & O_CREAT) ) {
121      rc = ENOENT;
122      goto done;
123    }
124
125    /* Create the node for the new regular file */
126    rc = mknod( pathname, S_IFREG | mode, 0LL );
127    if ( rc ) {
128      rc = errno;
129      goto done;
130    }
131
132    /* Sanity check to see if the file name exists after the mknod() */
133    status = rtems_filesystem_evaluate_path( pathname, 0x0, &loc, TRUE );
134    if ( status != 0 ) {   /* The file did not exist */
135      rc = EACCES;
136      goto done;
137    }
138
139  } else if ((flags & (O_EXCL|O_CREAT)) == (O_EXCL|O_CREAT)) {
140    /* We were trying to create a file that already exists */
141    rc = EEXIST;
142    loc_to_free = &loc;
143    goto done;
144  }
145
146  loc_to_free = &loc;
147
148  /*
149   *  Fill in the file control block based on the loc structure
150   *  returned by successful path evaluation.
151   */
152
153  iop->handlers   = loc.handlers;
154  iop->file_info  = loc.node_access;
155  iop->flags     |= rtems_libio_fcntl_flags( flags );
156  iop->pathinfo   = loc;
157
158  if ( !iop->handlers->open_h ) {
159    rc = ENOTSUP;
160    goto done;
161  }
162
163  rc = (*iop->handlers->open_h)( iop, pathname, flags, mode );
164  if ( rc )
165    goto done;
166
167  /*
168   *  Optionally truncate the file.
169   */
170
171  if ( (flags & O_TRUNC) == O_TRUNC ) {
172    rc = ftruncate( iop - rtems_libio_iops, 0 );
173  }
174   
175  /*
176   *  Single exit and clean up path.
177   */
178
179done:
180  va_end(ap);
181
182  if ( rc ) {
183    if ( iop )
184      rtems_libio_free( iop );
185    if ( loc_to_free )
186      rtems_filesystem_freenode( loc_to_free );
187    set_errno_and_return_minus_one( rc );
188  }
189
190  return iop - rtems_libio_iops;
191}
192
193/*
194 *  _open_r
195 *
196 *  This is the Newlib dependent reentrant version of open().
197 */
198
199#if defined(RTEMS_NEWLIB)
200
201#include <reent.h>
202
203int _open_r(
204  struct _reent *ptr,
205  const char    *buf,
206  int            flags,
207  int            mode
208)
209{
210  return open( buf, flags, mode );
211}
212#endif
Note: See TracBrowser for help on using the repository browser.