-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
113 lines (95 loc) · 1.71 KB
/
utils.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
#include "includes.h"
#include "utils.h"
#include "log.h"
#include <dirent.h>
#include <sys/stat.h>
int
listdir(char *path, char *buffer)
{
struct dirent **entry_list;
int count;
int i,j=0;
count = scandir(path, &entry_list, 0, alphasort);
if (count < 0) {
return -1;
}
for (i = 0; i < count; i++) {
struct dirent *entry;
entry = entry_list[i];
j += snprintf(buffer+j, MAXDIRLEN, "%s ", entry->d_name);
free(entry);
}
free(entry_list);
return 0;
}
int
pwd(char **buffer)
{
char *res = getcwd(NULL, MAXDIRLEN);
if (NULL == res) {
error_exit("pwd()");
} else {
*buffer = res;
return 0;
}
}
int
createdir(char *path)
{
struct stat st = {0};
int err;
if(stat(path, &st) == -1){
err = mkdir(path, 0700);
return err;
}
return -1;
}
int
deldir(char *path)
{
return rmdir(path);
}
int
readfile(char *filename, char **buffer, int *size)
{
long numbyte;
FILE *f = fopen(filename, "rb");
fseek(f, 0L, SEEK_END);
numbyte = ftell(f);
fseek(f, 0L, SEEK_SET);
*buffer = (char *)malloc(numbyte);
fread(*buffer, sizeof(char), numbyte, f);
*size = numbyte;
return 0;
}
int
writefile(char *filename, char *buffer, int size)
{
FILE *f = fopen(filename, "wb");
int res = fwrite(buffer, size, 1, f);
fclose(f);
return res > 0?0:-1;
}
int
delfile(char *filename)
{
return remove(filename);
}
int
cd(char *path)
{
if(opendir(path) != NULL){
return chdir(path);
}
return -1;
}
void
str_dump(char *str, size_t len)
{
int i;
char *ucp = str;
for (i = 0; i < len; i++) {
printf("%02x ", ucp[i]);
}
printf("\n");
}