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