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
144
145
146
147
148
149
150
151
152
153
|
#include "data_struct.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void add_new_item(struct file_item **list, struct file_item **entry,
const char *file_name)
{
*entry = malloc(sizeof(struct file_item));
if(!*entry) {
perror("malloc");
exit(1);
}
(*entry)->filename = malloc(strlen(file_name)+1);
if(!(*entry)->filename) {
perror("malloc");
exit(1);
}
/* safety */
strcpy((*entry)->filename, file_name);
(*entry)->next = *list;
(*entry)->info_line = NULL;
*list = *entry;
}
/*
* Initialize arg_arrays: allocate memory for directories and files
* arrays and zero them
*/
void init_arg_arrays(struct arg_arrays *split, int argc)
{
split->dirs = malloc(argc * sizeof(char*));
if(!split->dirs) {
perror("malloc");
exit(1);
}
split->files = malloc(argc * sizeof(char*));
if(!split->files) {
perror("malloc");
exit(1);
}
memset(split->dirs, 0, argc * sizeof(char*));
memset(split->files, 0, argc * sizeof(char*));
}
void init_item_array(struct file_item_array *arr)
{
arr->array = NULL;
arr->length = 0;
}
static size_t get_list_length(struct file_item *list)
{
size_t counter = 0;
while(list) {
++counter;
list = list->next;
}
return counter;
}
void copy_list_to_array(struct file_item *list, struct file_item_array *arr)
{
size_t list_length = get_list_length(list);
size_t i;
arr->array = malloc(list_length * sizeof(struct file_item*));
if(!arr->array) {
perror("malloc");
exit(1);
}
arr->length = list_length;
for(i = 0; i < list_length; list = list->next, ++i)
arr->array[i] = list;
}
void free_file_item_array(struct file_item_array *arr, struct file_item **list)
{
size_t i;
for(i = 0; i < arr->length; ++i) {
free(arr->array[i]->filename);
free(arr->array[i]->info_line);
free(arr->array[i]);
}
if(arr->array)
free(arr->array);
arr->array = NULL;
arr->length = 0;
*list = NULL;
}
void free_arg_arrays(struct arg_arrays *split)
{
free(split->dirs);
free(split->files);
}
int is_empty_array(const char **paths_array, int argc)
{
int empty_flag = 1;
size_t i;
for(i = 0; i < argc; ++i)
if(paths_array[i] != 0) {
empty_flag = 0;
break;
}
return empty_flag;
}
int is_last_arr_element(const char **paths_array, int argc, size_t pos)
{
int last_flag = 1;
for(++pos ; pos < argc; ++pos)
if(paths_array[pos]) {
last_flag = 0;
break;
}
return last_flag;
}
static void swap(struct file_item **prev_element,
struct file_item **next_element)
{
struct file_item *tmp_element = NULL;
if(prev_element != next_element)
if(strcmp((*prev_element)->filename, (*next_element)->filename) > 0) {
tmp_element = *prev_element;
*prev_element = *next_element;
*next_element = tmp_element;
}
}
/*
* bubble sort
*/
void sort_array_by_names(struct file_item_array *arr)
{
size_t i, j;
if(arr->length == 1)
return;
for(i = arr->length-1; i > 0; --i)
for(j = 1; j <= i; ++j)
swap(arr->array+j-1, arr->array+j);
}
|