tRemove dependency to pthreads - synk - synchronize files between hosts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit b3dca9f5dc37c9e6190b5f6532ce9099b49ad2e7
 (DIR) parent 01aea7ee2faa01002207932529972d3c491dfca1
 (HTM) Author: Willy <willyatmailoodotorg>
       Date:   Tue, 16 Aug 2016 20:36:27 +0200
       
       Remove dependency to pthreads
       
       Diffstat:
         M config.mk                           |       2 +-
         M mkfile                              |       4 +---
         M synk.c                              |      32 ++++++++++++++-----------------
       
       3 files changed, 16 insertions(+), 22 deletions(-)
       ---
 (DIR) diff --git a/config.mk b/config.mk
       t@@ -9,4 +9,4 @@ MANDIR = ${PREFIX}/man
        CPPFLAGS = -DVERSION=\"${VERSION}\"
        CFLAGS = ${CPPFLAGS} -Wall -Wextra -pedantic -g
        LDFLAGS =
       -LIBS = -lpthread
       +LIBS =
 (DIR) diff --git a/mkfile b/mkfile
       t@@ -1,8 +1,6 @@
        <config.mk
        
       -all:V: synk
       -
       -%: %.o
       +synk: synk.o
                $LD -o $target $prereq $LDFLAGS $LIBS
        
        %.o: %.c
 (DIR) diff --git a/synk.c b/synk.c
       t@@ -1,5 +1,4 @@
        #include <limits.h>
       -#include <pthread.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
       t@@ -33,14 +32,13 @@ usage(char *name)
                exit(1);
        }
        
       -void *
       -handleclient(void *arg)
       +int
       +handleclient(struct client_t *c)
        {
                int i = 0;
                char path[PATH_MAX] = "", ts[32] = "";
                size_t len = 0;
                struct stat sb;
       -        struct client_t *c = *(struct client_t **)arg;
        
                printf("%s: connected\n", inet_ntoa(c->in));
                while ((len = read(c->fd, &path, PATH_MAX)) > 0) {
       t@@ -62,8 +60,8 @@ handleclient(void *arg)
                close(c->fd);
                len = 0;
                printf("%s: disconnected\n", inet_ntoa(c->in));
       -        free(c);
       -        pthread_exit((int *)&len);
       +
       +        return 0;
        }
        
        int
       t@@ -75,7 +73,6 @@ server(in_addr_t host, in_port_t port)
                struct sockaddr_in clt;
                struct sockaddr_in srv;
                struct client_t *c = NULL;
       -        pthread_t th;
        
                if ((sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
                        perror("socket");
       t@@ -97,19 +94,18 @@ server(in_addr_t host, in_port_t port)
                        return 1;
                }
        
       -        for (;;) {
       -                len = sizeof(clt);
       -                if ((cfd = accept(sfd, (struct sockaddr *)&clt, &len)) < 0) {
       -                        perror("accept");
       -                        return 1;
       -                }
       +        len = sizeof(clt);
       +        if ((cfd = accept(sfd, (struct sockaddr *)&clt, &len)) < 0) {
       +                perror("accept");
       +                return 1;
       +        }
        
       -                c = malloc(sizeof(struct client_t));
       -                c->fd = cfd;
       -                c->in = clt.sin_addr;
       +        c = malloc(sizeof(struct client_t));
       +        c->fd = cfd;
       +        c->in = clt.sin_addr;
        
       -                pthread_create(&th, NULL, handleclient, &c);
       -        }
       +        handleclient(c);
       +        free(c);
        
                return 0;
        }