/* * Retreive the current working directory including drive letter * Returns in a dynamically allocated buffer (free'ed by the caller) * on error: Displays "out of memory" */ char *cwd(int drive) { char *h; if((h = dfnpath(drive)) != 0) return h; if(drive) error_no_cwd(drive); else error_out_of_memory(); return 0; }
static int directory_handler(char *rest, int (*func) (const char *), char *fctname) { char **argv, *dir; int argc, opts; assert(func); if((argv = scanCmdline(rest, NULL, NULL, &argc, &opts)) == NULL) return 1; /* if doing a CD and no parameters given, print out current directory */ if (func == chdir && argc == 0) { if((dir = dfnpath(0)) == NULL) { error_out_of_memory(); freep(argv); return 1; } else { puts(dir); free(dir); } } else if(argc != 1) { error_req_param_missing(); freep(argv); return 1; } else { assert(argv[0]); dir = strchr(argv[0], '\0'); /* take off trailing \ if any, but ONLY if dir is not the root dir */ if(dir > &argv[0][1] && *--dir == '\\' && dir[-1] != ':') *dir = '\0'; dprintf(("%s: '%s'\n", fctname, argv[0])); if (func(argv[0]) != 0) { perror(fctname); freep(argv); return 1; } } freep(argv); return 0; }
char *dfnexpand(const char * const fnam, char * const path) { char *h, *p; /* intermediate pointers */ char *dr, *pa, *na, *ex; /* filename components */ char pathDr, *pathPa; /* drive & path of 'path' */ char *dynPath; #ifdef SUPPORT_UNC_PATH DBG_ENTER("dfnuexpand", Suppl_dfn) #else DBG_ENTER("dfnexpand", Suppl_dfn) #endif assert(fnam); DBG_ARGUMENTS( ("fnam=\"%s\", path=\"%s\"", fnam, path) ) chkHeap if((h = dfnsqueeze(fnam)) == 0) DBG_RETURN_S( 0) #ifdef SUPPORT_UNC_PATH if(isUNCpath(h)) { /* UNC paths are always fully-qualified */ /* check if the trailing '\\' is present to mark the root direc */ DBG_RETURN_BS((*UNCpath(h) != '\\')? StrAppChr(h, '\\') : h) } #endif chkHeap if(!*h || h[1] != ':' || h[2] != '\\') { /* the spec is not fully-qualified or completely empty */ pathDr = 0; dynPath = 0; if((pathPa = path) != 0 && *pathPa) { if(pathPa[1] == ':') { /* path[] has drive spec */ pathDr = *path; if(!*(pathPa += 2)) { pathPa = 0; goto noPath; } } if(dfndelim(*pathPa) && !pathPa[1]) ++pathPa; /* Trans "/" || "\\" --> "" */ noPath:; } chkHeap if(dfnsplit(h, &dr, &pa, &na, &ex)) { StrFree(h); if(dr) { /* drive specified */ if(pathDr && toFUpper(pathDr) != *dr) /* The specified path is for a different drive */ pathPa = 0; } else { /* drive spec missing */ if((dr = StrChar(pathDr? pathDr: 'A' + getdisk())) == 0) goto errRet; } if(!pa || *pa != '\\' && *pa != NUL) { /* no path or a relative one */ if(!pathPa) { /* path has no path spec in it */ if((dynPath = dfnpath(*dr)) == 0) goto errRet; pathPa = dynPath + 2; } if((p = dfnmerge(0, 0, pathPa, pa, 0)) == 0) goto errRet; StrRepl(pa, p); } h = dfnmerge(0, dr, pa, na, ex); } else StrFree(h); errRet: chkHeap free(dr); free(pa); free(na); free(ex); free(dynPath); }