#include #include #include #include #include #include int change_directory(char * filename) { char path[PATH_MAX], * terminator; if (!realpath(filename, path) || !(terminator = strrchr(path, '/'))) { return -1; } *terminator = '\0'; return chdir(path); }