home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FLN_FIX.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  141 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  FLN_FIX.C
  5. **
  6. **  Original Copyright 1988-1991 by Bob Stout as part of
  7. **  the MicroFirm Function Library (MFL)
  8. **
  9. **  The user is granted a free limited license to use this source file
  10. **  to create royalty-free programs, subject to the terms of the
  11. **  license restrictions specified in the LICENSE.MFL file.
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <dos.h>
  17. #include <io.h>
  18. #include "sniptype.h"
  19. #include "filnames.h"
  20.  
  21. /****************************************************************/
  22. /*                                                              */
  23. /*  Function to `crunch' dot directories and check for          */
  24. /*  DOS-valid path strings. Drive specifiers in the path        */
  25. /*  ignored.                                                    */
  26. /*                                                              */
  27. /****************************************************************/
  28.  
  29. char *fln_fix(char *path)
  30. {
  31.       Boolean_T dir_flag = False_, root_flag = False_;
  32.       char *r, *p, *q, *s;
  33.  
  34.       if (path)
  35.             strupr(path);
  36.  
  37.       /* Ignore leading drive specs   */
  38.  
  39.       if (NULL == (r = strrchr(path, ':')))
  40.             r = path;
  41.       else  ++r;
  42.  
  43.       unix2dos(r);                      /* Convert Unix to DOS style    */
  44.  
  45.       while ('\\' == *r)                /* Ignore leading backslashes   */
  46.       {
  47.             if ('\\' == r[1])
  48.                   strcpy(r, &r[1]);
  49.             else
  50.             {
  51.                   root_flag = True_;
  52.                   ++r;
  53.             }
  54.       }
  55.  
  56.       p = r;                            /* Change "\\" to "\"           */
  57.       while (NULL != (p = strchr(p, '\\')))
  58.       {
  59.             if ('\\' ==  p[1])
  60.                   strcpy(p, &p[1]);
  61.             else  ++p;
  62.       }
  63.  
  64.       while ('.' == *r)                 /* Scrunch leading ".\"         */
  65.       {
  66.             if ('.' == r[1])
  67.             {
  68.                   /* Ignore leading ".."                                */
  69.  
  70.                   for (p = (r += 2); *p && (*p != '\\'); ++p)
  71.                         ;
  72.             }
  73.             else
  74.             {
  75.                   for (p = r + 1 ;*p && (*p != '\\'); ++p)
  76.                         ;
  77.             }
  78.             strcpy(r, p + ((*p) ? 1 : 0));
  79.       }
  80.  
  81.       while ('\\' == LAST_CHAR(path))   /* Strip trailing backslash     */
  82.       {
  83.             dir_flag = True_;
  84.             LAST_CHAR(path) = '\0';
  85.       }
  86.  
  87.       s = r;
  88.  
  89.       /* Look for "\." in path        */
  90.  
  91.       while (NULL != (p = strstr(s, "\\.")))
  92.       {
  93.             if ('.' == p[2])
  94.             {
  95.                   /* Execute this section if ".." found                 */
  96.  
  97.                   q = p - 1;
  98.                   while (q > r)           /* Backup one level           */
  99.                   {
  100.                         if (*q == '\\')
  101.                               break;
  102.                         --q;
  103.                   }
  104.                   if (q > r)
  105.                   {
  106.                         strcpy(q, p + 3);
  107.                         s = q;
  108.                   }
  109.                   else if ('.' != *q)
  110.                   {
  111.                         strcpy(q + ((*q == '\\') ? 1 : 0),
  112.                               p + 3 + ((*(p + 3)) ? 1 : 0));
  113.                         s = q;
  114.                   }
  115.                   else  s = ++p;
  116.  
  117.             }
  118.             else
  119.             {
  120.                   /* Execute this section if "." found                  */
  121.  
  122.                   q = p + 2;
  123.                   for ( ;*q && (*q != '\\'); ++q)
  124.                         ;
  125.                   strcpy (p, q);
  126.             }
  127.       }
  128.  
  129.       if (root_flag)  /* Embedded ".." could have bubbled up to root  */
  130.       {
  131.             for (p = r; *p && ('.' == *p || '\\' == *p); ++p)
  132.                   ;
  133.             if (r != p)
  134.                   strcpy(r, p);
  135.       }
  136.  
  137.       if (dir_flag)
  138.             strcat(path, "\\");
  139.       return path;
  140. }
  141.