lstat.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 Lesser 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* written by Jim Meyering */
  14. #include <config.h>
  15. #if !HAVE_LSTAT
  16. /* On systems that lack symlinks, our replacement <sys/stat.h> already
  17. defined lstat as stat, so there is nothing further to do other than
  18. avoid an empty file. */
  19. typedef int dummy;
  20. #else /* HAVE_LSTAT */
  21. /* Get the original definition of lstat. It might be defined as a macro. */
  22. # define __need_system_sys_stat_h
  23. # include <sys/types.h>
  24. # include <sys/stat.h>
  25. # undef __need_system_sys_stat_h
  26. static inline int
  27. orig_lstat (const char *filename, struct stat *buf)
  28. {
  29. return lstat (filename, buf);
  30. }
  31. /* Specification. */
  32. # include <sys/stat.h>
  33. # include <string.h>
  34. # include <errno.h>
  35. /* lstat works differently on Linux and Solaris systems. POSIX (see
  36. `pathname resolution' in the glossary) requires that programs like
  37. `ls' take into consideration the fact that FILE has a trailing slash
  38. when FILE is a symbolic link. On Linux and Solaris 10 systems, the
  39. lstat function already has the desired semantics (in treating
  40. `lstat ("symlink/", sbuf)' just like `lstat ("symlink/.", sbuf)',
  41. but on Solaris 9 and earlier it does not.
  42. If FILE has a trailing slash and specifies a symbolic link,
  43. then use stat() to get more info on the referent of FILE.
  44. If the referent is a non-directory, then set errno to ENOTDIR
  45. and return -1. Otherwise, return stat's result. */
  46. int
  47. rpl_lstat (const char *file, struct stat *sbuf)
  48. {
  49. size_t len;
  50. int lstat_result = orig_lstat (file, sbuf);
  51. if (lstat_result != 0)
  52. return lstat_result;
  53. /* This replacement file can blindly check against '/' rather than
  54. using the ISSLASH macro, because all platforms with '\\' either
  55. lack symlinks (mingw) or have working lstat (cygwin) and thus do
  56. not compile this file. 0 len should have already been filtered
  57. out above, with a failure return of ENOENT. */
  58. len = strlen (file);
  59. if (file[len - 1] != '/' || S_ISDIR (sbuf->st_mode))
  60. return 0;
  61. /* At this point, a trailing slash is only permitted on
  62. symlink-to-dir; but it should have found information on the
  63. directory, not the symlink. Call stat() to get info about the
  64. link's referent. Our replacement stat guarantees valid results,
  65. even if the symlink is not pointing to a directory. */
  66. if (!S_ISLNK (sbuf->st_mode))
  67. {
  68. errno = ENOTDIR;
  69. return -1;
  70. }
  71. return stat (file, sbuf);
  72. }
  73. #endif /* HAVE_LSTAT */