home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / libg++-2.5.3-src.lha / src / amiga / libg++-2.5.3 / libg++-2.5.3-amiga / libiberty / fdmatch.c < prev    next >
C/C++ Source or Header  |  1992-09-30  |  2KB  |  72 lines

  1. /* Compare two open file descriptors to see if they refer to the same file.
  2.    Copyright (C) 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. Libiberty is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with libiberty; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /*
  22.  
  23. NAME
  24.  
  25.     fdmatch -- see if two file descriptors refer to same file
  26.  
  27. SYNOPSIS
  28.  
  29.     int fdmatch (int fd1, int fd2)
  30.  
  31. DESCRIPTION
  32.  
  33.     Check to see if two open file descriptors refer to the same file.
  34.     This is useful, for example, when we have an open file descriptor
  35.     for an unnamed file, and the name of a file that we believe to 
  36.     correspond to that fd.  This can happen when we are exec'd with
  37.     an already open file (stdout for example) or from the SVR4 /proc
  38.     calls that return open file descriptors for mapped address spaces.
  39.     All we have to do is open the file by name and check the two file
  40.     descriptors for a match, which is done by comparing major&minor
  41.     device numbers and inode numbers.
  42.  
  43. BUGS
  44.  
  45.     (FIXME: does this work for networks?)
  46.     It works for NFS, which assigns a device number to each mount.
  47.  
  48. */
  49.  
  50. #include <sys/types.h>
  51. #include <sys/stat.h>
  52.  
  53. int fdmatch (fd1, fd2)
  54.     int fd1;
  55.     int fd2;
  56. {
  57.   struct stat sbuf1;
  58.   struct stat sbuf2;
  59.  
  60.   if ((fstat (fd1, &sbuf1) == 0) &&
  61.       (fstat (fd2, &sbuf2) == 0) &&
  62.       (sbuf1.st_dev == sbuf2.st_dev) &&
  63.       (sbuf1.st_ino == sbuf2.st_ino))
  64.     {
  65.       return (1);
  66.     }
  67.   else
  68.     {
  69.       return (0);
  70.     }
  71. }
  72.