~aritra1911/mc_server

0ccfbc0befd6a1cf0d16fde01cffb6884566498e — Aritra Sarkar 1 year, 11 months ago ecb6f8e
Compile more cleanly on FreeBSD
5 files changed, 18 insertions(+), 16 deletions(-)

M Makefile
M Makefile.gmake
M events.c
M main.c
M response.c
M Makefile => Makefile +2 -1
@@ 4,7 4,8 @@ PROGS				=	server

CFLAGS				=	-std=iso9899:1999 -pedantic -pedantic-errors -Wall -Wextra \
					-Werror-implicit-function-declaration -Wno-reserved-id-macro \
					-Wno-missing-prototypes -g -O0 -fno-fast-math -fno-builtin
					-Wno-missing-prototypes -Wno-thread-safety-analysis -Wno-padded \
					-Wno-cast-align -g -O0 -fno-fast-math -fno-builtin
CFLAGS_x86_64-linux-gcc		=	-m64
CFLAGS_i686-linux-gcc		=	-m32
CFLAGS_x86_64-linux-clang	=	-Weverything -m64

M Makefile.gmake => Makefile.gmake +4 -4
@@ 1,12 1,12 @@
CFLAGS		=	-std=iso9899:1999 -pedantic -pedantic-errors -Wall -Wextra \
			-Werror-implicit-function-declaration -Wno-reserved-id-macro \
			-Wno-missing-prototypes -g -O0 -fno-fast-math -fno-builtin
			-Wno-missing-prototypes -Wno-thread-safety-analysis -Wno-padded \
			-Wno-cast-align -g -O0 -fno-fast-math -fno-builtin
CPPFLAGS	=	-D_XOPEN_SOURCE=600 -D_POSIX_PTHREAD_SEMANTICS \
			-D_LARGEFILE64_SOURCE
LDFLAGS		=	-pthread

SRCS = main.c session.c response.c tpool.c events.c helpers.c
OBJS = $(SRCS:.c=.o)
SRCS		=	main.c session.c response.c tpool.c events.c helpers.c
OBJS		=	$(SRCS:.c=.o)

.PHONY: all clean


M events.c => events.c +5 -4
@@ 471,7 471,7 @@ recv_and_send(int thread_id, void *arg)
            sprintf(buf + 1, "%s connection closed",
                    sessionp->clients[i].nick);
            buf[0] = RES_QUIT;
            num_bytes = 1 + strlen(buf + 1);
            num_bytes = (ssize_t) (1 + strlen(buf + 1));
            announce = 1;
        }



@@ 483,7 483,7 @@ recv_and_send(int thread_id, void *arg)
                     buf, &num_bytes, &err_code) == -1 ) {

            buf[0] = RES_ERROR;
            buf[1] = err_code;
            buf[1] = (char) err_code;
            num_bytes = 2;
        }



@@ 523,7 523,7 @@ disconnect:
    if ( announce ) {
        printf("DBUG : [THREAD #%02i] Announcing...\n", thread_id);
        if ( buf[0] == RES_ERROR ) {
            if ( send(fd, buf, num_bytes, 0) == -1 ) {
            if ( send(fd, buf, (size_t) num_bytes, 0) == -1 ) {
                get_err_str(errno, err_str);
                fprintf(stderr, " ERR : [THREAD #%02i] %s:%i =>"
                                      " `send()` to fd = %i failed: %s\n",


@@ 532,7 532,8 @@ disconnect:
        } else {
            pthread_rwlock_rdlock(&sessionp->lock);
            for (i = 1 /* avoid listener */; i < sessionp->fd_count; i++) {
                if ( send(sessionp->pfds[i].fd, buf, num_bytes, 0) == -1 ) {
                if ( send(sessionp->pfds[i].fd,
                          buf, (size_t) num_bytes, 0) == -1 ) {
                    get_err_str(errno, err_str);
                    fprintf(stderr, " ERR : [THREAD #%02i] %s:%i =>"
                                          " `send()` to fd = %i failed: %s\n",

M main.c => main.c +3 -3
@@ 40,7 40,6 @@

#define NUM_THREADS 16
#define MAX_QUEUE_SZ 32
#define MAX_ARGS 4
#define BACKLOG 10

typedef struct {


@@ 68,7 67,7 @@ static int get_listener(prefs_t *prefs)
     */

    struct addrinfo hints, *res, *p;
    int status, listener, yes;
    int status, listener=-1, yes;
    char err_str[STRERROR_BUFLEN];

    /* `memset()` `hints` to zeroes, and set a few necessary fields. This way


@@ 301,7 300,8 @@ int main(int argc, char **argv)
    }

    while ( 1 ) {
        if ( (num_events = poll(session.pfds, session.fd_count, -1)) == -1 ) {
        if ( (num_events = poll(session.pfds,
                                (nfds_t) session.fd_count, -1)) == -1 ) {
            if ( errno == EINTR ) {
                /* We got Ctrl-C'ed, we need to gracefully release
                 * any allocated memory in heap and then exit */

M response.c => response.c +4 -4
@@ 43,10 43,10 @@ static inline int append_nick(ResponseCode res_code, const char *nick,
    strcpy(temp + 1, nick); /* Put user's nick first */
    msg_ptr = temp + 1 + nick_len;  /* Get to where user's nick ends */
    *msg_ptr++ = ' ';   /* Put whitespace separator there and move forward */
    memcpy(msg_ptr, buf + 1, *len - 1); /* Copy actual message next */
    memcpy(msg_ptr, buf + 1, (size_t) *len - 1); /* Copy actual message next */
    *len += nick_len + 1;   /* Recompute new length including
                               the nick and a whitespace */
    memcpy(buf, temp, *len);    /* Finally copy back everything */
    memcpy(buf, temp, (size_t) *len);    /* Finally copy back everything */

    return 0;
}


@@ 69,7 69,7 @@ static inline int set_nick(SendCode snd_code, client_t *client,
                           char *buf, ssize_t *len, ResponseError *err_code)
{
    char nick[NICKLEN] = "";
    strncpy(nick, buf + 1, (NICKLEN < *len ? NICKLEN : *len) - 1);
    strncpy(nick, buf + 1, (size_t) (NICKLEN < *len ? NICKLEN : *len) - 1);

    if ( !*nick ) {
        if ( err_code )


@@ 102,7 102,7 @@ int respond(client_t *client, const client_t *clients_list, size_t list_size,
            char *buf, ssize_t *len, ResponseError *err_code)
{
    /* Extract the first byte as the send code */
    SendCode snd_code = buf[0];
    SendCode snd_code = (SendCode) buf[0];

    /* Assume everything's gonna be fine */
    if ( err_code )