Skip to content

Instantly share code, notes, and snippets.

@mjtko
Created October 4, 2012 19:39
Show Gist options
  • Save mjtko/3835906 to your computer and use it in GitHub Desktop.
Save mjtko/3835906 to your computer and use it in GitHub Desktop.
Re-exec after splitting argv[1] by spaces (/usr/bin/env replacement that 'works')
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define MAX_ARGS 255
int main(const int argc, char** argv) {
const char delimiters[] = " ";
char *args[MAX_ARGS], *running, *token;
int c, i;
running = argv[1];
for ( c = 0; (token = strsep(&running, delimiters)) != NULL; ) {
args[c++] = token;
}
for ( i = 2; i < argc; ) {
args[c++] = argv[i++];
}
args[c] = NULL;
execvp(args[0], args);
printf("%s: %s\n", argv[0], strerror(errno));
return errno;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment