home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / quot210s.zip / src / dir.c < prev    next >
C/C++ Source or Header  |  1998-06-07  |  4KB  |  177 lines

  1. /*
  2.  * dir.c
  3.  *
  4.  * Miscellaneous path-name-handling functions.
  5.  *
  6.  *      Created: 25th November, 1997 (was part of general.c)
  7.  * Verison 2.00: 15th December, 1997
  8.  *
  9.  * (C) 1997 Nicholas Paul Sheppard
  10.  *
  11.  * This file is distributed under the GNU General Public License. See the
  12.  * file copying.txt for details.
  13.  */
  14.  
  15. #include <string.h>
  16. #include "general.h"
  17.  
  18. char *dircat(char *pszPath1, const char *pszPath2)
  19. /*
  20.  * Concatenate directory names.
  21.  *
  22.  * char *pszPath1    - first path in the concatenation
  23.  * char *pszPath2    - second path in the concatenation
  24.  *
  25.  * Returns        - pszPath1
  26.  */
  27. {
  28.     if ((pszPath1[0] != '\0') && (pszPath2[0] != '\0'))
  29.         strcat(pszPath1, "\\");
  30.     strcat(pszPath1, pszPath2);
  31.  
  32.     return (pszPath1);
  33. }
  34.  
  35.  
  36. char *dirfname(char *pszFileName)
  37. /*
  38.  * Exract the file name only from a path name.
  39.  *
  40.  * char *pszFileName    - the path name, to be overwritten with file name
  41.  *
  42.  * Returns        - pszFileName
  43.  */
  44. {
  45.      char *pch1, *pch2;
  46.  
  47.     /* look for last backslash */
  48.     if ((pch2 = strrchr(pszFileName, '\\')) == NULL)
  49.         /* no backslashes; look for colon (after drive letter) */
  50.         if ((pch2 = strchr(pszFileName, ':')) == NULL)
  51.             /* nothing to do */
  52.                 return (pszFileName);
  53.  
  54.     pch2++;
  55.     pch1 = pszFileName;
  56.     while ((*pch2) != '\0') {
  57.         (*pch1) = (*pch2);
  58.         pch1++;
  59.         pch2++;
  60.     }
  61.     (*pch1) = '\0';
  62.  
  63.     return (pszFileName);
  64. }
  65.  
  66.  
  67. char *dirnoext(char *pszFileName)
  68. /*
  69.  * Strip the extension (if one exists) from a file name.
  70.  *
  71.  * char *pszFileName    - the file name to be stripped of its extension
  72.  *
  73.  * Returns:        - pszFileName
  74.  */
  75. {
  76.     char *pchLastFS, *pchLastBS;
  77.  
  78.     pchLastFS = strrchr(pszFileName, '.');
  79.     pchLastBS = strrchr(pszFileName, '\\');
  80.     if (pchLastFS > pchLastBS)
  81.         (*pchLastFS) = '\0';
  82.  
  83.     return (pszFileName);
  84. }
  85.  
  86.  
  87. char *dirsimp(char *pszPathName)
  88. /*
  89.  * Rationalise a path name i.e. remove . entries, move all .. entries
  90.  * to the front if they cannot be removed and remove any repeated
  91.  * directory separators.
  92.  *
  93.  * char *pszPathName    - the path name to be rationalised.
  94.  *
  95.  * Returns:        - pszPathName
  96.  */
  97. {
  98.     char *pch1, *pch2, *pch3, *pchStart;
  99.     char szPortion[256], szPathName[256];
  100.  
  101.     if (pszPathName[0] == '\0')
  102.         /* trivial case */
  103.         return (pszPathName);
  104.  
  105.     /* skip drive letter, if any */
  106.     if (pszPathName[1] == ':')
  107.         pch1 = pszPathName + 2;
  108.     else
  109.         pch1 = pszPathName;
  110.     pchStart = pch1;
  111.  
  112.     /* skip leading backslash, if any */
  113.     if ((*pch1) == '\\')
  114.         pch1++;
  115.  
  116.     /* skip repeated initial directory separators */
  117.     pch2 = strcpy(szPathName, pszPathName) + (pchStart - pszPathName);
  118.     while ((*pch2) == '\\')
  119.         pch2++;
  120.  
  121.     /* rationalise the rest of the path name */
  122.     while ((*pch2) != '\0') {
  123.         /* get next portion of path name */
  124.         if ((pch3 = strchr(pch2, '\\')) == NULL)
  125.             pch3 = strchr(pch2, '\0');
  126.         strncpy(szPortion, pch2, pch3 - pch2);
  127.         szPortion[pch3 - pch2] = '\0';
  128.         pch2 = pch3;
  129.  
  130.         if (strcmp(szPortion, "..") == 0) {
  131.             /* .. entry; move pch1 back a step */
  132.             (*pch1) = '\0';
  133.             if (pch1 > (pchStart + 1))
  134.                 pch1 -= 2;
  135.             else if ((*pchStart) != '\\') {
  136.                 /* bring the .. entry back to the start */
  137.                 strpre(pch1, "..\\");
  138.                 pch1 = strchr(pch1, '\0');
  139.             }
  140.             while ((pch1 > pchStart) && ((*pch1) != '\\') && ((*pch1) != '.'))
  141.                 pch1--;
  142.             if ((*pch1) == '\\') {
  143.                 pch1++;
  144.             }
  145.             if ((*pch1) == '.') {
  146.                 /* there are .. entries at the start */
  147.                 strpre(pch1 + 1, "\\..");
  148.                 pch1 = strchr(pch1, '\0');
  149.             }
  150.         } else if (strcmp(szPortion, ".") != 0) {
  151.             /* normal entry; append */
  152.             for (pch3 = szPortion; (*pch3) != '\0'; pch3++, pch1++)
  153.                 (*pch1) = (*pch3);
  154.             (*pch1) = '\\';
  155.             pch1++;
  156.         }
  157.  
  158.         /* skip repeated directory separators */
  159.         while ((*pch2) == '\\')
  160.             pch2++;
  161.     }
  162.  
  163.     /* strip trailing backslash */
  164.     if (pch1 > pchStart) {
  165.         if ((pch1 != (pchStart + 1)) || ((*pchStart) != '\\'))
  166.             pch1--;
  167.         (*pch1) = '\0';
  168.     } else if ((*pch1) == '\\') {
  169.         pch1++;
  170.         (*pch1) = '\0';
  171.     } else {
  172.         (*pch1) = '\0';
  173.     }
  174.  
  175.     return (pszPathName);
  176. }
  177.