mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 10:57:34 +00:00
parent
6a37285d93
commit
ee548ca5b9
1 changed files with 30 additions and 17 deletions
|
@ -43,39 +43,52 @@ int main(int argc, char** argv)
|
||||||
bool force = false;
|
bool force = false;
|
||||||
bool verbose = false;
|
bool verbose = false;
|
||||||
|
|
||||||
const char* old_path = nullptr;
|
Vector<const char*> paths;
|
||||||
const char* new_path = nullptr;
|
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.add_option(force, "Force", "force", 'f');
|
args_parser.add_option(force, "Force", "force", 'f');
|
||||||
args_parser.add_option(verbose, "Verbose", "verbose", 'v');
|
args_parser.add_option(verbose, "Verbose", "verbose", 'v');
|
||||||
args_parser.add_positional_argument(old_path, "The file or directory being moved", "source");
|
args_parser.add_positional_argument(paths, "Paths to files being moved followed by target location", "paths");
|
||||||
args_parser.add_positional_argument(new_path, "destination of the move operation", "destination");
|
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
|
if (paths.size() < 2) {
|
||||||
|
args_parser.print_usage(stderr, argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto original_new_path = paths.take_last();
|
||||||
|
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
int rc = lstat(new_path, &st);
|
int rc = lstat(original_new_path, &st);
|
||||||
if (rc != 0 && errno != ENOENT) {
|
if (rc != 0 && errno != ENOENT) {
|
||||||
perror("lstat");
|
perror("lstat");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
String combined_new_path;
|
if (paths.size() > 1 && !S_ISDIR(st.st_mode)) {
|
||||||
if (rc == 0 && S_ISDIR(st.st_mode)) {
|
warnln("Target is not a directory: {}", original_new_path);
|
||||||
auto old_basename = LexicalPath(old_path).basename();
|
|
||||||
combined_new_path = String::format("%s/%s", new_path, old_basename.characters());
|
|
||||||
new_path = combined_new_path.characters();
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = rename(old_path, new_path);
|
|
||||||
if (rc < 0) {
|
|
||||||
perror("rename");
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verbose)
|
for (auto& old_path : paths) {
|
||||||
printf("renamed '%s' -> '%s'\n", old_path, new_path);
|
String combined_new_path;
|
||||||
|
const char* new_path = original_new_path;
|
||||||
|
if (rc == 0 && S_ISDIR(st.st_mode)) {
|
||||||
|
auto old_basename = LexicalPath(old_path).basename();
|
||||||
|
combined_new_path = String::format("%s/%s", original_new_path, old_basename.characters());
|
||||||
|
new_path = combined_new_path.characters();
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = rename(old_path, new_path);
|
||||||
|
if (rc < 0) {
|
||||||
|
perror("rename");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (verbose)
|
||||||
|
printf("renamed '%s' -> '%s'\n", old_path, new_path);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue