forked from tinylcy/vino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
61 lines (54 loc) · 1.64 KB
/
util.c
File metadata and controls
61 lines (54 loc) · 1.64 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
57
58
59
60
61
/*
* Copyright (C) Chenyang Li
* Copyright (C) tinyhttpd
*/
#include <stdlib.h>
#include <sys/stat.h>
/*-------------------------------------------------------*
check whether the request resource is a directory.
-------------------------------------------------------*/
int is_directory(const char *filepath) {
struct stat info;
if (stat(filepath, &info) == 0 && S_ISDIR(info.st_mode)) {
return 1;
}
return 0;
}
/*-------------------------------------------------------*
check if a file is executable.
-------------------------------------------------------*/
int is_executable(const char *filepath) {
struct stat info;
return (stat(filepath, &info) == 0 && S_ISREG(info.st_mode) && (S_IXUSR & info.st_mode));
}
/*-------------------------------------------------------*
check if a file is readable.
-------------------------------------------------------*/
int is_readable(const char *filepath) {
struct stat info;
return ((stat(filepath, &info) == 0) && S_ISREG(info.st_mode) && (S_IRUSR & info.st_mode));
}
/*-------------------------------------------------------*
check if a file exists.
-------------------------------------------------------*/
int file_exist(const char *filepath) {
struct stat info;
return (stat(filepath, &info) == 0);
}
char *contains(char *start, char *end, char target) {
char *p = NULL;
for (p = start; p <= end; p++) {
if (*p == target) {
return p;
}
}
return NULL;
}
/*-------------------------------------------------------*
get file size.
-------------------------------------------------------*/
int file_size(const char *uri) {
struct stat buf;
stat(uri, &buf);
return (int)buf.st_size;
}