#include "xalloc.h"
#include "c.h"
#include "closestream.h"
+#include "rpmatch.h"
#define RENAME_EXIT_SOMEOK 2
#define RENAME_EXIT_NOTHING 4
return 0;
}
-static int do_symlink(char *from, char *to, char *s, int verbose, int noact, int nooverwrite)
+static int ask(char *name)
+{
+ int c;
+ char buf[2];
+ printf(_("%s: overwrite `%s'? "), program_invocation_short_name, name);
+ fflush(stdout);
+ if ((c = fgetc(stdin)) == EOF) {
+ buf[0] = '\0';
+ clearerr(stdin); errno = 0;
+ printf("\n");
+ }
+ else {
+ buf[0] = c; buf[1] = '\0';
+ if (c != '\n')
+ while ((c = fgetc(stdin)) != '\n' && c != EOF);
+ }
+ if (rpmatch(buf) == RPMATCH_YES)
+ return 0;
+ else
+ return 1;
+}
+
+static int do_symlink(char *from, char *to, char *s, int verbose, int noact,
+ int nooverwrite, int interactive)
{
char *newname = NULL, *target = NULL;
int ret = 1;
if (string_replace(from, to, target, target, &newname))
ret = 0;
- if (ret == 1 && nooverwrite && lstat(target, &sb) == 0) {
+ if (ret == 1 && (nooverwrite || interactive) && lstat(newname, &sb) != 0)
+ nooverwrite = interactive = 0;
+
+ if ( ret == 1 &&
+ (nooverwrite || (interactive && (noact || ask(newname) != 0))) )
+ {
if (verbose)
printf(_("Skipping existing link: `%s' -> `%s'\n"), s, target);
-
ret = 0;
}
return ret;
}
-static int do_file(char *from, char *to, char *s, int verbose, int noact, int nooverwrite)
+static int do_file(char *from, char *to, char *s, int verbose, int noact,
+ int nooverwrite, int interactive)
{
char *newname = NULL, *file=NULL;
int ret = 1;
if (string_replace(from, to, file, s, &newname))
return 0;
- if (nooverwrite && access(newname, F_OK) == 0) {
+ if ((nooverwrite || interactive) && access(newname, F_OK) != 0)
+ nooverwrite = interactive = 0;
+
+ if (nooverwrite || (interactive && (noact || ask(newname) != 0))) {
if (verbose)
printf(_("Skipping existing file: `%s'\n"), newname);
ret = 0;
fputs(_(" -s, --symlink act on the target of symlinks\n"), out);
fputs(_(" -n, --no-act do not make any changes\n"), out);
fputs(_(" -o, --no-overwrite don't overwrite existing files\n"), out);
+ fputs(_(" -i, --interactive prompt before overwrite\n"), out);
fputs(USAGE_SEPARATOR, out);
printf(USAGE_HELP_OPTIONS(21));
printf(USAGE_MAN_TAIL("rename(1)"));
int main(int argc, char **argv)
{
char *from, *to;
- int i, c, ret = 0, verbose = 0, noact = 0, nooverwrite = 0;
- int (*do_rename)(char *from, char *to, char *s, int verbose, int noact, int nooverwrite) = do_file;
+ int i, c, ret = 0, verbose = 0, noact = 0, nooverwrite = 0, interactive = 0;
+ int (*do_rename)(char *from, char *to, char *s, int verbose, int noact,
+ int nooverwrite, int interactive) = do_file;
static const struct option longopts[] = {
{"verbose", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{"no-act", no_argument, NULL, 'n'},
{"no-overwrite", no_argument, NULL, 'o'},
+ {"interactive", no_argument, NULL, 'i'},
{"symlink", no_argument, NULL, 's'},
{NULL, 0, NULL, 0}
};
textdomain(PACKAGE);
atexit(close_stdout);
- while ((c = getopt_long(argc, argv, "vsVhno", longopts, NULL)) != -1)
+ while ((c = getopt_long(argc, argv, "vsVhnoi", longopts, NULL)) != -1)
switch (c) {
case 'n':
noact = 1;
break;
case 'o':
nooverwrite = 1;
+ interactive = 0;
+ break;
+ case 'i':
+ interactive = 1;
+ nooverwrite = 0;
break;
case 's':
do_rename = do_symlink;
return RENAME_EXIT_NOTHING;
for (i = 2; i < argc; i++)
- ret |= do_rename(from, to, argv[i], verbose, noact, nooverwrite);
+ ret |= do_rename(from, to, argv[i], verbose, noact, nooverwrite, interactive);
switch (ret) {
case 0: