简体   繁体   中英

Using grep to find function

I need to find the usage of functions like system("rm filename") & system("rm -r filename") .

I tried grep -r --include=*.{cc,h} "system" . & grep -r --include=*.{cc,h} "rm" . but they are giving too many outcomes.

How do I search for all the instances of system("rm x") where 'x' can be anything. Kind of new with grep.

Try:

grep -E "system\(\"rm [a-zA-Z0-9 ]*\"\)" file.txt

Regexp [a-zA-Z0-9 ] builds a pattern for grep what it needs to find in x of system("rm x") . Unfortunately, grep don't supports groups for matching, so you will need to specify it directly what to search.

A possible way might be to work inside the GCC compiler. You could use the MELT domain specific language for that. It provides easy matching on Gimple internal representation of GCC.

It is more complex than textual solutions, but it would also find eg calls to system inside functions after inlining and other optimizations.

So customizing the GCC compiler is probably not worth the effort for your case, unless you have a really large code base (eg million of lines of source code).

In a simpler textual based approach, you might pipe two greps, eg

   grep -rwn system * | grep -w rm

or perhaps just

   grep -rn 'system.*rm' *

BTW, in some big enough software, you may probably have a lot of code like eg

  char cmdbuf[128];
  snprintf (cmdbuf, sizeof(cmdbuf), "rm %s", somefilepath);
  system (cmdbuf);

and in that case a simple textual grep based approach is not enough (unless you inspect visually surrounding code).

安装ackhttp://beyondgrep.com ),您的电话是:

ack --cc '\bsystem\(.+\brm\rb'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM