Phase 2

opening a file with a + means the implementation expects read OR write to occur.

Possible idea is to  modify the implementation to NOT treat this expecting read or write UNTIL a read call is called for example.

Searched libio for modifying this, looks like fileops.c contains the code relevant to how fopen treats different modes.

since the fwrite only needs the pointer to where the buffer is valid, fread SHOULDNT be called.

(Looking in libio)

function definition is in glibc/libio/stdio.h, but actual fopen function is in fileops.c


Going to examine both fileops.c and the fwrite function

//looking at fopen function, and how it handles the “w+” case, line 268

if (_IO_file_is_open (fp))
return 0;
switch (*mode)
case ‘r’:
omode = O_RDONLY;
read_write = _IO_NO_WRITES;
case ‘w’:
omode = O_WRONLY;
oflags = O_CREAT|O_TRUNC;
read_write = _IO_NO_READS;
case ‘a’:
omode = O_WRONLY;
oflags = O_CREAT|O_APPEND;
__set_errno (EINVAL);
return NULL;
#ifdef _LIBC
last_recognized = mode;
for (i = 1; i < 7; ++i)
switch (*++mode)
case ‘\0’:
case ‘+’:
omode = O_RDWR;
read_write &= _IO_IS_APPENDING;


//line 336

result = _IO_file_open (fp, filename, omode|oflags, oprot, read_write,

Noted here that I should not change the actual functionality of the modes (i.e to append what properties to what modes), rather should change how the function that handles the input into these modes, which is _IO_file_open.

// and the omode/read_write results are entered into result, where its handled by the _IO_FILE_OPEN function

//Around line 220 in fileops.c, _IO_FILE_OPEN function handles read/write mode

_IO_file_open (_IO_FILE *fp, const char *filename, int posix_mode, int prot,
int read_write, int is32not64)
int fdesc;
#ifdef _LIBC
if (__glibc_unlikely (fp->_flags2 & _IO_FLAGS2_NOTCANCEL))
fdesc = open_not_cancel (filename,
posix_mode | (is32not64 ? 0 : O_LARGEFILE), prot);
fdesc = open (filename, posix_mode | (is32not64 ? 0 : O_LARGEFILE), prot);
fdesc = open (filename, posix_mode, prot);
if (fdesc < 0)
return NULL;
fp->_fileno = fdesc;
_IO_mask_flags (fp, read_write,_IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
/* For append mode, send the file offset to the end of the file. Don’t
update the offset cache though, since the file handle is not active. */
if ((read_write & (_IO_IS_APPENDING | _IO_NO_READS))
_IO_off64_t new_pos = _IO_SYSSEEK (fp, 0, _IO_seek_end);
if (new_pos == _IO_pos_BAD && errno != ESPIPE)
close_not_cancel (fdesc);
return NULL;
_IO_link_in ((struct _IO_FILE_plus *) fp);
return fp;

the open function is what i’m looking for in what handles the w+ mode. <- explained here

int open(const char *pathname, int flags, mode_t mode);

this is what i’m looking for, so i search for it like how I traced the other functions (grep pattern).

the actual int open function is not in libio, defined in conform/data/fcntl.h-data line 180 ?

[dleung25@betty libio]$ grep -rnw ‘/home/dleung25/src/glibc’ -e “int open”

(search results for int open function)
/home/dleung25/src/glibc/conform/data/fcntl.h-data:97:function int open (const char*, int, …)
/home/dleung25/src/glibc/io/fcntl.h:180:extern int open (const char *__file, int __oflag, …) __nonnull ((1));
/home/dleung25/src/glibc/manual/llio.texi:84:@deftypefun int open (const char *@var{filename}, int @var{flags}[, mode_t @var{mode}])

it doesn’t look like i can actually find the source code for open, strangely enough.

fcntl.h in conform/data doesn’t provide any source code

the manual (of course) only describes what it does and how it works (which is not what i’m looking for)

I’ll put that aside for now and look for fwrite specifically (fseek has to do some reading so i’ll avoid doing any changes to that), maybe i can modify fwrite to handle w+ cases specifically).


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