There is significant utility for such a function in a variety of contexts. Have fun with it!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ssize_t readlink (const char *filename, char *buffer, size_t size) | |
char *readlink_malloc (const char *filename) | |
{ | |
int size = 100; | |
char *buffer = NULL; | |
while (1) | |
{ | |
buffer = (char *) xrealloc (buffer, size); | |
int nchars = readlink (filename, buffer, size); | |
if (nchars < 0) | |
{ | |
free (buffer); | |
return NULL; | |
} | |
if (nchars < size) | |
return buffer; | |
size *= 2; | |
} | |
} |
No comments:
Post a Comment