1169695Skan/* Compare two open file descriptors to see if they refer to the same file. 2169695Skan Copyright (C) 1991 Free Software Foundation, Inc. 3169695Skan 4169695SkanThis file is part of the libiberty library. 5169695SkanLibiberty is free software; you can redistribute it and/or 6169695Skanmodify it under the terms of the GNU Library General Public 7169695SkanLicense as published by the Free Software Foundation; either 8169695Skanversion 2 of the License, or (at your option) any later version. 9169695Skan 10169695SkanLibiberty is distributed in the hope that it will be useful, 11169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 12169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13169695SkanLibrary General Public License for more details. 14169695Skan 15169695SkanYou should have received a copy of the GNU Library General Public 16169695SkanLicense along with libiberty; see the file COPYING.LIB. If 17169695Skannot, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 18169695SkanBoston, MA 02110-1301, USA. */ 19169695Skan 20169695Skan 21169695Skan/* 22169695Skan 23169695Skan@deftypefn Extension int fdmatch (int @var{fd1}, int @var{fd2}) 24169695Skan 25169695SkanCheck to see if two open file descriptors refer to the same file. 26169695SkanThis is useful, for example, when we have an open file descriptor for 27169695Skanan unnamed file, and the name of a file that we believe to correspond 28169695Skanto that fd. This can happen when we are exec'd with an already open 29169695Skanfile (@code{stdout} for example) or from the SVR4 @file{/proc} calls 30169695Skanthat return open file descriptors for mapped address spaces. All we 31169695Skanhave to do is open the file by name and check the two file descriptors 32169695Skanfor a match, which is done by comparing major and minor device numbers 33169695Skanand inode numbers. 34169695Skan 35169695Skan@end deftypefn 36169695Skan 37169695SkanBUGS 38169695Skan 39169695Skan (FIXME: does this work for networks?) 40169695Skan It works for NFS, which assigns a device number to each mount. 41169695Skan 42169695Skan*/ 43169695Skan 44169695Skan#ifdef HAVE_CONFIG_H 45169695Skan#include "config.h" 46169695Skan#endif 47169695Skan#include "ansidecl.h" 48169695Skan#include "libiberty.h" 49169695Skan#include <sys/types.h> 50169695Skan#include <sys/stat.h> 51169695Skan 52169695Skanint fdmatch (int fd1, int fd2) 53169695Skan{ 54169695Skan struct stat sbuf1; 55169695Skan struct stat sbuf2; 56169695Skan 57169695Skan if ((fstat (fd1, &sbuf1) == 0) && 58169695Skan (fstat (fd2, &sbuf2) == 0) && 59169695Skan (sbuf1.st_dev == sbuf2.st_dev) && 60169695Skan (sbuf1.st_ino == sbuf2.st_ino)) 61169695Skan { 62169695Skan return (1); 63169695Skan } 64169695Skan else 65169695Skan { 66169695Skan return (0); 67169695Skan } 68169695Skan} 69