-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathos.c
191 lines (190 loc) · 4.48 KB
/
os.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#include <errno.h>
#include "os.h"
/*!
* @brief Tests if dirnm is a directory
*
* @param[in] dirnm name of directory to test
*
* @result true -> dirnm is an existing directory
* false -> dirnm is not a directory
*
* @author Ben Baker, ISTI
*
*/
bool os_path_isdir(const char *dirnm)
{
struct stat s;
int err;
if (dirnm == NULL){return false;}
if (strlen(dirnm) == 0){return false;}
err = stat(dirnm, &s);
// Doesn't exist
if (err == -1)
{
if (ENOENT == errno)
{
return false;
}
// Exists
else
{
return true;
}
}
// Exists
else
{
// Test it is a directory
if (S_ISDIR(s.st_mode))
{
return true;
}
else
{
return false;
}
}
}
//============================================================================//
/*!
* @brief Recursive directory creation function
*
* @param[in] path directory tree to make
*
* @result 0 indicates success
*
* @author Ben Baker, ISTI
*
*/
int os_makedirs(const char *path)
{
const char *fcnm = "os_makedirs\0";
char *where, *dname, *work, directory[PATH_MAX];
int ierr, indx, lenos;
const char find[] = "/\0";
//------------------------------------------------------------------------//
//
// Errors
if (path == NULL){return -1;}
lenos = strlen(path);
if (lenos == 0){return -1;}
if (lenos > PATH_MAX - 2)
{
printf("%s: Error directory %s is too long\n", fcnm, path);
return -1;
}
// Already exists
if (os_path_isdir(path)){return 0;}
// Initialize
work = (char *)calloc(lenos+2, sizeof(char));
strcpy(work, path);
memset(directory, 0, sizeof(directory));
dname = work;
dname[lenos] = '/'; // Try to catch the final case
where = strpbrk(dname, find);
while ((where != NULL))
{
indx = where - dname;
lenos = strlen(directory);
strncat(directory, dname, indx);
// If directory doesn't exist then make it
if (!os_path_isdir(directory))
{
ierr = os_mkdir(directory);
if (ierr != 0)
{
printf("%s: Error making subdirectory: %s\n",
fcnm, directory);
printf("%s: Error making directory: %s\n",
fcnm, path);
free(dname);
return -1;
}
} // End check on if directory exists
// Add directory delimiter
strcat(directory, "/\0");
dname = dname + indx + 1;
where = strpbrk(dname, find);
} // End while
// Add name of final subdirectory and make it
strcat(directory, dname);
if (!os_path_isdir(directory))
{
ierr = os_mkdir(directory);
if (ierr != 0)
{
printf("%s: Error making directory: %s\n", fcnm, directory);
return -1;
}
}
// Free space
dname = NULL;
free(work);
return ierr;
}
//============================================================================//
/*!
* Makes a directory named dirnm with full permissions
*
* @param[in] dirnm name of directory to make
*
* @result 0 if success
*
* @author Ben Baker, ISTI
*
*/
int os_mkdir(const char *dirnm)
{
const char *fcnm = "os_mkdir\0";
int ierr;
if (dirnm == NULL){return -1;}
if (strlen(dirnm) == 0){return -1;}
ierr = mkdir(dirnm, 0777);
if (ierr != 0)
{
printf("%s: Error making directory: %s\n", fcnm, dirnm);
return -1;
}
return 0;
}
//============================================================================//
/*!
* @brief Tests if filenm is a file
*
* @param[in] filenm name of file to test
*
* @result true -> filenm is an existing file
* false -> filenm is not a file
*
* @author Ben Baker, ISTI
*
*/
bool os_path_isfile(const char *filenm)
{
struct stat info;
if (filenm == NULL){return false;}
if (strlen(filenm) == 0){return false;}
// Doesn't exist
if (stat(filenm, &info) ==-1)
{
return false;
}
// Exists -> check it is a file
else
{
if (S_ISREG(info.st_mode))
{
return true;
}
else
{
return false;
}
}
}