Added more file utilities.
This commit is contained in:
parent
27046e6bbb
commit
d3633421aa
|
@ -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;
|
||||
}
|
|
@ -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 */
|
Loading…
Reference in New Issue