fdmatch.c 2.1 KB

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