synk

synchronize files between hosts
Log | Files | Refs | README | LICENSE

commit e355d45e1f7b7db572874e8e0a5770470a42a0a6
parent d19f7b2d444e65f4d780ac3132968e432110ede9
Author: Willy <willyatmailoodotorg>
Date:   Tue Aug 16 08:48:11 +0200

Moving client/server part into functions

Diffstat:
synk.c | 47++++++++++++++++++++++++++++-------------------
synkd.c | 55++++++++++++++++++++++++++++++++-----------------------
2 files changed, 60 insertions(+), 42 deletions(-)
diff --git a/synk.c b/synk.c @@ -22,23 +22,12 @@ usage(char *name) } int -main(int argc, char *argv[]) +client(in_addr_t host, in_port_t port, const char *fn) { int cfd; - char *argv0; size_t len = 0; - uint16_t port = SERVER_PORT; - uint32_t host = INADDR_LOOPBACK; struct sockaddr_in clt; - char path[PATH_MAX] = "", ts[32] = ""; - - ARGBEGIN{ - case 'h': host = atol(EARGF(usage(argv0))); break; - case 'p': port = atoi(EARGF(usage(argv0))); break; - }ARGEND; - - if (argc < 2) - return 1; + char path[PATH_MAX] = "", ts[PATH_MAX] = ""; if ((cfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { perror("socket"); @@ -55,17 +44,37 @@ main(int argc, char *argv[]) return 1; } - snprintf(path, PATH_MAX, "%s", argv[1]); + snprintf(path, PATH_MAX, "%s", fn); len = strnlen(path, PATH_MAX); - if (send(cfd, path, len, 0) < 0) { - perror("send"); + printf("%s: %s\n", inet_ntoa(clt.sin_addr), path); + if ((len = write(cfd, path, len)) < 0) { + perror("write"); return -1; } - send(cfd, "\n", 1, 0); - - recv(cfd, ts, 32, 0); + read(cfd, ts, PATH_MAX); printf("%s: %s\n", path, ts); + close(cfd); + + return 0; +} + +int +main(int argc, char *argv[]) +{ + char *argv0; + in_port_t port = SERVER_PORT; + in_addr_t host = INADDR_LOOPBACK; + + ARGBEGIN{ + case 'h': host = inet_network(EARGF(usage(argv0))); break; + case 'p': port = atoi(EARGF(usage(argv0))); break; + }ARGEND; + + if (argc < 1) + usage(argv0); + + client(host, port, argv[0]); return 0; } diff --git a/synkd.c b/synkd.c @@ -29,29 +29,26 @@ void * handle_client(void *arg) { int i = 0; - char buf, path[PATH_MAX] = "", ts[32] = ""; + 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 = recv(c->fd, &buf, 1, 0)) > 0) { + while ((len = read(c->fd, &path, PATH_MAX)) > 0) { if (i > PATH_MAX) { printf("%s: filename too long (>%d)\n", inet_ntoa(c->in), PATH_MAX); break; } - if (buf == '\n' || buf == '\0') { - printf("%s: %s\n", inet_ntoa(c->in), path); - stat(path, &sb); - snprintf(ts, 32, "%lu", sb.st_mtim.tv_sec); - len = strnlen(ts, 32); - send(c->fd, ts, len, 0); - memset(path, 0, PATH_MAX); - i = 0; - } else { - path[i++] = buf; - } + path[len] = '\0'; + printf("%s: %s\n", inet_ntoa(c->in), path); + stat(path, &sb); + snprintf(ts, 32, "%lu", sb.st_mtim.tv_sec); + len = strnlen(ts, 32); + write(c->fd, ts, len); + memset(path, 0, PATH_MAX); + i = 0; } close(c->fd); @@ -86,20 +83,11 @@ loop(int sfd) } int -main(int argc, char *argv[]) +server(in_addr_t host, in_port_t port) { int sfd; - char *argv0; - uint16_t port = LISTEN_PORT; - uint32_t host = INADDR_LOOPBACK; struct sockaddr_in srv; - ARGBEGIN{ - case 'p': - port = atoi(EARGF(usage(argv0))); - break; - }ARGEND; - if ((sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { perror("socket"); return 1; @@ -122,3 +110,24 @@ main(int argc, char *argv[]) return loop(sfd); } + +int +main(int argc, char *argv[]) +{ + char *argv0; + in_port_t port = LISTEN_PORT; + in_addr_t host = INADDR_LOOPBACK; + + ARGBEGIN{ + case 'h': + host = inet_network(EARGF(usage(argv0))); + break; + case 'p': + port = atoi(EARGF(usage(argv0))); + break; + }ARGEND; + + server(host, port); + + return 0; +}