-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.c
68 lines (58 loc) · 1.18 KB
/
util.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/errno.h>
// read user input from stdin
void readin(char *buffer, int len)
{
fgets(buffer, len, stdin);
if (strchr(buffer, '\n'))
*strchr(buffer, '\n') = 0;
}
// safely compare two strings
int cmp(char *str1, char *str2)
{
int str_len_1 = strlen(str1);
int str_len_2 = strlen(str2);
if (!(str_len_1 == str_len_2) | strncmp(str1, str2, str_len_1))
{
return 0;
}
return 1;
}
void free_all(char **buffer)
{
int i = 0;
while(buffer[i]) {
free(buffer[i]);
i++;
}
free(buffer);
}
void print_array(int * arr, int max_clients) {
for (int i = 0; i < max_clients; i++) {
printf("\t%d: %d", i, arr[i]);
}
}
int fix_array(int * arr, int max_clients) {
int i = 0;
int curr = 0;
for (i = 0; i < max_clients; i++) {
if (arr[i] != 0) {
arr[curr] = arr[i];
curr++;
}
}
while(curr < max_clients) {
arr[curr] = 0;
curr++;
}
return curr;
}
void check_error(int status)
{
if (status == -1)
{
printf("Error (%d): %s\n", errno, strerror(errno));
}
}