-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
78 lines (73 loc) · 3.2 KB
/
utils.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
#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "utils.h"
#include "unistd.h"
#include "defaults.h"
#include "logger.h"
void print_banner() {
printf("\n\n%s\n", " .\".");
printf("%s\n", " / |");
printf("%s\n", " / /");
printf("%s\n", " / ,\"");
printf("%s\n", " .-------.--- /");
printf("%s\n", " \"._ __.-/ o. o\\ Follow the_ _ _ _ _ ");
printf("%s\n", " \" ( Y ) =========| |===(_) |=================| |===| |===(_) |== ");
printf("%s\n", " ) / __ _| |__ _| |_ ___ _ __ __ _| |__ | |__ _| |_ ");
printf("%s\n", " / ( \\ \\ /\\ / / '_ \\| | __/ _ \\ | '__/ _` | '_ \\| '_ \\| | __| ");
printf("%s\n", " / Y \\ V V /| | | | | || __/ | | | (_| | |_) | |_) | | |_ ");
printf("%s\n", " .-\" | \\_/\\_/ |_| |_|_|\\__\\___| |_| \\__,_|_.__/|_.__/|_|\\__| ");
printf("%s\n", " / _ \\ \\ ======================================================== ");
printf("%s\n", " / `. \". ) /' ) ");
printf("%s\n", " Y )( / /(,/ ");
printf("%s\n", " ,| / ) ");
printf("%s\n", " ( | / /");
printf("%s\n", " \" \\_ (__ (__ ");
printf("%s\n\n\n", " \"-._,)--._,)");
}
uint32_t get_port() {
const char *port_str = getenv(PORT_ENV_KEY);
if (port_str == NULL) {
return DEFAULT_PORT;
}
const size_t port_size = strlen(port_str);
if (port_size != 4) {
log_message(stderr, "[ERRO]: PORT is not 4-sized, returning default.\r\n");
// exit(EXIT_FAILURE);
return DEFAULT_PORT;
}
// Use of strtol
char *end_ptr;
const uint32_t port = strtol(port_str, &end_ptr, 10);
if (port_str == end_ptr || port == 0) {
log_message(stderr, "[ERRO]: PORT is not a valid number, returning default.\r\n");
return DEFAULT_PORT;
}
return port;
}
uint32_t get_thread_pool_size() {
const char *threads_str = getenv(THREAD_POOL_SIZE_KEY);
if (threads_str == NULL) {
return DEFAULT_THREAD_POOL_SIZE;
}
const size_t threads_str_size = strlen(threads_str);
if (threads_str_size < 2) {
log_message(stderr, "[ERRO]: THREAD_POOL_SIZE_KEY must have al least one digit, returning default.\r\n");
// exit(EXIT_FAILURE);
return DEFAULT_THREAD_POOL_SIZE;
}
// Use of strtol
char *end_ptr;
const uint32_t threads = strtol(threads_str, &end_ptr, 10);
if (threads <= 1) {
log_message(stderr, "[ERRO]: THREAD_POOL_SIZE_KEY must be 1 or more, returning default.\r\n");
return DEFAULT_THREAD_POOL_SIZE;
}
return threads;
}
void get_hostname(char *hostname) {
if (gethostname(hostname, DEFAULT_HOSTNAME_BUFFER_SIZE) == -1) {
strncpy(hostname, DEFAULT_HOSTNAME, DEFAULT_HOSTNAME_LENGTH);
}
}