Phase 1

Working with stdio.h

Seems to be a fixable issue for specifically w+ file

Running fseek (places file position indicator x bytes away from an indicated origin) and fwrite (write data to file stream) calls doesn’t seem to be a problem.

test file
test fopen

#include <stdio.h>
int main (void){

printf(“Start Test\n” );
int fileSize = 40;
int position;
int fError;
char buffer[] = { ‘a’ };
FILE *f;
//open test_file for writing
f = fopen( “test_file”, “w+” );
//fail to open for writing
if( !f )
printf( “File failed to open\n” );
return 1;
//loop, places an a each write, moves position indicator one byte more away from start of file each time
for( position = 0; position < 1000; position += sizeof( buffer ) )
//place position indicator position bytes from start of file
fError = fseek( f, position, SEEK_SET);
//fseek fail
if( fError != 0)
printf( “Fseek failed\n” );
return 2;
//write to file
fError = fwrite( buffer, sizeof(char), sizeof(buffer), f);
//fwrite fail
if( fError != 1 )
printf( “Fwrite failed\n” );
return 3;
//close file
fclose( f );
printf(“Test Finished\n”);
return 0;

This is the following output in betty:

[dleung25@betty glibc]$ gcc -g -o test test.c
[dleung25@betty glibc]$ ./test
Start Test
Test Finished
[dleung25@betty glibc]$

Looking at the output file test_file:


looks like the expected write into this test file.

however, using the strace function(lets us check what system calls are being made by this program):


seek and write calls are expected. pointless read calls to kernel not expected, even though w+ expects that both write and read could occur


WIP on finding out where and why read is being called when sample program doesn’t request for it.

buffering logic doesnt seem to be located in the fopen.c

looking at the libio’s iofopen.c

#ifdef _IO_MTSAFE_IO
_IO_lock_t lock;
struct _IO_wide_data wd;
} *new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));

if (new_f == NULL)
return NULL;
#ifdef _IO_MTSAFE_IO
new_f->fp.file._lock = &new_f->lock;
#if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
_IO_no_init (&new_f->fp.file, 0, 0, &new_f->wd, &_IO_wfile_jumps);
_IO_no_init (&new_f->fp.file, 1, 0, NULL, NULL);
_IO_JUMPS (&new_f->fp) = &_IO_file_jumps;
_IO_new_file_init_internal (&new_f->fp);
new_f->fp.vtable = NULL;
if (_IO_file_fopen ((_IO_FILE *) new_f, filename, mode, is32) != NULL)
return __fopen_maybe_mmap (&new_f->fp.file);

_IO_un_link (&new_f->fp);
free (new_f);
return NULL;

_IO_new_fopen (const char *filename, const char *mode)
return __fopen_internal (filename, mode, 1);

#ifdef _LIBC
strong_alias (_IO_new_fopen, __new_fopen)
versioned_symbol (libc, _IO_new_fopen, _IO_fopen, GLIBC_2_1);
versioned_symbol (libc, __new_fopen, fopen, GLIBC_2_1);

# if !defined O_LARGEFILE || O_LARGEFILE == 0
weak_alias (_IO_new_fopen, _IO_fopen64)
weak_alias (_IO_new_fopen, fopen64)
# endif



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s