user1775655
user1775655

Reputation: 327

execlp behaving strangely

I am attempting to run a very simple program with fork and execlp, but it does not work as I would expect. I presently have a file in my working directory simply named '1'. So the command rm 1* should remove it. However when tried via execlp, it does not.

int main()
{

  if(fork()==0)
    {
      execlp("rm", "rm", "1*",  NULL);
      perror("Problem\n");
    }

  return 0;
}

Thank you.

Upvotes: 1

Views: 392

Answers (1)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215257

For what you're trying to do, you want:

execlp("sh", "sh", "rm 1*", (char *)0);

Note that this is a rather bad idea, from the standpoints of security, robustness, and efficiency. If you want to remove files matching a pattern, you should do this directly in C. It's easy with the glob function and a simple loop.

Upvotes: 1

Related Questions