What am I overlooking with my test code?
Been trying to learn opengl but the project I was adapting to it has become a mess so I created a new project with fresh code in mind, figured I'd refresh myself on the TCHAR crap that windows introduced but found there to not be a decent API for it (for example the StringCchLength that limits the length it will check for), so I decided to write my own and map the linux equiv to plain char, I got the string functions working fine (I think, didn't fully test but no problems thus far), however a problem appeared when I tried to load a file like so:
Code:
int main()
{
char const *dir = getcwd(NULL,0);
BYTES size = strlen(dir) + 1;
BYTES want = size + strlen(__FILE__) + 1;
TCS *path = new_tcs( want );
TCF *file;
tch *line;
fprintf( stderr, "path = %p\n", path );
if ( !path )
return EXIT_FAILURE;
sprintf( path->addr, "%s/%s", dir, __FILE__ );
fprintf( stderr, "Opening '%s'\n", path->addr );
file = tcf_open( NULL, path->addr, 0444, 0, 0 );
del_tcs( path );
if ( !file )
return EXIT_FAILURE;
while ( (line = tcf_next_line( file )) )
{
puts(line);
}
return EXIT_SUCCESS;
}
tcf_open is defined like this (code that matters is at the bottom):
Code:
TCF* tcf_open( TCF *kept, tch const * const path, uint pref, BYTES size, bool make )
{
TCF *file = kept ? kept : calloc( 1, sizeof(TCF) );
TCS *_temp, *_path;
BUFF *_buff;
INDEX leng;
uint flags = 0;
uint as = 0;
int err = 0;
if ( !file )
return NULL;
leng = tcslen( path );
size = size ? size : BUFSIZ;
_buff = alt_list( &(file->buff), size, 1, 1 );
_path = alt_list( &(file->path), leng + 1, 1, sizeof(tch) );
if ( !_buff || !_path )
{
tcf_shut( file, !!kept );
return NULL;
}
tcsncpy( _path->addr, path, _path->have );
#ifdef _WIN32
as = make ? CREATE_ALWAYS : OPEN_EXISTING;
flags |= (pref & 04) ? GENERIC_READ : 0;
flags |= (pref & 02) ? GENERIC_WRITE : 0;
flags |= (pref & 01) ? GENERIX_EXECUTE : 0;
file->handle = CreateFile( _path->addr, flags, 0, NULL, as, 0, NULL );
if ( !(file->handle) )
{
tcf_shut( file, !!kept );
return NULL;
}
#else
as = ( make && access( path, 0 ) != 0 ) ? O_CREAT : 0;
if ( (pref & TCF_PREF_OWN_RW) == TCF_PREF_OWN_RW )
{
flags |= O_RDWR;
}
else if ( pref & TCF_PREF_OWN_RD )
{
flags |= O_RDONLY;
}
else
{
flags |= O_WRONLY;
}
//flags |= (pref & 01) ? O_EXECUTE : 0;
flags |= (pref & TCF_PREF_AS_DIR) ? O_DIRECTORY : 0;
flags |= as;
EXEC( err, file->fd = open( _path->addr, flags ), file->fd > 0 );
#endif
return file;
}
And this is the output (directory has been modfied to ~):
Code:
make rebuild run
rm -f *.out *.o
cc -D _LARGEFILE64_SOURCE -o crafters.c.o -c crafters.c -l glfw -l GL -l GLEW
cc -D _LARGEFILE64_SOURCE -o data.c.o -c data.c -l glfw -l GL -l GLEW
cc -D _LARGEFILE64_SOURCE -o error.c.o -c error.c -l glfw -l GL -l GLEW
cc -D _LARGEFILE64_SOURCE -o main.c.o -c main.c -l glfw -l GL -l GLEW
cc -D _LARGEFILE64_SOURCE -o tcf.c.o -c tcf.c -l glfw -l GL -l GLEW
cc -D _LARGEFILE64_SOURCE -o tcs.c.o -c tcs.c -l glfw -l GL -l GLEW
cc -o a.out crafters.c.o data.c.o error.c.o main.c.o tcf.c.o tcs.c.o -l glfw -l GL -l GLEW
./a.out
path = 0x563b631d0d70
Opening '~/opengl_example/main.c'
tcf.c:89: file->fd = open( _path->addr, flags )
Error 0x00000002 (2) 'No such file or directory'
a.out: tcf.c:89: tcf_open: Assertion `file->fd > 0' failed.
make: *** [GNUmakefile:29: run] Aborted (core dumped)
Compilation failed.
Any ideas as to why an attempt to open a file via an absolute path would somehow result in a not found scenario?