summaryrefslogtreecommitdiffstats
path: root/mplay.c
blob: 0c76ea585a7e1433f75df14423fd8ef4fc510b30 (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
72
73
74
75
76
77
78
79
80
81
#include <unistd.h>
#include <dirent.h>

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void
die(const char *fmt, ...)
{
	va_list ap;

	fputs("mplay: ", stderr);
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	if (*fmt && fmt[strlen(fmt)-1] == ':') {
		perror(NULL);
	} else {
		fputc('\n', stderr);
	}
	exit(1);
}

int
main(int argc, char **argv)
{
	char *path, *bin;
	char *tok, *sep, *end;
	char **arg, *fmt;
	struct dirent *ent;
	DIR *dir;

	if (argc < 2) die("mplay [-d] [-f FMT] ... FILE[.FMT]");

	path = strdup(getenv("PATH"));
	if (!path) die("PATH not set");

	fmt = NULL;
	for (arg = argv; arg[1]; arg++) {
		if (!strcmp(arg[1], "-f")) {
			fmt = arg[2];
			arg++;
		} else {
			break;
		}
	}
	arg[0] = argv[0];

	if (fmt == NULL) {
		fmt = strrchr(argv[argc - 1], '.');
		if (!fmt) die("no file extension");
		fmt += 1;
	}

	tok = path;
	do {
		sep = strchr(tok, ':');
		end = sep ? sep : tok + strlen(tok);
		*end = '\0';
		dir = opendir(tok);
		while (dir && (ent = readdir(dir))) {
			if (!strncmp(ent->d_name, "mplay.", 6)
					&& !strcmp(ent->d_name + 6, fmt)) {
				bin = malloc(strlen(tok) + strlen(ent->d_name) + 2);
				if (!bin) die("malloc:");
				strcpy(bin, tok);
				strcat(bin, "/");
				strcat(bin, ent->d_name);
				closedir(dir);
				execv(bin, arg);
				die("execv %s:", bin);
			}
		}
		if (dir) closedir(dir);
		tok = sep + 1;
	} while (sep);

	die("no handler");
}