#include #include #include #include int main(int argc, char **argv) { char *cwd=argv[1]; char path[MAXPATHLEN]; char ncwd[MAXPATHLEN]; char *argt; char *p; int rem; strncpy(path, argv[2], MAXPATHLEN); /* * assertion: * cwd : "/" or ("/name")* where name != "." or ".." */ if ( *path == '/' ) ncwd[0]=0x00; else { if ( 0==strcmp( cwd, "/") ) ncwd[0]=0x00; else strncpy(ncwd, cwd, MAXPATHLEN); } rem = MAXPATHLEN - strlen(ncwd); for( argt = strtok( path, "/" ); argt != NULL; argt = strtok( NULL, "/" ) ) { if ( 0==strcmp( argt, ".") ) continue; if ( 0==strcmp( argt, "..")) { if ( p = (strrchr(ncwd, (int)'/'))) { *p = 0x00; rem = MAXPATHLEN - strlen(ncwd); } } else { rem -= 1 + strlen(argt); if ( rem > 0 ) { strcat( ncwd, "/" ); strcat( ncwd, argt ); } else { errno = ENAMETOOLONG; return -1; } } } puts( ncwd ); return 0; }