i am currently trying to delete directories from system call using c and i am facing a weird problem. In my deleteFunction()
after using the char * path
to open the directory. the value of path
changes
here is part of the code:
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void deleteDir(char *path){
//opening the directory
printf("BEFORE %s\n",path );
DIR *p = opendir(path);
if (p == NULL){
printf("Directory not Opened!\n");
exit(2);
}
printf("AFTER %s\n",path );
}
void main(int argc,char *argv[]){
if (argc != 2){
printf("Not enough Arguments!\n");
exit(1);
}
//creating the path
char * currentDir = getcwd(NULL,0);
strcat(currentDir,"/");
strcat(currentDir,argv[1]);
//deleting the directory
deleteDir(currentDir);
exit(0);
}
the output produced is:
BEFORE /home/tarounen/test/newfolder
AFTER /home/tarounen/test/!�
note: i am only taking the directory name as parameter