lstat.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Work around a bug of lstat on some systems
  2. Copyright (C) 1997-2006, 2008-2011 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* written by Jim Meyering */
  14. /* If the user's config.h happens to include <sys/stat.h>, let it include only
  15. the system's <sys/stat.h> here, so that orig_lstat doesn't recurse to
  16. rpl_lstat. */
  17. #define __need_system_sys_stat_h
  18. #include <config.h>
  19. #if !HAVE_LSTAT
  20. /* On systems that lack symlinks, our replacement <sys/stat.h> already
  21. defined lstat as stat, so there is nothing further to do other than
  22. avoid an empty file. */
  23. typedef int dummy;
  24. #else /* HAVE_LSTAT */
  25. /* Get the original definition of lstat. It might be defined as a macro. */
  26. # include <sys/types.h>
  27. # include <sys/stat.h>
  28. # undef __need_system_sys_stat_h
  29. static inline int
  30. orig_lstat (const char *filename, struct stat *buf)
  31. {
  32. return lstat (filename, buf);
  33. }
  34. /* Specification. */
  35. /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
  36. eliminates this include because of the preliminary #include <sys/stat.h>
  37. above. */
  38. # include "sys/stat.h"
  39. # include <string.h>
  40. # include <errno.h>
  41. /* lstat works differently on Linux and Solaris systems. POSIX (see
  42. `pathname resolution' in the glossary) requires that programs like
  43. `ls' take into consideration the fact that FILE has a trailing slash
  44. when FILE is a symbolic link. On Linux and Solaris 10 systems, the
  45. lstat function already has the desired semantics (in treating
  46. `lstat ("symlink/", sbuf)' just like `lstat ("symlink/.", sbuf)',
  47. but on Solaris 9 and earlier it does not.
  48. If FILE has a trailing slash and specifies a symbolic link,
  49. then use stat() to get more info on the referent of FILE.
  50. If the referent is a non-directory, then set errno to ENOTDIR
  51. and return -1. Otherwise, return stat's result. */
  52. int
  53. rpl_lstat (const char *file, struct stat *sbuf)
  54. {
  55. size_t len;
  56. int lstat_result = orig_lstat (file, sbuf);
  57. if (lstat_result != 0)
  58. return lstat_result;
  59. /* This replacement file can blindly check against '/' rather than
  60. using the ISSLASH macro, because all platforms with '\\' either
  61. lack symlinks (mingw) or have working lstat (cygwin) and thus do
  62. not compile this file. 0 len should have already been filtered
  63. out above, with a failure return of ENOENT. */
  64. len = strlen (file);
  65. if (file[len - 1] != '/' || S_ISDIR (sbuf->st_mode))
  66. return 0;
  67. /* At this point, a trailing slash is only permitted on
  68. symlink-to-dir; but it should have found information on the
  69. directory, not the symlink. Call stat() to get info about the
  70. link's referent. Our replacement stat guarantees valid results,
  71. even if the symlink is not pointing to a directory. */
  72. if (!S_ISLNK (sbuf->st_mode))
  73. {
  74. errno = ENOTDIR;
  75. return -1;
  76. }
  77. return stat (file, sbuf);
  78. }
  79. #endif /* HAVE_LSTAT */