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 |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 6 | * Permission has been granted to redistribute this code under the GPL. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 7 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
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 | /* |
| 15 | * Return TRUE if a 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 | */ |
| 18 | int is_directory(const char *fileName, const int followLinks, struct stat *statBuf) |
| 19 | { |
| 20 | int status; |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 21 | struct stat astatBuf; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 22 | |
| 23 | if (statBuf == NULL) { |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 24 | /* set from auto stack buffer */ |
| 25 | statBuf = &astatBuf; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 28 | if (followLinks) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 29 | status = stat(fileName, statBuf); |
| 30 | else |
| 31 | status = lstat(fileName, statBuf); |
| 32 | |
| 33 | if (status < 0 || !(S_ISDIR(statBuf->st_mode))) { |
| 34 | status = FALSE; |
| 35 | } |
| 36 | else status = TRUE; |
| 37 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 38 | return status; |
| 39 | } |