source: rtems/c/src/lib/libc/fstat.c @ c0438add

4.104.114.84.95
Last change on this file since c0438add was 2d733c42, checked in by Joel Sherrill <joel.sherrill@…>, on 01/20/99 at 15:48:22

More general fix based on bug report and patch from Ian Lance Taylor
<ian@…> to fix this problem:

There is a small bug in rtems_close in c/src/lib/libc/libio.c. It
does not check whether the file descriptor it is passed is open. This
can cause it to make a null dereference if it is passed a file
descriptor which is in the valid range but which was not opened, or
which was already closed.

  • Property mode set to 100644
File size: 1.8 KB
Line 
1/*
2 *  fstat() - POSIX 1003.1b 5.6.2 - Get File Status
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 <sys/stat.h>
16#include <unistd.h>
17#include <string.h>
18
19#include "libio_.h"
20
21int fstat(
22  int          fd,
23  struct stat *sbuf
24)
25{
26  rtems_libio_t *iop;
27
28  /*
29   *  Check to see if we were passed a valid pointer.
30   */
31
32  if ( !sbuf )
33    set_errno_and_return_minus_one( EFAULT );
34
35  /*
36   *  Zero out the stat structure so the various support
37   *  versions of stat don't have to.
38   */
39
40  memset( sbuf, 0, sizeof(struct stat) );
41
42  /*
43   *  If this file descriptor is mapped to an external set of handlers,
44   *  then pass the request on to them.
45   */
46
47  if (rtems_file_descriptor_type(fd)) {
48    switch (rtems_file_descriptor_type (fd)) {
49      case RTEMS_FILE_DESCRIPTOR_TYPE_FILE:
50        break;
51
52      case RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET:
53#if !defined(__GO32__)
54        sbuf->st_mode = S_IFSOCK;
55        break;
56#endif
57
58      default:
59        set_errno_and_return_minus_one( EBADF );
60    }
61  }
62
63  /*
64   *  Now process the stat() request.
65   */
66
67  iop = rtems_libio_iop( fd );
68  rtems_libio_check_fd( fd );
69  rtems_libio_check_is_open(iop);
70
71  if ( !iop->handlers->fstat )
72    set_errno_and_return_minus_one( ENOTSUP );
73
74  return (*iop->handlers->fstat)( &iop->pathinfo, sbuf );
75}
76
77/*
78 *  _fstat_r
79 *
80 *  This is the Newlib dependent reentrant version of fstat().
81 */
82
83#if defined(RTEMS_NEWLIB)
84
85#include <reent.h>
86
87int _fstat_r(
88  struct _reent *ptr,
89  int            fd,
90  struct stat   *buf
91)
92{
93  return fstat( fd, buf );
94}
95#endif
Note: See TracBrowser for help on using the repository browser.