blob: b35919869e92f0c644287a0abc3f2d7befd43ef5 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
Eric Andersenbdfd0d72001-10-24 05:00:29 +00006 * Permission has been granted to redistribute this code under the GPL.
Eric Andersenaad1a882001-03-16 22:47:14 +00007 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
10
Eric Andersenaad1a882001-03-16 22:47:14 +000011#include <sys/stat.h>
12#include "libbb.h"
13
14/*
15 * Return TRUE if a fileName is a directory.
Eric Andersenaff114c2004-04-14 17:51:38 +000016 * Nonexistent files return FALSE.
Eric Andersenaad1a882001-03-16 22:47:14 +000017 */
18int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
19{
20 int status;
Glenn L McGrath393183d2003-05-26 14:07:50 +000021 struct stat astatBuf;
Eric Andersenaad1a882001-03-16 22:47:14 +000022
23 if (statBuf == NULL) {
Glenn L McGrath393183d2003-05-26 14:07:50 +000024 /* set from auto stack buffer */
25 statBuf = &astatBuf;
Eric Andersenaad1a882001-03-16 22:47:14 +000026 }
27
Matt Kraai1f0c4362001-12-20 23:13:26 +000028 if (followLinks)
Eric Andersenaad1a882001-03-16 22:47:14 +000029 status = stat(fileName, statBuf);
30 else
31 status = lstat(fileName, statBuf);
32
33 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
34 status = FALSE;
35 }
36 else status = TRUE;
37
Eric Andersenaad1a882001-03-16 22:47:14 +000038 return status;
39}