11debfc3dSmrg /* Compare two open file descriptors to see if they refer to the same file.
2*8feb0f0bSmrg Copyright (C) 1991-2020 Free Software Foundation, Inc.
31debfc3dSmrg
41debfc3dSmrg This file is part of the libiberty library.
51debfc3dSmrg Libiberty is free software; you can redistribute it and/or
61debfc3dSmrg modify it under the terms of the GNU Library General Public
71debfc3dSmrg License as published by the Free Software Foundation; either
81debfc3dSmrg version 2 of the License, or (at your option) any later version.
91debfc3dSmrg
101debfc3dSmrg Libiberty is distributed in the hope that it will be useful,
111debfc3dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
121debfc3dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
131debfc3dSmrg Library General Public License for more details.
141debfc3dSmrg
151debfc3dSmrg You should have received a copy of the GNU Library General Public
161debfc3dSmrg License along with libiberty; see the file COPYING.LIB. If
171debfc3dSmrg not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
181debfc3dSmrg Boston, MA 02110-1301, USA. */
191debfc3dSmrg
201debfc3dSmrg
211debfc3dSmrg /*
221debfc3dSmrg
231debfc3dSmrg @deftypefn Extension int fdmatch (int @var{fd1}, int @var{fd2})
241debfc3dSmrg
251debfc3dSmrg Check to see if two open file descriptors refer to the same file.
261debfc3dSmrg This is useful, for example, when we have an open file descriptor for
271debfc3dSmrg an unnamed file, and the name of a file that we believe to correspond
281debfc3dSmrg to that fd. This can happen when we are exec'd with an already open
291debfc3dSmrg file (@code{stdout} for example) or from the SVR4 @file{/proc} calls
301debfc3dSmrg that return open file descriptors for mapped address spaces. All we
311debfc3dSmrg have to do is open the file by name and check the two file descriptors
321debfc3dSmrg for a match, which is done by comparing major and minor device numbers
331debfc3dSmrg and inode numbers.
341debfc3dSmrg
351debfc3dSmrg @end deftypefn
361debfc3dSmrg
371debfc3dSmrg BUGS
381debfc3dSmrg
391debfc3dSmrg (FIXME: does this work for networks?)
401debfc3dSmrg It works for NFS, which assigns a device number to each mount.
411debfc3dSmrg
421debfc3dSmrg */
431debfc3dSmrg
441debfc3dSmrg #ifdef HAVE_CONFIG_H
451debfc3dSmrg #include "config.h"
461debfc3dSmrg #endif
471debfc3dSmrg #include "ansidecl.h"
481debfc3dSmrg #include "libiberty.h"
491debfc3dSmrg #include <sys/types.h>
501debfc3dSmrg #include <sys/stat.h>
511debfc3dSmrg
fdmatch(int fd1,int fd2)521debfc3dSmrg int fdmatch (int fd1, int fd2)
531debfc3dSmrg {
541debfc3dSmrg struct stat sbuf1;
551debfc3dSmrg struct stat sbuf2;
561debfc3dSmrg
571debfc3dSmrg if ((fstat (fd1, &sbuf1) == 0) &&
581debfc3dSmrg (fstat (fd2, &sbuf2) == 0) &&
591debfc3dSmrg (sbuf1.st_dev == sbuf2.st_dev) &&
601debfc3dSmrg (sbuf1.st_ino == sbuf2.st_ino))
611debfc3dSmrg {
621debfc3dSmrg return (1);
631debfc3dSmrg }
641debfc3dSmrg else
651debfc3dSmrg {
661debfc3dSmrg return (0);
671debfc3dSmrg }
681debfc3dSmrg }
69