#include <sys/param.h>
|
#include <stdlib.h>
|
char *
realpath (const char *pathname, char resolved_path[PATH_MAX]); |
The realpath function will resolve both absolute and relative paths and return the absolute pathname corresponding to pathname. All but the last component of pathname must exist when realpath is called.
#include<stdlib.h> #include<stdio.h> //printf #include<sys/stat.h> //S_IWUSR #include<sys/syslimits.h> //PATH_MAX #include<unistd.h> //chdir int main() { char resolvepath[PATH_MAX]; FILE *fp = NULL; char *rpath = NULL; int isymlink = 0; fp = fopen("c:\\xyz.txt", "w"); if(!fp) { printf("fopen failed!!"); return -1; } mkdir("c:\\tmdir", S_IWUSR); int c = chdir("c:\\"); if(c == -1) { printf("chdir failed!!"); return -1; } rpath = realpath(".\\tmdir\\..\\xyz.txt", resolvepath); printf("resolvepath: %s\n", resolvepath); if(rpath != NULL) printf("rpath: %s\n\n", rpath); fclose(fp); rmdir("c:\\tmdir"); unlink("c:\\xyz.txt"); mkdir("c:\\tdir", S_IWUSR); fp = fopen("c:\\tdir\\xyz.txt", "w"); if(!fp) { printf("fopen failed!!"); return -1; } fclose(fp); unlink("c:\\linkname.txt"); isymlink = symlink("c:\\tdir\\xyz.txt", "c:\\linkname.txt"); if (isymlink == -1) { printf("symlink failed!!"); return -1; } rpath = realpath("c:\\linkname.txt", resolvepath); printf("resolvepath: %s\n", resolvepath); if(rpath != NULL) printf("rpath: %s\n", rpath); unlink("c:\\tdir\\xyz.txt"); rmdir("c:\\tdir"); return 0; }
Output
resolvepath: C:\xyz.txt rpath: C:\xyz.txt resolvepath: c:\tdir\xyz.txt rpath: c:\tdir\xyz.txt
This function may permit buffer overflow depeding upon its user.
© 2005-2007 Nokia |