blob: ba6c52ce8d16fb3d6b33622bddf50f059c780000 [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 Vlasenkof282c6b2011-12-18 03:27:46 +010018int FAST_FUNC is_directory(const char *fileName, int followLinks)
Eric Andersenaad1a882001-03-16 22:47:14 +000019{
20 int status;
Denys Vlasenkof282c6b2011-12-18 03:27:46 +010021 struct stat statBuf;
Eric Andersenaad1a882001-03-16 22:47:14 +000022
Matt Kraai1f0c4362001-12-20 23:13:26 +000023 if (followLinks)
Denys Vlasenkof282c6b2011-12-18 03:27:46 +010024 status = stat(fileName, &statBuf);
Eric Andersenaad1a882001-03-16 22:47:14 +000025 else
Denys Vlasenkof282c6b2011-12-18 03:27:46 +010026 status = lstat(fileName, &statBuf);
Eric Andersenaad1a882001-03-16 22:47:14 +000027
Denys Vlasenkof282c6b2011-12-18 03:27:46 +010028 status = (status == 0 && S_ISDIR(statBuf.st_mode));
Eric Andersenaad1a882001-03-16 22:47:14 +000029
Eric Andersenaad1a882001-03-16 22:47:14 +000030 return status;
31}