-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.c
More file actions
69 lines (60 loc) · 1.15 KB
/
Copy pathreader.c
File metadata and controls
69 lines (60 loc) · 1.15 KB
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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "functions.h"
#include <sys/file.h>
int main(int argc, char *argv[]){
int i, fd;
int aux = NUMBER_OF_LINES*POSITION_OF_NEWLINE;
int lineCounter=0;
char currentChar;
char* buf = (char*)malloc(sizeof(char)*(aux+1));
char* file;
printf("%s\n", argv[1]);
if(argv[1] == NULL){
file = fileSelector();
}else{
file = argv[1];
}
if((fd = open(file, O_RDONLY)) < 0){
perror("Cannot open file");
exit(-1);
}
if(flock(fd, LOCK_SH) < 0){
perror("Failed to lock file");
exit(-1);
}
if((read(fd, buf, aux)) < 0){
perror("Failed to read file");
exit(-1);
}
if(validLetter(buf[0])){
for(i=0; lineCounter < NUMBER_OF_LINES; i++){
if(i%POSITION_OF_NEWLINE != NUMBER_OF_LETTERS){
if(buf[i]!=buf[0]) {
return -1;
}
}else{
if(buf[i] != '\n') {
return -1;
}
lineCounter++;
}
}
if(!read(fd, buf, aux)){
if(flock(fd, LOCK_UN) < 0){
perror("Failed to unlock file");
exit(-1);
}
if((close(fd))< 0){
perror("Failed to close file");
exit(-1);
}
return 0;
}else{
return -1;
}
}else{
return -1;
}
}