Write to Linux sysfs node in C

c, linux

Solution

Open the sysfs node like a file, write '1' to it, and close it again.

For example:

#include <stdio.h>
#include <fcntl.h>

void enable_led() {
  int fd;
  char d = '1';
  fd = open("sys/class/leds/NAME:COLOR:LOCATION/brightness", O_WRONLY);
  write (fd, &d, 1);
  close(fd);
}

Problem

From the shell I can activate the leds on my system like this: ``` #echo 1 > /sys/class/leds/NAME:COLOR:LOCATION/brightness ``` I want to do the exact same thing from a C program, but I have not been able to find a simple example on how to accomplish this?

Original source