Problem:
In Unix, how can one determine the existence of a directory using a system call without opening it or handling related errors?
Answer:
POSIX systems provide two essential functions for this purpose: stat() and lstat(). These functions allow you to ascertain whether a pathname reflects a valid accessible object and retrieve information about its type.
Key Differences Between stat() and lstat():
Implementation Using stat():
#include <sys/stat.h> struct stat sb; if (stat(pathname, &sb) == 0 && S_ISDIR(sb.st_mode)) { // True if pathname is a directory }
Additional File Type Verification:
Using the S_IS* macros from
The above is the detailed content of How to Check Directory Existence in Unix Using System Calls?. For more information, please follow other related articles on the PHP Chinese website!