~q3cpma/mus

ref: cf29c2237d0c99e5240a55895c07564ef4635392 mus/mus_player/mus_udsend.c -rw-r--r-- 2.5 KiB
cf29c223q3cpma Add a #pragma once to the new xoshiro128plus.h header 1 year, 7 months ago
                                                                                
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <errno.h>
#include <getopt.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

#include "misc.h"
#include "log.h"

#define SUN_PATH_LEN (sizeof(struct sockaddr_un) - \
		offsetof(struct sockaddr_un, sun_path))
#define RECV_BUF_LEN 1024


_Noreturn void usage(int exit_status)
{
	printf(
		"NAME\n"
		"    %s - UNIX domain socket message sender\n"
		"\n"
		"SYNOPSIS\n"
		"    %s [-h] UD_SOCK [STRING] \n"
		"\n"
		"DESCRIPTION\n"
		"    Send STRING to the UNIX domain socket UD_SOCK and output the "
		"answer to stdout. If no STRING is given, send stdin instead.\n"
		"\n"
		"OPTIONS\n"
		"    -h\n"
		"        Print this notice and exit.\n"
		"\n"
		"    -v\n"
		"        Print version, supported formats and exit.\n"
		"\n",
		PROG_NAME, PROG_NAME);
	exit(exit_status);
}

int main(int argc, char **argv)
{
	for (int opt; (opt = getopt(argc, argv, "hv")) != -1; )
	{
		switch (opt)
		{
			case 'h':
				usage(EXIT_SUCCESS);

			case 'v':
				printf("Git commit: %s\n", PROG_VERSION);
				exit(EXIT_SUCCESS);

			default:
				usage(EXIT_FAILURE);
		}
	}
	if (argc - optind < 1)
		usage(EXIT_FAILURE);

	signals_nointerrupt();
	log_init(LOG_TYPE_STDERR, PROG_NAME);

	if (strlen(argv[1]) > SUN_PATH_LEN - 1)
		LOG_DIE("Socket path is too long, max length %zu", SUN_PATH_LEN - 1);

	static char buf[RECV_BUF_LEN];
	int conn_sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (conn_sock == -1)
		LOG_DIE("socket: %s", strerror(errno));

	struct sockaddr_un addr = {AF_UNIX, {0}};
	strcpy(addr.sun_path, argv[1]);

	if (connect(conn_sock, (struct sockaddr *) &addr,
			sizeof(struct sockaddr_un)) == -1)
		LOG_DIE("connect: %s", strerror(errno));

	if (argc - optind == 2)
	{
		if (!send_full(conn_sock, argv[optind + 1], strlen(argv[optind + 1])))
			LOG_DIE("send: %s", strerror(errno));
	}
	else
	{
		for (ssize_t ret; (ret = read(STDIN_FILENO, buf, sizeof(buf))); )
		{
			if (ret == -1)
				LOG_DIE("read(STDIN_FILENO): %s", strerror(errno));
			if (!send_full(conn_sock, buf, ret))
				LOG_DIE("send: %s", strerror(errno));
		}
	}
	shutdown(conn_sock, SHUT_WR);

	ssize_t rcvd = recv_full(conn_sock, buf, sizeof(buf));
	if (rcvd == -1)
		LOG_DIE("recv: %s", strerror(errno));
	if (rcvd == 0)
		fprintf(stderr, "No response received\n");
	else
	{
		if (rcvd == sizeof(buf) + 1)
		{
			fprintf(stderr, "Message got truncated (max length: %zu)\n",
				sizeof(buf));
			--rcvd;
		}
		printf("%.*s\n", (int)rcvd, buf);
	}
	xclose(conn_sock);
}