-
Notifications
You must be signed in to change notification settings - Fork 0
/
finger.c
186 lines (151 loc) · 2.98 KB
/
finger.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <inc.h>
void display_fingerprint(t_fingerprint *sig)
{
t_return *tmp_ret;
if (! sig)
return ;
tmp_ret = sig->ret_list;
printf("unknown fingerprint:\n");
printf("-New signature-");
while (tmp_ret->next)
{
printf(":%d", tmp_ret->ret);
tmp_ret = tmp_ret->next;
}
printf("\n");
}
void debug_fingerprints(t_fingerprint *list)
{
t_fingerprint *tmp;
t_return *tmp_ret;
tmp = list;
while (tmp)
{
tmp_ret = tmp->ret_list;
while (tmp_ret)
{
/* printf("index %d - ret: %d\n", tmp_ret->index, tmp_ret->ret); */
tmp_ret = tmp_ret->next;
}
tmp = tmp->next;
}
}
int count(t_fingerprint *list)
{
int count;
t_fingerprint *tmp;
count = 0;
if (! list)
return (count);
tmp = list;
while (tmp)
{
count++;
tmp = tmp->next;
}
return count;
}
t_return *new_ret(int ret_code, int index)
{
t_return *new;
if (! (new = malloc(sizeof(t_return))))
{
fprintf(stderr, "%s:%d Can't malloc new in new_ret()\n",
__FILE__, __LINE__);
exit (1);
}
new->index = index;
new->ret = ret_code;
new->next = 0;
return (new);
}
t_return *add_ret(t_return *list, t_return *new)
{
t_return *tmp;
if (! list)
return (new);
if (! new)
return (list);
tmp = list;
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
return (list);
}
/*
** t_fingerprint *new(char *string)
**
** string is a line of a fingerprint entry in the fingerprint file
*/
/* create a new fingerprint entry */
t_fingerprint *new(char *string)
{
t_fingerprint *new;
t_return *ret_list;
char *p;
int index = 1;
if (! (new = (t_fingerprint *) malloc(sizeof(t_fingerprint))))
{
fprintf(stderr, "%s:%d Can't malloc in new()\n", __FILE__, __LINE__);
exit(1);
}
ret_list = 0;
p = strtok(string, ":");
new->version = strdup(p);
while ((p = strtok(0, ":")))
ret_list = add_ret(ret_list, new_ret(atoi(p), index++));
new->ret_list = ret_list;
new->next = 0;
return (new);
}
/* add an existing fingerprint entry to an existing or not index */
t_fingerprint *add(t_fingerprint *list, t_fingerprint *new)
{
if (! list)
return (new);
if (! new)
return (list);
new->next = list;
return (new);
}
/* delete an entry from a fingerprint list */
t_fingerprint *del(t_fingerprint *list, t_fingerprint *destroy)
{
int first = 1;
t_fingerprint *save;
t_fingerprint *tmp;
tmp = list;
while (tmp)
{
if (tmp == destroy)
{
if (first == 1)
{
/* we re destroying the list */
if (! tmp->next)
return (0);
else
/* we're jumping on the next one */
return (tmp->next);
}
else /* not the first */
{
/* if followed */
if (tmp->next)
{
save->next = tmp->next;
return (list);
}
else /* not followed */
{
save->next = 0;
return (list);
}
}
}
first = 0;
save = tmp;
tmp = tmp->next;
}
return (tmp);
}