-
Notifications
You must be signed in to change notification settings - Fork 1
/
files.c
104 lines (84 loc) · 2.13 KB
/
files.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
#include "files.h"
//https://stackoverflow.com/questions/2180079/how-can-i-copy-a-file-on-unix-using-c
int cp(const char *to, const char *from)
{
int fd_to, fd_from;
char buf[4096];
ssize_t nread;
int saved_errno;
fd_from = open(from, O_RDONLY);
if (fd_from < 0)
return -1;
fd_to = open(to, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd_to < 0)
goto out_error;
while (nread = read(fd_from, buf, sizeof buf), nread > 0){
char *out_ptr = buf;
ssize_t nwritten;
do{
nwritten = write(fd_to, out_ptr, nread);
if (nwritten >= 0){
nread -= nwritten;
out_ptr += nwritten;
}else if (errno != EINTR){
goto out_error;
}
}while(nread > 0);
}
if (nread == 0){
if (close(fd_to) < 0){
fd_to = -1;
goto out_error;
}
close(fd_from);
/* Success! */
return 0;
}
out_error:
saved_errno = errno;
close(fd_from);
if (fd_to >= 0)
close(fd_to);
errno = saved_errno;
return -1;
}
int checkWordInFile(FILE* file, char* str){
const int tempSize = 1024;
char temp[tempSize];
rewind(file);
while(fgets(temp, tempSize, file) != NULL) {
if((strstr(temp, str)) != NULL) {
return RESULT_FAILURE;
}
}
return RESULT_SUCCESS;
}
void readFileLine(char* filename, int lineNumber, char* buffer){
FILE* file;
file = fopen(filename, "rb");
const int tempSize = 1024;
char temp[tempSize];
if (file == NULL) {
return;
} else {
int i=0;
while(fgets(temp, tempSize, file) != NULL) {
if(i==lineNumber){
strcpy(buffer, temp);
break;
}
i++;
}
fclose(file);
}
}
int getLastModification(char* path, time_t* lastModification){
int file=0;
if((file=open(path,O_RDONLY)) < -1)
return 1;
struct stat fileStat;
if(fstat(file,&fileStat) < 0)
return 1;
*lastModification = fileStat.st_mtime;
return 0;
}