-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.c
More file actions
247 lines (185 loc) · 7.22 KB
/
Copy pathcommand.c
File metadata and controls
247 lines (185 loc) · 7.22 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
void listDir() {
DIR *dir;
struct dirent *ent;
dir = opendir(".");
if (dir == NULL) {
perror("Unable to open directory\n");
return;
}
while ((ent = readdir(dir)) != NULL) {
write(fileno(stdout), ent->d_name, strlen(ent->d_name));
write(fileno(stdout), " ", 1);
}
write(fileno(stdout), "\n", 1); // Ensure newline at the end
closedir(dir);
}
void showCurrentDir() {
// write(fileno(stdout), "showCurrentDir works\n", 21);
char cwd[1024];
getcwd(cwd, sizeof(cwd));
write(fileno(stdout), cwd, strlen(cwd));
write(fileno(stdout), "\n", 1);
} /*for the pwd command*/
void makeDir(char *dirName) {
//write(fileno(stdout), "makeDir works\n", 14);
if (mkdir(dirName, 0777) == -1){
write(fileno(stdout), "Directory already exists!\n", strlen("Directory already exists!\n"));
}
else{
return;
}
} /*for the mkdir command*/
void changeDir(char *dirName) {
//write(fileno(stdout), "changeDir works\n", 16);
if (chdir(dirName) == -1){
perror("chdir failed");
}
} /*for the cd command*/
void copyFile(char *sourcePath, char *destinationPath) {
struct stat statbuf;
// Open the source file for reading
int sourceFd = open(sourcePath, O_RDONLY);
if (sourceFd == -1) {
write(fileno(stdout), "Source file does not exist\n", strlen("Source file does not exist\n"));
return;
}
// Check if the destination path is a directory
if (stat(destinationPath, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
// If the destination is a directory, append the filename to the destination path
char *filename = strrchr(sourcePath, '/');
if (filename == NULL) {
filename = sourcePath; // No '/' in sourcePath, it's a local file
} else {
filename++; // Skip the '/'
}
char fullDestPath[1024];
//snprintf(fullDestPath, sizeof(fullDestPath), "%s/%s", destinationPath, filename);
strcpy(fullDestPath, destinationPath); // Copy the destination path to fullDestPath
strcat(fullDestPath, "/"); // Append the "/" character
strcat(fullDestPath, filename); // Append the filename to complete the full path
// Open the destination file for writing
int destFd = open(fullDestPath, O_WRONLY | O_CREAT, 0644);
if (destFd == -1) {
write(fileno(stdout), "Failed to create destination file\n", strlen("Failed to create destination file\n"));
close(sourceFd);
return;
}
// Copy the file content
char buffer[1024];
int bytesRead;
while ((bytesRead = read(sourceFd, buffer, sizeof(buffer))) > 0) {
write(destFd, buffer, bytesRead); // Write to destination file
}
// Close file descriptors
close(sourceFd);
close(destFd);
} else {
// If the destination is not a directory, copy the file directly to the destination
int destFd = open(destinationPath, O_WRONLY | O_CREAT, 0644);
if (destFd == -1) {
write(fileno(stdout), "Failed to create destination file\n", strlen("Failed to create destination file\n"));
close(sourceFd);
return;
}
// Copy the file content
char buffer[1024];
int bytesRead;
while ((bytesRead = read(sourceFd, buffer, sizeof(buffer))) > 0) {
write(destFd, buffer, bytesRead); // Write to destination file
}
// Close file descriptors
close(sourceFd);
close(destFd);
}
}
void moveFile(char *sourcePath, char *destinationPath) {
struct stat statbuf;
// Open the source file for reading
int sourceFd = open(sourcePath, O_RDONLY);
if (sourceFd == -1) {
write(fileno(stdout), "Source file does not exist\n", strlen("Source file does not exist\n"));
return;
}
// Check if the destination path is a directory
if (stat(destinationPath, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
// If the destination is a directory, append the filename to the destination path
char *filename = strrchr(sourcePath, '/');
if (filename == NULL) {
filename = sourcePath;
} else {
filename++; // Skip the '/'
}
char fullDestPath[1024];
//snprintf(fullDestPath, sizeof(fullDestPath), "%s/%s", destinationPath, filename);
strcpy(fullDestPath, destinationPath); // Copy the destination path to fullDestPath
strcat(fullDestPath, "/"); // Append the "/" character
strcat(fullDestPath, filename); // Append the filename to complete the full path
// Open the destination file for writing
int destFd = open(fullDestPath, O_WRONLY | O_CREAT, 0644);
if (destFd == -1) {
write(fileno(stdout), "Failed to create destination file\n", strlen("Failed to create destination file\n"));
close(sourceFd);
return;
}
// Move the file content
char buffer[1024];
int bytesRead;
while ((bytesRead = read(sourceFd, buffer, sizeof(buffer))) > 0) {
write(destFd, buffer, bytesRead); // Write to destination file
}
// Close file descriptors and remove the source file
close(sourceFd);
close(destFd);
unlink(sourcePath); // Remove the source file
} else {
// If the destination is not a directory, just move the file directly
int destFd = open(destinationPath, O_WRONLY | O_CREAT, 0644);
if (destFd == -1) {
write(fileno(stdout), "Failed to create destination file\n", strlen("Failed to create destination file\n"));
close(sourceFd);
return;
}
// Move the file content
char buffer[1024];
int bytesRead;
while ((bytesRead = read(sourceFd, buffer, sizeof(buffer))) > 0) {
write(destFd, buffer, bytesRead); // Write to destination file
}
// Close file descriptors and remove the source file
close(sourceFd);
close(destFd);
unlink(sourcePath); // Remove the source file
}
}
void deleteFile(char *filename) {
//write(fileno(stdout), "deleteFile works\n",17);
int fileExist = open(filename, O_RDONLY);
if (fileExist == -1){
write(fileno(stdout), "File does not exist\n", strlen("File does not exist\n"));
return;
}
remove(filename);
} /*for the rm command*/
void displayFile(char *filename) {
//write(fileno(stdout), "displayFile works\n", 18);
int sourceFd = open(filename, O_RDONLY); // open source file for reading
if (sourceFd == -1) {
perror("Cannot open file");
exit(EXIT_FAILURE);
}
char buffer[1024];
int byteCount = read(sourceFd, buffer, 1024);
while (byteCount > 0) {
write(fileno(stdout), buffer, byteCount); // write to destination file
byteCount = read(sourceFd, buffer, 1024);
}
} /*for the cat command*/