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