source: rtems/cpukit/libcsupport/src/open.c @ 4af849f

4.115
Last change on this file since 4af849f was 4af849f, checked in by Joel Sherrill <joel.sherrill@…>, on 07/07/10 at 15:55:22

2010-07-07 Joel Sherrill <joel.sherrill@…>

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