Skip to content

Instantly share code, notes, and snippets.

@ryanwoodsmall
Created March 1, 2020 10:19
Show Gist options
  • Save ryanwoodsmall/bd018c75a424e86059b1cc496ae30036 to your computer and use it in GitHub Desktop.
Save ryanwoodsmall/bd018c75a424e86059b1cc496ae30036 to your computer and use it in GitHub Desktop.
edash.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char *findbin(char *b);
int main(int argc, char **argv, char **envp)
{
char *s = "dash";
char *e = "-E";
char *a[argc + 2];
char *f = findbin(s);
a[0] = f;
a[1] = e;
// https://wiki.gentoo.org/wiki/Embedded_Handbook/General/Compiling_with_qemu_user_chroot
if(argc > 1){
memcpy(&a[2], &argv[1], sizeof(*argv) * (argc - 1));
}
a[argc + 1] = NULL;
return(execve(f, a, envp));
}
char *findbin(char *s)
{
char *p = getenv("PATH");
char *a = "/";
char *t = strtok(p, ":");
while(t != NULL){
char *e;
e = malloc(strlen(t) + strlen(a) + strlen(s) + 1);
sprintf(e, "%s%s%s", t, a, s);
if(access(e, X_OK) != -1){
return(e);
}
free(e);
t = strtok(NULL, ":");
}
return(NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment