Shop OBEX P1 Docs P2 Docs Learn Events
fopen bug — Parallax Forums

fopen bug

Mike PetryMike Petry Posts: 14
edited 2012-08-01 13:56 in Propeller 1
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.
#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

  • jazzedjazzed Posts: 11,803
    edited 2012-07-09 21:56
    Thanks for this report. @Dave Hein can you please have a look?
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-07-10 08:35
    Yes, I'll look into it.
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-07-30 17:24
    I finally got around to checking in the fix for the fopen problem. While I was testing the fix I found a few other problems, and fixed them also. I discovered that we were losing an available file handle whenever an fopen failed. We weren't clearing a flag in the FILE struct, and it was making it unavailable for future use. I also found a problem with the remove function if the file did not already exist. I commented out some code that tries to determine if the remove failed because the target was a directory. I'll have to look at that in more detail to see what was causing the problem.

    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.
  • jazzedjazzed Posts: 11,803
    edited 2012-07-30 20:26
    Thanks Dave.
  • Mike PetryMike Petry Posts: 14
    edited 2012-08-01 13:56
    Looking at the code, the "r+" case looks good. I'll build a system and give it a go.
    Thanks.
Sign In or Register to comment.