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