fork download
  1. #define _POSIX_C_SOURCE 200112L
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7.  
  8. int main(int argc, char *argv[], char *envp[])
  9. {
  10. char self[1024];
  11. size_t rc = readlink("/proc/self/exe", self, sizeof(self));
  12. if (rc != -1) {
  13. self[rc] = '\0';
  14. } else {
  15. strcpy(self, "<unknown>");
  16. }
  17.  
  18. printf("Real executable name is '%s'\n", self);
  19.  
  20. for (int i = 0; i < argc; i++) {
  21. printf(" argv[%i] = \"%s\"\n", i, argv[i]);
  22. }
  23.  
  24. printf("(Re)executing '%s'...\n\n", argv[0]);
  25. fflush(stdout);
  26.  
  27. execve(argv[0], (char *[]) { "huipizda", "arg1", "arg2", NULL}, envp);
  28. printf("Unable to run '%s' (%i): %s\n", argv[0], errno, strerror(errno));
  29. }
  30.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
Real executable name is '/home/0GkWuP/prog'
    argv[0] = "./prog"
(Re)executing './prog'...

Real executable name is '/home/0GkWuP/prog'
    argv[0] = "huipizda"
    argv[1] = "arg1"
    argv[2] = "arg2"
(Re)executing 'huipizda'...

Unable to run 'huipizda' (2): No such file or directory