Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!leah!rpi!sun.soe.clarkson.edu!nelson From: nelson@sun.soe.clarkson.edu (Russ Nelson) Newsgroups: comp.sys.ibm.pc Subject: Re: Full pathname of file wanted Message-ID: Date: 12 Apr 89 20:50:26 GMT References: <883@syma.sussex.ac.uk> Sender: news@sun.soe.clarkson.edu Reply-To: nelson@clutx.clarkson.edu Organization: Clarkson University, Postdam NY Lines: 130 In-reply-to: leilabd@syma.sussex.ac.uk's message of 11 Apr 89 11:55:20 GMT Written for Turbo C. I can't remember if I wrote this or if it's based on some anonymous code from the IBMPC-Digest. -russ #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 -- --russ (nelson@clutx [.bitnet | .clarkson.edu]) America -- Socialism for the rich people, Capitalism for the rest of us. - Michael Harrington, Co-Chair, Democratic Socialists of America