system()
我的代码里面有一个使用awk 的命令.我无法弄清楚如何解决\x00
十六进制值的问题.显然他们需要以不同的方式被终止,但这超出了我所知道的范围.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char command[128];
snprintf(command, sizeof(command), "awk '{ gsub (/\xAB\x00\x00\xBC/,\"\xBC\x00\x00\xAB\") ; print }' %s", argv[1]);
system(command);
}
Run Code Online (Sandbox Code Playgroud)
警告/错误:
> test.c:8:56: warning: format string contains '\0' within the string body [-Wformat]
> snprintf(command, sizeof(command), "awk '{ gsub (/\xAB\x00\xBC/,\"\xBC\x00\x00\xAB\") ; print }' %s", argv[1]);
> /usr/include/secure/_stdio.h:57:62: note: expanded from macro 'snprintf'
> __builtin___snprintf_chk (str, len, 0, __darwin_obsz(str), __VA_ARGS__)
^
1 warning generated.
sh: -c: line 0: unexpected …
Run Code Online (Sandbox Code Playgroud) 我在 bash shell 中使用 grep 来查找文件中的一系列十六进制字节:
$ find . -type f -exec grep -ri "\x5B\x27\x21\x3D\xE9" {} \;
Run Code Online (Sandbox Code Playgroud)
搜索工作正常,尽管我知道在不使用-a
仅返回结果的选项时匹配存在限制:
Binary file ./file_with_bytes matches
Run Code Online (Sandbox Code Playgroud)
我想获取匹配结果的偏移量,这可能吗?我愿意使用另一个类似的工具,我只是不确定它会是什么。