-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcp.c
More file actions
210 lines (185 loc) · 7.55 KB
/
cp.c
File metadata and controls
210 lines (185 loc) · 7.55 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <fcntl.h>
#include <stdio.h>
// for stat()
#include <sys/stat.h>
#include <unistd.h>
// for errorno
#include <errno.h>
// for exit() free() malloc()
#include <stdlib.h>
// for opendir(), readdir(), closedir()
#include <dirent.h>
// for string manipulation methods
#include <string.h>
// for basename()
#include <libgen.h>
// for mkdir()
#include <sys/types.h>
#define BUF_SIZE 1024
int cp(int argc, char* args[]){
if (strcmp(args[1],"-r")==0){
// open the source directory
DIR *d = opendir(args[2]);
// if open fails, exit
if (d<0){
printf("%s\n",strerror(errno));
exit(EXIT_SUCCESS);
}
else{
// malloc memory to store destination path
char* dest = malloc(strlen(args[3])*sizeof(char*) + 1);
dest = strcpy(dest, args[3]);
// make a path inside dest path
strcat(dest,"/");
strcat(dest,basename(args[2]));
// mkdir() inside dest dir
if (mkdir(dest,777)<0){
printf("%s\n",strerror(errno));
exit(EXIT_FAILURE);
}
// read source dir
struct dirent *p;
int empty = 1;
while((p = readdir(d))!=NULL){
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
continue;
empty = 0;
// malloc memory to store dest pathname
char* temp = malloc(strlen(dest)*sizeof(char*) + 1);
temp = strcpy(temp, dest);
strcat(temp,"/");
// malloc memory to store source file
char* source = malloc(strlen(args[2])*sizeof(char*) + 1);
source = strcpy(source, args[2]);
strcat(source,"/");
strcat(source, p->d_name);
// stat the source dir file
struct stat sb;
if (stat(source,&sb)<0){
printf("%s\n",strerror(errno));
exit(EXIT_FAILURE);
}
// concatenate basename of source
strcat(temp, basename(source));
// if dir inside source dir
if (S_ISDIR(sb.st_mode)){
if (mkdir(temp,777)<0){
printf("%s\n",strerror(errno));
exit(EXIT_FAILURE);
}
}
// if file inside source dir
else if (S_ISREG(sb.st_mode)){
char* buf[BUF_SIZE];
// open source file
int sourceFd = open(source, O_RDONLY);
if (sourceFd<0){
printf("%s\n",strerror(errno));
exit(EXIT_FAILURE);
}
// create that file in destination directory
int destFd = open(temp, O_CREAT | O_RDONLY | O_WRONLY | O_TRUNC);
if (destFd<0){
printf("%s\n",strerror(errno));
exit(EXIT_FAILURE);
}
// copy contents of source file to destination file
size_t linesRead = 0;
while((linesRead = read(sourceFd,buf,BUF_SIZE)) > 0){
if (write(destFd,buf,linesRead)!=linesRead){
printf("\nError writing file %s\n",p->d_name);
}
}
// close all files and free the memory
if (close(sourceFd)<0){
printf("%s\n",strerror(errno));
}
if (close(destFd)<0){
printf("%s\n",strerror(errno));
}
}
free(temp);
free(source);
}
free(dest);
}
}
else{
for (int i = 1; i < argc-1; i+=1){
char* buf[BUF_SIZE];
// open ith source file
int sourceFd = open(args[i], O_RDONLY);
if (sourceFd<0){
printf("%s\n",strerror(errno));
exit(EXIT_FAILURE);
}
printf("SRCOPENINNOT-R\n");
// malloc memory to store destination path
char* dest = malloc(strlen(args[argc-1])*sizeof(char*) + 1);
dest = strcpy(dest, args[argc-1]);
struct stat sb;
// if stat fails
// it means the destination given is not a directory
if (stat(dest, &sb)<0){
// create a dest file
int destFd = open(dest, O_CREAT | O_RDONLY | O_WRONLY | O_TRUNC);
if (destFd<0){
printf("%s\n",strerror(errno));
exit(EXIT_FAILURE);
}
// copy every line of source to dest
size_t linesRead = 0;
while((linesRead = read(sourceFd,buf,BUF_SIZE)) > 0){
if (write(destFd,buf,linesRead)!=linesRead){
printf("\nError writing file %s\n",args[2]);
}
}
if (close(sourceFd)<0){
printf("%s\n",strerror(errno));
}
if (close(destFd)<0){
printf("%s\n",strerror(errno));
}
}
else if (S_ISDIR(sb.st_mode)){
// concatenate source filename to destination
strcat(dest, "/");
strcat(dest, basename(args[i]));
// create that file in destination directory
int destFd = open(dest, O_CREAT | O_RDONLY | O_WRONLY | O_TRUNC);
if (destFd<0){
printf("%s\n",strerror(errno));
exit(EXIT_FAILURE);
}
// copy contents of source file to destination file
size_t linesRead = 0;
while((linesRead = read(sourceFd,buf,BUF_SIZE)) > 0){
if (write(destFd,buf,linesRead)!=linesRead){
printf("\nError writing file %s\n",args[i]);
}
}
// close all files and free the memory
if (close(sourceFd)<0){
printf("%s\n",strerror(errno));
}
if (close(destFd)<0){
printf("%s\n",strerror(errno));
}
}
free(dest);
}
}
return 0;
}
int main(int argc, char* args[])
{
// if not all three arguments are passed
if (argc<3){
printf("Expected argument: Cp [OPTIONAL] <source> <destination>");
exit(EXIT_FAILURE);
}
else{
int temp = cp(argc,args);
}
return 0;
}