~kdsch/bl

0542a500ae4828d0f349a4518aa5383c357e13b9 — Karl Schultheisz 2 years ago b468ca8
lib: reduce number of syscalls

Nine is getting to a tolerable range.
1 files changed, 39 insertions(+), 20 deletions(-)

M lib.c
M lib.c => lib.c +39 -20
@@ 89,17 89,10 @@ die(const char *s)
}

static unsigned long
get(const char *pathname)
get(int fd)
{
	int fd = open(pathname, O_RDONLY, 0);

	if (-1 == fd) {
		die("get: open");
	}

	char buf[5] = {0};
	ssize_t rc = read(fd, buf, sizeof(buf));
	close(fd);

	if (-1 == rc) {
		die("read");


@@ 109,15 102,43 @@ get(const char *pathname)
}

static void
put(const char *pathname, unsigned long x)
put(int fd, unsigned long x)
{
	dprintf(fd, "%lu\n", x);
}

static unsigned long
path_get(const char *pathname)
{
	int fd = open(pathname, O_WRONLY | O_TRUNC, 0);
	int fd = open(pathname, O_RDONLY, 0);

	if (-1 == fd) {
		die("put: open");
		die("path_get: open");
	}

	unsigned long x = get(fd);
	close(fd);
	return x;
}

static void
scale(char dir)
{
	int fd = open(BR, O_RDWR | O_TRUNC, 0);

	if (-1 == fd) {
		die("scale: open");
	}

	switch (dir) {
	case '+':
		put(fd, lim(inc(get(fd)), MAX_BRIGHTNESS));
		break;
	case '-':
		put(fd, dec(get(fd)));
		break;
	}

	dprintf(fd, "%lu\n", x);
	close(fd);
}



@@ 125,18 146,16 @@ int
app(int argc, char *argv[])
{
	if (argc < 2) {
		printf("%.3g\n", 100*(double)get(BR)/
		printf("%.3g\n", 100*(double)path_get(BR)/
		                     (double)MAX_BRIGHTNESS);
		return 0;
	}

	if (!strncmp("+", argv[1], 1)) {
		put(BR, lim(inc(get(BR)), MAX_BRIGHTNESS));
		return 0;
	}

	if (!strncmp("-", argv[1], 1)) {
		put(BR, dec(get(BR)));
	char dir = argv[1][0];
	switch (dir) {
	case '+':
	case '-':
		scale(dir);
		return 0;
	}