1#include <stdio.h>
2#include <string.h>
3#include <stdarg.h>
4#include <stdlib.h>
5#include <fcntl.h>
6#include <unistd.h>
7#include <errno.h>
8
9#include <sys/types.h>
10#include <sys/stat.h>
11
12#define DBG 0
13
14#if defined(__GNUC__) && (__GNUC__ > 3)
15__attribute__((format(printf, 1, 2), noreturn))
16#endif
17static void die(const char *format, ...)
18{
19	if (DBG) {
20		va_list args;
21
22		va_start(args, format);
23		vfprintf(stderr, format, args);
24		va_end(args);
25	}
26
27	exit(1);
28}
29
30int main(int argc, char *argv[])
31{
32	struct stat st;
33	char buf[1024];
34	int len, fd;
35
36	if (argc != 2)
37		die("Usage: xf86-video-intel-backlight-helper <iface>\n");
38
39	if (strchr(argv[1], '/') != NULL)
40		die("Invalid interface '%s': contains '/'\n", argv[1]);
41
42	if (snprintf(buf, sizeof(buf),
43		     "/sys/class/backlight/%s/brightness",
44		     argv[1]) >= sizeof(buf))
45		die("Invalid interface '%s': name too long\n", argv[1]);
46
47	fd = open(buf, O_RDWR);
48	if (fd < 0 || fstat(fd, &st) || major(st.st_dev))
49		die("Invalid interface '%s': unknown backlight file\n", argv[1]);
50
51	while (fgets(buf, sizeof(buf), stdin)) {
52		len = strlen(buf);
53		if (write(fd, buf, len) != len)
54			die("Failed to update backlight interface '%s': errno=%d\n", argv[1], errno);
55	}
56
57	return 0;
58}
59