extreme
2016-08-31 15:21:51 +08:00
C 的 argv[0],哈哈哈……
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
char *argv[] = {"Hi, argv[0]", "argv[1]", NULL}, *env[] = {NULL};
if (execve("./print_argv_0", argv, env) < 0) {
perror("execve()");
return -1;
}
return 0;
}
print_argv_0.c:
#define _BSD_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int pid = getpid();
char buffer[100], result_buffer[100];
printf("%s\n", argv[0]);
sprintf(buffer, "/proc/%d/exe", pid);
printf("%s\n", realpath(buffer, result_buffer));
return 0;
}
Example output:
user@hostname:/tmp$ /tmp/main
Hi, argv[0]
/tmp/print_argv_0