1/* Compare two open file descriptors to see if they refer to the same file.
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
3
4This file is part of the libiberty library.
5Libiberty is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public
7License as published by the Free Software Foundation; either
8version 2 of the License, or (at your option) any later version.
9
10Libiberty is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with libiberty; see the file COPYING.LIB. If
17not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18Boston, MA 02110-1301, USA. */
19
20
21/*
22
23@deftypefn Extension int fdmatch (int @var{fd1}, int @var{fd2})
24
25Check to see if two open file descriptors refer to the same file.
26This is useful, for example, when we have an open file descriptor for
27an unnamed file, and the name of a file that we believe to correspond
28to that fd. This can happen when we are exec'd with an already open
29file (@code{stdout} for example) or from the SVR4 @file{/proc} calls
30that return open file descriptors for mapped address spaces. All we
31have to do is open the file by name and check the two file descriptors
32for a match, which is done by comparing major and minor device numbers
33and inode numbers.
34
35@end deftypefn
36
37BUGS
38
39 (FIXME: does this work for networks?)
40 It works for NFS, which assigns a device number to each mount.
41
42*/
43
44#ifdef HAVE_CONFIG_H
45#include "config.h"
46#endif
47#include "ansidecl.h"
48#include "libiberty.h"
49#include <sys/types.h>
50#include <sys/stat.h>
51
52int fdmatch (int fd1, int fd2)
53{
54 struct stat sbuf1;
55 struct stat sbuf2;
56
57 if ((fstat (fd: fd1, buf: &sbuf1) == 0) &&
58 (fstat (fd: fd2, buf: &sbuf2) == 0) &&
59 (sbuf1.st_dev == sbuf2.st_dev) &&
60 (sbuf1.st_ino == sbuf2.st_ino))
61 {
62 return (1);
63 }
64 else
65 {
66 return (0);
67 }
68}
69

source code of libiberty/fdmatch.c