xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/fdmatch.c (revision 7e120ff03ede3fe64e2c8620c01465d528502ddb)
198b9484cSchristos /* Compare two open file descriptors to see if they refer to the same file.
2*7e120ff0Schristos    Copyright (C) 1991-2024 Free Software Foundation, Inc.
398b9484cSchristos 
498b9484cSchristos This file is part of the libiberty library.
598b9484cSchristos Libiberty is free software; you can redistribute it and/or
698b9484cSchristos modify it under the terms of the GNU Library General Public
798b9484cSchristos License as published by the Free Software Foundation; either
898b9484cSchristos version 2 of the License, or (at your option) any later version.
998b9484cSchristos 
1098b9484cSchristos Libiberty is distributed in the hope that it will be useful,
1198b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1298b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1398b9484cSchristos Library General Public License for more details.
1498b9484cSchristos 
1598b9484cSchristos You should have received a copy of the GNU Library General Public
1698b9484cSchristos License along with libiberty; see the file COPYING.LIB.  If
1798b9484cSchristos not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1898b9484cSchristos Boston, MA 02110-1301, USA.  */
1998b9484cSchristos 
2098b9484cSchristos 
2198b9484cSchristos /*
2298b9484cSchristos 
2398b9484cSchristos @deftypefn Extension int fdmatch (int @var{fd1}, int @var{fd2})
2498b9484cSchristos 
2598b9484cSchristos Check to see if two open file descriptors refer to the same file.
2698b9484cSchristos This is useful, for example, when we have an open file descriptor for
2798b9484cSchristos an unnamed file, and the name of a file that we believe to correspond
2898b9484cSchristos to that fd.  This can happen when we are exec'd with an already open
2998b9484cSchristos file (@code{stdout} for example) or from the SVR4 @file{/proc} calls
3098b9484cSchristos that return open file descriptors for mapped address spaces.  All we
3198b9484cSchristos have to do is open the file by name and check the two file descriptors
3298b9484cSchristos for a match, which is done by comparing major and minor device numbers
3398b9484cSchristos and inode numbers.
3498b9484cSchristos 
3598b9484cSchristos @end deftypefn
3698b9484cSchristos 
3798b9484cSchristos BUGS
3898b9484cSchristos 
3998b9484cSchristos 	(FIXME: does this work for networks?)
4098b9484cSchristos 	It works for NFS, which assigns a device number to each mount.
4198b9484cSchristos 
4298b9484cSchristos */
4398b9484cSchristos 
4498b9484cSchristos #ifdef HAVE_CONFIG_H
4598b9484cSchristos #include "config.h"
4698b9484cSchristos #endif
4798b9484cSchristos #include "ansidecl.h"
4898b9484cSchristos #include "libiberty.h"
4998b9484cSchristos #include <sys/types.h>
5098b9484cSchristos #include <sys/stat.h>
5198b9484cSchristos 
5298b9484cSchristos int fdmatch (int fd1, int fd2)
5398b9484cSchristos {
5498b9484cSchristos   struct stat sbuf1;
5598b9484cSchristos   struct stat sbuf2;
5698b9484cSchristos 
5798b9484cSchristos   if ((fstat (fd1, &sbuf1) == 0) &&
5898b9484cSchristos       (fstat (fd2, &sbuf2) == 0) &&
5998b9484cSchristos       (sbuf1.st_dev == sbuf2.st_dev) &&
6098b9484cSchristos       (sbuf1.st_ino == sbuf2.st_ino))
6198b9484cSchristos     {
6298b9484cSchristos       return (1);
6398b9484cSchristos     }
6498b9484cSchristos   else
6598b9484cSchristos     {
6698b9484cSchristos       return (0);
6798b9484cSchristos     }
6898b9484cSchristos }
69