home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dmake40.zip / path.c < prev    next >
C/C++ Source or Header  |  1994-10-23  |  4KB  |  171 lines

  1. /* RCS      -- $Header: /u5/dvadura/src/public/dmake/src/RCS/path.c,v 1.1 1994/10/06 17:41:58 dvadura Exp $
  2. -- SYNOPSIS -- pathname manipulation code
  3. -- 
  4. -- DESCRIPTION
  5. --    Pathname routines to handle building and pulling appart
  6. --    pathnames.
  7. -- 
  8. -- AUTHOR
  9. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  10. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  11. --
  12. -- COPYRIGHT
  13. --      Copyright (c) 1992,1994 by Dennis Vadura.  All rights reserved.
  14. -- 
  15. --      This program is free software; you can redistribute it and/or
  16. --      modify it under the terms of the GNU General Public License
  17. --      (version 1), as published by the Free Software Foundation, and
  18. --      found in the file 'LICENSE' included with this distribution.
  19. -- 
  20. --      This program is distributed in the hope that it will be useful,
  21. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  22. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. --      GNU General Public License for more details.
  24. -- 
  25. --      You should have received a copy of the GNU General Public License
  26. --      along with this program;  if not, write to the Free Software
  27. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. --
  29. -- LOG
  30. --     $Log: path.c,v $
  31.  * Revision 1.1  1994/10/06  17:41:58  dvadura
  32.  * dmake Release Version 4.0, Initial revision
  33.  *
  34. */
  35.  
  36. #include "extern.h"
  37.  
  38.  
  39. /*
  40. ** Return the suffix portion of a filename, assumed to begin with a `.'.
  41. */
  42. PUBLIC char *
  43. Get_suffix(name)
  44. char *name;
  45. {
  46.    char *suff;
  47.  
  48.    if(name == NIL(char)  || (suff = strrchr(name, '.')) == NIL(char))
  49.       suff = ".NULL";
  50.  
  51.    return (suff);
  52. }
  53.  
  54.  
  55. PUBLIC char *
  56. Basename(path)
  57. char *path;
  58. {
  59.    char *p;
  60.    char *q;
  61.  
  62.    if( path && *(q = path) ) {
  63.       for(; *(p=DmStrPbrk(q, DirBrkStr)) != '\0'; q = p+1 );
  64.       if( !*q ) {
  65.      for( p=q-1; p != path; --p )
  66.         if( strchr( DirBrkStr, *p ) == NIL(char) ) return( p+1 );
  67.      return( strchr(DirBrkStr, *p)?path:(p+1) );
  68.       }
  69.       path = q;
  70.    }
  71.    return( path );
  72. }
  73.  
  74.  
  75. PUBLIC char *
  76. Filedir(path)
  77. char *path;
  78. {
  79.    char *p;
  80.    char *q;
  81.  
  82.    if( path && *(q = path) ) {
  83.       for(; *(p=DmStrPbrk(q,DirBrkStr)) != '\0'; q=p+1 );
  84.  
  85.       if (q == path) return("");
  86.  
  87.       for(p=q-1; p!=path; --p)
  88.      if( strchr(DirBrkStr,*p) == NIL(char) )
  89.         break;
  90.  
  91.       p[1] = '\0';
  92.    }
  93.  
  94.    return(path);
  95. }
  96.  
  97.  
  98.  
  99. /*
  100. ** Take dir and name, and return a path which has dir as the directory
  101. ** and name afterwards.
  102. **
  103. ** N.B. Assumes that the dir separator string is in DirSepStr.
  104. **      Return path is built in a static buffer, if you need to use it
  105. **      again you must strdup the result returned by Build_path.
  106. */
  107. PUBLIC char *
  108. Build_path(dir, name)
  109. char *dir;
  110. char *name;
  111. {
  112.    register char *p;
  113.    register char *q;
  114.    static char     *path  = NIL(char);
  115.    static unsigned buflen = 0;
  116.    int  plen = 0;
  117.    int  dlen = 0;
  118.    int  len;
  119.  
  120.    if( dir  != NIL(char) ) dlen = strlen( dir  );
  121.    if( name != NIL(char) ) plen = strlen( name );
  122.    len = plen+dlen+strlen(DirSepStr)+1;
  123.  
  124.    if( len > buflen ) {
  125.       buflen = (len+16) & ~0xf;        /* buf is always multiple of 16 */
  126.  
  127.       if( path == NIL(char) )
  128.          path = MALLOC( buflen, char );
  129.       else
  130.          path = realloc( path, (unsigned) (buflen*sizeof(char)) );
  131.    }
  132.    
  133.    *path = '\0';
  134.  
  135.    if( dlen ) {
  136.       strcpy( path, dir );
  137.       if( *path && strchr(DirBrkStr, dir[dlen-1]) == NIL(char) )
  138.      strcat( path, DirSepStr );
  139.    }
  140.  
  141.    if ( plen ) {
  142.       while ( *name && strchr(DirBrkStr,*name) != 0 ) name++;
  143.       strcat( path, name );
  144.    }
  145.  
  146.    q=path;
  147.    while( *q ) {
  148.       char *t;
  149.  
  150.       p=DmStrPbrk(q,DirBrkStr);
  151.       t=DmStrPbrk(p+1,DirBrkStr);
  152.       if( !*p || !*t ) break;
  153.  
  154.       if ( p-q == 1 && *q == '.' ) {
  155.      strcpy(q,DmStrSpn(p,DirBrkStr));
  156.      q = path;
  157.       }
  158.       else if (
  159.      !(p-q == 2 && strncmp(q,"..",2) == 0)
  160.       && (t-p-1 == 2 && strncmp(p+1,"..",2) == 0)
  161.       ) {
  162.      strcpy(q,DmStrSpn(t,DirBrkStr));
  163.      q = path;
  164.       }
  165.       else
  166.      q = p+1;
  167.    }
  168.  
  169.    return( path );
  170. }
  171.