home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DMAKE38A.ZIP / PATH.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  3KB  |  117 lines

  1. /* RCS      -- $Header: /u2/dvadura/src/generic/dmake/src/RCS/path.c,v 1.1 1992/01/24 03:27:54 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) 1990 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  1992/01/24  03:27:54  dvadura
  32.  * dmake Version 3.8, Initial revision
  33.  *
  34. */
  35.  
  36. #include "extern.h"
  37.  
  38. /*
  39. ** Return the suffix portion of a filename, assumed to begin with a `.'.
  40. */
  41. PUBLIC char *
  42. Get_suffix(name)
  43. char *name;
  44. {
  45.    char *suff;
  46.  
  47.    if(name == NIL(char)  || (suff = strrchr(name, '.')) == NIL(char))
  48.       suff = ".NULL";
  49.  
  50.    return (suff);
  51. }
  52.  
  53.  
  54.  
  55. /*
  56. ** Take dir and name, and return a path which has dir as the directory
  57. ** and name afterwards.
  58. **
  59. ** N.B. Assumes that the dir separator string is in DirSepStr.
  60. **      Return path is built in a static buffer, if you need to use it
  61. **      again you must _strdup the result returned by Build_path.
  62. */
  63. PUBLIC char *
  64. Build_path(dir, name)
  65. char *dir;
  66. char *name;
  67. {
  68.    register char *p;
  69.    register char *q;
  70.    static char     *path  = NIL(char);
  71.    static unsigned buflen = 0;
  72.    int  plen = 0;
  73.    int  dlen = 0;
  74.    int  len;
  75.  
  76.    if( dir  != NIL(char) ) dlen = strlen( dir  );
  77.    if( name != NIL(char) ) plen = strlen( name );
  78.    len = plen+dlen+strlen(DirSepStr)+1;
  79.  
  80.    if( len > buflen ) {
  81.       buflen = (len+16) & ~0xf;        /* buf is always multiple of 16 */
  82.  
  83.       if( path == NIL(char) )
  84.          path = MALLOC( buflen, char );
  85.       else
  86.          path = realloc( path, (unsigned) (buflen*sizeof(char)) );
  87.    }
  88.    
  89.    *path = '\0';
  90.  
  91.    if( dlen ) {
  92.       strcpy( path, dir );
  93.       if( *path && strchr(DirBrkStr, dir[dlen-1]) == NIL(char) )
  94.      strcat( path, DirSepStr );
  95.    }
  96.    strcat( path, name );
  97.  
  98.    q=path;
  99.    while( *q ) {
  100.       char *t;
  101.  
  102.       p=_strpbrk(q,DirBrkStr);
  103.       t=_strpbrk(p+1,DirBrkStr);
  104.       if( !*p || !*t ) break;
  105.  
  106.       if(    !(p-q == 2 && strncmp(q,"..",2) == 0)
  107.           && t-p-1 == 2 && strncmp(p+1,"..",2) == 0 ) {
  108.      t = _strspn(t,DirBrkStr);
  109.      strcpy(q,t);
  110.       }
  111.       else
  112.      q = p+1;
  113.    }
  114.  
  115.    return( path );
  116. }
  117.