0

Hello I'm trying to do this shell command "rm -rf test" by doing this:

#include <stdio.h>
#include <stdlib.h>


char name[] = "test";
char buffer[64];
int main()
{
        snprintf(buffer,sizeof(buffer),"rm -rf s% s%", name);
        system(buffer);
        return 0;
}

It will compile and run but doesn't remove the directory

Any help will be greatly appreciated!

user2341069
  • 389
  • 5
  • 8
  • 14

2 Answers2

1

Don't use system to run external processes, especially if the command line you're passing isn't constant. It will only make your life miserable. See man fork and man exec for the right way to do this.

Staven
  • 3,113
  • 16
  • 20
-1
#include <stdio.h>
#include <stdlib.h>

char name[] = "test";
char buffer[64];

int main()
{
        snprintf(buffer,sizeof(buffer),"rm -rf %s", name);
        system(buffer);
        return 0;
}

it works for me.

lamdoc
  • 1