1
0
Fork 0

Added more file utilities.

This commit is contained in:
Wael Karram 2022-07-23 21:23:35 +03:00
parent 27046e6bbb
commit d3633421aa
Signed by: wael
GPG Key ID: 3B356038CCB10808
2 changed files with 85 additions and 0 deletions

43
utils/file_utils.c Normal file
View File

@ -0,0 +1,43 @@
/*
* C_lib Copyright (C) 2021 Wael Karram.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* Include header. */
#include "file_utils.h"
/* This function checks if a file or directory exists at the given path. */
static inline int check_file_exists(const char *path) {
return (path && access(path, F_OK) == 0);
}
/* This function checks if the path points to a file or directory, assumes existence! */
/* Returns 1 on file, 0 on directory, 2 for other; note that -1 indicates internal error! */
static inline int check_if_file(const char *path) {
/* Sanity check. */
assert(check_file_exists(path));
/* Check type. */
struct stat path_stat;
/* Check for failure. */
if (stat(path, &path_stat) == PASS_FAIL_FILE_UTILS_H)
return PASS_FAIL_FILE_UTILS_H;
/* Return the result. */
if (S_ISREG(path_stat.st_mode))
return TRUE_FILE_UTILS_H;
else if (S_ISDIR(path_stat.st_mode))
return FALSE_FILE_UTILS_H;
else
return OTHER_FILE_UTILS_H;
}

42
utils/file_utils.h Normal file
View File

@ -0,0 +1,42 @@
/*
* C_lib Copyright (C) 2022 Wael Karram.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* This header defines various file utility functions. */
#ifndef FILE_UTILS_H
#define FILE_UTILS_H
/* Include unistd.h (for access). */
#include <unistd.h>
/* Include sys/types and sys/stat to check type. */
#include <sys/types.h>
#include <sys/stat.h>
/* Set debug if needed. */
#ifdef DEBUG
#ifndef NDEBUG
#define NDEBUG
#endif /* NDEBUG */
#endif /* DEBUG */
/* Include assert.h to allow code generation. */
#include <assert.h>
/* This function checks if a file or directory exists at the given path. */
static inline int check_file_exists(const char *path);
/* This function checks if the path points to a file or directory, assumes existence! */
static inline int check_if_file(const char *path);
#endif /* FILE_UTILS_H */