isdirectory.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <sys/stat.h>
  23. #include "libbb.h"
  24. /*
  25. * Return TRUE if a fileName is a directory.
  26. * Nonexistent files return FALSE.
  27. */
  28. int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
  29. {
  30. int status;
  31. struct stat astatBuf;
  32. if (statBuf == NULL) {
  33. /* set from auto stack buffer */
  34. statBuf = &astatBuf;
  35. }
  36. if (followLinks)
  37. status = stat(fileName, statBuf);
  38. else
  39. status = lstat(fileName, statBuf);
  40. if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
  41. status = FALSE;
  42. }
  43. else status = TRUE;
  44. return status;
  45. }
  46. /* END CODE */
  47. /*
  48. Local Variables:
  49. c-file-style: "linux"
  50. c-basic-offset: 4
  51. tab-width: 4
  52. End:
  53. */