]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - util.c
Replace gcc by cc in Makefile
[dragonblocks_alpha.git] / util.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <arpa/inet.h>
5 #include "util.h"
6
7 const char *program_name;
8
9 void syscall_error(const char *err)
10 {
11         perror(err);
12         exit(EXIT_FAILURE);
13 }
14
15 void internal_error(const char *err)
16 {
17         fprintf(stderr, "%s: %s\n", program_name, err);
18         exit(EXIT_FAILURE);
19 }
20
21 unsigned short get_port_from_args(int argc, char **argv, int index)
22 {
23         if (argc <= index)
24                 internal_error("missing port");
25
26         unsigned int port = atoi(argv[index]);
27
28         if (port == 0 || port > USHRT_MAX)
29                 internal_error("invalid port");
30
31         return htons(port);
32 }