<commit_before>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <signal.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>

int verbose = 0;
int slots = 32;
char *port = NULL;
int family = AF_UNSPEC;

int server = 0;
int *clients = NULL;

static struct option options[] = {
    {"verbose", no_argument, 0, 'v'},
    {"port", required_argument, 0, 'p'},
    {"ipv4", required_argument, 0, '4'},
    {"ipv6", required_argument, 0, '6'},
    {"slots", required_argument, 0, 'n'},
    {0, 0, 0, 0}
};

int parse_args(int argc, char **argv){
    char c;
    while((c = getopt_long(argc, argv, "vp:n:46", options, NULL)) != -1){
        switch(c){
            case 'v':
                verbose = 1;
                break;
            case 'p':
                port = optarg;
                break;
            case 'n':
                slots = atoi(optarg);
                if(slots <= 0){
                    fprintf(stderr, "invalid number of slots: %s\n", optarg);
                    return 0;
                }
                break;
            case '4':
                family = AF_INET;
                break;
            case '6':
                family = AF_INET6;
                break;
            default:
                return 0;
        }
    }
    if(optind != argc){
        fputs("invalid arguments\n", stderr);
        return 0;
    }
    if(port == NULL){
        fputs("no port specified\n", stderr);
        return 0;
    }
    return 1;
}

int server_init(){
    struct addrinfo *addrs, *addr;
    struct addrinfo hints;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = family;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    int rc = getaddrinfo(NULL, port, &hints, &addrs);
    if(rc != 0){
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rc));
        return 0;
    }
    for(addr = addrs; addr != NULL; addr = addr->ai_next){
        server = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
        if(server == -1)
            continue;
        if(bind(server, addr->ai_addr, addr->ai_addrlen) != -1)
            break;
        close(server);
    }
    freeaddrinfo(addrs);
    if(addr == NULL)
        return 0;
    rc = listen(server, slots);
    if(rc == -1){
        perror("listen");
        close(server);
        return 0;
    }
    if(verbose)
        fprintf(stderr, "listening on %s\n", port);
    return 1;
}

int server_accept(){
    struct sockaddr_in incomer;
    socklen_t len = sizeof(incomer);
    memset(&incomer, 0, len);
    int fd = accept(server, (struct sockaddr*) &incomer, &len);
    if(fd == -1){
        perror("accept");
        return 0;
    }
    int i;
    for(i=0; i<slots; i++){
        if(clients[i] == -1){
            clients[i] = fd;
            if(verbose)
                fprintf(stderr, "incoming connection (fd %d)\n", fd);
            return 1;
        }
    }
    fputs("can't accept more connections\n", stderr);
    close(fd);
    return 0;
}

void shut(int *fd){
    if(fd < 0)
        return;
    shutdown(*fd, SHUT_RDWR);
    close(*fd);
    if(verbose)
        fprintf(stderr, "connection closed (fd %d)\n", *fd);
    *fd = -1;
}

void clean_up(){
    close(server);
    int i;
    for(i=0; i<slots; i++)
        shut(clients+i);
    free(clients);
    exit(EXIT_SUCCESS);
}

int forward(int src){
    static char buffer[512];<commit_msg>fixed stupid condition in shut()<commit_after>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <signal.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>

int verbose = 0;
int slots = 32;
char *port = NULL;
int family = AF_UNSPEC;

int server = 0;
int *clients = NULL;

static struct option options[] = {
    {"verbose", no_argument, 0, 'v'},
    {"port", required_argument, 0, 'p'},
    {"ipv4", required_argument, 0, '4'},
    {"ipv6", required_argument, 0, '6'},
    {"slots", required_argument, 0, 'n'},
    {0, 0, 0, 0}
};

int parse_args(int argc, char **argv){
    char c;
    while((c = getopt_long(argc, argv, "vp:n:46", options, NULL)) != -1){
        switch(c){
            case 'v':
                verbose = 1;
                break;
            case 'p':
                port = optarg;
                break;
            case 'n':
                slots = atoi(optarg);
                if(slots <= 0){
                    fprintf(stderr, "invalid number of slots: %s\n", optarg);
                    return 0;
                }
                break;
            case '4':
                family = AF_INET;
                break;
            case '6':
                family = AF_INET6;
                break;
            default:
                return 0;
        }
    }
    if(optind != argc){
        fputs("invalid arguments\n", stderr);
        return 0;
    }
    if(port == NULL){
        fputs("no port specified\n", stderr);
        return 0;
    }
    return 1;
}

int server_init(){
    struct addrinfo *addrs, *addr;
    struct addrinfo hints;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = family;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    int rc = getaddrinfo(NULL, port, &hints, &addrs);
    if(rc != 0){
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rc));
        return 0;
    }
    for(addr = addrs; addr != NULL; addr = addr->ai_next){
        server = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
        if(server == -1)
            continue;
        if(bind(server, addr->ai_addr, addr->ai_addrlen) != -1)
            break;
        close(server);
    }
    freeaddrinfo(addrs);
    if(addr == NULL)
        return 0;
    rc = listen(server, slots);
    if(rc == -1){
        perror("listen");
        close(server);
        return 0;
    }
    if(verbose)
        fprintf(stderr, "listening on %s\n", port);
    return 1;
}

int server_accept(){
    struct sockaddr_in incomer;
    socklen_t len = sizeof(incomer);
    memset(&incomer, 0, len);
    int fd = accept(server, (struct sockaddr*) &incomer, &len);
    if(fd == -1){
        perror("accept");
        return 0;
    }
    int i;
    for(i=0; i<slots; i++){
        if(clients[i] == -1){
            clients[i] = fd;
            if(verbose)
                fprintf(stderr, "incoming connection (fd %d)\n", fd);
            return 1;
        }
    }
    fputs("can't accept more connections\n", stderr);
    close(fd);
    return 0;
}

void shut(int *fd){
    if(*fd < 0)
        return;
    shutdown(*fd, SHUT_RDWR);
    close(*fd);
    if(verbose)
        fprintf(stderr, "connection closed (fd %d)\n", *fd);
    *fd = -1;
}

void clean_up(){
    close(server);
    int i;
    for(i=0; i<slots; i++)
        shut(clients+i);
    free(clients);
    exit(EXIT_SUCCESS);
}

int forward(int src){
    static char buffer[512];
{'fraction_non_alphanumeric': 0.15223665223665223, 'fraction_numerical': 0.015007215007215007, 'mean_word_length': 1.8843112775697046, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2379860', 'n_tokens_mistral': 2464, 'n_tokens_neox': 2373, 'n_words': 589}