home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 04 / makepath.cpp < prev    next >
C/C++ Source or Header  |  1992-02-05  |  2KB  |  70 lines

  1. // makepath.cpp RHS 2/5/92
  2.  
  3. #include<dir.h>
  4. #include<string.h>
  5. #include<ctype.h>
  6. #include<stdio.h>
  7.  
  8. #include"makepath.h"
  9.  
  10. char *makepath(char *buf, char *spec)
  11.     {
  12.     char drivelet;
  13.  
  14.     if(spec[1] != ':')                  // no drive letter
  15.         drivelet = (getdisk()+'A');     // get one
  16.     else                                // drive letter specified
  17.         {
  18.         drivelet = toupper(*spec);      // get it
  19.         spec = &spec[2];
  20.         }
  21.  
  22.         // spec now points to path portion (after drivelet+colon)
  23.  
  24.     if(spec[strlen(spec)-1] == '\\')    // if trailing backslash
  25.         spec[strlen(spec)-1] = '\0';    // remove it
  26.  
  27.     buf[0] = drivelet;                  // add drive letter
  28.     strcpy(&buf[1],":\\");              // add colon and backslash
  29.  
  30.     if(*spec == '\\')                   // if starts with backslash,
  31.         strcat(buf,&spec[1]);           // add it after that point
  32.     else                                // otherwise, it's a relative dir
  33.         {
  34.         getcurdir(drivelet-'A'+1,&buf[3]);  // add cwd
  35.         if(buf[strlen(buf)-1] != '\\')  // if no backslash, 
  36.             strcat(buf,"\\");           // add one
  37.         strcat(buf,spec);               // add dirspec
  38.         }
  39.  
  40.     char *p,*q;
  41.     while(p = strstr(buf,"\\.."))       // weed out ..
  42.         {
  43.         q = &p[3];
  44.         for( p-- ; p > &buf[2] && *p != '\\'; p--);
  45.         if(*p == '\\')
  46.             strcpy(p,q);
  47.         }
  48.  
  49.     if(p = strstr(buf,"\\."))           // weed out .
  50.         strcpy(p,&p[2]);
  51.  
  52.     strupr(buf);
  53.     return buf;
  54.     }
  55.  
  56. #if defined(MAIN)
  57. void main(int argc, char **argv)
  58.     {
  59.     if(argc == 2)
  60.         {
  61.         char buffer[80];
  62.  
  63.         makepath(buffer, argv[1]);
  64.         printf("%s\n", buffer);
  65.         }
  66.     }
  67. #endif
  68.  
  69.  
  70.