You could also use popen(3) (instead of system(3)). But you always need to fork a process (both system
and popen
are calling fork(2)). BTW, if you have a CUPS server you might use some HTTP client protocol library like libcurl but that is probably inconvenient. Better popen
or system
an lp
(or lpr
) command.
BTW, printing is a relatively slow and complex operation, so the overhead of forking a process is negligible (I believe you could do that in a server; after all people usually don't print millions of pages). Some libraries might give you some API (e.g. QPrinter in Qt).
Notice that the lp
(or lpr
) command is not actually doing the printing, it is simply interacting with some print daemon (cupsd
, lpd
...) and its spooling system. See e.g. CUPS. So running the lp
or lpr
command is reasonably fast (much faster than the printing itself), generally a few milliseconds (certainly compatible with a multi-threaded or server application).
Quite often, the command passed to popen
or system
is constructed (e.g. with snprintf(3) etc...), e.g.
char cmdbuf[128];
snprintf (cmdbuf, sizeof(cmdbuf), "lp %s", filename);
but beware of code injection (think about filename
containing foo; rm -rf $HOME
) and of buffer overflow
Of course, notice that library functions like system
, popen
, fopen
are generally built above existing syscalls(2). Read Advanced Linux Programming