-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelbbs.c
143 lines (121 loc) · 2.71 KB
/
delbbs.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "delbbs.h"
char* symbol;
int main(int argc, char **argv){
symbol = argv[1];
confirm_file(argv[1]);
delDirFile(argv[1]);
set_BoardIndex(argv[1]);
return 1;
}
void confirm_file(const char *symbol){
int bValue = access("board.index", 0);
int aValue = access("article.index", 0);
int dValue = access(".", W_OK);
char buf[255];
if(bValue==-1){
printf("BBS 게시판이 아닙니다.\n");
exit(1);
}else{
if(aValue==0){ // article.index 파일 존재
printf("하위 게시판을 생성할 수 없습니다.\n");
exit(1);
}
}
if(dValue==-1){
printf("시스템 오류: 권한이 없습니다.\n");
exit(1);
}
ftw(symbol, searchDIR, 1);
}
int delDirFile(const char* folder)
{
DIR* dp;
struct dirent *dirp;
struct stat file_stat;
char targetfile[512];
int cnt = 0;
if((dp = opendir(folder)) == NULL)
{
printf("해당 게시판이 존재하지 않습니다.\n");
exit(1);
}
while(1)
{
if(!(dirp = readdir(dp)))
{
closedir(dp);
remove(folder);
return -1;
}
sprintf(targetfile, "%s/%s", folder, dirp->d_name); // Path와 파일이름을 합친다
stat(targetfile, &file_stat);
if((file_stat.st_mode & S_IFMT) == S_IFDIR) // Directory 이면...
{
continue;
}
if(remove(targetfile) == 0)
{
printf("게시판 %s가 삭제되었습니다.\n", folder);
continue;
}
else
{
perror("remove Error : ");
break;
}
}
return 1;
}
void set_BoardIndex(const char* symbol){
FILE* file = fopen("board.index", "r+");
char buf[160];
char* p;
char s[4];
int cnt = 0;
int flag = 2;
while(fgets(buf, 160, file)!=NULL){
if((p=strchr(buf, '\n'))!=NULL) *p = '\0';
if(cnt >= 6){
if(cnt==6){
if(strcmp(buf, symbol)==0){
flag = 1;
fseek(file, -1-strlen(symbol), SEEK_CUR);
fputs(" ", file);
}
}
if(flag==0){
if(buf[0]=='#') flag++;
else if(strcmp(buf, symbol)==0){
flag = 1;
fseek(file, -1-strlen(symbol), SEEK_CUR);
fputs(" ", file);
}else
flag = 2;
}
flag--;
}
cnt++;
}
}
int searchDIR(const char *name, const struct stat *status, int type)
{
switch ( type )
{
case FTW_D:
/*if ( rmdir(name) == -1 )
perror("unlink");
printf("% - 30s*\t0%3o-%s\n", name, status->st_mode&0777, symbol);*/
if(strcmp(name, symbol)!=0){
printf("해당 게시판은 하위 게시판을 가지고 있습니다. 하위 게시판을 먼저 삭제해야합니다.");
exit(1);
}
break;
/*case FTW_F:
case FTW_SL:
printf("% - 30s\t0%3o\n", name, status->st_mode&0777);
if ( unlink(name) == -1 )
perror("unlink");
break;*/
}
return 0;
}