use execv()

using execv() instead of execl() allows to more simply run a command 
with arguments
xkbcommon
Alessandro Mauri 5 years ago
parent 8292e4df75
commit d71e2c98e1
  1. 17
      hkd.c

17
hkd.c

@ -54,7 +54,7 @@ int key_buffer_remove (struct key_buffer*, unsigned short);
int convert_key_value (unsigned short); int convert_key_value (unsigned short);
void int_handler (int signum); void int_handler (int signum);
void die (const char *, int); void die (const char *, int);
void exec_command(const char *); void exec_command(char *);
void update_descriptors_list (struct pollfd **, int *); void update_descriptors_list (struct pollfd **, int *);
// TODO: use getopts() to parse command line options // TODO: use getopts() to parse command line options
@ -159,11 +159,12 @@ int main (void)
case 1: case 1:
/* You can use keys defined in input.h */ /* You can use keys defined in input.h */
if (pb.buf[0] == KEY_MUTE) if (pb.buf[0] == KEY_MUTE)
exec_command("/home/ale/hello"); exec_command((char *)"/home/ale/hello");
break;
case 2: case 2:
if (pb.buf[0] == 56 || pb.buf[0] == 31) if (pb.buf[0] == 56 || pb.buf[0] == 31)
if (pb.buf[1] == 31 || pb.buf[1] == 56) if (pb.buf[1] == 31 || pb.buf[1] == 56)
exec_command("/home/ale/hello"); exec_command((char *)"/home/ale/hello");
break; break;
} }
} }
@ -243,18 +244,22 @@ void die (const char *msg, int err)
exit(err); exit(err);
} }
void exec_command (const char *path) void exec_command (char *path)
{ {
char *argv[] = {path, NULL};
switch (fork()) { switch (fork()) {
case -1: case -1:
die("Could not fork: %s", errno); die("Could not fork: %s", errno);
break; break;
case 0: case 0:
/* we are the child */ /* we are the child */
if(execl(path, path, (char *) NULL) != 0) if(execv(path, argv) < 0) {
/* execl only returns if an error occured, so we exit /* execv only returns if an error occured, so we exit
* otherwise we duplicate the process */ * otherwise we duplicate the process */
fprintf(stderr, ANSI_COLOR_RED "Could not run %s\n" ANSI_COLOR_RESET
, path);
exit(-1); exit(-1);
}
/* we shouldn't be here */ /* we shouldn't be here */
break; break;
} }

Loading…
Cancel
Save