summaryrefslogtreecommitdiff
path: root/files/files.c
blob: badd542d1955fc6c13a164273a5d301b9bf96821 (plain) (blame)
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
#include "files.h"


int dir_exists(char *dirpath){
	struct stat st;
	if(stat(dirpath, &st) == 0){
		return S_ISDIR(st.st_mode);
	}
	return 0;
}

int file_exists(char *filepath){
	struct stat st;
	if(stat(filepath, &st) == 0){
		return S_ISREG(st.st_mode);
	}
	return 0;
}

int path_exists(char *path){
	return dir_exists(path) || file_exists(path);
}

u64 get_fd_size(int fd){
	struct stat st;
	if(fstat(fd, &st) == 0){
		return st.st_size;
	}
	return 0;
}

u64 get_fp_size(FILE *fp){
	if(fp != NULL){
		return get_fd_size(fileno(fp));
	}
	return 0;
}

u64 get_file_size(char *filename){
	struct stat st;
	if(stat(filename, &st) == 0){
		return st.st_size;
	}
	return 0;
}

char *get_extension(char *filename){
	int len = 0, i = 0;
	while(filename[len] != '\0') len++;
	while(len-i > 0){
		if(filename[len-i-1] == '.') break;
		i++;
	}
	if(i == 0 || i == len){
		return NULL;
	}
	return filename + len - i;
}