int test_dot(void) { errorf("\n"); /* prints the function name */ File *file = newfile("", "."); assert(strcmp(getname(file), ".") == 0); assert(strcmp(getpath(file), ".") == 0); char *dir = getdirname(file); assert(strcmp(dir, ".") == 0); free(dir); free(file); return 0; }
int test_bare_file(void) { errorf("\n"); /* prints the function name */ File *file = newfile(".", "buf.c"); assert(strcmp(getname(file), "buf.c") == 0); assert(strcmp(getpath(file), "./buf.c") == 0); char *dir = getdirname(file); assert(strcmp(dir, ".") == 0); free(dir); char *base = getbasename(file); assert(strcmp(base, "buf.c") == 0); free(base); free(file); return 0; }
int test_relative_file(void) { errorf("\n"); /* prints the function name */ File *file = newfile(".", "../tmp/foo"); assert(strcmp(getname(file), "../tmp/foo") == 0); assert(strcmp(getpath(file), "./../tmp/foo") == 0); char *dir = getdirname(file); assert(strcmp(dir, "./../tmp") == 0); free(dir); char *base = getbasename(file); assert(strcmp(base, "foo") == 0); free(base); free(file); return 0; }
File *gettarget(File *file) { if (!file) { errorf("file is NULL\n"); return NULL; } if (!islink(file)) { errorf("%s is not a symlink\n", file->path); return NULL; } if (!file->target) { char targetpath[PATH_MAX]; /* note: readlink(3p) not readlink(2) */ errno = 0; int nchars = readlink(file->path, targetpath, sizeof(targetpath)-1); if (nchars == -1) { errorf("Error getting symlink target: %s\n", strerror(errno)); return NULL; } else if (nchars == sizeof(targetpath)-1) { errorf("Symlink target too long for buffer\n"); return NULL; } targetpath[nchars] = '\0'; char *dir = getdirname(file); if (dir == NULL) { errorf("getdirname returned NULL\n"); return NULL; } file->target = newfile(dir, targetpath); free(dir); if (file->target == NULL) { errorf("newfile returned NULL\n"); return NULL; } } return file->target; }