BlinkenArea - GitList
Repositories
Blog
Wiki
uproctrace
Code
Commits
Branches
Tags
Search
Tree:
840c0b1
Branches
Tags
master
uproctrace
libuptev
src
symlink.c
add copyright headers and license (LGPL)
Stefan Schuermans
commited
840c0b1
at 2020-05-24 15:59:07
symlink.c
Blame
History
Raw
/** * UProcTrace: User-space Process Tracing * Copyright 2020: Stefan Schuermans, Aachen, Germany <stefan@schuermans.info> * Copyleft: GNU LESSER GENERAL PUBLIC LICENSE version 3 (see LICENSE) */ #include "symlink.h" #include "cleaner.h" #include <stdlib.h> #include <unistd.h> /** * @brief read target of symlink * @param[in] pathname path to symbolic link * @return malloc-ed string containting link target or NULL */ static char *uptev_symlink_read_target(char const *pathname) { size_t sz = 256; char *target = NULL; while (1) { /* get buffer */ target = malloc(sz); if (!target) { return NULL; } /* get link target */ ssize_t len = readlink(pathname, target, sz); if (len < 0) { free(target); return NULL; } /* link target fit into buffer -> terminate string and return */ if ((size_t)len + 1 < sz) { target[len] = 0; return target; } /* free buffer and try again with larger buffer */ free(target); sz *= 2; } } char *uptev_symlink_read(char const *pathname, uptev_cleaner_t *cleaner) { char *target = uptev_symlink_read_target(pathname); if (!target) { return NULL; } /* add malloc-ed object to cleaner */ if (uptev_cleaner_add_ptr(cleaner, target) != 0) { return NULL; } /* success, return target string */ return target; }