hek*_*mgl 16
我假设程序将调用glibc函数isatty()来检查stdout是否是终端.对于在终端上使用彩色输出或ANSI终端的其他功能(如光标定位或线擦除/重绘)的程序而言,这是很常见的.
您可以使用LD_PRELOAD环境变量欺骗程序.LD_PRELOAD由ELF链接器处理,并告知应在所有其他链接之前加载动态库.使用此功能可以覆盖库函数,在您的情况下可以使用glibc函数isatty().例如,您可以按照本文进行操作.
我为你准备了一个例子:
首先创建文件libisatty.c:
/**
* Overrides the glibc function. Will always return true.
*
* Note: Although this should be ok for most applications it can
* lead to unwanted side effects. It depends on the question
* why the programm calls isatty()
*/
int isatty(int param) {
return 1;
}
Run Code Online (Sandbox Code Playgroud)
并将其编译为共享库:
gcc -shared -o libisatty.so libisatty.c
Run Code Online (Sandbox Code Playgroud)
它应该建立好.
现在是时候测试库了.:)我已经使用该命令ls --color=auto进行测试.ls要求isatty()决定是否应该对其输出进行着色.如果输出重定向到文件或管道,则不会着色.您可以使用以下命令轻松测试:
ls --color=auto # should give you colorized output
ls --color=auto | cat # will give you monochrome output
Run Code Online (Sandbox Code Playgroud)
现在我们将使用LD_PRELOAD环境var再次尝试第二个命令:
LD_PRELOAD=./libisatty.so ls --color=auto | cat
Run Code Online (Sandbox Code Playgroud)
你应该看到彩色输出.
btw cool usename:uʍopǝpısdn!!:D