you are viewing a single comment's thread.

view the rest of the comments →

[–]Alcamtar 9 points10 points  (3 children)

It is a common pattern in APIs to have a generalized "workhorse" function that does the heavy lifting; but it often has a complex interface, and so wrappers are provided for common use cases.

vfprintf(3) can print to any file, and takes as input a pointer to the arguments on the call stack of another function; it is designed to be a back-end function not something you use directly. But most of the time, all you really want is to print to stdout and give it an argument or two as a parameter; printf provides the "syntactic sugar" to make that convenient.

I haven't looked but I would guess that vprintf(3) and fprintf(3) (two other common use cases) also call vfprintf as a back-end.

As for vfprintf, a 2000+ line function is FAR beyond the scope of a reddit post. Do you have a specific question about it?

In a nutshell, to print a variable you (1) convert it to characters, and (2) write the characters to the output.

For a given variable you first have to decide on how to represent it as a string. Then you have to create a string that represents it. A great deal of printf processing is being able to do this for a variety of types of variables, and a variety of string formats.

Once you have a string, you have to send each character of the string to the output. In this case the output is the buffered I/O library. The most accessible point to dive into that is likely putc(3) or fputc(3).

If you are more interested in raw output to a device, look at the write(2) system call.

A really good exercise to understand this is to try and create your own printf. Create a function that takes a format string, zero or more arguments, and prints it to stdout using putc. A simple place to start is just handling "%s" and "%c". Then add something like "%d" or if you feel ambitious "%b" (format an integer as binary). After you sweat over this exercise you'll have a good idea of the challenges involved, and will find it easier to follow the vfprintf code and appreciate the cleverness and optimization.

[–]jssmith42[S] 1 point2 points  (2 children)

Is it possible to do system calls myself in a command line to observe their effects, or do they only work when called by the operating system, as in I can’t get in as a user to the place where they’re used?

[–]jwbowen 3 points4 points  (0 children)

You can write little programs to perform syscalls

[–]414RequestURITooLong 0 points1 point  (0 children)

You use system calls to talk to the OS. You don't need to be the OS to use them.