blob: 9861be6f84844203a05114ab748e11f70cb83f44 [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
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Permission has been granted to redistribute this code under GPL.
Eric Andersenaad1a882001-03-16 22:47:14 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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 */
Denys Vlasenko33d8d082009-09-10 01:46:02 +020018int FAST_FUNC is_directory(const char *fileName, int followLinks, struct stat *statBuf)
Eric Andersenaad1a882001-03-16 22:47:14 +000019{
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}