12a6b7db3Sskrll /* Compare two open file descriptors to see if they refer to the same file.
2*f22f0ef4Schristos    Copyright (C) 1991-2022 Free Software Foundation, Inc.
32a6b7db3Sskrll 
42a6b7db3Sskrll This file is part of the libiberty library.
52a6b7db3Sskrll Libiberty is free software; you can redistribute it and/or
62a6b7db3Sskrll modify it under the terms of the GNU Library General Public
72a6b7db3Sskrll License as published by the Free Software Foundation; either
82a6b7db3Sskrll version 2 of the License, or (at your option) any later version.
92a6b7db3Sskrll 
102a6b7db3Sskrll Libiberty is distributed in the hope that it will be useful,
112a6b7db3Sskrll but WITHOUT ANY WARRANTY; without even the implied warranty of
122a6b7db3Sskrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
132a6b7db3Sskrll Library General Public License for more details.
142a6b7db3Sskrll 
152a6b7db3Sskrll You should have received a copy of the GNU Library General Public
162a6b7db3Sskrll License along with libiberty; see the file COPYING.LIB.  If
172a6b7db3Sskrll not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
182a6b7db3Sskrll Boston, MA 02110-1301, USA.  */
192a6b7db3Sskrll 
202a6b7db3Sskrll 
212a6b7db3Sskrll /*
222a6b7db3Sskrll 
232a6b7db3Sskrll @deftypefn Extension int fdmatch (int @var{fd1}, int @var{fd2})
242a6b7db3Sskrll 
252a6b7db3Sskrll Check to see if two open file descriptors refer to the same file.
262a6b7db3Sskrll This is useful, for example, when we have an open file descriptor for
272a6b7db3Sskrll an unnamed file, and the name of a file that we believe to correspond
282a6b7db3Sskrll to that fd.  This can happen when we are exec'd with an already open
292a6b7db3Sskrll file (@code{stdout} for example) or from the SVR4 @file{/proc} calls
302a6b7db3Sskrll that return open file descriptors for mapped address spaces.  All we
312a6b7db3Sskrll have to do is open the file by name and check the two file descriptors
322a6b7db3Sskrll for a match, which is done by comparing major and minor device numbers
332a6b7db3Sskrll and inode numbers.
342a6b7db3Sskrll 
352a6b7db3Sskrll @end deftypefn
362a6b7db3Sskrll 
372a6b7db3Sskrll BUGS
382a6b7db3Sskrll 
392a6b7db3Sskrll 	(FIXME: does this work for networks?)
402a6b7db3Sskrll 	It works for NFS, which assigns a device number to each mount.
412a6b7db3Sskrll 
422a6b7db3Sskrll */
432a6b7db3Sskrll 
442a6b7db3Sskrll #ifdef HAVE_CONFIG_H
452a6b7db3Sskrll #include "config.h"
462a6b7db3Sskrll #endif
472a6b7db3Sskrll #include "ansidecl.h"
482a6b7db3Sskrll #include "libiberty.h"
492a6b7db3Sskrll #include <sys/types.h>
502a6b7db3Sskrll #include <sys/stat.h>
512a6b7db3Sskrll 
fdmatch(int fd1,int fd2)522a6b7db3Sskrll int fdmatch (int fd1, int fd2)
532a6b7db3Sskrll {
542a6b7db3Sskrll   struct stat sbuf1;
552a6b7db3Sskrll   struct stat sbuf2;
562a6b7db3Sskrll 
572a6b7db3Sskrll   if ((fstat (fd1, &sbuf1) == 0) &&
582a6b7db3Sskrll       (fstat (fd2, &sbuf2) == 0) &&
592a6b7db3Sskrll       (sbuf1.st_dev == sbuf2.st_dev) &&
602a6b7db3Sskrll       (sbuf1.st_ino == sbuf2.st_ino))
612a6b7db3Sskrll     {
622a6b7db3Sskrll       return (1);
632a6b7db3Sskrll     }
642a6b7db3Sskrll   else
652a6b7db3Sskrll     {
662a6b7db3Sskrll       return (0);
672a6b7db3Sskrll     }
682a6b7db3Sskrll }
69