I am writing a pseudo-shell in c for a class and my cp command needs to account for the destination being a file or a directory. I realized after I implemented everything I currently only truly account for a file name as destinationPath. How should I go about accounting for that?
void copyFile(char *sourcePath, char *destinationPath) {
//cp command
int srcCheck, destCheck, standardCheck;
char* buff[1024];
srcCheck = open(sourcePath, O_RDONLY);
if (srcCheck < 0) {
write(1, "Error with opening source\n", strlen("Error with opening source\n"));
return;
}
destCheck = open(destinationPath, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (destCheck < 0) {
close(srcCheck);
write(1, "Error something went wrong with dest\n", strlen("Error something went wrong with dest\n"));
return;
}
while ((standardCheck = read(srcCheck, buff, BUFSIZ)) != 0) {
write(destCheck, buff, standardCheck);
}
if (standardCheck == -1) {
write(1, "Error reading\n", strlen("Error reading\n"));
return;
}
if (close(srcCheck) == -1) {
write(1, "Error closing source\n", strlen("Error closing source\n"));
return;
}
if(close(destCheck) == -1) {
write(1, "Error closing dest\n", strlen("Error closing dest\n"));
return;
}
}
I already looked through a couple different cp implementations but couldn’t find any that considered destination may be a directory
You need to sign in to view this answers
Leave feedback about this