Problem:
On linux machine, I want to read the link target line. From the documentation, I found the following code example (without error handling):
struct stat sb; ssize_t r; char * linkname; lstat("<some link>", &sb); linkname = malloc(sb.st_size + 1); r = readlink("/proc/self/exe", linkname, sb.st_size + 1);
The problem is that sb.st_size returns 0 for links on my system.
So, how to allocate memory dynamically for readline on such systems?
Many thanks!
One possible solution:
In future. Using the points made by jilles:
struct stat sb; ssize_t r = INT_MAX; int linkSize = 0; const int growthRate = 255; char * linkTarget = NULL; // get length of the pathname the link points to if (lstat("/proc/self/exe", &sb) == -1) { // could not lstat: insufficient permissions on directory? perror("lstat"); return; } // read the link target into a string linkSize = sb.st_size + 1 - growthRate; while (r >= linkSize) { // ie symlink increased in size since lstat() or non-POSIX compliant filesystem // allocate sufficient memory to hold the link linkSize += growthRate; free(linkTarget); linkTarget = malloc(linkSize); if (linkTarget == NULL) { // insufficient memory fprintf(stderr, "setProcessName(): insufficient memory\n"); return; } // read the link target into variable linkTarget r = readlink("/proc/self/exe", linkTarget, linkSize); if (r < 0) { // readlink failed: link was deleted? perror("lstat"); return; } } linkTarget[r] = '\0'; // readlink does not null-terminate the string
source share