An awfully complicated way to do almost what you want is to use the dup2() system call. This requires executing fflush(stdout); dup2(silentfd, stdout);
before function()
is called, and copying back afterwards: fflush(stdout); dup2(savedstdoutfd, stdout);
. So it is not possible to do as just silence(function())
, since this construct only allows to execute code after function()
has already been executed.
The file descriptors silentfd
and savedstdoutfd
have to be prepared in advance (untested code):
int silentfd = open("/dev/null",O_WRONLY);
int savedstdoutfd = dup(stdout);
This is almost certainly not what you really want, but inasmuch as your question is phrased as “is it possible?”, the answer is “almost”.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…