Back to index

cell-binutils  2.17cvs20070401
fdmatch.c
Go to the documentation of this file.
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/or
00006 modify it under the terms of the GNU Library General Public
00007 License as published by the Free Software Foundation; either
00008 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 of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Library General Public License for more details.
00014 
00015 You should have received a copy of the GNU Library General Public
00016 License along with libiberty; see the file COPYING.LIB.  If
00017 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 for
00027 an unnamed file, and the name of a file that we believe to correspond
00028 to that fd.  This can happen when we are exec'd with an already open
00029 file (@code{stdout} for example) or from the SVR4 @file{/proc} calls
00030 that return open file descriptors for mapped address spaces.  All we
00031 have to do is open the file by name and check the two file descriptors
00032 for a match, which is done by comparing major and minor device numbers
00033 and inode numbers.
00034 
00035 @end deftypefn
00036 
00037 BUGS
00038 
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_H
00045 #include "config.h"
00046 #endif
00047 #include "ansidecl.h"
00048 #include "libiberty.h"
00049 #include <sys/types.h>
00050 #include <sys/stat.h>
00051 
00052 int fdmatch (int fd1, int fd2)
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   else
00065     {
00066       return (0);
00067     }
00068 }