Using the dirname function, the following example reads a
pathname, changes the current working directory to the parent
directory, and opens a file.
char path [MAXPATHLEN], *pathcopy;
int fd;
fgets(path, MAXPATHLEN, stdin);
pathcopy = strdup(path);
chdir(dirname(pathcopy));
fd = open(basename(path), O_RDONLY);