-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.c
More file actions
56 lines (47 loc) · 1.5 KB
/
Copy pathutils.c
File metadata and controls
56 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// Created by flassabe on 16/11/2021.
//
#include "utils.h"
#include <limits.h>
#include <dirent.h>
#include <stdio.h>
/*!
* \brief make_full_path concatenates path and basename and returns the result
* \param path the path to the database directory basename, can be NULL (i.e. path is current directory)
* Path may end with '/' but it is not required.
* \param basename the database name.
*
* \return a pointer to the full path. Its content must be freed by make_full_path caller.
*/
char *make_full_path(char *path, char *basename) {
if(basename){
if(path){
if(path[strlen(path)-1] != '/'){ //verify if path ends with '/'
if(strlen(path)+1 < PATH_MAX){
sprintf(path, "%s/", path); //append '/' at the end of the path
} else {
return NULL;
}
}
if(directory_exists(path)){ //verify is path exists
if(strlen(path)+strlen(basename) < PATH_MAX){ //verify that the full path is of correct size
return strcat(path, basename);
} else {
return NULL;
}
}
} else {
return basename; //if path is NULL then path is current directory
}
} else {
return NULL; //basename can't be NULL
}
}
bool directory_exists(char *path) {
DIR *my_dir = opendir(path);
if (my_dir) {
closedir(my_dir);
return true;
}
return false;
}