Advertisement
gocha

Remove relative directory symbols from path (Unix/Linux)

Jan 28th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4.  
  5. static char *path_remove_reldir(const char * path, char *normalized_path)
  6. {
  7. #ifdef WIN32
  8.     // Not implemented (I think nobody needs it)
  9.     errno = EINVAL;
  10.     return NULL;
  11. #else
  12.     int ilastsep = -1;
  13.     int iread = 0;
  14.     int iwrite = 0;
  15.  
  16.     if (path == NULL || normalized_path == NULL)
  17.     {
  18.         errno = EINVAL;
  19.         return NULL;
  20.     }
  21.  
  22.     do
  23.     {
  24.         if (path[iread] == PATH_SEPARATOR_CHAR || path[iread] == '\0')
  25.         {
  26.             int len_dir = iread - (ilastsep + 1);
  27.  
  28.             if (len_dir == 2 && path[ilastsep + 1] == '.' && path[ilastsep + 2] == '.')
  29.             {
  30.                 // ".."
  31.  
  32.                 // Find the separator of parent directory.
  33.                 iwrite -= 2;
  34.                 while (iwrite >= 0 && normalized_path[iwrite] != PATH_SEPARATOR_CHAR)
  35.                 {
  36.                     iwrite--;
  37.                 }
  38.                 // Raise an error if not available.
  39.                 if (iwrite < 0)
  40.                 {
  41.                     normalized_path[0] = '\0';
  42.                     errno = EINVAL;
  43.                     return NULL;
  44.                 }
  45.  
  46.                 // Prevent erasing root directory symbol by NUL.
  47.                 if (iwrite == 0 && normalized_path[iwrite] == PATH_SEPARATOR_CHAR &&
  48.                     path[iread] == '\0')
  49.                 {
  50.                     iwrite++;
  51.                 }
  52.  
  53.                 // overwrite separator/NUL character.
  54.                 normalized_path[iwrite] = path[iread];
  55.                 iwrite++;
  56.             }
  57.             else
  58.             {
  59.                 if (len_dir == 1 && path[ilastsep + 1] == '.')
  60.                 {
  61.                     // note that "./" must not be converted to "/".
  62.                     if (iwrite > 0)
  63.                     {
  64.                         iwrite--;
  65.  
  66.                         // prevent erasing root directory symbol by NUL.
  67.                         if (iwrite == 0 && normalized_path[iwrite] == PATH_SEPARATOR_CHAR &&
  68.                             path[iread] == '\0')
  69.                         {
  70.                             iwrite++;
  71.                         }
  72.  
  73.                         normalized_path[iwrite] = path[iread];
  74.                         iwrite++;
  75.                     }
  76.                 }
  77.                 else
  78.                 {
  79.                     memcpy(&normalized_path[iwrite], &path[ilastsep + 1], len_dir);
  80.                     iwrite += len_dir;
  81.                     normalized_path[iwrite] = path[iread];
  82.                     iwrite++;
  83.                 }
  84.             }
  85.  
  86.             ilastsep = iread;
  87.         }
  88.     } while (path[iread++] != '\0');
  89.  
  90.     return normalized_path;
  91. #endif
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement