summary refs log tree commit diff
path: root/.bin/pbd.c
blob: 713800d4f857e32980d9a01900a418a60c77c6c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#if 0
exec clang -Weverything $@ -o $(dirname $0)/pbd $0
#endif

#include <arpa/inet.h>
#include <err.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sysexits.h>
#include <unistd.h>

int main() {
    int server = socket(PF_INET, SOCK_STREAM, 0);
    if (server < 0) err(EX_OSERR, "socket");

    struct sockaddr_in addr = {
        .sin_family = AF_INET,
        .sin_port = htons(7062),
        .sin_addr = { .s_addr = htonl(0x7f000001) },
    };

    if (bind(server, (struct sockaddr *) &addr, sizeof(addr)) < 0)
        err(EX_OSERR, "bind");

    if (listen(server, 1) < 0)
        err(EX_OSERR, "listen");

    for (;;) {
        int client = accept(server, NULL, NULL);
        if (client < 0) err(EX_OSERR, "accept");

        pid_t pid_paste = fork();
        if (pid_paste < 0) err(EX_OSERR, "fork");

        if (pid_paste) {
            if (waitpid(pid_paste, NULL, 0) < 0)
                warn("waitpid");
            // TODO: Check child status.
        } else {
            if (dup2(client, STDOUT_FILENO) < 0)
                err(EX_OSERR, "dup2");
            if (execlp("pbpaste", "pbpaste") < 0)
                err(EX_OSERR, "execlp");
        }

        uint8_t x;
        ssize_t peek = recv(client, &x, 1, MSG_PEEK);
        if (peek < 0) err(EX_IOERR, "recv");

        if (peek) {
            pid_t pid_copy = fork();
            if (pid_copy < 0) err(EX_OSERR, "fork");

            if (pid_copy) {
                if (waitpid(pid_copy, NULL, 0) < 0)
                    warn("waitpid");
                // TODO: Check child status.
            } else {
                if (dup2(client, STDIN_FILENO) < 0)
                    err(EX_OSERR, "dup2");
                if (execlp("pbcopy", "pbcopy") < 0)
                    err(EX_OSERR, "execlp");
            }
        }

        if (close(client) < 0) warn("close");
    }
}