Compare commits

...

13 Commits

Author SHA1 Message Date
Eyal Sawady a663003bc5
Makefile: remove -DVERSION="unknown"
This is handled by the #ifndef in main.c
2020-12-11 14:19:50 -05:00
Eyal Sawady 584ddb3654
Makefile: fix redirection
&>/dev/null is unspecified by POSIX
2020-12-11 14:15:33 -05:00
Ultracoolguy 83d06b048e
Revert "Rewrite Makefile"
This reverts commit 94b7c83fd4.
2020-12-11 13:59:43 -05:00
Eyal Sawady c3f2d9d552
Improve socket error handling 2020-12-09 12:39:59 -05:00
Philip K 6693c97749
Ensure port number is valid 2020-12-06 12:56:17 -05:00
Eyal Sawady 9a742e0ca6
.gitignore: add *.o 2020-12-06 11:44:35 -05:00
Philip K 94b7c83fd4
Rewrite Makefile 2020-12-06 11:42:44 -05:00
Philip K a990d49313
Catch all undefined flags 2020-12-06 11:41:33 -05:00
Ultracoolguy 6d74f2c4b9
Add ifndef guard for VERSION 2020-12-02 14:48:22 -05:00
j3s f9cf6faa70
Make /skip work properly 2020-12-02 03:10:50 -05:00
j3s 4aeebd4f53
Correct murder being gendered 2020-12-02 00:26:25 -05:00
Ultracoolguy e69c479dd5
Make Makefile slightly more readable 2020-11-30 15:33:47 -05:00
Ultracoolguy 6ff83c2761
Print version of among-sus on client connection 2020-11-30 15:08:44 -05:00
3 changed files with 36 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
among-sus
build
*.o

View File

@ -1,6 +1,9 @@
.POSIX:
all:
$(CC) -o among-sus $(CFLAGS) main.c $(LDFLAGS)
$(CC) -o among-sus $(CFLAGS) \
$$(git rev-parse --short HEAD >/dev/null 2>/dev/null && \
printf -- '-DVERSION="%s"' "$$(git rev-parse --short HEAD)") \
main.c $(LDFLAGS)
clean:
rm -f among-sus

41
main.c
View File

@ -17,6 +17,10 @@
#include <unistd.h>
#include <getopt.h>
#ifndef VERSION
#define VERSION "unknown"
#endif
#define NUM_PLAYERS 10
#define NUM_SHORT 6
#define NUM_LONG 2
@ -430,7 +434,7 @@ check_win_condition(void)
}
if (nalive == 1) {
broadcast("The impostor is alone with the last crewmate and murders him", -1);
broadcast("The impostor is alone with the last crewmate and murders them", -1);
snprintf(buf, sizeof(buf), "The impostor was [%s] all along...", players[iid].name);
broadcast(buf, -1);
end_game();
@ -681,8 +685,9 @@ discussion(size_t pid, char *input)
}
}
if(vote < -1 || vote > NUM_PLAYERS-1 || players[vote].fd == -1
|| players[vote].stage != PLAYER_STAGE_DISCUSS) {
if (vote != -1 && (vote < -1 || vote > NUM_PLAYERS-1
|| players[vote].fd == -1
|| players[vote].stage != PLAYER_STAGE_DISCUSS)) {
snprintf(buf, sizeof(buf), "Invalid vote, no such player\n");
write(players[pid].fd, buf, strlen(buf));
return;
@ -1371,6 +1376,9 @@ welcome_player(int fd)
continue;
}
snprintf(buf, sizeof(buf), "among-sus server: version %s\n", VERSION);
write(fd, buf, strlen(buf));
if(state.stage != STAGE_LOBBY) {
snprintf(buf, sizeof(buf), "There is a game in progress, waiting for the match to finish...\n");
write(fd, buf, strlen(buf));
@ -1406,13 +1414,20 @@ main(int argc, char *argv[])
int opt;
while ((opt = getopt(argc, argv, "hp:")) != -1) {
switch (opt) {
case 'p':
port = strtol(optarg, &endptr, 10);
case 'p':;
errno = 0;
long _port = strtol(optarg, &endptr, 10);
if (*endptr != '\0' || errno != 0 ||
_port <= 0 || _port >= 65536) {
fprintf(stderr, "Invalid port: %s\n", optarg);
exit(EXIT_FAILURE);
}
port = (uint16_t) _port;
break;
case 'h':
printf("%s", usage);
exit(EXIT_SUCCESS);
case '?':
default:
printf("%s", usage);
exit(EXIT_FAILURE);
}
@ -1428,19 +1443,25 @@ main(int argc, char *argv[])
}
srand((unsigned)time(NULL));
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
listen6_fd = socket(AF_INET6, SOCK_STREAM, 0);
if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("IPv4 socket");
exit(EXIT_FAILURE);
}
if ((listen6_fd = socket(AF_INET6, SOCK_STREAM, 0)) == -1) {
perror("IPv6 socket");
exit(EXIT_FAILURE);
}
i = 1;
if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR,
&i, sizeof(i))) {
perror("setsockopt");
perror("IPv4 setsockopt");
exit(EXIT_FAILURE);
}
if (setsockopt(listen6_fd, SOL_SOCKET, SO_REUSEADDR,
&i, sizeof(i))) {
perror("setsockopt");
perror("IPv6 setsockopt");
exit(EXIT_FAILURE);
}