fopen bug
Mike Petry
Posts: 14
I think I've found a bug in the fopen code when using the FileDriver.
You can't open a file for writing without truncating it or forcing append mode
I need to open an existing file for read and write. open("foo","r+")
In lib/drivers/FileDrivers.c, the File_fopen code only looks at the first character of the mode (mode[0]).
As a result, only DFS_READ is set on DFS_OpenFile and writes are ignored.
If mode[0] == 'r' and mode[1] =='+' then both DFS_READ and DFS_WRITE need to be set.
Below is the code I used to test. Changing "r+" to "w+' shows the failure.
You can't open a file for writing without truncating it or forcing append mode
I need to open an existing file for read and write. open("foo","r+")
In lib/drivers/FileDrivers.c, the File_fopen code only looks at the first character of the mode (mode[0]).
As a result, only DFS_READ is set on DFS_OpenFile and writes are ignored.
If mode[0] == 'r' and mode[1] =='+' then both DFS_READ and DFS_WRITE need to be set.
Below is the code I used to test. Changing "r+" to "w+' shows the failure.
#define printf __simple_printf #include <stdio.h> #include <stdlib.h> #include <string.h> #include <cog.h> #include <propeller.h> #include <unistd.h> uint8_t dbuf1[128]; uint8_t dbuf2[128]; extern _Driver _SimpleSerialDriver; extern _Driver _FileDriver; /* This is a list of all drivers we can use in the * program. The default _InitIO function opens stdin, * stdout, and stderr based on the first driver in * the list (the serial driver, for us) */ _Driver *_driverlist[] = { &_SimpleSerialDriver, &_FileDriver, NULL }; FILE *dp; /* * Test the fopen/fread/fwrite/fseek/fclose routines */ int main(void) { int i; int diskoffset; for(i=0; i<128; i++) { dbuf1[i] = 0xaa; } printf("Starting fopentest\n"); if ((dp = fopen("foo","r+")) == 0) { printf("fopen failure\n"); exit(-1); } printf("fopen ok\n"); diskoffset = 0; fseek(dp, diskoffset, SEEK_SET); printf("write seek: %d\n", diskoffset); printf("write data: %x\n", dbuf1[0]); fwrite(dbuf1, 1, 128, dp); fflush(dp); fseek(dp, diskoffset, SEEK_SET); printf("read seek: %d\n", diskoffset); fread(dbuf2, 1, 128, dp); printf("read data: %x\n", dbuf2[0]); }
Comments
I also discovered that c3files no longer works in LMM mode because there wasn't enough memory for the heap and the stack. The libraries are using up a bit more space than they used to. I disabled mkdir and rmdir in c3files when in the LMM mode to free up some space.
Thanks.