home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / internet / tcpip / src205 / TCPIP_Src / Misc / c / PATHNAME < prev    next >
Encoding:
Text File  |  1994-02-28  |  3.4 KB  |  99 lines

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