home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / fileutil / tcggrep2.arj / NORM_FIL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-24  |  3.7 KB  |  142 lines

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    routine to normalise a path name, replacing '.' and '..' entries        */
  4. /*    J Segrave 00:39:59 Sat Nov 24 1990 originate                            */
  5. /*                                                                            */
  6. /****************************************************************************/
  7.  
  8. #include    <stdio.h>
  9. #include    <stdlib.h>
  10. #include    <string.h>
  11. #include    <dir.h>
  12.  
  13. /****************************************************************************/
  14. /*                                                                            */
  15. /*    routine to remove any self references from a path name - used since        */
  16. /*    Novel Netware does not have '.' or '..' entries, so stat() on these        */
  17. /*    fails. We will take the oldpath name and replace all the '.' and '..'    */
  18. /*    bits with the appropriate directory names                                */
  19. /*    return TRUE (non-zero) on error                                            */
  20. /*                                                                            */
  21. /****************************************************************************/
  22.  
  23. int norm_path (char *fullpath, char *oldpath)
  24.     {
  25.     char    buf[256],            /*    working copy of source                    */
  26.             cwd[256],            /*    current working dir of drive            */
  27.             *bp,
  28.             *cp,
  29.             *firstp, *lastp,    /*    ptrs to first and last '/' in target    */
  30.             *nextp;                /*    ptr to next '/' in source                */
  31.     int        drive = 0;            /*    set for default drive                    */
  32.  
  33.     /*    we want to work on a copy of the source    */
  34.                 
  35.     strcpy (buf, oldpath);
  36.  
  37.     /*    normalise the name to lowercase with forward slashes    */
  38.     
  39.     strlwr (buf);
  40.     for (bp = buf; *bp != '\0'; ++bp)
  41.         if (*bp == '\\')
  42.             *bp = '/';
  43.  
  44.     /*    if there's a drive letter, extract it    */
  45.  
  46.     bp = buf;
  47.     if ((strlen (buf) >= 2) && (buf[1] == ':'))
  48.         {
  49.         strncpy (fullpath, buf, 2);
  50.         fullpath[2] = '\0';
  51.         bp += 2;                        /*    skip past drive letters    */
  52.         drive = (fullpath[0] - 'a' + 1);
  53.         }
  54.     else
  55.         fullpath[0] = '\0';
  56.  
  57.     if (strlen (bp) == 0)
  58.         strcat (bp, ".");
  59.  
  60.     /*    get a copy of the current working directory for the drive    */
  61.  
  62.     if (getcurdir (drive, cwd) != 0)
  63.         return (1);
  64.         
  65.     /*    normalise current working directory    */
  66.     
  67.     strlwr (cwd);
  68.     for (cp = cwd; *cp != '\0'; ++cp)
  69.         if (*cp == '\\')
  70.             *cp = '/';
  71.  
  72.     /*    ensure source & current working dir end in  a '/'    */
  73.     /*    Turbo C's getcurdir never has leading or trailing    */
  74.     /*    separators.                                            */
  75.     
  76.     strcat (cwd, "/");
  77.     if (*(bp + strlen (bp) - 1) != '/')
  78.         strcat (buf, "/");
  79.  
  80.     /*    if the input path name is not from the root, copy     */
  81.     /*    the current working directory to the target            */
  82.  
  83.     strcat (fullpath, "/");
  84.     if (*bp != '/')
  85.         {
  86.         strcat (fullpath, cwd);
  87.         }
  88.     else
  89.         ++bp;                    /*    discard leading slash    */
  90.  
  91.     /*    now begin copying the source path to the target,     */
  92.     /*    coping with '.' and '..' as they occur                */
  93.  
  94.     firstp = strchr (fullpath, '/');    /*    track first and    */
  95.     lastp = strrchr (fullpath, '/');    /*    last '/'s        */
  96.  
  97.     while (*bp != '\0')
  98.         {
  99.         if (strncmp (bp, "../", 3) == 0)
  100.             {
  101.             /*    remove one element from the target (ignore    */
  102.             /*    any attempt to go back past the root)        */
  103.  
  104.             if (firstp != lastp)
  105.                 {
  106.                 *lastp = '\0';
  107.                 lastp = strrchr (fullpath, '/');
  108.                 lastp[1] = '\0';
  109.                 }
  110.  
  111.             bp += 3;
  112.             continue;
  113.             }
  114.  
  115.         /*    discard any refernces to self    */
  116.         
  117.         if (strncmp (bp, "./", 2) == 0)
  118.             {
  119.             bp += 2;
  120.             continue;
  121.             }
  122.  
  123.         /*    copy one directory element to the target    */
  124.  
  125.         nextp = strchr (bp, '/');    /*    terminate source at '/'        */
  126.         *nextp = '\0';
  127.         strcat (fullpath, bp);        /*    and add element to target    */
  128.         strcat (fullpath, "/");        /*      with a separator...        */
  129.         lastp = strrchr (fullpath, '/');
  130.         bp = nextp + 1;                /*    to next source element        */
  131.  
  132.         }
  133.  
  134.     /*    remove trailing '/', unless it's the root    */
  135.  
  136.     if (lastp != firstp)
  137.         *lastp = 0;
  138.  
  139.     return (0);
  140.     }
  141.                 
  142.