-
Notifications
You must be signed in to change notification settings - Fork 3
/
list.c
87 lines (77 loc) · 1.45 KB
/
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "sender.h"
static struct List list;
static struct ListingMessage message;
static int size;
void ListInit()
{
list.count = 0;
}
/* Add an item to the list of files
* Takes a string.
* Returns nothing.
*/
void ListAdd(char * string)
{
if (list.count < 56) {
strcpy(list.list[list.count], string);
list.count++;
}
}
/* Return the number of elements in the list.
* Takes no parameters.
*/
int ListCount()
{
return list.count;
}
/* Return a populated ListingMessage
* that is ready to be sent accross the
* network.
*
*/
struct ListingMessage ListGetMessage()
{
int i, length = 0;
char buf[20*56];
// Setup the message.
message.cont = 0xCC;
message.code = 0x4C;
message.count = htons(list.count);
// Add all the files.
for (i = 0; i < list.count; i++) {
strcpy(buf + length, list.list[i]);
length += strlen(list.list[i]) + 1;
buf[length - 1] = '\0';
}
message.entries = malloc(sizeof(char) * length);
memcpy(message.entries, buf, length);
size = length + 4;
return message;
}
/* Return the size of the message
* that will be sent over the network.
*/
int ListMessageSize()
{
return size;
}
/* Return the list of items.
*
*/
struct List ListGet()
{
return list;
}
int ListSearch(char *file)
{
int i;
for (i = 0; i < list.count; i++) {
if (strcmp(file, list.list[i]) == 0)
return 1;
}
return 0;
}