user1888502
user1888502

Reputation: 373

Taking input without using libc

How would I take input or print output without using libc? I can use system calls, but didn't know if that would help.

Upvotes: 2

Views: 446

Answers (2)

templatetypedef
templatetypedef

Reputation: 372814

There is no platform-independent way to do this. In fact, the whole point of having libc is to have a common interface to a set of functionality that most systems provide, but do so in fundamentally different ways.

Your best option would probably be to consult the documentation for whatever system you are currently using. You could look up your OS's set of interrupts and then try using the asm keyword to write assembly instructions that tell the OS to read input or display output. You could look into libraries provided by the OS for doing input and output on file descriptors, then use those functions instead. Or, you could look at process creation libraries, then spawn off a process to read or write data from the console, where the second program uses libc. None of these are guaranteed to be at all portable, though.

Hope this helps!

Upvotes: 2

DigitalRoss
DigitalRoss

Reputation: 146073

I stepped through write(2) with gdb to get an idea of how the system call ABI works.

Anyway, no libc at all. Note that without special tricks, the cc(1) compiler/linker front-end will still link you with libc, but you won't be using it for anything. The C runtime start-up code will make some libc calls, but this program won't.

void mywrite(int fd, const void *b, int c) {
  asm("movl $1, %eax");
  asm("syscall");
}

int main(void) { const char *s = "Hello world.\n"; return mywrite(1, s, 13), 0; }

Upvotes: 2

Related Questions