Skip to content

Instantly share code, notes, and snippets.

@gdetor
Created February 7, 2023 00:40
Show Gist options
  • Save gdetor/668ab2fe704e742126264a76ec27e096 to your computer and use it in GitHub Desktop.
Save gdetor/668ab2fe704e742126264a76ec27e096 to your computer and use it in GitHub Desktop.
Converts a symbolic path to an absolute one (C, bash)
#include <stdio.h>
#include <stdlib.h>
#define PATH_MAX 3000
/* Compile with -D_BSD_SOURCE */
/* sym2actual converts a path to an absolute one
*
* Args:
* symlinkpath (pointer to char): The input path string
*
* Returns:
* a pointer at the beggin of absolute path string
*
*/
char *sym2actual(char *symlinkpath) {
char *actualpath;
char *ptr;
actualpath = (char *)malloc(PATH_MAX*sizeof(char));
if (!actualpath) goto fail;
ptr = realpath(symlinkpath, actualpath);
if (!ptr) goto fail;
return actualpath;
fail:
printf("Cannot resolve full path!\n");
exit(-2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment