File: oggfwd.c

package info (click to toggle)
oggfwd 0.2-6
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 132 kB
  • ctags: 50
  • sloc: ansic: 376; makefile: 75
file content (241 lines) | stat: -rw-r--r-- 4,967 bytes parent folder | download | duplicates (2)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * oggfwd    -- Forward an Ogg stream from stdin to an Icecast server
 *		A useful demonstration of the libshout API
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * This program is provided "as-is", with no explicit or implied warranties of
 * any kind.
 *
 * Copyright (C) 2003-2006,	J <j@v2v.cc>,
 *				rafael2k <rafael(at)riseup(dot)net>,
 *				Moritz Grimm <gtgbr@gmx.net>
 */
/* thanx to rhatto <rhatto (AT) riseup (DOT) net> and others at submidialogia :-P */

#include <sys/types.h>
#include <sys/param.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef NO_UNISTD_H
#  include <unistd.h>
#endif /* no-NO_UNISTD_H */

#include <shout/shout.h>

extern char    *__progname;
extern char    *optarg;
extern int	optind;
extern int	errno;

volatile sig_atomic_t	print_total = 0;
volatile sig_atomic_t	quit = 0;

#define BUFFERSIZE	4096

#if defined(__dead)
__dead void
#else
void
#endif /* __dead */
usage(void)
{
	printf("usage: %s "
	       "[-hp] "
	       "[-d description] "
	       "[-g genre] "
	       "[-n name] "
	       "[-u URL]\n"
	       "              address port password mountpoint\n",
	       __progname);
	exit(1);
}

void
sig_handler(int sig)
{
	switch (sig) {
	case SIGHUP:
		print_total = 1;
		break;
	case SIGTERM:
	case SIGINT:
		quit = 1;
		break;
	default:
		/* ignore */
		break;
	}
}

void
set_argument_string(char **param, char *opt, char optname)
{
	size_t siz;

	if (*param) {
		fprintf(stderr, "%s: Parameter -%c given multiple times\n",
			__progname, optname);
		usage();
	}

	siz = strlen(opt) + 1;
	if (siz >= MAXPATHLEN) {
		fprintf(stderr, "%s: Argument for parameter -%c too long\n",
			__progname, optname);
		exit(1);
	}

	if ((*param = malloc(siz)) == NULL) {
		fprintf(stderr, "%s: %s\n", __progname, strerror(errno));
		exit(1);
	}

	snprintf(*param, siz, "%s", opt);
}

int
main(int argc, char **argv)
{
	shout_t        *shout;
	unsigned char	buff[BUFFERSIZE];
	int		ret, ch;
	unsigned int	pFlag;
	char	       *description, *genre, *name, *url;
	size_t		bytes_read;
	unsigned short	port;
	unsigned long long total;

	pFlag = 0;
	description = genre = name = url = NULL;
	while ((ch = getopt(argc, argv, "d:g:hn:pu:")) != -1) {
		switch (ch) {
		case 'd':
			set_argument_string(&description, optarg, 'D');
			break;
		case 'g':
			set_argument_string(&genre, optarg, 'g');
			break;
		case 'n':
			set_argument_string(&name, optarg, 'n');
			break;
		case 'p':
			pFlag = 1;
			break;
		case 'u':
			set_argument_string(&url, optarg, 'u');
			break;
		case 'h':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 4) {
		fprintf(stderr, "%s: Wrong number of arguments\n", __progname);
		usage();
	}

	if ((shout = shout_new()) == NULL) {
		fprintf(stderr, "%s: Could not allocate shout_t\n",
			__progname);
		return (1);
	}

	if (shout_set_host(shout, argv[0]) != SHOUTERR_SUCCESS) {
		fprintf(stderr, "%s: Error setting hostname: %s\n", __progname,
			shout_get_error(shout));
		return (1);
	}

	if (sscanf(argv[1], "%hu", &port) != 1) {
		fprintf(stderr, "Invalid port `%s'\n", argv[1]);
		usage();
	}
	if (shout_set_port(shout, port) != SHOUTERR_SUCCESS) {
		fprintf(stderr, "%s: Error setting port: %s\n", __progname,
			shout_get_error(shout));
		return (1);
	}

	if (shout_set_password(shout, argv[2]) != SHOUTERR_SUCCESS) {
		fprintf(stderr, "%s: Error setting password: %s\n", __progname,
			shout_get_error(shout));
		return (1);
	}

	if (shout_set_mount(shout, argv[3]) != SHOUTERR_SUCCESS) {
		fprintf(stderr, "%s: Error setting mount: %s\n", __progname,
			shout_get_error(shout));
		return 1;
	}

	shout_set_format(shout, SHOUT_FORMAT_VORBIS);

	shout_set_public(shout, pFlag);

	if (description)
		shout_set_description(shout, description);

	if (genre)
		shout_set_genre(shout, genre);

	if (name)
		shout_set_name(shout, name);

	if (url)
		shout_set_url(shout, url);

	if (shout_open(shout) == SHOUTERR_SUCCESS) {
		printf("%s: Connected to server\n", __progname);
		total = 0;

		signal(SIGHUP, sig_handler);
		signal(SIGTERM, sig_handler);
		signal(SIGINT, sig_handler);

		while (quit == 0) {
			bytes_read = fread(buff, 1, sizeof(buff), stdin);
			total += bytes_read;

			if (bytes_read > 0) {
				ret = shout_send(shout, buff, bytes_read);
				if (ret != SHOUTERR_SUCCESS) {
					printf("%s: Send error: %s\n",
					       __progname,
					       shout_get_error(shout));
					quit = 1;
				}
			} else
				quit = 1;

			if (quit) {
				printf("%s: Quitting ...\n", __progname);
				print_total = 1;
			}

			if (print_total) {
				printf("%s: Total bytes read: %llu\n",
				       __progname, total);
				print_total = 0;
			}

			shout_sync(shout);
		}
	} else {
		fprintf(stderr, "%s: Error connecting: %s\n", __progname,
		       shout_get_error(shout));
		return 1;
	}

	shout_close(shout);

	return (0);
}