14fee23f9Smrg /* Compare two open file descriptors to see if they refer to the same file.
2*b1e83836Smrg Copyright (C) 1991-2022 Free Software Foundation, Inc.
34fee23f9Smrg
44fee23f9Smrg This file is part of the libiberty library.
54fee23f9Smrg Libiberty is free software; you can redistribute it and/or
64fee23f9Smrg modify it under the terms of the GNU Library General Public
74fee23f9Smrg License as published by the Free Software Foundation; either
84fee23f9Smrg version 2 of the License, or (at your option) any later version.
94fee23f9Smrg
104fee23f9Smrg Libiberty is distributed in the hope that it will be useful,
114fee23f9Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
124fee23f9Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
134fee23f9Smrg Library General Public License for more details.
144fee23f9Smrg
154fee23f9Smrg You should have received a copy of the GNU Library General Public
164fee23f9Smrg License along with libiberty; see the file COPYING.LIB. If
174fee23f9Smrg not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
184fee23f9Smrg Boston, MA 02110-1301, USA. */
194fee23f9Smrg
204fee23f9Smrg
214fee23f9Smrg /*
224fee23f9Smrg
234fee23f9Smrg @deftypefn Extension int fdmatch (int @var{fd1}, int @var{fd2})
244fee23f9Smrg
254fee23f9Smrg Check to see if two open file descriptors refer to the same file.
264fee23f9Smrg This is useful, for example, when we have an open file descriptor for
274fee23f9Smrg an unnamed file, and the name of a file that we believe to correspond
284fee23f9Smrg to that fd. This can happen when we are exec'd with an already open
294fee23f9Smrg file (@code{stdout} for example) or from the SVR4 @file{/proc} calls
304fee23f9Smrg that return open file descriptors for mapped address spaces. All we
314fee23f9Smrg have to do is open the file by name and check the two file descriptors
324fee23f9Smrg for a match, which is done by comparing major and minor device numbers
334fee23f9Smrg and inode numbers.
344fee23f9Smrg
354fee23f9Smrg @end deftypefn
364fee23f9Smrg
374fee23f9Smrg BUGS
384fee23f9Smrg
394fee23f9Smrg (FIXME: does this work for networks?)
404fee23f9Smrg It works for NFS, which assigns a device number to each mount.
414fee23f9Smrg
424fee23f9Smrg */
434fee23f9Smrg
444fee23f9Smrg #ifdef HAVE_CONFIG_H
454fee23f9Smrg #include "config.h"
464fee23f9Smrg #endif
474fee23f9Smrg #include "ansidecl.h"
484fee23f9Smrg #include "libiberty.h"
494fee23f9Smrg #include <sys/types.h>
504fee23f9Smrg #include <sys/stat.h>
514fee23f9Smrg
fdmatch(int fd1,int fd2)524fee23f9Smrg int fdmatch (int fd1, int fd2)
534fee23f9Smrg {
544fee23f9Smrg struct stat sbuf1;
554fee23f9Smrg struct stat sbuf2;
564fee23f9Smrg
574fee23f9Smrg if ((fstat (fd1, &sbuf1) == 0) &&
584fee23f9Smrg (fstat (fd2, &sbuf2) == 0) &&
594fee23f9Smrg (sbuf1.st_dev == sbuf2.st_dev) &&
604fee23f9Smrg (sbuf1.st_ino == sbuf2.st_ino))
614fee23f9Smrg {
624fee23f9Smrg return (1);
634fee23f9Smrg }
644fee23f9Smrg else
654fee23f9Smrg {
664fee23f9Smrg return (0);
674fee23f9Smrg }
684fee23f9Smrg }
69