133965Sjdp/* Compare two open file descriptors to see if they refer to the same file.
233965Sjdp   Copyright (C) 1991 Free Software Foundation, Inc.
333965Sjdp
433965SjdpThis file is part of the libiberty library.
533965SjdpLibiberty is free software; you can redistribute it and/or
633965Sjdpmodify it under the terms of the GNU Library General Public
733965SjdpLicense as published by the Free Software Foundation; either
833965Sjdpversion 2 of the License, or (at your option) any later version.
933965Sjdp
1033965SjdpLibiberty is distributed in the hope that it will be useful,
1133965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of
1233965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1333965SjdpLibrary General Public License for more details.
1433965Sjdp
1533965SjdpYou should have received a copy of the GNU Library General Public
1633965SjdpLicense along with libiberty; see the file COPYING.LIB.  If
17218822Sdimnot, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18218822SdimBoston, MA 02110-1301, USA.  */
1933965Sjdp
2033965Sjdp
2133965Sjdp/*
2233965Sjdp
2389857Sobrien@deftypefn Extension int fdmatch (int @var{fd1}, int @var{fd2})
2433965Sjdp
2589857SobrienCheck to see if two open file descriptors refer to the same file.
2689857SobrienThis is useful, for example, when we have an open file descriptor for
2789857Sobrienan unnamed file, and the name of a file that we believe to correspond
2889857Sobriento that fd.  This can happen when we are exec'd with an already open
2989857Sobrienfile (@code{stdout} for example) or from the SVR4 @file{/proc} calls
3089857Sobrienthat return open file descriptors for mapped address spaces.  All we
3189857Sobrienhave to do is open the file by name and check the two file descriptors
3289857Sobrienfor a match, which is done by comparing major and minor device numbers
3389857Sobrienand inode numbers.
3433965Sjdp
3589857Sobrien@end deftypefn
3633965Sjdp
3733965SjdpBUGS
3833965Sjdp
3933965Sjdp	(FIXME: does this work for networks?)
4033965Sjdp	It works for NFS, which assigns a device number to each mount.
4133965Sjdp
4233965Sjdp*/
4333965Sjdp
44218822Sdim#ifdef HAVE_CONFIG_H
45218822Sdim#include "config.h"
46218822Sdim#endif
4733965Sjdp#include "ansidecl.h"
4833965Sjdp#include "libiberty.h"
4933965Sjdp#include <sys/types.h>
5033965Sjdp#include <sys/stat.h>
5133965Sjdp
52218822Sdimint fdmatch (int fd1, int fd2)
5333965Sjdp{
5433965Sjdp  struct stat sbuf1;
5533965Sjdp  struct stat sbuf2;
5633965Sjdp
5733965Sjdp  if ((fstat (fd1, &sbuf1) == 0) &&
5833965Sjdp      (fstat (fd2, &sbuf2) == 0) &&
5933965Sjdp      (sbuf1.st_dev == sbuf2.st_dev) &&
6033965Sjdp      (sbuf1.st_ino == sbuf2.st_ino))
6133965Sjdp    {
6233965Sjdp      return (1);
6333965Sjdp    }
6433965Sjdp  else
6533965Sjdp    {
6633965Sjdp      return (0);
6733965Sjdp    }
6833965Sjdp}
69