OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdio/] [fdopen.c] - Blame information for rev 1773

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1010 ivang
/*
2
FUNCTION
3
<<fdopen>>---turn open file into a stream
4
 
5
INDEX
6
        fdopen
7
INDEX
8
        _fdopen_r
9
 
10
ANSI_SYNOPSIS
11
        #include <stdio.h>
12
        FILE *fdopen(int <[fd]>, const char *<[mode]>);
13
        FILE *_fdopen_r(void *<[reent]>,
14
                     int <[fd]>, const char *<[mode]>);
15
 
16
TRAD_SYNOPSIS
17
        #include <stdio.h>
18
        FILE *fdopen(<[fd]>, <[mode]>)
19
        int <[fd]>;
20
        char *<[mode]>;
21
 
22
        FILE *_fdopen_r(<[reent]>, <[fd]>, <[mode]>)
23
        char *<[reent]>;
24
        int <[fd]>;
25
        char *<[mode]>);
26
 
27
DESCRIPTION
28
<<fdopen>> produces a file descriptor of type <<FILE *>>, from a
29
descriptor for an already-open file (returned, for example, by the
30
system subroutine <<open>> rather than by <<fopen>>).
31
The <[mode]> argument has the same meanings as in <<fopen>>.
32
 
33
RETURNS
34
File pointer or <<NULL>>, as for <<fopen>>.
35
 
36
PORTABILITY
37
<<fdopen>> is ANSI.
38
*/
39
 
40
#include <sys/types.h>
41
#include <sys/fcntl.h>
42
 
43
#include <stdio.h>
44
#include <errno.h>
45
#include "local.h"
46
#include <_syslist.h>
47
 
48
extern int __sflags ();
49
 
50
FILE *
51
_DEFUN (_fdopen_r, (ptr, fd, mode),
52
        struct _reent *ptr _AND
53
        int fd _AND
54
        _CONST char *mode)
55
{
56
  register FILE *fp;
57
  int flags, oflags;
58
#ifdef HAVE_FCNTL
59
  int fdflags, fdmode;
60
#endif
61
 
62
  if ((flags = __sflags (ptr, mode, &oflags)) == 0)
63
    return 0;
64
 
65
  /* make sure the mode the user wants is a subset of the actual mode */
66
#ifdef HAVE_FCNTL
67
  if ((fdflags = _fcntl (fd, F_GETFL, 0)) < 0)
68
    return 0;
69
  fdmode = fdflags & O_ACCMODE;
70
  if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
71
    {
72
      ptr->_errno = EBADF;
73
      return 0;
74
    }
75
#endif
76
 
77
  if ((fp = __sfp (ptr)) == 0)
78
    return 0;
79
  fp->_flags = flags;
80
  /*
81
   * If opened for appending, but underlying descriptor
82
   * does not have O_APPEND bit set, assert __SAPP so that
83
   * __swrite() will lseek to end before each write.
84
   */
85
  if ((oflags & O_APPEND)
86
#ifdef HAVE_FCNTL
87
       && !(fdflags & O_APPEND)
88
#endif
89
      )
90
    fp->_flags |= __SAPP;
91
  fp->_file = fd;
92
  fp->_cookie = (_PTR) fp;
93
 
94
#undef _read
95
#undef _write
96
#undef _seek
97
#undef _close
98
 
99
  fp->_read = __sread;
100
  fp->_write = __swrite;
101
  fp->_seek = __sseek;
102
  fp->_close = __sclose;
103
 
104
#ifdef __SCLE
105
  /* Explicit given mode results in explicit setting mode on fd */
106
  if (oflags & O_BINARY)
107
    setmode(fp->_file, O_BINARY);
108
  else if (oflags & O_TEXT)
109
    setmode(fp->_file, O_TEXT);
110
  if (__stextmode(fp->_file))
111
    fp->_flags |= __SCLE;
112
#endif
113
 
114
  return fp;
115
}
116
 
117
#ifndef _REENT_ONLY
118
 
119
FILE *
120
_DEFUN (fdopen, (fd, mode),
121
        int fd _AND
122
        _CONST char *mode)
123
{
124
  return _fdopen_r (_REENT, fd, mode);
125
}
126
 
127
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.