2 c linux error-code
在我维护的软件基准中,在各种C应用程序中分布有150条语句,这些语句使用调用另一个Linux命令(例如rm -rf ...)或自定义应用程序status = system(cmd)/256。当调用任一方法时,从Linux命令或自定义应用程序返回的状态代码将被256除。这样,当状态码大于0时,我们知道存在问题。但是,按照编写软件的方式,它并不总是记录什么命令或应用程序返回了状态代码。因此,如果状态码为32768,则除以256时,报告的状态码为128。
该软件很旧,虽然我可以进行更改,但是如果任何调用的命令或所调用的应用程序在其他地方报告了其原始状态代码,那将是很好的。
有没有办法确定标准Linux日志文件和返回它的应用程序中的原始状态代码?
如何编写包装器
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
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
Add this line to all modules using system():
#include "system_wrapper.h"
Run Code Online (Sandbox Code Playgroud)