-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_ctrl.c
90 lines (77 loc) · 1.96 KB
/
file_ctrl.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
#include "bp.h"
int file_len(FILE *fp)
{
int len = 0, pos;
if(NULL == fp) {
err_log("in file_len fp == null");
return 0;
}
pos = ftell(fp);
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fseek(fp, pos, SEEK_SET);
return len;
}
/*
int file_len(char *filename)
{
struct stat temp;
stat(filename, &temp);
return temp.st_size;
}
*/
int file_content(FILE *fp, char *buf)
{
int len = 0;
if( NULL == fp || NULL == buf) {
err_log("file_content fp == NULL or buf == NULL ");
return 0;
}
len = file_len(fp);
while(!feof(fp)) {
*(buf++) = getc(fp);
}
// *buf='\0';
return len;
}
enum type file_type(char *file_name)
{// return 1 is dir return 0 is regular file
struct stat buf;
if(lstat(file_name, &buf) < 0) {
NOTICE("%s lstat error", file_name);
return FILE_OTHER;
}
if(S_ISREG(buf.st_mode)) {
NOTICE("%s is reg file", file_name);
return FILE_REG;
}
if(S_ISDIR(buf.st_mode)) {
NOTICE("%s is dir file", file_name);
return FILE_FOLD;
}
DEBUG("file is not a reg or folder");
/*need to test if here is a soft link*/
return FILE_OTHER;
}
char *process_dir(char *dir_name)
{
DIR* dir;
struct dirent* dir_content;
char *buf;
char *result;
dir = opendir(dir_name);
if(NULL == dir) {
NOTICE("open dir %s failed \n", dir_name);
return NULL;
}
buf = (char*)malloc(BUF_SIZE*sizeof(char));
result = (char*)malloc(BUF_SIZE*sizeof(char));
*result = '\0';
while((dir_content = readdir(dir)) != NULL) {
sprintf(buf, "<p><a href=\"%s/%s\">%s</a></p>\r\n", dir_name, dir_content->d_name, dir_content->d_name);
fprintf(stderr, "<a href=\"%s/%s\">%s</a>\n", dir_name, dir_content->d_name, dir_content->d_name);
strcat(result, buf);
}
fprintf(stderr, "hello %s\n", result);
return result;
}