-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmv.h
72 lines (69 loc) · 2.16 KB
/
gmv.h
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Function accepts args as parameter and return 1 to continue the loop() on successful completion with no error
int count_no_args(char **cmd_list){
int i=0;
while(cmd_list[i]!=NULL){
i++;
}
return i;
} // Function to count the no. of arguments present
char *check_path(char **args){
char *input = args[1];
char *output = NULL;
output = strrchr(input, '/');
if(output != NULL){
return output+1 ;
}
return input;
}
int gmv (char **args){
int argc = count_no_args(args);
if(argc<3){
printf("Format : gmv <file1_path> <directory_name_or_path>\n");
return 1;
}
char *file = check_path(args); // Getting file name from path if it is a path specified by the user
char *dest= args[2]; // Destination directory
char current_dir[50];
if (args[2]==NULL){
printf("Enter the destination dir\n");
}
else
{
if(dest[0]=='/') //Checking if the destination is a name or path
{
strcat(dest,"/"); //Preparing path for the complete path by appending the strings.
strcat(dest,file);
if(rename(args[1], dest) == 0) // Moving
printf("Successful\n");
else
printf("Error: Directory not found\n");
}
else
{
DIR *dir;
dir = opendir(dest); // Opeining the directory
if(dir==NULL)
{
if( rename(args[1],dest)!= 0)
printf("Error: File not moved\n");
else
printf("Successful\n");
}
else
{
char *ptr;
ptr = getcwd(current_dir, 50); // Get CWD.
strcat(current_dir,"/");
strcat(current_dir,dest); // Attaching the dest
strcat(current_dir,"/");
strcat(current_dir, file); // Complete path for the File
if(rename(args[1],ptr)!=-1)
printf("Successful\n");
else
printf("Error: Directory not found in CWD\n");
closedir(dir);
}
}
}
return 1;
}