Skip to content

Instantly share code, notes, and snippets.

@joshkunz
Last active December 22, 2015 03:29
Show Gist options
  • Save joshkunz/6410613 to your computer and use it in GitHub Desktop.
Save joshkunz/6410613 to your computer and use it in GitHub Desktop.
Wrapper that will close VLC on OSX with a SIGKILL when it receives a SIGINT (useful if you want to terminate it from the command-line with Control-C).
/* Build, (with your favorite compiler) run:
* cc -o vlcwrap vlcwrap.c */
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define VLC_BIN "/Applications/VLC.app/Contents/MacOS/VLC"
pid_t child_pid = -1;
/* when we get an interrupt, kill the child process */
void sigint_handler(int signal) {
kill(child_pid, SIGKILL);
}
int main (int argc, char* argv[]) {
/* Create a new null-terminated array of arguments */
char ** child_args = malloc(sizeof(char *) * (argc + 1));
memcpy(child_args, argv, sizeof(char *) * argc);
/* set the first argument to the program we're running */
child_args[0] = VLC_BIN;
child_args[argc] = NULL;
signal(SIGINT, sigint_handler);
child_pid = fork();
/* if this is the child, run VLC */
if (child_pid == 0) {
execve(VLC_BIN, child_args, NULL);
perror("Couldn't start vlc");
exit(1);
/* otherwise, wait for VLC to exit (or a signal to terminate it) */
} else {
int status = 0;
waitpid(child_pid, &status, 0);
if (WIFEXITED(status)) {
exit(WEXITSTATUS(status));
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment