Subversion Repositories Kolibri OS

Rev

Rev 4874 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4874 Rev 4921
1
/*
1
/*
2
 * Copyright (c) 1990 The Regents of the University of California.
2
 * Copyright (c) 1990 The Regents of the University of California.
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms are permitted
5
 * Redistribution and use in source and binary forms are permitted
6
 * provided that the above copyright notice and this paragraph are
6
 * provided that the above copyright notice and this paragraph are
7
 * duplicated in all such forms and that any documentation,
7
 * duplicated in all such forms and that any documentation,
8
 * advertising materials, and other materials related to such
8
 * advertising materials, and other materials related to such
9
 * distribution and use acknowledge that the software was developed
9
 * distribution and use acknowledge that the software was developed
10
 * by the University of California, Berkeley.  The name of the
10
 * by the University of California, Berkeley.  The name of the
11
 * University may not be used to endorse or promote products derived
11
 * University may not be used to endorse or promote products derived
12
 * from this software without specific prior written permission.
12
 * from this software without specific prior written permission.
13
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
13
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
14
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16
 */
16
 */
17
 
17
 
18
/*
18
/*
19
FUNCTION
19
FUNCTION
20
<>---close a file
20
<>---close a file
21
 
21
 
22
INDEX
22
INDEX
23
	fclose
23
	fclose
24
INDEX
24
INDEX
25
	_fclose_r
25
	_fclose_r
26
 
26
 
27
ANSI_SYNOPSIS
27
ANSI_SYNOPSIS
28
	#include 
28
	#include 
29
	int fclose(FILE *<[fp]>);
29
	int fclose(FILE *<[fp]>);
30
	int _fclose_r(struct _reent *<[reent]>, FILE *<[fp]>);
30
	int _fclose_r(struct _reent *<[reent]>, FILE *<[fp]>);
31
 
31
 
32
TRAD_SYNOPSIS
32
TRAD_SYNOPSIS
33
	#include 
33
	#include 
34
	int fclose(<[fp]>)
34
	int fclose(<[fp]>)
35
	FILE *<[fp]>;
35
	FILE *<[fp]>;
36
 
36
 
37
	int fclose(<[fp]>)
37
	int fclose(<[fp]>)
38
        struct _reent *<[reent]>
38
        struct _reent *<[reent]>
39
	FILE *<[fp]>;
39
	FILE *<[fp]>;
40
 
40
 
41
DESCRIPTION
41
DESCRIPTION
42
If the file or stream identified by <[fp]> is open, <> closes
42
If the file or stream identified by <[fp]> is open, <> closes
43
it, after first ensuring that any pending data is written (by calling
43
it, after first ensuring that any pending data is written (by calling
44
<)>>).
44
<)>>).
45
 
45
 
46
The alternate function <<_fclose_r>> is a reentrant version.
46
The alternate function <<_fclose_r>> is a reentrant version.
47
The extra argument <[reent]> is a pointer to a reentrancy structure.
47
The extra argument <[reent]> is a pointer to a reentrancy structure.
48
 
48
 
49
RETURNS
49
RETURNS
50
<> returns <<0>> if successful (including when <[fp]> is
50
<> returns <<0>> if successful (including when <[fp]> is
51
<> or not an open file); otherwise, it returns <>.
51
<> or not an open file); otherwise, it returns <>.
52
 
52
 
53
PORTABILITY
53
PORTABILITY
54
<> is required by ANSI C.
54
<> is required by ANSI C.
55
 
55
 
56
Required OS subroutines: <>, <>, <>, <>,
56
Required OS subroutines: <>, <>, <>, <>,
57
<>, <>, <>.
57
<>, <>, <>.
58
*/
58
*/
59
 
59
 
60
#include <_ansi.h>
60
#include <_ansi.h>
61
#include 
61
#include 
62
#include 
62
#include 
63
#include 
63
#include 
64
#include 
64
#include 
65
#include "local.h"
65
#include "local.h"
66
 
66
 
67
int
67
int
68
_DEFUN(_fclose_r, (rptr, fp),
68
_DEFUN(_fclose_r, (rptr, fp),
69
      struct _reent *rptr _AND
69
      struct _reent *rptr _AND
70
      register FILE * fp)
70
      register FILE * fp)
71
{
71
{
72
  int r;
72
  int r;
73
 
73
 
74
  if (fp == NULL)
74
  if (fp == NULL)
75
    return (0);			/* on NULL */
75
    return (0);			/* on NULL */
76
 
76
 
77
  CHECK_INIT (rptr, fp);
77
  CHECK_INIT (rptr, fp);
-
 
78
 
-
 
79
  /* We can't use the _newlib_flockfile_XXX macros here due to the
-
 
80
     interlocked locking with the sfp_lock. */
-
 
81
#ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
-
 
82
  int __oldcancel;
-
 
83
  pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &__oldcancel);
78
 
84
#endif
79
  _flockfile (fp);
85
  _flockfile (fp);
80
 
86
 
81
  if (fp->_flags == 0)		/* not open! */
87
  if (fp->_flags == 0)		/* not open! */
82
    {
88
    {
83
      _funlockfile (fp);
89
      _funlockfile (fp);
-
 
90
#ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
-
 
91
      pthread_setcancelstate (__oldcancel, &__oldcancel);
-
 
92
#endif
84
      return (0);
93
      return (0);
85
    }
94
    }
86
  /* Unconditionally flush to allow special handling for seekable read
95
  /* Unconditionally flush to allow special handling for seekable read
87
     files to reposition file to last byte processed as opposed to
96
     files to reposition file to last byte processed as opposed to
88
     last byte read ahead into the buffer.  */
97
     last byte read ahead into the buffer.  */
89
  r = _fflush_r (rptr, fp);
98
  r = _fflush_r (rptr, fp);
90
  if (fp->_close != NULL && fp->_close (rptr, fp->_cookie) < 0)
99
  if (fp->_close != NULL && fp->_close (rptr, fp->_cookie) < 0)
91
    r = EOF;
100
    r = EOF;
92
  if (fp->_flags & __SMBF)
101
  if (fp->_flags & __SMBF)
93
    _free_r (rptr, (char *) fp->_bf._base);
102
    _free_r (rptr, (char *) fp->_bf._base);
94
  if (HASUB (fp))
103
  if (HASUB (fp))
95
    FREEUB (rptr, fp);
104
    FREEUB (rptr, fp);
96
  if (HASLB (fp))
105
  if (HASLB (fp))
97
    FREELB (rptr, fp);
106
    FREELB (rptr, fp);
98
  __sfp_lock_acquire ();
107
  __sfp_lock_acquire ();
99
  fp->_flags = 0;		/* release this FILE for reuse */
108
  fp->_flags = 0;		/* release this FILE for reuse */
100
  _funlockfile (fp);
109
  _funlockfile (fp);
101
#ifndef __SINGLE_THREAD__
110
#ifndef __SINGLE_THREAD__
102
  __lock_close_recursive (fp->_lock);
111
  __lock_close_recursive (fp->_lock);
103
#endif
112
#endif
104
 
113
 
105
  __sfp_lock_release ();
114
  __sfp_lock_release ();
-
 
115
#ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
-
 
116
  pthread_setcancelstate (__oldcancel, &__oldcancel);
-
 
117
#endif
106
 
118
 
107
  return (r);
119
  return (r);
108
}
120
}
109
 
121
 
110
#ifndef _REENT_ONLY
122
#ifndef _REENT_ONLY
111
 
123
 
112
int
124
int
113
_DEFUN(fclose, (fp),
125
_DEFUN(fclose, (fp),
114
       register FILE * fp)
126
       register FILE * fp)
115
{
127
{
116
  return _fclose_r(_REENT, fp);
128
  return _fclose_r(_REENT, fp);
117
}
129
}
118
 
130
 
119
#endif
131
#endif