I'm porting net-snmp to an embedded platform that only has limited access to the filesystem and I stumbled upon a big problem. There's a part of the core code that uses the ungetc() function, which I don't have. There are of course 2 solutions:
A) write my own ungetc() using what I have
B) modify net-snmp code in order to achieve the same result without ungetc()
Solution (B) will be eventually discussed in the net-snmp coders mailing list since requires deep understanding of the library's internals, so let's please focus on feasibility of (A)
What I have on my embedded system is:
fopen()
fclose()
fcreate()
fwrite()
fread()
fdelete()
fclear()
fcopy()
ffindfirst()
ffindnext()
frename()
fgetsize()
ftell()
fseek()
fgetc()
fgets()
The main difference is that my file functions work with INT32* file handles instead of FILE* types. I don't have the FILE* type.
What the ungetc() function does is to basically "put back the char in the stream" , either the char that it just read or another one.
In the first case the solution is easy, I rewind the pointer with fseek() one position backwards.
But in the second case I have a problem. I would be modifying the stream and not the file, except I don't have streams! I'm reading the file directly.
With ungetc() you can do something like
FILE *fp = fopen("file.txt", "r");
int c = getc (fp);
if( c == 'a' ) ungetc ('b', fp);
If "file.txt" contains "abcdefghi", a subsequent read with gets() will read "bbcdefghi" and not "abcdefghi" because the content IN THE STREAM has been changed, but not the file!
How can I replicate this behavior if I don't have "streams" ? My getc() and gets() read from an INT32* file handle and I don't have a puts() or putc() equivalent.
I can only write with fwrite() but that alters the content on the NV memory.
Thank you for your insight