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
|
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include "handerror.h"
/* Buff size: source == path = file
splitPath thinks, that in end path always stay FILE, not directory */
char* splitPath(char *source, char *path, char *file)
{
int fSymbol = 0, f = 0;
char *main_path = malloc(sizeof(char) * strlen(source) + 1);
char *file_path = malloc(sizeof(char) * strlen(source) + 1);
for(int i=0; i < strlen(source); i++)
{
if(fSymbol == 1)
{
switch(source[i])
{
case '/':
{
fSymbol = 0;
f = 0;
strcat(main_path, file_path);
strcat(main_path, "/");
file_path[0] = '\0';
break;
}
default:
{
file_path[f] = source[i];
file_path[f+1] = '\0';
f++;
break;
}
}
}
else // if it's beginning of string
{
// handling first symbol
switch(source[i])
{
case '.':
case '\\':
case '/':
{
printError("lpass: You can't use these symbol at the beginning: '.', '/', '\\' \n");
break;
}
default:
fSymbol = 1;
// enter first symbol
file_path[0] = source[i];
file_path[1] = '\0';
f++;
break;
}
}
}
strcpy(path, main_path);
strcpy(file, file_path);
free(main_path);
free(file_path);
if(*file) return file;
return NULL;
}
int deleteFile(char *file_path)
{
int pid;
pid = fork();
if(pid == -1) callError(112);
if(pid == 0) { /* new process */
execlp("rm", "rm", file_path, NULL);
perror("rm");
exit(4);
}
wait(&pid);
return 1;
}
int deleteEmptyDir(char *dir_path)
{
int pid;
pid = fork();
if(pid == -1) callError(113);
if(pid == 0) { /* new process */
#if defined(DEBUG)
execlp("rmdir", "rmdir", "-p", dir_path, NULL);
#else
execlp("rmdir", "rmdir", "-p", "--ignore-fail-on-non-empty", dir_path, NULL);
#endif
perror("rmdir");
exit(4);
}
wait(&pid);
return 1;
}
int checkFileExist(char *path_to_file)
{
FILE *pFile;
pFile = fopen(path_to_file, "r");
if(pFile == NULL) {
if(errno == ENOENT) { // file doesn't exist
return 0;
}
else callError(120);
}
fclose(pFile);
return 1;
}
|