Hi,
I am trying to add functions through add_device as shown below.
add_device("mydevice", _MSA, my_open, my_close, my_read, my_write, my_lseek, my_unlink, my_rename);
With this, the functions my_open etc are getting called when I call fopen etc.,
The problem is when I call fscanf and fprintf functions, the address the of the buffer is getting changed when the call goes to my_read and my_write functions.
When I do following,
char str[20];
fscanf(fp,"%s",str);
fscanf calls my_read. But the buf value in my_read(int fd, char *buf, unsigned int count) is different. So the copied data in my_read does not come to str.
Can somebody point me the problem here.
Also is there any document that gives the flow from fscanf function to my_read function and also how the buffer address changes.
thanks, Durga
//~~~~~~~~~~~ Code sample ~~~~~~~~~~~~~~~~
typedef struct
{
unsigned char inuse;
int fpos;
char fname[24];
} my_file_type;
my_file_type file[10];
int my_open(const char *path, unsigned int flags, int mode)
{
int file_num;
file_num = get_unused_file();
file[file_num].inuse = TRUE;
strcpy(file[file_num].fname,path);
file[file_num].fpos = 0;
return(file_num);
}
int my_close(int fd)
{
file[fd].inuse = FALSE;
file[fd].fpos = 0;
return 0;
}
my_read(int fd, char *buf, unsigned int count)
{
FILE *fp;
printf("my_read:: 0x%x\n",buf);
fp = fopen (file[fd].fname,"r");
fscanf(fp,"%s",buf);
fclose(fp);
}
my_write(int fd, const char *buf, unsigned int count)
{
FILE *fp;
fp = fopen (file[fd].fname,"r");
fprintf(fp,"%s",buf);
fclose(fp);
}
int my_lseek(int dev_fd, off_t offset, int origin)
{
return -1;
}
int my_rename(const char *old_name, const char *new_name)
{
return -1;
}
int my_unlink(const char *path)
{
return -1;
}
main()
{
FILE *fp;
char str_write[] = "Hello World!";
char str_read[24];
printf("main:: 0x%x\n",str_read);
add_device("mydevice", _MSA,
my_open,
my_close,
my_read,
my_write,
my_lseek,
my_unlink,
my_rename);
fp = fopen("mydevice:testRead.txt","r");
fscanf(fp,"%s",str_read);
fclose(fp);
printf("main:: %s\n",str_read);
}