Created
October 4, 2012 19:39
-
-
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')
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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