Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||||
3 | * Utility routines. | ||||
4 | * | ||||
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Permission has been granted to redistribute this code under GPL. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
11 | |||||
12 | /* | ||||
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 13 | * Return TRUE if fileName is a directory. |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 14 | * Nonexistent files return FALSE. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 15 | */ |
Denys Vlasenko | f282c6b | 2011-12-18 03:27:46 +0100 | [diff] [blame] | 16 | int FAST_FUNC is_directory(const char *fileName, int followLinks) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | { |
18 | int status; | ||||
Denys Vlasenko | f282c6b | 2011-12-18 03:27:46 +0100 | [diff] [blame] | 19 | struct stat statBuf; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 21 | if (followLinks) |
Denys Vlasenko | f282c6b | 2011-12-18 03:27:46 +0100 | [diff] [blame] | 22 | status = stat(fileName, &statBuf); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 23 | else |
Denys Vlasenko | f282c6b | 2011-12-18 03:27:46 +0100 | [diff] [blame] | 24 | status = lstat(fileName, &statBuf); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 25 | |
Denys Vlasenko | f282c6b | 2011-12-18 03:27:46 +0100 | [diff] [blame] | 26 | status = (status == 0 && S_ISDIR(statBuf.st_mode)); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 27 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 28 | return status; |
29 | } |