xref: /netbsd-src/external/gpl3/binutils.old/dist/libiberty/fdmatch.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
116dce513Schristos /* Compare two open file descriptors to see if they refer to the same file.
2*e992f068Schristos    Copyright (C) 1991-2022 Free Software Foundation, Inc.
316dce513Schristos 
416dce513Schristos This file is part of the libiberty library.
516dce513Schristos Libiberty is free software; you can redistribute it and/or
616dce513Schristos modify it under the terms of the GNU Library General Public
716dce513Schristos License as published by the Free Software Foundation; either
816dce513Schristos version 2 of the License, or (at your option) any later version.
916dce513Schristos 
1016dce513Schristos Libiberty is distributed in the hope that it will be useful,
1116dce513Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1216dce513Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1316dce513Schristos Library General Public License for more details.
1416dce513Schristos 
1516dce513Schristos You should have received a copy of the GNU Library General Public
1616dce513Schristos License along with libiberty; see the file COPYING.LIB.  If
1716dce513Schristos not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1816dce513Schristos Boston, MA 02110-1301, USA.  */
1916dce513Schristos 
2016dce513Schristos 
2116dce513Schristos /*
2216dce513Schristos 
2316dce513Schristos @deftypefn Extension int fdmatch (int @var{fd1}, int @var{fd2})
2416dce513Schristos 
2516dce513Schristos Check to see if two open file descriptors refer to the same file.
2616dce513Schristos This is useful, for example, when we have an open file descriptor for
2716dce513Schristos an unnamed file, and the name of a file that we believe to correspond
2816dce513Schristos to that fd.  This can happen when we are exec'd with an already open
2916dce513Schristos file (@code{stdout} for example) or from the SVR4 @file{/proc} calls
3016dce513Schristos that return open file descriptors for mapped address spaces.  All we
3116dce513Schristos have to do is open the file by name and check the two file descriptors
3216dce513Schristos for a match, which is done by comparing major and minor device numbers
3316dce513Schristos and inode numbers.
3416dce513Schristos 
3516dce513Schristos @end deftypefn
3616dce513Schristos 
3716dce513Schristos BUGS
3816dce513Schristos 
3916dce513Schristos 	(FIXME: does this work for networks?)
4016dce513Schristos 	It works for NFS, which assigns a device number to each mount.
4116dce513Schristos 
4216dce513Schristos */
4316dce513Schristos 
4416dce513Schristos #ifdef HAVE_CONFIG_H
4516dce513Schristos #include "config.h"
4616dce513Schristos #endif
4716dce513Schristos #include "ansidecl.h"
4816dce513Schristos #include "libiberty.h"
4916dce513Schristos #include <sys/types.h>
5016dce513Schristos #include <sys/stat.h>
5116dce513Schristos 
fdmatch(int fd1,int fd2)5216dce513Schristos int fdmatch (int fd1, int fd2)
5316dce513Schristos {
5416dce513Schristos   struct stat sbuf1;
5516dce513Schristos   struct stat sbuf2;
5616dce513Schristos 
5716dce513Schristos   if ((fstat (fd1, &sbuf1) == 0) &&
5816dce513Schristos       (fstat (fd2, &sbuf2) == 0) &&
5916dce513Schristos       (sbuf1.st_dev == sbuf2.st_dev) &&
6016dce513Schristos       (sbuf1.st_ino == sbuf2.st_ino))
6116dce513Schristos     {
6216dce513Schristos       return (1);
6316dce513Schristos     }
6416dce513Schristos   else
6516dce513Schristos     {
6616dce513Schristos       return (0);
6716dce513Schristos     }
6816dce513Schristos }
69