isdirectory.c 841 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
  6. * Permission has been granted to redistribute this code under the GPL.
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  9. */
  10. #include <sys/stat.h>
  11. #include "libbb.h"
  12. /*
  13. * Return TRUE if a fileName is a directory.
  14. * Nonexistent files return FALSE.
  15. */
  16. int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
  17. {
  18. int status;
  19. struct stat astatBuf;
  20. if (statBuf == NULL) {
  21. /* set from auto stack buffer */
  22. statBuf = &astatBuf;
  23. }
  24. if (followLinks)
  25. status = stat(fileName, statBuf);
  26. else
  27. status = lstat(fileName, statBuf);
  28. if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
  29. status = FALSE;
  30. }
  31. else status = TRUE;
  32. return status;
  33. }