Skip to content

Instantly share code, notes, and snippets.

@sortie
Created September 28, 2016 21:06
Show Gist options
  • Select an option

  • Save sortie/6074a121f4cb7b4e6fa73e6bed5bb127 to your computer and use it in GitHub Desktop.

Select an option

Save sortie/6074a121f4cb7b4e6fa73e6bed5bb127 to your computer and use it in GitHub Desktop.
size_t size = 0;
char* buffer = NULL;
while ( true )
{
size = size ? 2 * size : 4096;
buffer = malloc(size);
if ( !buffer )
err(1, "malloc");
ssize_t used = readlink(...);
/* error check */
if ( (size_t) used == size )
{
free(buffer);
continue;
}
/* nul terminate */
break;
}
printf("%s\n", buffer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment