1
0
Fork 0
mirror of https://gitlab.com/niansa/simpsh-httpd.git synced 2025-03-06 20:53:36 +01:00
simpsh-httpd/cport/filelib.h
2020-08-04 14:53:23 +02:00

50 lines
1.2 KiB
C

#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
bool file_accessible(const char *pathname, int mode) {
return access(pathname, mode) != -1;
}
bool file_exists(const char *pathname) {
return file_accessible(pathname, F_OK);
}
mode_t file_getmode(const char *pathname) {
struct stat statbuf;
assert(stat(pathname, &statbuf) == 0);
return statbuf.st_mode;
}
bool file_isdir(const char *pathname) {
if (!file_exists(pathname)) {
return false;
}
mode_t filemode = file_getmode(pathname);
return S_ISDIR(filemode);
}
bool file_isregfile(const char *pathname) {
if (!file_exists(pathname)) {
return false;
}
mode_t filemode = file_getmode(pathname);
return S_ISREG(filemode);
}
struct databuffer file_listdir(const char *pathname) { // Returns databuffer with filenames in pathnames seperated by null-terminators
struct databuffer filelist;
struct databuffer filename;
DIR *dir;
struct dirent *entry;
db_init(&filelist);
assert((dir = opendir(pathname)) != NULL);
while ((entry = readdir(dir)) != NULL) {
filename = sized_db(entry->d_name, strlen(entry->d_name) + 1);
db_append(&filelist, &filename);
}
closedir(dir);
return filelist;
}