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