How to write a wrapper
Following an example on how to apply a wrapper around the libc function system()
.
Create a new module (translation units) called system_wrapper.c
like so:
The header system_wrapper.h
:
#ifndef _SYSTEM_WRAPPER
#define _SYSTEM_WRAPPER
#define system(cmd) system_wrapper(cmd)
int system_wrapper(const char *);
#endif
The module system_wrapper.c
:
#include <stdlib.h> /* to prototype the original function, that is libc's system() */
#include "system_wrapper.h"
#undef system
int system_wrapper(const char * cmd)
{
int result = system(cmd);
/* Log result here. */
return result;
}
Add this line to all modules using system()
:
#include "system_wrapper.h"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…