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,10 +112,11 @@ 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; char* exp_fn = nullptr;
rc_t rc; 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
{ {
errno = 0; exp_fn = filesys::expandPath(fn);
if((p->fp = fopen(fn,mode)) == nullptr ) errno = 0;
{
rc_t rc = p->lastRC = cwLogSysError(kOpenFailRC,errno,"File open failed on file:'%s'.",cwStringNullGuard(fn)); if((p->fp = fopen(exp_fn,mode)) == nullptr )
mem::release(p); rc = p->lastRC = cwLogSysError(kOpenFailRC,errno,"File open failed on file:'%s'.",cwStringNullGuard(fn));
return rc;
} mem::release(exp_fn);
} }
hRef.set(p);
return kOkRC; if( rc != kOkRC )
mem::release(p);
else
hRef.set(p);
return rc;
} }
cw::rc_t cw::file::close( handle_t& hRef ) cw::rc_t cw::file::close( handle_t& hRef )