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 | */ |
10 | |||||
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 11 | #include <sys/stat.h> |
12 | #include "libbb.h" | ||||
13 | |||||
14 | /* | ||||
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 15 | * Return TRUE if fileName is a directory. |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 16 | * Nonexistent files return FALSE. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | */ |
Denys Vlasenko | f282c6b | 2011-12-18 03:27:46 +0100 | [diff] [blame] | 18 | int FAST_FUNC is_directory(const char *fileName, int followLinks) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 19 | { |
20 | int status; | ||||
Denys Vlasenko | f282c6b | 2011-12-18 03:27:46 +0100 | [diff] [blame] | 21 | struct stat statBuf; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 22 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 23 | if (followLinks) |
Denys Vlasenko | f282c6b | 2011-12-18 03:27:46 +0100 | [diff] [blame] | 24 | status = stat(fileName, &statBuf); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 25 | else |
Denys Vlasenko | f282c6b | 2011-12-18 03:27:46 +0100 | [diff] [blame] | 26 | status = lstat(fileName, &statBuf); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 27 | |
Denys Vlasenko | f282c6b | 2011-12-18 03:27:46 +0100 | [diff] [blame] | 28 | status = (status == 0 && S_ISDIR(statBuf.st_mode)); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 29 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 30 | return status; |
31 | } |