Category : File Managers
Archive   : UNIXFIND.ZIP
Filename : ROOTPATH.C

 
Output of file : ROOTPATH.C contained in archive : UNIXFIND.ZIP

#include
#include
#include
#include

/*** rootpath -- convert a pathname argument to root based cannonical form
*
* rootpath determines the current directory, appends the path argument (which
* may affect which disk the current directory is relative to), and qualifies
* "." and ".." references. The result is a complete, simple, path name with
* drive specifier.
*
* If the relative path the user specifies does not include a drive spec., the
* default drive will be used as the base. (The default drive will never be
* changed.)
*
* entry: relpath -- pointer to the pathname to be expanded
* fullpath -- must point to a working buffer, see warning
* exit: fullpath -- the full path which results
* return: true if an error occurs, false otherwise
*
* calls: getcurdir getdisk
*
* warning: fullpath must point to a working buffer large enough to hold the
* longest possible relative path argument plus the longest possible
* current directory path.
*
*/
int rootpath(char *relpath, char *fullpath)
{
int drivenum ;
char tempchar;
register char *lead, *follow ;


/* extract drive spec */
if ((*relpath != '\0') && (relpath[1] == ':')) {
drivenum = toupper(*relpath) - 'A' ;
relpath += 2 ;
} else drivenum = getdisk() ;
fullpath[0] = (char) ('A' + drivenum) ;
fullpath[1] = ':' ;

/* append relpath to fullpath/base */
if (*relpath == '\\' || *relpath == '/') {
/* relpath starts at base */
strcpy(fullpath+2, relpath) ;
} else {
/* must get base path first */
fullpath[2] = '\\';
if (getcurdir(drivenum+1, fullpath+3))
return 1; /* terrible error */
if ((*relpath != '\0') && (strlen(fullpath) > 3))
strcat(fullpath, "\\") ;
strcat(fullpath, relpath) ;
}


/* convert path to cannonical form */
lead = fullpath ;
while(*lead != '\0') {
/* mark next path segment */
follow = lead ;
if ((lead = strchr(follow+1, '\\')) != '\0') {
char *fslash;

tempchar = '\\';
if ((fslash = strchr(follow+1, '/')) != '\0') {
if (fslash < lead) lead = fslash;
}
}
else if ((lead = strchr(follow+1, '/')) != '\0') tempchar = '\\';
else {
lead = fullpath + strlen(fullpath) ;
tempchar = '\0';
}
*lead = '\0';

/* "." segment? */
if (strcmp(follow+1, ".") == 0) {
*lead = tempchar ;
strcpy(follow, lead); /* remove "." segment */
lead = follow ;
}

/* ".." segment? */
else if (strcmp(follow+1, "..") == 0) {
*lead = tempchar ;
do {
if (--follow < fullpath)
return 1;
} while (*follow != '\\') ;
strcpy(follow, lead); /* remove ".." segment */
lead = follow ;
}

/* normal segment */
else
*lead = tempchar ;
}
if (strlen(fullpath) == 2) /* 'D:' or some such */
strcat(fullpath, "\\") ;

/* shift to upper case */
strupr(fullpath) ;

return 0;
}

#ifdef DEBUG
void main(int argc, char *argv[])
{
char fullpath[64];
if (argc != 2) cputs("usage: rootpath pathname");
else {
rootpath(argv[1], fullpath);
cputs(fullpath);
}
}
#endif