1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv)
{
char* cwd = calloc(FILENAME_MAX, 1),
* loc = calloc(FILENAME_MAX, 1);
getcwd(cwd, FILENAME_MAX);
printf("Current working directory: %s\n", cwd);
if (argv[0][0] == '/') {
strncpy(loc, argv[0], FILENAME_MAX);
} else {
size_t i = strlen(argv[0]);
while (--i) {
if (argv[0][i] == '/')
break;
}
strncpy(loc, &(argv[0][i]), FILENAME_MAX);
strncat(cwd, loc, FILENAME_MAX);
loc = cwd;
}
printf("Executable file location: %s\n", loc);
return 0;
}
|
$ ./tmp
Current working directory: /home/chris
Executable file location: /home/chris/tmp
[Fri Jul 9 22:05:01 BST 2010]-[~]
[chris@chris-al] $ /home/chris/tmp
Current working directory: /home/chris
Executable file location: /home/chris/tmp |