i am learning how to write a mkdir with -p option.
I am struggling for creating subfolders.
the chdir() is not working and thus subdirectories can't be created.
could someone please have a look for me,thanks?
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
void tokenizer(char *path);
main(int ac,char *av[])
{
int result;
if (strcmp(av[1],"-p")!=0)
{
if (mkdir(av[2],0644)==-1)
{
perror("cant make it");
exit(1);
}
}
else if (strcmp(av[1],"-p")==0)
tokenizer(av[2]);
return EXIT_SUCCESS;
}
void tokenizer(char *path)
{
char *dir;
if (mkdir(path, 0644)==-1)
{
dir=strtok(path,"/");
while (dir!=NULL)
{
printf("%s\n",dir);
mkdir(dir, 0644);
if (chdir(dir)==-1)
printf("not right\n");
dir=strtok(NULL,"/");
}
}
}