cwFile.cpp : open() now calls filesys::expandPath() on all file names.

This commit is contained in:
kevin 2021-02-26 13:33:31 -05:00
parent c2409ca4be
commit 9a7461a147

View File

@ -112,9 +112,10 @@ namespace cw
cw::rc_t cw::file::open( handle_t& hRef, const char* fn, unsigned flags ) cw::rc_t cw::file::open( handle_t& hRef, const char* fn, unsigned flags )
{ {
char mode[] = "/0/0/0";
this_t* p = nullptr; this_t* p = nullptr;
rc_t rc; char* exp_fn = nullptr;
rc_t rc = kOkRC;
char mode[] = "/0/0/0";
if((rc = close(hRef)) != kOkRC ) if((rc = close(hRef)) != kOkRC )
return rc; return rc;
@ -153,34 +154,44 @@ cw::rc_t cw::file::open( handle_t& hRef, const char* fn, unsigned flags )
fn = "stdin"; fn = "stdin";
} }
// verify the filename is not empty
if( fn == nullptr ) if( fn == nullptr || strlen(fn)==0 )
return cwLogError(kInvalidArgRC,"File object allocation failed due to empty file name."); return cwLogError(kInvalidArgRC,"File object allocation failed due to empty file name.");
unsigned byteCnt = sizeof(this_t) + strlen(fn) + 1; unsigned byteCnt = sizeof(this_t) + strlen(fn) + 1;
// create the file object
if((p = mem::allocZ<this_t>(byteCnt)) == nullptr ) if((p = mem::allocZ<this_t>(byteCnt)) == nullptr )
return cwLogError(kOpFailRC,"File object allocation failed for file '%s'.",cwStringNullGuard(fn)); return cwLogError(kOpFailRC,"File object allocation failed for file '%s'.",cwStringNullGuard(fn));
// copy in the file name
p->fnStr = (char*)(p+1); p->fnStr = (char*)(p+1);
strcpy(p->fnStr,fn); strcpy(p->fnStr,fn);
// if a special file was requestd
if( sfp != nullptr ) if( sfp != nullptr )
p->fp = sfp; p->fp = sfp;
else else
{ {
exp_fn = filesys::expandPath(fn);
errno = 0; errno = 0;
if((p->fp = fopen(fn,mode)) == nullptr )
{ if((p->fp = fopen(exp_fn,mode)) == nullptr )
rc_t rc = p->lastRC = cwLogSysError(kOpenFailRC,errno,"File open failed on file:'%s'.",cwStringNullGuard(fn)); rc = p->lastRC = cwLogSysError(kOpenFailRC,errno,"File open failed on file:'%s'.",cwStringNullGuard(fn));
mem::release(p);
return rc; mem::release(exp_fn);
}
} }
if( rc != kOkRC )
mem::release(p);
else
hRef.set(p); hRef.set(p);
return kOkRC; return rc;
} }
cw::rc_t cw::file::close( handle_t& hRef ) cw::rc_t cw::file::close( handle_t& hRef )