I wrote a function in C that implements the mv command using Linux system calls. However, my implementation only works when renaming files. When I try to use my "mv" implementation to move a file to a different directory, the move seems to be unsuccessful, meaning that the file just stays in its current directory.
Here is my function for my "mv" implementation:
void moveFile(char *sourcePath, char *destinationPath) {
char *error;
/* determine if Source Path exists */
if(access(sourcePath, F_OK) != 0) {
error = "mv: cannot stat '";
write(2, error, strlen(error));
write(2, sourcePath, strlen(sourcePath));
write(2, "': No such file or directory\n", strlen("': No such file or directory\n"));
}
/* determine if Source Path and Destination Path are identical */
if(strcmp(sourcePath, destinationPath) == 0) {
write(2, "mv: ", strlen("mv: "));
write(2, sourcePath, strlen(sourcePath));
write(2, " and ", strlen(" and "));
write(2, destinationPath, strlen(destinationPath));
write(2, " are the same\n", strlen(" are the same\n"));
}
else {
/* rename or move the file from sourcePath to destinationPath */
int ret_val = rename(sourcePath, destinationPath);
if(ret_val != 0) {
write(2, "move unsuccessful\n", strlen("move unsuccessful\n"));
}
}
}
How can I fix this, so that it works for moving files to different directories?