Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / fileno.c
blob4faa6a8433b04558fd2368048e41157a755c6773
1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
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 * and/or 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.
19 FUNCTION
20 <<fileno>>, <<fileno_unlocked>>---return file descriptor associated with stream
22 INDEX
23 fileno
24 INDEX
25 fileno_unlocked
27 SYNOPSIS
28 #include <stdio.h>
29 int fileno(FILE *<[fp]>);
31 #define _BSD_SOURCE
32 #include <stdio.h>
33 int fileno_unlocked(FILE *<[fp]>);
35 DESCRIPTION
36 You can use <<fileno>> to return the file descriptor identified by <[fp]>.
38 <<fileno_unlocked>> is a non-thread-safe version of <<fileno>>.
39 <<fileno_unlocked>> may only safely be used within a scope
40 protected by flockfile() (or ftrylockfile()) and funlockfile(). This
41 function may safely be used in a multi-threaded program if and only
42 if they are called while the invoking thread owns the (FILE *)
43 object, as is the case after a successful call to the flockfile() or
44 ftrylockfile() functions. If threads are disabled, then
45 <<fileno_unlocked>> is equivalent to <<fileno>>.
47 RETURNS
48 <<fileno>> returns a non-negative integer when successful.
49 If <[fp]> is not an open stream, <<fileno>> returns -1.
51 PORTABILITY
52 <<fileno>> is not part of ANSI C.
53 POSIX requires <<fileno>>.
55 <<fileno_unlocked>> is a BSD extension also provided by GNU libc.
57 Supporting OS subroutines required: none.
60 #include <_ansi.h>
61 #include <stdio.h>
62 #include <errno.h>
63 #include "local.h"
65 int
66 fileno (FILE * f)
68 int result;
69 CHECK_INIT (_REENT, f);
70 _newlib_flockfile_start (f);
71 if (f->_flags)
72 result = __sfileno (f);
73 else
75 result = -1;
76 _REENT_ERRNO(_REENT) = EBADF;
78 _newlib_flockfile_end (f);
79 return result;