int dir_print_body(char *arg) { int rv; unsigned long dircount, filecount, bytecount; char *pattern, *cachedPattern; dprintf( ("[DIR: path=\"%s\"]\n", arg) ); if((path = dfnexpand(arg, NULL)) == NULL) { error_out_of_memory(); return E_NoMem; } dircount = filecount = bytecount = 0; pattern = strchr(path, '\0'); if(pattern[-1] == '\\') { /* trailing backslash means that this has to be a directory */ if(!StrAppChr(path, '.')) { error_out_of_memory(); return E_NoMem; } } dprintf( ("[DIR: absolute path=\"%s\"]\n", path) ); /* print the header */ if ((rv = dir_print_header(toupper(path[0]) - 'A')) == 0) { if(dfnstat(path) & DFN_DIRECTORY) { pattern = strchr(path, '\0'); if(pattern[-1] != '\\') ++pattern; rv = dir_list(pattern - path, "*.*", &dircount, &filecount , &bytecount); } else { if((cachedPattern = strdup(pattern = dfnfilename(path))) == NULL) { error_out_of_memory(); rv = E_NoMem; } else { rv = dir_list(pattern - path, cachedPattern, &dircount, &filecount , &bytecount); free(cachedPattern); } } } free(path); return rv; }
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); }