blob: 6225a34b2e91389866d4892585637457ddae0c07 [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 */
Eric Andersenaad1a882001-03-16 22:47:14 +000010#include "libbb.h"
11
12/*
Denis Vlasenko394eebe2008-02-25 20:30:24 +000013 * Return TRUE if fileName is a directory.
Eric Andersenaff114c2004-04-14 17:51:38 +000014 * Nonexistent files return FALSE.
Eric Andersenaad1a882001-03-16 22:47:14 +000015 */
Denys Vlasenkof282c6b2011-12-18 03:27:46 +010016int FAST_FUNC is_directory(const char *fileName, int followLinks)
Eric Andersenaad1a882001-03-16 22:47:14 +000017{
18 int status;
Denys Vlasenkof282c6b2011-12-18 03:27:46 +010019 struct stat statBuf;
Eric Andersenaad1a882001-03-16 22:47:14 +000020
Matt Kraai1f0c4362001-12-20 23:13:26 +000021 if (followLinks)
Denys Vlasenkof282c6b2011-12-18 03:27:46 +010022 status = stat(fileName, &statBuf);
Eric Andersenaad1a882001-03-16 22:47:14 +000023 else
Denys Vlasenkof282c6b2011-12-18 03:27:46 +010024 status = lstat(fileName, &statBuf);
Eric Andersenaad1a882001-03-16 22:47:14 +000025
Denys Vlasenkof282c6b2011-12-18 03:27:46 +010026 status = (status == 0 && S_ISDIR(statBuf.st_mode));
Eric Andersenaad1a882001-03-16 22:47:14 +000027
Eric Andersenaad1a882001-03-16 22:47:14 +000028 return status;
29}