Last active
December 22, 2015 04:49
-
-
Save bones-codes/6419544 to your computer and use it in GitHub Desktop.
locate /dev/input/event no. with popen()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <fcntl.h> | |
char event_path[30]; | |
char *get_event_path(void){ | |
FILE *event_num; | |
char data[10]; | |
const char path[] = "/dev/input/event"; | |
/* setup a pipe for reading and execute the command */ | |
event_num = popen("/bin/grep 'sysrq kbd event' /proc/bus/input/devices | /usr/bin/awk -F 'event' '{print $2}'","r"); | |
if(!event_num){ | |
fprintf(stderr, "Could not open pipe for output\n"); | |
exit(1); | |
} | |
/* grab data from grep/awk execution */ | |
fgets(data, sizeof(data), event_num); | |
/* merge path and data in single variable */ | |
snprintf(event_path, sizeof(event_path), "%s%s", path, data); | |
if (pclose(event_num) != 0){ | |
fprintf(stderr," Error: Failed to close command stream\n"); | |
exit(1); | |
} | |
return event_path; | |
} | |
int main(void) { | |
int input_device; | |
get_event_path(); | |
input_device = open(event_path, O_RDONLY); | |
if (-1 == input_device) { | |
fprintf(stderr, "ERROR: Could not open %s\n", event_path); | |
exit(1); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment