freopen.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:5k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * Copyright (c) 1993, 1994 Chris Provenzano. 
  4.  * All rights reserved.
  5.  *
  6.  * This code is derived from software contributed to Berkeley by
  7.  * Chris Torek.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  * This product includes software developed by the University of
  20.  * California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. /*static char *sccsid = "from: @(#)freopen.c 5.6 (Berkeley) 2/24/91";*/
  39. static char *rcsid = "$Id$";
  40. #endif /* LIBC_SCCS and not lint */
  41. #include <pthread.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #include <errno.h>
  46. #include <unistd.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include "local.h"
  50. extern pthread_mutex_t __sfp_mutex;
  51. extern pthread_cond_t __sfp_cond;
  52. extern int __sfp_state;
  53. /* 
  54.  * Re-direct an existing, open (probably) file to some other file. 
  55.  * ANSI is written such that the original file gets closed if at
  56.  * all possible, no matter what.
  57.  */
  58. FILE *
  59. freopen(file, mode, fp)
  60. const char *file, *mode;
  61. register FILE *fp;
  62. {
  63. int f, flags, oflags;
  64. FILE *ret;
  65. if ((flags = __sflags(mode, &oflags)) == 0) {
  66. (void) fclose(fp);
  67. return (NULL);
  68. }
  69. __sinit ();
  70. /*
  71.  * There are actually programs that depend on being able to "freopen"
  72.  * descriptors that weren't originally open.  Keep this from breaking.
  73.  * Remember whether the stream was open to begin with, and which file
  74.  * descriptor (if any) was associated with it.  If it was attached to
  75.  * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin)
  76.  * should work.  This is unnecessary if it was not a Unix file.
  77.  */
  78. /* while lock __sfp_mutex, to block out fopen, and other freopen calls */
  79. while (pthread_mutex_lock(&__sfp_mutex) == OK) {
  80. if (ftrylockfile(fp) == OK) {
  81. if (fp->_flags) {
  82. /* flush the stream; ANSI doesn't require this. */
  83. if (fp->_flags & __SWR) 
  84. (void) __sflush(fp);
  85. __sclose(fp);
  86. /*
  87.      * Finish closing fp.  We cannot keep fp->_base:
  88.  * it may be the wrong size.  This loses the effect
  89.  * of any setbuffer calls, but stdio has always done
  90.  * this before.
  91.              * NOTE: We do this even if __ftrylockfilr failed with
  92.  * an error to avoid memory leaks.
  93.      */
  94. if (fp->_flags & __SMBF)
  95. free((char *)fp->_bf._base);
  96. fp->_w = 0;
  97. fp->_r = 0;
  98. fp->_p = NULL;
  99. fp->_bf._base = NULL;
  100. fp->_bf._size = 0;
  101. fp->_lbfsize = 0;
  102. if (HASUB(fp))
  103. FREEUB(fp);
  104. fp->_ub._size = 0;
  105. if (HASLB(fp))
  106. FREELB(fp);
  107. fp->_lb._size = 0;
  108. /* Get a new descriptor to refer to the new file. */
  109. if ((f = open(file, oflags, 0666)) < OK) 
  110. ret = NULL;
  111. /*
  112.  * If reopening something that was open before on a real file, try
  113.    * to maintain the descriptor.  Various C library routines (perror)
  114.    * assume stderr is always fd STDERR_FILENO, even if being 
  115.  * freopen'd.
  116.    */
  117. /* Testing f == fp->_file may no longer be necessary */
  118. if (fp->_file >= 0 && f != fp->_file) {
  119. if (dup2(f, fp->_file) >= OK) {
  120. (void)close(f);
  121. f = fp->_file;
  122. }
  123. }
  124. fp->_flags = flags;
  125. fp->_file = f;
  126. ret = fp;
  127. } else {
  128. /* unlock __sfp_mutex, and try again later */
  129. pthread_mutex_unlock(&__sfp_mutex);
  130. pthread_yield();
  131. continue;
  132. }
  133. /* @@ Yo, Chris!  Between the "break" and "continue" statements
  134.    above, the program will never get here.  What gives?  */
  135. pthread_mutex_unlock(&__sfp_mutex);
  136. funlockfile(fp);
  137. return(ret);
  138. }
  139. (void)fclose(fp);
  140. return(NULL);
  141. }