1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-07-06 09:26:44 +02:00

Add enum for file_type() and a little cleanup

This commit is contained in:
jfreegman
2018-10-08 22:28:36 -04:00
parent 258736995d
commit 8f0e6026f0
5 changed files with 30 additions and 18 deletions

View File

@ -481,22 +481,25 @@ bool file_exists(const char *path)
return stat(path, &s) == 0;
}
/* Returns 0 if path points to a directory.
* Returns 1 if path points to a regular file.
* Returns -1 on any other result.
/*
* Checks the file type path points to and returns a File_Type enum value.
*
* Returns FILE_TYPE_DIRECTORY if path points to a directory.
* Returns FILE_TYPE_REGULAR if path points to a regular file.
* Returns FILE_TYPE_OTHER on any other result, including an invalid path.
*/
int file_type(const char *path)
File_Type file_type(const char *path)
{
struct stat s;
stat(path, &s);
switch (s.st_mode & S_IFMT) {
case S_IFDIR:
return 0;
return FILE_TYPE_DIRECTORY;
case S_IFREG:
return 1;
return FILE_TYPE_REGULAR;
default:
return -1;
return FILE_TYPE_OTHER;
}
}