blob: 1d2477f47ad47304e15f77d1cc011b3bf307a3ab [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/*
Denis Vlasenko394eebe2008-02-25 20:30:24 +000015 * Return TRUE if 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) {
Denis Vlasenko394eebe2008-02-25 20:30:24 +000024 /* use 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
Denis Vlasenko394eebe2008-02-25 20:30:24 +000033 status = (status == 0 && S_ISDIR(statBuf->st_mode));
Eric Andersenaad1a882001-03-16 22:47:14 +000034
Eric Andersenaad1a882001-03-16 22:47:14 +000035 return status;
36}