I have no issues with the library functions. I know that they work well. I am interested in their implementation. My question is: Can I write working versions of these functions for Windows x64 using only C?
Many of the stnadard library functions are written in C, and fopen, fread etc. are no exception. You can write a wrapper around open, read, write etc. which are usually lower level functions.
If those are not available, you can also do the same, calling the respective OS functions and wrapping them with your own implementation, you just have to make sure that they are complying to the standard.
Just as an example you can find a source for fopen here.
I'm writing some C library bindings using inline-c, and some of the C functions take FILE* parameters. Is there a preferred way to convert a Haskell Handle to a FILE*? Or is this the wrong approach to interfacing with the library?
My initial thoughts were to use handleToFd
to first convert a given Handle to a file-descriptor and then call fopen() to get a FILE* to pass to library functions. The docs for handleToFd state that using it on a Handle has the effect of flushing and closing it, so do do the aforementioned I'd have to record the state of the Handle(?) and reinstate it when calling fopen(). This seems like a bad idea to me because AFAIK there's no way to reopen a standard stream, and I might have to resort to black magic if I use Handle internals.
I want to know what happen when compiler encountered "fopen()" in C program.
Where is the function which calls the 'open' system call?
I see fopen() is declared in stdio.h, but I cannot find where it is implemented!!
I want to see where system call number and other parameters of system calls are written to CPU registers.
Thank you.
You can look at glibc implementation here: https://sourceware.org/git/?p=glibc.git;a=blob;f=libio/iofopen.c;h=be2bbb663bc04093493313af266fcdad85c62c28;hb=HEAD#l95
_IO_new_fopen is a symbolic constant (macro) that expands to fopen The fopen function is a wrapper around an internal function to a libio function.
fopen isn't a system call, you might be confusing it with open which is a system call.
I'm writing a university project. Writing in standard C99. One of the requirements is the lack of exit(); function. Is it possible to implement a similar function?
I tried to make a function that calls main with a minus argc to detect exit. It was a stupid attempt, because the first main continues.
Just the description of the project specified that the scores will be reduced for the use of exit by exit().I understand that it asks me to code running through pointers and returns an error in the return values of the function. I'm more interested in the practice. Only for myself.
I think you misunderstood the requirement: They probably said something like do not use exit(). This does not mean you are supposed to implement your own exit(), quite to the contrary: they probably mean that the only exit-point of your program shall be the end of your main-function (or a return-statement within the main function) which is considered good programming style.
exit() is a system level facility that you can't implement on your own without knowing how the operating system implements it (Linux? Windows? embedded system?) works. As Daniel Fischer mentioned, you could call abort() which will basically do the same thing that exit will do and quit the program.
There are other "hacks" to get your program to abort without calling exit() explicitly, but these are just hacks and should not be used in production code.
Create a C++ function with C linkage and throw an exception
extern "C" MyExit() { throw std::exception(); }
Call signal() with SIGKILL
Call abort()
Write some assembly code to unwind the call stack until it gets to the function that called main and insert the return value in to the proper return register and go from there. I don't think you can do this in pure C, as the ABI is not accessible directly. But at least this would be only method that doesn't involve the operating system (just the ABI).
I started writing a little program in C as a development excercise and I want to have a lot of unit test for this programs. The problem is that from time to time it uses fread/fwrite and other standard functions - I don't want my unit tests to open any file, so it would be great if I could write stub for fread/fwrite.
Anyone have solution for this problem?
Thanks in advance for answers!
edit: I forgot about one important thing: stub for read need to behave differently in different tests. In one, read return value must be < then passed number of elements to read (simulates too early eof) and in other read return value must be equal to passed number of elements to read
There are multiple options:
Turn fopen(), fread(), etc into indirect function calls, through function pointers. For normal operation set them to point to fopen(), etc. For testing, set them to point to your functions.
Define your replacement functions myfopen(), myfread(), etc. Compile the main part of the program with the macros named fopen, fread, etc and expanding into myfopen, myfread, etc.
Define your own fopen(), fread(), etc. Play with the linker to make them replace the ones from the standard library. This is very error-prone and hard to do.
Hook fopen(), fread(), etc functions using disassembly, assembly and run-time code patching. This isn't easy either.
I can recommend the Working Effectively with Legacy Code book as it deals with this in a number of chapters. I've used it myself in similar situations.
There's two recommendations for unit testing in procedural languages (other than converting to an object-oriented language):
Using the "linker seam". Use wrapper functions around the API calls and compile with different object files depending on whether you're compiling the tests or deliverables. The test version would stub out the calls inside the wrapper functions, whereas the version for release code would pass the calls through to fread, fwrite, etc.
Using the "preprocessing seam", where you use #ifdefs to change the behaviour of either fread/fwrite/etc by defining local versions under a test-specific #ifdef or having the slightly more maintainable option of wrapper functions (again) that change behaviour depending on whether the test-specific #ifdef is defined.
If I understand the question correctly, the easiest way to do this is to use macro (skipping discussing about its evilness):
#ifdef UNIT_TESTS
#define fwrite(ptr, size, nmemb, stream) (nmemb)
#endif
And same for fread. Now, you can use compiler flag to specify macro UNIT_TESTS when compiling with unit tests (like gcc -DUNIT_TESTS ... for gcc).
I'd go with the macro too.
Create a file with:
#define fopen FOPEN
#define fread FREAD
#define fwrite FWRITE
and then add that as a forced include so it gets in before all other includes.
You can then implement FOPEN in you stub code to do whatever you want.