#include<fcntl.h>#include<stdio.h>#include<sys/time.h>#include<sys/wait.h>#include<unistd.h>/* config, avec un qwerty en exemple */structconfig{intkey;char**cmd;};staticchar*echo_q[]={"echo","touche q active",NULL};staticchar*echo_w[]={"echo","touche","w","active",NULL};staticchar*sleep_9[]={"sleep","9",NULL};staticstructconfigmyconf[]={/* { key code, cmd } */{16,echo_q},{17,echo_w},{18,sleep_9},};intmain(intargc,char*argv[]){intkbd,pid;structconfig*p;struct{structtimevaltime;unsignedshorttype;unsignedshortcode;unsignedintvalue;}ev;if(argc!=2)return2;kbd=open(argv[1],O_RDONLY);if(kbd<0)return1;loop_forever:read(kbd,&ev,sizeof(ev));if(1==ev.type&&1==ev.value){for(p=myconf;p<myconf+sizeof(myconf)/sizeof(structconfig);p++)if(p->key==ev.code){pid=fork();if(0==pid){if(0==fork())/* execlp pour exclure la recherche dans $PATH */execvp(p->cmd[0],p->cmd);return0;}else{if(pid>0)waitpid(pid,NULL,0);}gotoloop_forever;}/* pour avoir le codekey, sinon commenter: */printf("%d:%d:%d\n",ev.type,ev.code,ev.value);}gotoloop_forever;}
[^] # Re: Côté Xorg...
Posté par PR . En réponse au message Lancer une commande lors de l'appui sur une touche du clavier. Évalué à 3.
Voici le code sans zombies :
Mort aux cons !