sfxd/src/sfxc.c

43 lines
1.3 KiB
C
Raw Normal View History

2024-02-21 15:36:58 +03:00
#include "common.h"
2024-02-21 13:27:25 +03:00
#include <stdio.h>
2024-02-21 15:36:58 +03:00
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
2024-02-21 13:27:25 +03:00
2024-02-21 15:36:58 +03:00
int main(int argc, char **argv) {
struct sockaddr_un sa_server, sa_client;
int sock_fd;
static char buffer[BUFFER_SIZE];
EXPECT(sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0), > 0);
memset(&sa_server, 0, sizeof(sa_server));
memset(&sa_client, 0, sizeof(sa_client));
sa_server.sun_family = sa_client.sun_family = AF_UNIX;
snprintf(sa_client.sun_path, sizeof(sa_client.sun_path), CLIENT_SOCKET_PATH, (long)getpid());
strncpy(sa_server.sun_path, DAEMON_SOCKET_PATH, sizeof(sa_server.sun_path) - 1);
EXPECT(bind(sock_fd, (struct sockaddr *)&sa_client, sizeof(struct sockaddr_un)), == 0);
for (int i = 1; i < argc; i++) {
2024-02-22 13:24:41 +03:00
strncat(buffer, argv[i], BUFFER_SIZE - strlen(buffer) - 1);
2024-02-21 15:36:58 +03:00
if (i != argc - 1) {
strncat(buffer, " ", BUFFER_SIZE - strlen(buffer) - 1);
}
}
2024-02-22 13:24:41 +03:00
printf("send: '%s'\n", buffer);
2024-02-21 15:36:58 +03:00
EXPECT(sendto(sock_fd, buffer, strlen(buffer), 0, (struct sockaddr *)&sa_server, sizeof(sa_server)), == strlen(buffer));
int data_len;
while ((data_len = recv(sock_fd, buffer, BUFFER_SIZE, 0)) > 0) {
printf("%.*s", data_len, buffer);
}
unlink(sa_client.sun_path);
exit(EXIT_SUCCESS);
2024-02-21 13:27:25 +03:00
}