-
Notifications
You must be signed in to change notification settings - Fork 0
/
connections.c
89 lines (72 loc) · 1.43 KB
/
connections.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "connections.h"
connections *add_connection(connections *c, int descriptor)
{
connections *temp = malloc(sizeof(connections));
temp->descriptor = descriptor;
temp->read = 1;
temp->write = 1;
temp->next = c;
return temp;
}
void set_connection(connections *c, int descriptor, char read, char write)
{
while (c)
{
if (c->descriptor == descriptor)
{
c->read = read;
c->write = write;
return;
}
c = c->next;
}
}
connections *remove_connection(connections *c, int descriptor)
{
if (!c)
{
return c;
}
close(descriptor);
if (c->descriptor == descriptor)
{
connections *temp = c;
c = c->next;
free(temp);
return c;
}
connections *head = c;
while (c->next)
{
if (c->next->descriptor == descriptor)
{
connections *temp = c->next;
c->next = c->next->next;
free(temp);
return head;
}
c = c->next;
}
return head;
}
connections *free_connections(connections *c)
{
while (c)
{
connections *temp = c;
c = c->next;
free(temp);
}
return NULL;
}
void print_connections(connections *c)
{
while (c)
{
printf("descriptor: %d \n", c->descriptor);
c = c->next;
}
}