Skip to content

Instantly share code, notes, and snippets.

@bones-codes
Last active December 22, 2015 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bones-codes/6419544 to your computer and use it in GitHub Desktop.
Save bones-codes/6419544 to your computer and use it in GitHub Desktop.
locate /dev/input/event no. with popen()
#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