-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_list.c
110 lines (90 loc) · 1.95 KB
/
file_list.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
#include "inverted_search.h"
void file_validation_n_file_list(Flist **f_head, char *argv[])
{
int i=1, empty;
while ( argv[i] != NULL )
{
empty = isFileEmpty(argv[i] );
if ( empty == FILE_NOTAVAILABLE )
{
printf("File : %s is not available\n", argv[i]);
printf("Hence we are not adding this file into file list\n");
i++;
continue;
}
else if ( empty == FILE_EMPTY )
{
printf("File : %s is Empty\n", argv[i]);
printf("Hence we are not adding this file into file list\n");
i++;
continue;
}
else
{
int ret_val= to_create_list_of_files(f_head, argv[i] );
if ( ret_val == SUCCESS)
{
printf("Succesfully : Inserting the file name : %s into the file linkedlist \n",argv[i] );
}
else if ( ret_val == REPEATATION )
{
printf("This file name : %s is repeated . Do not add more than once\n", argv[i] );
}
else
{
printf("Failure......\n");
}
i++;
}
}
}
int isFileEmpty(char *filename)
{
//Check if file exists or not
FILE *fptr = fopen(filename, "r");
if ( fptr == NULL )
{
if( errno == ENOENT )
{
return FILE_NOTAVAILABLE;
}
}
//check file contains contents or not
fseek(fptr, 0, SEEK_END );
if (ftell(fptr) == 0 )
{
return FILE_EMPTY;
}
}
int to_create_list_of_files(Flist **f_head, char *name)
{
//Check files are repeated or not and if not repeated add the file name into the list
Flist *new = malloc( sizeof(Flist));
if ( new == NULL )
{
return FAILURE;
}
strcpy ( new->file_name, name );
new ->link = NULL;
Flist *temp = *f_head;
Flist *prev = *f_head;
if ( *f_head == NULL )
{
*f_head = new;
return SUCCESS;
}
while ( temp != NULL )
{
if ( strcmp (temp->file_name, name ) == 0 )
{
return REPEATATION;
}
else
{
prev = temp;
temp = temp->link;
}
}
prev->link = new;
return SUCCESS;
}