fooforbar wrote:I am trying to figure out how to write to a file in c. I could only find how to do this with libraries, and I really, really don't want to spend my time linking (shudder
). Thanks! BTW I'm compiling with GCC.
I assume you know how to do this
with the C Standard I/O library, and just need to bypass it for some reason; otherwise, this is not the right forum for you, and you need to ask the question again in a forum better suited to answering it. If you aren't an experienced programmer already, and well-versed in general application programming, then diving into OS dev at this point is about as sensible as skydiving without any lessons, preparations, or
parachute.
The disdain for linking doesn't make sense, though - even with OS dev (maybe
especially with OS dev), linking is a basic part of the development process. Indeed,
implementing (or at least porting) a linker is a key part of OS dev. A single binary blob isn't going to cut it with any general-purpose OS.
Stipulating that, I we'd also need more details as to what you are trying to accomplish... a lot more. Starting with the platform (presumably PC, but you never know with this group) and OS (and there has to be one, otherwise the concept of 'file' wouldn't exist.
If this is for your own OS, then the answer is, "first, write the disk drivers and the filesystem". You wouldn't be writing to a 'file' at that point, but to raw disk sectors, which aren't at all the same thing. I assume that this would, in fact, be the question you want to answer if you are OS-Dev'ing, but I wouldn't expect it to be posted in "General Programming" in that case.
Otherwise, the answer is either, "with calls out to the file system driver, via whatever system calls and/or IPC the operating system provides". This means either linking to the libraries which exposed the APIs for the OS and the file system, or linking to the libraries with all the C standard I/O functions which are already wrapped around those system calls for your convenience.
Depending on the OS, there may be some way to do it without a library of some sort by invoking the system calls manually (which could mean a lot of different possible mechanisms depending on the OS and platform; just on x86 alone this might mean an INT, a SYSENTER, a non-impl trap, or a call gate, among other things). However, that would a) require some assembly coding, which may or may not work as inline assembly, and b) be entirely dependent on not only the OS, but possibly on the specific
version of the OS in question, since low-level mechanisms are actually more subject to change than high-level ones.
So again, we'd need more detail to give a meaningful answer.