00001 /* Compare two open file descriptors to see if they refer to the same file.00002 Copyright (C) 1991 Free Software Foundation, Inc.00003 00004 This file is part of the libiberty library.00005 Libiberty is free software; you can redistribute it and/or00006 modify it under the terms of the GNU Library General Public00007 License as published by the Free Software Foundation; either00008 version 2 of the License, or (at your option) any later version.00009 00010 Libiberty is distributed in the hope that it will be useful,00011 but WITHOUT ANY WARRANTY; without even the implied warranty of00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU00013 Library General Public License for more details.00014 00015 You should have received a copy of the GNU Library General Public00016 License along with libiberty; see the file COPYING.LIB. If00017 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,00018 Boston, MA 02110-1301, USA. */00019
00020
00021 /*00022 00023 @deftypefn Extension int fdmatch (int @var{fd1}, int @var{fd2})00024 00025 Check to see if two open file descriptors refer to the same file.00026 This is useful, for example, when we have an open file descriptor for00027 an unnamed file, and the name of a file that we believe to correspond00028 to that fd. This can happen when we are exec'd with an already open00029 file (@code{stdout} for example) or from the SVR4 @file{/proc} calls00030 that return open file descriptors for mapped address spaces. All we00031 have to do is open the file by name and check the two file descriptors00032 for a match, which is done by comparing major and minor device numbers00033 and inode numbers.00034 00035 @end deftypefn00036 00037 BUGS00038 00039 (FIXME: does this work for networks?)00040 It works for NFS, which assigns a device number to each mount.00041 00042 */00043
00044 #ifdef HAVE_CONFIG_H00045 #include "config.h"00046 #endif00047 #include "ansidecl.h"00048 #include "libiberty.h"00049 #include <sys/types.h>00050 #include <sys/stat.h>00051
00052intfdmatch (int fd1, intfd2)
00053 {
00054 struct stat sbuf1;
00055 struct stat sbuf2;
00056
00057 if ((fstat (fd1, &sbuf1) == 0) &&
00058 (fstat (fd2, &sbuf2) == 0) &&
00059 (sbuf1.st_dev == sbuf2.st_dev) &&
00060 (sbuf1.st_ino == sbuf2.st_ino))
00061 {
00062 return (1);
00063 }
00064 else00065 {
00066 return (0);
00067 }
00068 }