Created
February 7, 2023 00:40
-
-
Save gdetor/668ab2fe704e742126264a76ec27e096 to your computer and use it in GitHub Desktop.
Converts a symbolic path to an absolute one (C, bash)
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
#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