3
0

isdirectory.c 790 B

123456789101112131415161718192021222324252627282930313233343536
  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 fileName is a directory.
  14. * Nonexistent files return FALSE.
  15. */
  16. int FAST_FUNC is_directory(const char *fileName, int followLinks, struct stat *statBuf)
  17. {
  18. int status;
  19. struct stat astatBuf;
  20. if (statBuf == NULL) {
  21. /* use auto stack buffer */
  22. statBuf = &astatBuf;
  23. }
  24. if (followLinks)
  25. status = stat(fileName, statBuf);
  26. else
  27. status = lstat(fileName, statBuf);
  28. status = (status == 0 && S_ISDIR(statBuf->st_mode));
  29. return status;
  30. }