lstat.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Work around a bug of lstat on some systems
  2. Copyright (C) 1997-2006, 2008-2021 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 <https://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 int
  30. orig_lstat (const char *filename, struct stat *buf)
  31. {
  32. return lstat (filename, buf);
  33. }
  34. /* Specification. */
  35. # ifdef __osf__
  36. /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
  37. eliminates this include because of the preliminary #include <sys/stat.h>
  38. above. */
  39. # include "sys/stat.h"
  40. # else
  41. # include <sys/stat.h>
  42. # endif
  43. # include "stat-time.h"
  44. # include <string.h>
  45. # include <errno.h>
  46. /* lstat works differently on Linux and Solaris systems. POSIX (see
  47. "pathname resolution" in the glossary) requires that programs like
  48. 'ls' take into consideration the fact that FILE has a trailing slash
  49. when FILE is a symbolic link. On Linux and Solaris 10 systems, the
  50. lstat function already has the desired semantics (in treating
  51. 'lstat ("symlink/", sbuf)' just like 'lstat ("symlink/.", sbuf)',
  52. but on Solaris 9 and earlier it does not.
  53. If FILE has a trailing slash and specifies a symbolic link,
  54. then use stat() to get more info on the referent of FILE.
  55. If the referent is a non-directory, then set errno to ENOTDIR
  56. and return -1. Otherwise, return stat's result. */
  57. int
  58. rpl_lstat (const char *file, struct stat *sbuf)
  59. {
  60. int result = orig_lstat (file, sbuf);
  61. /* This replacement file can blindly check against '/' rather than
  62. using the ISSLASH macro, because all platforms with '\\' either
  63. lack symlinks (mingw) or have working lstat (cygwin) and thus do
  64. not compile this file. 0 len should have already been filtered
  65. out above, with a failure return of ENOENT. */
  66. if (result == 0)
  67. {
  68. if (S_ISDIR (sbuf->st_mode) || file[strlen (file) - 1] != '/')
  69. result = stat_time_normalize (result, sbuf);
  70. else
  71. {
  72. /* At this point, a trailing slash is permitted only on
  73. symlink-to-dir; but it should have found information on the
  74. directory, not the symlink. Call 'stat' to get info about the
  75. link's referent. Our replacement stat guarantees valid results,
  76. even if the symlink is not pointing to a directory. */
  77. if (!S_ISLNK (sbuf->st_mode))
  78. {
  79. errno = ENOTDIR;
  80. return -1;
  81. }
  82. result = stat (file, sbuf);
  83. }
  84. }
  85. return result;
  86. }
  87. #endif /* HAVE_LSTAT */