home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / PATHNAME.C < prev    next >
C/C++ Source or Header  |  1993-03-07  |  4KB  |  158 lines

  1. /* Convert relative to absolute pathnames
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4.  
  5. /****************************************************************************
  6. *    09 Jun 92    1.2        GT    MS-DOS: watch out for <drive>:<path>.            *
  7. ****************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include "global.h"
  11. #include "dirutil.h"
  12.  
  13. static void crunch __ARGS((char *buf,char *path));
  14.  
  15. /* Given a working directory and an arbitrary pathname, resolve them into
  16.  * an absolute pathname. Memory is allocated for the result, which
  17.  * the caller must free
  18.  */
  19. char *
  20. pathname(cd,path)
  21. char *cd;    /* Current working directory */
  22. char *path;    /* Pathname argument */
  23. {
  24.     register char *buf;
  25. #ifdef    MSDOS
  26.     char *cp,c;
  27.     char *tbuf;
  28.     int tflag = 0;
  29.     char *path_ptr;                            /* -> path component            */
  30. #endif
  31.  
  32.     if(cd == NULLCHAR || path == NULLCHAR)
  33.         return NULLCHAR;
  34.  
  35. #ifdef    MSDOS
  36.     /* If path has any backslashes, make a local copy with them
  37.      * translated into forward slashes
  38.      */
  39.     if(strchr(path,'\\') != NULLCHAR){
  40.         tflag = 1;
  41.         cp = tbuf = mallocw(strlen(path));
  42.         while((c = *path++) != '\0'){
  43.             if(c == '\\')
  44.                 *cp++ = '/';
  45.             else
  46.                 *cp++ = c;
  47.         }
  48.         *cp = '\0';
  49.         path = tbuf;
  50.     }
  51. #endif
  52.  
  53.     /* Strip any leading white space on args */
  54.     while(*cd == ' ' || *cd == '\t')
  55.         cd++;
  56.     while(*path == ' ' || *path == '\t')
  57.         path++;
  58.  
  59.     /* Allocate and initialize output buffer; user must free */
  60.     buf = mallocw((unsigned)strlen(cd) + strlen(path) + 10);    /* fudge factor */
  61.     buf[0] = '\0';
  62.  
  63.     /* Interpret path relative to cd only if it doesn't begin with "/" */
  64.     if (path[0] != '/'
  65. #ifdef    MSDOS
  66.          || (path[1] == ':' && path[2] != '/')
  67. #endif
  68.         )
  69.         crunch(buf,cd);
  70.  
  71. #ifdef    MSDOS
  72.     if (path[1] == ':')
  73.         path_ptr = &path[2];
  74.     else
  75.         path_ptr = path;
  76.  
  77.     crunch (buf, path_ptr);
  78. #else
  79.     crunch(buf,path);
  80. #endif
  81.  
  82.     /* Special case: null final path means the root directory */
  83.     if(buf[0] == '\0'){
  84.         buf[0] = '/';
  85.         buf[1] = '\0';
  86.     }
  87. #ifdef    MSDOS
  88.     if(tflag)
  89.         free(tbuf);
  90. #endif
  91.     return buf;
  92. }
  93.  
  94. /* Process a path name string, starting with and adding to
  95.  * the existing buffer
  96.  */
  97. static void
  98. crunch(buf,path)
  99. char *buf;
  100. register char *path;
  101. {
  102.     register char *cp;
  103.  
  104. #ifdef    MSDOS
  105.     /* If <path> starts with "<drive>:", nail that into the buffer first. */
  106.  
  107.     if (*buf == '\0' && path[1] == ':')
  108.         {
  109.         *buf++ = *path++;
  110.         *buf++ = *path++;
  111.         *buf = '\0';
  112.         }
  113.     else if (path[1] == ':')
  114.         {
  115.         /* Something already in buffer - skip drive. */
  116.         
  117.         path += 2;
  118.         }
  119. #endif
  120.  
  121.     cp = buf + strlen(buf);    /* Start write at end of current buffer */
  122.     
  123.     /* Now start crunching the pathname argument */
  124.     for(;;){
  125.         /* Strip leading /'s; one will be written later */
  126.         while(*path == '/')
  127.             path++;
  128.         if(*path == '\0')
  129.             break;        /* no more, all done */
  130.         /* Look for parent directory references, either at the end
  131.          * of the path or imbedded in it
  132.          */
  133.         if(strcmp(path,"..") == 0 || strncmp(path,"../",3) == 0){
  134.             /* Hop up a level */
  135.             if((cp = strrchr(buf,'/')) == NULLCHAR)
  136.                 cp = buf;    /* Don't back up beyond root */
  137.             *cp = '\0';        /* In case there's another .. */
  138.             path += 2;        /* Skip ".." */
  139.             while(*path == '/')    /* Skip one or more slashes */
  140.                 path++;
  141.         /* Look for current directory references, either at the end
  142.          * of the path or imbedded in it
  143.          */
  144.         } else if(strcmp(path,".") == 0 || strncmp(path,"./",2) == 0){
  145.             /* "no op" */
  146.             path++;            /* Skip "." */
  147.             while(*path == '/')    /* Skip one or more slashes */
  148.                 path++;
  149.         } else {
  150.             /* Ordinary name, copy up to next '/' or end of path */
  151.             *cp++ = '/';
  152.             while(*path != '/' && *path != '\0')
  153.                 *cp++ = *path++;
  154.         }
  155.     }
  156.     *cp++ = '\0';
  157. }
  158.