-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
66 lines (60 loc) · 1.89 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shell.h"
#include "socket.h"
int main(int argc, char *argv[]) {
char *host = NULL;
char *port = NULL;
if (argc > 1) {
if (argc > 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
goto print_usage;
// parse host:port
char *sp;
if ((sp = strchr(argv[1], ':'))) {
host = strndup(argv[1], sp - argv[1]);
port = strdup(sp + 1);
} else {
host = strdup(argv[1]);
port = strdup("25575");
}
if (!host || !port) goto print_usage;
} else {
host = strdup("localhost");
port = strdup("25575");
}
int connfd = socket_tryconnect(host, port);
if (connfd < 0) {
fprintf(stderr, "Failed to connect to %s:%s\n", host, port);
goto print_usage;
}
if (host) free(host);
if (port) free(port);
shell_loop(connfd);
close(connfd);
return 0;
print_usage:
if (host) free(host);
if (port) free(port);
printf(
"\n Nicholas' Minecraft RCON Client (nmcrcon)\n"
" Copyright (C) 2025 Nicholas Wang <me@nicho1as.wang>\n"
"\n"
" This program is free software: you can redistribute it and/or "
"modify\n"
" it under the terms of the GNU General Public License as published "
"by\n"
" the Free Software Foundation, either version 3 of the License.\n"
"\n"
" This program is distributed in the hope that it will be useful,\n"
" but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
" GNU General Public License for more details.\n"
"\n"
" You should have received a copy of the GNU General Public License\n"
" along with this program. If not, see "
"<https://www.gnu.org/licenses/>.\n\n");
printf("Usage: %s [HOST[:PORT]]\n", argv[0]);
return 1;
}