-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkdir.c
49 lines (48 loc) · 1.1 KB
/
mkdir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
#include<string.h>
int main(int argc, char const *argv[])
{
//printf("%s\n","hiii" );
if(strcmp(argv[1],"-v")==0){//to display messages
struct stat st={0};
if(stat(argv[2],&st)==-1){//checks if such dirtectory does not exists
mkdir(argv[2],0700);
printf("mkdir: created directory '%s'\n",argv[2] );
}
else{
printf("mkdir: cannot create directory ‘%s’: File exists\n",argv[2]);
}
}
else if(strcmp(argv[1],"-p")==0){
printf("%s\n","hellooo" );
char path[1000];
snprintf(path,sizeof path,"%s",argv[2]);
size_t length=strlen(path);
char *f;
/*if(path[length-1] == '/'){
path[length-1] = 0;
}*/
for(f=path+1;*f;f++){
if(*f=='/'){
*f='\0';
mkdir(path,S_IRWXU);
*f='/';
}
}
mkdir(path,S_IRWXU);
printf("mkdir: created directory '%s'\n",argv[2] );
}
else{
struct stat st={0};
if(stat(argv[1],&st)==-1){//checks if such dirtectory does not exists
mkdir(argv[1],0700);
}
else{
printf("mkdir: cannot create directory ‘%s’: File exists\n",argv[1]);
}
}
return 0;
}