-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.c
141 lines (80 loc) · 2.21 KB
/
support.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
#pragma once
#include "header.h"
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
//read a line from socket_descriptor channel, putting its content in *vptr.
ssize_t Readline( int socket_descriptor, void *vptr, size_t maxlenght ) {
ssize_t n, read_character;
char c, *buffer;
buffer = vptr;
for ( n = 1; n < maxlenght; n++ ) {
read_character = recv(socket_descriptor, &c, 1, MSG_NOSIGNAL );
if ( read_character == 1 ) { //a character has been read.
*buffer++ = c;
if ( c == '\n' ) break;
if ( c == '\0' ) break;
}
else {
if ( read_character == 0 ) {
if ( n == 1 ) return 0; //nothing to read.
else break;
}
else {
if ( errno == EINTR ) {
n = 0;
break;
}
}
}
}
*buffer = 0;
return n; //line lenght (number of read characters).
}
//write on socket descriptor channel.
ssize_t Writeline( int socket_descriptor , const void *vptr, size_t n ) {
size_t nleft; ssize_t nwritten; const char *buffer;
buffer = vptr;
nleft = n;
while ( nleft > 0 ) { //make sure of writing all characters on socket.
if ( ( nwritten = send( socket_descriptor, buffer, nleft, MSG_NOSIGNAL ) ) <= 0 ) {
if ( errno == EINTR ) nwritten = 0;
else {
close(socket_descriptor);
printf("\n Socket n° %d has been closed. Thread exits.", socket_descriptor);
fflush(stdout);
pthread_exit((void*)-1);
}
}
nleft -= nwritten;
buffer += nwritten;
}
return n;
}
int Lines_counter( FILE * f ) {
int ret, num_lines = 0;
char *line = NULL;
size_t len = 0;
while ( getline( &line , &len , f) != -1 ) {
num_lines ++;
}
return num_lines;
}
int Writefile( int socket_descriptor, FILE * stream ) {
int size = 0;
char *l = NULL;
size_t len = 0;
while ( getline( &l, &len, stream) != -1 ) {
size += Writeline( socket_descriptor, l, strlen(l) );
if (size == -1) return -1;
}
return size;
}
typedef struct seat_{
int line;
int place;
char is_reserved;
struct seat_ *next;
} seat;