home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1 / HamRadio.cdr / misc / tcpipsrc / pathname.c < prev    next >
C/C++ Source or Header  |  1991-01-26  |  3KB  |  121 lines

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