+static char * canonpath(const char *path, char *path_resolved)
+{
+ char path_copy[PATH_MAX];
+ char *path_cpy = path_copy;
+ char *path_res = path_resolved;
+
+ struct stat s;
+
+
+ /* relative -> absolute */
+ if( *path != '/' )
+ {
+ getcwd(path_copy, PATH_MAX);
+ strncat(path_copy, "/", PATH_MAX - strlen(path_copy));
+ strncat(path_copy, path, PATH_MAX - strlen(path_copy));
+ }
+ else
+ {
+ strncpy(path_copy, path, PATH_MAX);
+ }
+
+ /* normalize */
+ while( (*path_cpy != '\0') && (path_cpy < (path_copy + PATH_MAX - 2)) )
+ {
+ if( *path_cpy == '/' )
+ {
+ /* skip repeating / */
+ if( path_cpy[1] == '/' )
+ {
+ path_cpy++;
+ continue;
+ }
+
+ /* /./ or /../ */
+ else if( path_cpy[1] == '.' )
+ {
+ /* skip /./ */
+ if( (path_cpy[2] == '/') || (path_cpy[2] == '\0') )
+ {
+ path_cpy += 2;
+ continue;
+ }
+
+ /* collapse /x/../ */
+ else if( (path_cpy[2] == '.') &&
+ ((path_cpy[3] == '/') || (path_cpy[3] == '\0'))
+ ) {
+ while( (path_res > path_resolved) && (*--path_res != '/') )
+ ;
+
+ path_cpy += 3;
+ continue;
+ }
+ }
+ }
+
+ *path_res++ = *path_cpy++;
+ }
+
+ /* remove trailing slash if not root / */
+ if( (path_res > (path_resolved+1)) && (path_res[-1] == '/') )
+ path_res--;
+ else if( path_res == path_resolved )
+ *path_res++ = '/';
+
+ *path_res = '\0';
+
+ /* test access */
+ if( !stat(path_resolved, &s) && (s.st_mode & S_IROTH) )
+ return path_resolved;
+
+ return NULL;
+}