Subversion Repositories Kolibri OS

Rev

Rev 4874 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. /*
  19. FUNCTION
  20. <<fdopen>>---turn open file into a stream
  21.  
  22. INDEX
  23.         fdopen
  24. INDEX
  25.         _fdopen_r
  26.  
  27. ANSI_SYNOPSIS
  28.         #include <stdio.h>
  29.         FILE *fdopen(int <[fd]>, const char *<[mode]>);
  30.         FILE *_fdopen_r(struct _reent *<[reent]>,
  31.                         int <[fd]>, const char *<[mode]>);
  32.  
  33. TRAD_SYNOPSIS
  34.         #include <stdio.h>
  35.         FILE *fdopen(<[fd]>, <[mode]>)
  36.         int <[fd]>;
  37.         char *<[mode]>;
  38.  
  39.         FILE *_fdopen_r(<[reent]>, <[fd]>, <[mode]>)
  40.         struct _reent *<[reent]>;
  41.         int <[fd]>;
  42.         char *<[mode]>);
  43.  
  44. DESCRIPTION
  45. <<fdopen>> produces a file descriptor of type <<FILE *>>, from a
  46. descriptor for an already-open file (returned, for example, by the
  47. system subroutine <<open>> rather than by <<fopen>>).
  48. The <[mode]> argument has the same meanings as in <<fopen>>.
  49.  
  50. RETURNS
  51. File pointer or <<NULL>>, as for <<fopen>>.
  52.  
  53. PORTABILITY
  54. <<fdopen>> is ANSI.
  55. */
  56.  
  57. #include <_ansi.h>
  58. #include <reent.h>
  59. #include <sys/types.h>
  60. #include <sys/fcntl.h>
  61. #include <stdio.h>
  62. #include <errno.h>
  63. #include "local.h"
  64. #include <_syslist.h>
  65.  
  66. FILE *
  67. _DEFUN(_fdopen_r, (ptr, fd, mode),
  68.        struct _reent *ptr _AND
  69.        int fd             _AND
  70.        _CONST char *mode)
  71. {
  72.   register FILE *fp;
  73.   int flags, oflags;
  74. #ifdef HAVE_FCNTL
  75.   int fdflags, fdmode;
  76. #endif
  77.  
  78.   if ((flags = __sflags (ptr, mode, &oflags)) == 0)
  79.     return 0;
  80.  
  81.   /* make sure the mode the user wants is a subset of the actual mode */
  82. #ifdef HAVE_FCNTL
  83.   if ((fdflags = _fcntl_r (ptr, fd, F_GETFL, 0)) < 0)
  84.     return 0;
  85.   fdmode = fdflags & O_ACCMODE;
  86.   if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
  87.     {
  88.       ptr->_errno = EBADF;
  89.       return 0;
  90.     }
  91. #endif
  92.  
  93.   if ((fp = __sfp (ptr)) == 0)
  94.     return 0;
  95.  
  96.   _newlib_flockfile_start (fp);
  97.  
  98.   fp->_flags = flags;
  99.   /* POSIX recommends setting the O_APPEND bit on fd to match append
  100.      streams.  Someone may later clear O_APPEND on fileno(fp), but the
  101.      stream must still remain in append mode.  Rely on __sflags
  102.      setting __SAPP properly.  */
  103. #ifdef HAVE_FCNTL
  104.   if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
  105.     _fcntl_r (ptr, fd, F_SETFL, fdflags | O_APPEND);
  106. #endif
  107.   fp->_file = fd;
  108.   fp->_cookie = (_PTR) fp;
  109.  
  110. #undef _read
  111. #undef _write
  112. #undef _seek
  113. #undef _close
  114.  
  115.   fp->_read = __sread;
  116.   fp->_write = __swrite;
  117.   fp->_seek = __sseek;
  118.   fp->_close = __sclose;
  119.  
  120. #ifdef __SCLE
  121.   /* Explicit given mode results in explicit setting mode on fd */
  122.   if (oflags & O_BINARY)
  123.     setmode (fp->_file, O_BINARY);
  124.   else if (oflags & O_TEXT)
  125.     setmode (fp->_file, O_TEXT);
  126.   if (__stextmode (fp->_file))
  127.     fp->_flags |= __SCLE;
  128. #endif
  129.  
  130.   _newlib_flockfile_end (fp);
  131.   return fp;
  132. }
  133.  
  134. #ifndef _REENT_ONLY
  135.  
  136. FILE *
  137. _DEFUN(fdopen, (fd, mode),
  138.        int fd _AND
  139.        _CONST char *mode)
  140. {
  141.   return _fdopen_r (_REENT, fd, mode);
  142. }
  143.  
  144. #endif
  145.