-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_utils.c
More file actions
53 lines (46 loc) · 1.16 KB
/
Copy pathfile_utils.c
File metadata and controls
53 lines (46 loc) · 1.16 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
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include "string_utils.h"
int file_is_socket(const char *path) {
struct stat st;
if (stat(path, &st) != 0) {
return 0;
}
return S_ISSOCK(st.st_mode) ? 1 : 0;
}
int file_is_readable_regular_file(const char *path) {
struct stat st;
if (stat(path, &st) != 0) {
return 0;
}
if (!S_ISREG(st.st_mode)) {
return 0;
}
return access(path, R_OK) == 0 ? 1 : 0;
}
int directory_exists_and_accessible(const char *path) {
struct stat st;
if (stat(path, &st) != 0) {
return 0;
}
if (!S_ISDIR(st.st_mode)) {
return 0;
}
return access(path, R_OK | X_OK) == 0 ? 1 : 0;
}
int get_home_directory_for_current_user(char *out_home, size_t out_home_size) {
const char *home_env = getenv("HOME");
if (!is_string_null_or_empty(home_env)) {
snprintf(out_home, out_home_size, "%s", home_env);
return 1;
}
struct passwd *pw = getpwuid(getuid());
if (!pw || !pw->pw_dir) {
return 0;
}
snprintf(out_home, out_home_size, "%s", pw->pw_dir);
return 1;
}