1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <unistd.h>
6 #include <sys/syslimits.h>
7
8 // On Darwin there's this secret fourth argument, 'apple', which is a pointer
9 // to a string that contains the executable path, like argv[0], but unlike
10 // argv[0] it can't be changed using exec().
11
main(int argc,char * argv[],char * envp[],char * apple[])12 int main(int argc, char *argv[], char *envp[], char *apple[])
13 {
14 char *pargv = calloc((PATH_MAX+1), sizeof(char)),
15 *pappl = calloc((PATH_MAX+1), sizeof(char));
16 int i;
17
18 for (i = 0; envp[i]; i++)
19 ;
20
21 // envp[i]==NULL; envp[i+1]==apple[0]==executable_path
22 assert(envp[i+1] == apple[0]);
23
24 // Make sure realpath(argv[0]) == realpath(apple[0]). (realpath resolves
25 // symlinks.)
26 realpath(argv[0], pargv);
27 realpath(apple[0], pappl);
28 assert(0 == strcmp(pargv, pappl));
29
30 return 0;
31 }
32
33