#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char **argv) //传递参数:LED位置序号,控制亮/灭信息
{
int on; //亮/灭信息 1-亮,0-灭
int led_no; //LED位置信息 0-3
int fd;
if (argc != 3 || sscanf(argv[1], "%d", &led_no) != 1 || sscanf(argv[2],"%d", &on)
!= 1 ||on < 0 || on > 1 || led_no < 0 || led_no > 3) {
fprintf(stderr, "Usage: leds led_no 0|1\n");
exit(1); } //将第1个参数赋值给led_no,将第2个参数赋值给on,做边界判断
fd = open("/dev/leds0", 0); //系统open
if (fd < 0) {
fd = open("/dev/leds", 0); } //系统open
if (fd < 0) {
perror("open device leds");
exit(1); }
ioctl(fd, on, led_no); //系统ioctl
close(fd); //系统close
return 0;
}