-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycpoy.c
129 lines (107 loc) · 3.08 KB
/
mycpoy.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* mode 3 not working yet, pls don't add first argument as a directory*/
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <signal.h>
int mode1 ( int argc, char *argv[] )
{
FILE *file1,*file2;//File pointers for copy
char c,bin[128],*dump;
//Copy function checks for first argument as file
file1=fopen(argv[1],"rb");
if(!file1)
return(printf("error, unable to open file..................does the file exist?\n"));
//Checks if last character of the second arg is / or not to find if its a directory
if(argv[2][strlen(argv[2])-1] != '/')
{
//Open file 2
file2=fopen(argv[2],"wb");
if(!file2)
{
return(printf("unable to find the file........pls check the path and retry!\n"));
}
}
else
{
DIR* dir = opendir(argv[2]);
if(!dir)
{
return(printf("error, directory doesn't exist...........please create the directory before copying\n"));
}
if(argv[2][strlen(argv[2])-1]=='/')
{
strcpy(bin,argv[1]);
dump=strtok(bin,"/");
while(dump)
{
strcpy(bin,dump);
dump=strtok(NULL,"/");
}
strcat(argv[2],bin);
}
else if(argv[1][0]!='/')
strcat(argv[2],argv[1]);
printf("\n%s %s\n", argv[1],argv[2]);
file2=fopen(argv[2],"wb");
if(!file2)
{
return(printf("error, directory doesn't exist...........please create the directory before copying\n"));
}
}
int n;
while ((n = fread(bin, 1, 128, file1)) > 0)
{
fwrite(bin,1, n, file2);
}
fclose(file1);
fclose(file2);
}
int mode2( int argc, char *argv[] )
{
//printf("%c\n",argv[argc-1][strlen(argv[argc-1])-1]);
if(argv[argc-1][strlen(argv[argc-1])-1]!='/')
return(printf("Please give a directory as the last argument!\n"));
return(printf(""));
char *new[3];
for(int i=1;i<argc-1;i++)
{
new[1] = argv[i];
strcpy(new[2],argv[argc-1]);
mode1(3, new);
}
}
int mode3( int argc, char *argv[] )
{
DIR* dir = opendir(argv[1]);
struct dirent* in_files;
if (NULL == (dir = opendir(argv[1])))
{
return(printf("Unable to open directory..........does the directory exist?"));
}
char *new[3],tmp[2][128];
while (in_files = readdir(dir))
{
strcpy(tmp[1],argv[2]);
new[2]=tmp[1];
if (in_files->d_type!=DT_REG)
continue;
strcpy(tmp[0],argv[1]);
strcat(tmp[0],in_files->d_name);
new[1]=tmp[0];
printf("%s\t%s\n",new[1],new[2]);
mode1(3,new);
}
}
int main( int argc, char *argv[] )
{
//Checks number of command line arguments
if((argc==3) && (argv[1][strlen(argv[1])-1] != '/'))
mode1(argc,argv);
else if((argc==3) && (argv[2][strlen(argv[2])-1] == '/'))
mode3(argc,argv);
else if(argc>3)
mode2(argc,argv);
return(0);
}