0

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.

3
  • 2
    grep -r 'system("rm' Commented Aug 7, 2013 at 10:25
  • 1
    I'd add a space to that pattern, aragaer. But calls to rmdir or similar should be rare anyway. Commented Aug 7, 2013 at 10:38
  • 2
    For files, better use the unlink(2) syscall or the remove(3) library function that calling system("rm Commented Aug 7, 2013 at 10:58

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 e.g. 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 (e.g. million of lines of source code).

In a simpler textual based approach, you might pipe two greps, e.g.

   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 e.g.

  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).

Comments

0

Install ack (http://beyondgrep.com) and your call is:

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

2 Comments

Not sure how using your tool (in this case) is different from grep -P. Also, it seems --cc is not supported (according to the docs)
Functionally it's not different. It's just less typing and less to get wrong. --cc is a filetype. See ack --help-types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.