2006-12-06 17:25:41 -06:00
|
|
|
/*
|
|
|
|
* rename.c - aeb 2000-01-01
|
|
|
|
*
|
|
|
|
--------------------------------------------------------------
|
|
|
|
#!/bin/sh
|
|
|
|
if [ $# -le 2 ]; then echo call: rename from to files; exit; fi
|
|
|
|
FROM="$1"
|
|
|
|
TO="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done
|
|
|
|
--------------------------------------------------------------
|
|
|
|
* This shell script will do renames of files, but may fail
|
|
|
|
* in cases involving special characters. Here a C version.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
2018-04-05 04:50:07 -05:00
|
|
|
#ifdef HAVE_STDIO_EXT_H
|
|
|
|
# include <stdio_ext.h>
|
|
|
|
#endif
|
2018-09-26 01:23:09 -05:00
|
|
|
#ifndef HAVE___FPURGE
|
|
|
|
# ifdef HAVE_FPURGE
|
2018-04-05 04:50:07 -05:00
|
|
|
# define HAVE___FPURGE 1
|
|
|
|
# define __fpurge fpurge
|
2018-09-26 01:23:09 -05:00
|
|
|
# endif
|
2018-04-05 04:50:07 -05:00
|
|
|
#endif
|
2006-12-06 17:25:41 -06:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2011-06-11 16:21:31 -05:00
|
|
|
#include <getopt.h>
|
2018-03-29 06:28:10 -05:00
|
|
|
#include <fcntl.h>
|
2012-11-26 09:50:08 -06:00
|
|
|
#include <unistd.h>
|
2018-03-29 05:08:46 -05:00
|
|
|
#include <termios.h>
|
2012-11-26 09:50:08 -06:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2010-10-15 09:59:18 -05:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
#include "nls.h"
|
2010-10-15 09:59:18 -05:00
|
|
|
#include "xalloc.h"
|
2011-06-11 16:21:31 -05:00
|
|
|
#include "c.h"
|
2012-04-04 12:56:48 -05:00
|
|
|
#include "closestream.h"
|
2018-03-24 06:15:55 -05:00
|
|
|
#include "rpmatch.h"
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2014-06-28 12:42:49 -05:00
|
|
|
#define RENAME_EXIT_SOMEOK 2
|
|
|
|
#define RENAME_EXIT_NOTHING 4
|
|
|
|
#define RENAME_EXIT_UNEXPLAINED 64
|
|
|
|
|
2018-03-29 05:08:46 -05:00
|
|
|
static int tty_cbreak = 0;
|
|
|
|
|
2014-05-25 18:41:06 -05:00
|
|
|
static int string_replace(char *from, char *to, char *s, char *orig, char **newname)
|
2011-06-11 16:21:31 -05:00
|
|
|
{
|
2014-05-25 18:41:06 -05:00
|
|
|
char *p, *q, *where;
|
|
|
|
|
|
|
|
where = strstr(s, from);
|
|
|
|
if (where == NULL)
|
|
|
|
return 1;
|
|
|
|
p = orig;
|
|
|
|
*newname = xmalloc(strlen(orig) + strlen(to) + 1);
|
|
|
|
q = *newname;
|
2006-12-06 17:25:41 -06:00
|
|
|
while (p < where)
|
|
|
|
*q++ = *p++;
|
|
|
|
p = to;
|
|
|
|
while (*p)
|
|
|
|
*q++ = *p++;
|
2014-05-25 18:41:06 -05:00
|
|
|
p = where + strlen(from);
|
2006-12-06 17:25:41 -06:00
|
|
|
while (*p)
|
|
|
|
*q++ = *p++;
|
2006-12-06 17:25:43 -06:00
|
|
|
*q = 0;
|
2014-05-25 18:41:06 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2018-03-24 06:15:55 -05:00
|
|
|
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) {
|
2018-03-31 08:01:27 -05:00
|
|
|
buf[0] = 'n';
|
|
|
|
printf("n\n");
|
2018-03-24 06:15:55 -05:00
|
|
|
}
|
|
|
|
else {
|
2018-03-31 08:01:27 -05:00
|
|
|
buf[0] = c;
|
2011-04-05 04:31:03 -05:00
|
|
|
if (c != '\n' && tty_cbreak) {
|
2018-04-05 04:50:07 -05:00
|
|
|
#ifdef HAVE___FPURGE
|
2011-04-05 04:31:03 -05:00
|
|
|
/* Possibly purge a multi-byte character; or do a
|
|
|
|
required purge of the rest of the line (including
|
|
|
|
the newline) if the tty has been put back in
|
|
|
|
canonical mode (for example by a shell after a
|
|
|
|
SIGTSTP signal). */
|
|
|
|
__fpurge(stdin);
|
2018-04-05 04:50:07 -05:00
|
|
|
#endif
|
2018-03-29 05:08:46 -05:00
|
|
|
printf("\n");
|
2011-04-05 04:31:03 -05:00
|
|
|
}
|
2018-03-29 05:08:46 -05:00
|
|
|
else if (c != '\n')
|
2018-03-24 06:15:55 -05:00
|
|
|
while ((c = fgetc(stdin)) != '\n' && c != EOF);
|
|
|
|
}
|
2018-03-31 08:01:27 -05:00
|
|
|
buf[1] = '\0';
|
2018-03-24 06:15:55 -05:00
|
|
|
if (rpmatch(buf) == RPMATCH_YES)
|
|
|
|
return 0;
|
2020-04-19 00:16:22 -05:00
|
|
|
|
|
|
|
return 1;
|
2018-03-24 06:15:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int do_symlink(char *from, char *to, char *s, int verbose, int noact,
|
|
|
|
int nooverwrite, int interactive)
|
2014-05-25 18:41:06 -05:00
|
|
|
{
|
|
|
|
char *newname = NULL, *target = NULL;
|
|
|
|
int ret = 1;
|
|
|
|
struct stat sb;
|
|
|
|
|
2018-04-09 08:35:40 -05:00
|
|
|
if ( faccessat(AT_FDCWD, s, F_OK, AT_SYMLINK_NOFOLLOW) != 0 &&
|
|
|
|
errno != EINVAL )
|
|
|
|
/* Skip if AT_SYMLINK_NOFOLLOW is not supported; lstat() below will
|
|
|
|
detect the access error */
|
|
|
|
{
|
2018-03-29 06:28:10 -05:00
|
|
|
warn(_("%s: not accessible"), s);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2014-05-25 18:41:06 -05:00
|
|
|
if (lstat(s, &sb) == -1) {
|
2015-01-29 14:21:48 -06:00
|
|
|
warn(_("stat of %s failed"), s);
|
2014-05-25 18:41:06 -05:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (!S_ISLNK(sb.st_mode)) {
|
|
|
|
warnx(_("%s: not a symbolic link"), s);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
target = xmalloc(sb.st_size + 1);
|
|
|
|
if (readlink(s, target, sb.st_size + 1) < 0) {
|
|
|
|
warn(_("%s: readlink failed"), s);
|
|
|
|
free(target);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
target[sb.st_size] = '\0';
|
2018-04-09 07:44:52 -05:00
|
|
|
if (string_replace(from, to, target, target, &newname) != 0)
|
2014-05-25 18:41:06 -05:00
|
|
|
ret = 0;
|
2017-05-27 15:07:13 -05:00
|
|
|
|
2018-03-24 06:15:55 -05:00
|
|
|
if (ret == 1 && (nooverwrite || interactive) && lstat(newname, &sb) != 0)
|
|
|
|
nooverwrite = interactive = 0;
|
|
|
|
|
|
|
|
if ( ret == 1 &&
|
|
|
|
(nooverwrite || (interactive && (noact || ask(newname) != 0))) )
|
|
|
|
{
|
2017-05-27 15:07:13 -05:00
|
|
|
if (verbose)
|
2018-03-24 17:55:58 -05:00
|
|
|
printf(_("Skipping existing link: `%s' -> `%s'\n"), s, target);
|
2017-05-27 15:07:13 -05:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 1) {
|
|
|
|
if (!noact && 0 > unlink(s)) {
|
|
|
|
warn(_("%s: unlink failed"), s);
|
|
|
|
ret = 2;
|
2018-04-09 07:44:52 -05:00
|
|
|
}
|
|
|
|
else if (!noact && symlink(newname, s) != 0) {
|
2017-05-27 15:07:13 -05:00
|
|
|
warn(_("%s: symlinking to %s failed"), s, newname);
|
|
|
|
ret = 2;
|
|
|
|
}
|
2012-11-26 09:50:08 -06:00
|
|
|
}
|
2017-02-13 06:44:38 -06:00
|
|
|
if (verbose && (noact || ret == 1))
|
2018-04-09 07:44:52 -05:00
|
|
|
printf("%s: `%s' -> `%s'\n", s, target, newname);
|
2011-06-11 16:21:31 -05:00
|
|
|
free(newname);
|
2013-03-27 10:29:48 -05:00
|
|
|
free(target);
|
2014-06-28 12:42:49 -05:00
|
|
|
return ret;
|
2006-12-06 17:25:41 -06:00
|
|
|
}
|
|
|
|
|
2018-03-24 06:15:55 -05:00
|
|
|
static int do_file(char *from, char *to, char *s, int verbose, int noact,
|
|
|
|
int nooverwrite, int interactive)
|
2014-05-25 18:41:06 -05:00
|
|
|
{
|
2015-06-22 12:26:22 -05:00
|
|
|
char *newname = NULL, *file=NULL;
|
2014-05-25 18:41:06 -05:00
|
|
|
int ret = 1;
|
rename: fix regression for symlink with non-existing target
Since commit 5454df9c3110 ("rename: check source file access early")
rename fails early for symlinks with non-existing target (regression),
because access() dereferences the link.
From access(2):
"access() checks whether the calling process can access the file pathname.
If pathname is a symbolic link, it is dereferenced."
Thus replace access() with faccessat() and lstat() as fallback,
(as in do_symlink()), that is equivalent for symlink and files.
From fsaccess(2) and stat(2):
"The faccessat() system call operates in exactly the same way as access(),
except for the differences described here.
[...]
If pathname is relative and dirfd is the special value AT_FDCWD, then pathname
is interpreted relative to the current working directory of the calling process
(like access()).
[...]
AT_SYMLINK_NOFOLLOW
If pathname is a symbolic link, do not dereference it:
instead return information about the link itself."
"lstat() is identical to stat(), except that if pathname is a symbolic link, then
it returns information about the link itself, not the file that it refers to."
Testing
-------
1) symlink with existing target
2) symlink with non-existing target
3) non-existing symlink
4) existing file
5) non-existing file
Before:
$ touch file-found
$ ln -s file-found symlink-1
$ ./rename sym symbolic- symlink-1 # XPASS.
$ echo $?
0
$ ln -s file-not-found symlink-2
$ ./rename sym symbolic- symlink-2 # FAIL! REGRESSION.
rename: symlink-2: not accessible: No such file or directory
$ echo $?
1
$ ./rename sym symbolic- symlink-3 # XFAIL.
rename: symlink-3: not accessible: No such file or directory
$ echo $?
1
$ touch file-found
$ ./rename found existing file-found # XPASS.
$ echo $?
0
$ ./rename found existing file-not-found # XFAIL.
rename: file-not-found: not accessible: No such file or directory
$ echo $?
1
After:
$ touch file-found
$ ln -s file-found symlink-1
$ ./rename sym symbolic- symlink-1 # XPASS.
$ echo $?
0
$ ln -s file-not-found symlink-2
$ ./rename sym symbolic- symlink-2 # PASS! REGRESSION FIXED.
$ echo $?
0
$ ./rename sym symbolic- symlink-3 # XFAIL.
rename: symlink-3: not accessible: No such file or directory
$ echo $?
1
$ touch file-found
$ ./rename found existing file-found # XPASS.
$ echo $?
0
$ ./rename found existing file-not-found # XFAIL.
rename: file-not-found: not accessible: No such file or directory
$ echo $?
1
And to test/simulate faccessat()'s EINVAL for AT_SYMLINK_NOFOLLOW
for Mac OS X, per commit 826538bf64c5 ("rename: skip faccessat()
failure if AT_SYMLINK_NOFOLLOW is not a valid flag"), forced 'if'
to evaluate to false so that lstat() is taken.
It still fails early; the error messages are slightly different
('not accessible' vs. 'stat of ... failed') but still tell same
'No such file or directory'; exit code is the same as well.
$ ./rename sym symbolic- symlink-3 # XFAIL. DIFF MSG/SAME RC.
rename: stat of symlink-3 failed: No such file or directory
$ echo $?
1
$ ./rename found existing file-not-found # XFAIL. DIFF MSG/SAME RC.
rename: stat of file-not-found failed: No such file or directory
$ echo $?
1
Tested on commit 2b41c409e ("Merge branch 'blkd-err' of ...")
Signed-off-by: Mauricio Faria de Oliveira <mfo@canonical.com>
2020-07-07 13:49:13 -05:00
|
|
|
struct stat sb;
|
2014-05-25 18:41:06 -05:00
|
|
|
|
rename: fix regression for symlink with non-existing target
Since commit 5454df9c3110 ("rename: check source file access early")
rename fails early for symlinks with non-existing target (regression),
because access() dereferences the link.
From access(2):
"access() checks whether the calling process can access the file pathname.
If pathname is a symbolic link, it is dereferenced."
Thus replace access() with faccessat() and lstat() as fallback,
(as in do_symlink()), that is equivalent for symlink and files.
From fsaccess(2) and stat(2):
"The faccessat() system call operates in exactly the same way as access(),
except for the differences described here.
[...]
If pathname is relative and dirfd is the special value AT_FDCWD, then pathname
is interpreted relative to the current working directory of the calling process
(like access()).
[...]
AT_SYMLINK_NOFOLLOW
If pathname is a symbolic link, do not dereference it:
instead return information about the link itself."
"lstat() is identical to stat(), except that if pathname is a symbolic link, then
it returns information about the link itself, not the file that it refers to."
Testing
-------
1) symlink with existing target
2) symlink with non-existing target
3) non-existing symlink
4) existing file
5) non-existing file
Before:
$ touch file-found
$ ln -s file-found symlink-1
$ ./rename sym symbolic- symlink-1 # XPASS.
$ echo $?
0
$ ln -s file-not-found symlink-2
$ ./rename sym symbolic- symlink-2 # FAIL! REGRESSION.
rename: symlink-2: not accessible: No such file or directory
$ echo $?
1
$ ./rename sym symbolic- symlink-3 # XFAIL.
rename: symlink-3: not accessible: No such file or directory
$ echo $?
1
$ touch file-found
$ ./rename found existing file-found # XPASS.
$ echo $?
0
$ ./rename found existing file-not-found # XFAIL.
rename: file-not-found: not accessible: No such file or directory
$ echo $?
1
After:
$ touch file-found
$ ln -s file-found symlink-1
$ ./rename sym symbolic- symlink-1 # XPASS.
$ echo $?
0
$ ln -s file-not-found symlink-2
$ ./rename sym symbolic- symlink-2 # PASS! REGRESSION FIXED.
$ echo $?
0
$ ./rename sym symbolic- symlink-3 # XFAIL.
rename: symlink-3: not accessible: No such file or directory
$ echo $?
1
$ touch file-found
$ ./rename found existing file-found # XPASS.
$ echo $?
0
$ ./rename found existing file-not-found # XFAIL.
rename: file-not-found: not accessible: No such file or directory
$ echo $?
1
And to test/simulate faccessat()'s EINVAL for AT_SYMLINK_NOFOLLOW
for Mac OS X, per commit 826538bf64c5 ("rename: skip faccessat()
failure if AT_SYMLINK_NOFOLLOW is not a valid flag"), forced 'if'
to evaluate to false so that lstat() is taken.
It still fails early; the error messages are slightly different
('not accessible' vs. 'stat of ... failed') but still tell same
'No such file or directory'; exit code is the same as well.
$ ./rename sym symbolic- symlink-3 # XFAIL. DIFF MSG/SAME RC.
rename: stat of symlink-3 failed: No such file or directory
$ echo $?
1
$ ./rename found existing file-not-found # XFAIL. DIFF MSG/SAME RC.
rename: stat of file-not-found failed: No such file or directory
$ echo $?
1
Tested on commit 2b41c409e ("Merge branch 'blkd-err' of ...")
Signed-off-by: Mauricio Faria de Oliveira <mfo@canonical.com>
2020-07-07 13:49:13 -05:00
|
|
|
if ( faccessat(AT_FDCWD, s, F_OK, AT_SYMLINK_NOFOLLOW) != 0 &&
|
|
|
|
errno != EINVAL )
|
|
|
|
/* Skip if AT_SYMLINK_NOFOLLOW is not supported; lstat() below will
|
|
|
|
detect the access error */
|
|
|
|
{
|
2018-03-29 06:28:10 -05:00
|
|
|
warn(_("%s: not accessible"), s);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
rename: fix regression for symlink with non-existing target
Since commit 5454df9c3110 ("rename: check source file access early")
rename fails early for symlinks with non-existing target (regression),
because access() dereferences the link.
From access(2):
"access() checks whether the calling process can access the file pathname.
If pathname is a symbolic link, it is dereferenced."
Thus replace access() with faccessat() and lstat() as fallback,
(as in do_symlink()), that is equivalent for symlink and files.
From fsaccess(2) and stat(2):
"The faccessat() system call operates in exactly the same way as access(),
except for the differences described here.
[...]
If pathname is relative and dirfd is the special value AT_FDCWD, then pathname
is interpreted relative to the current working directory of the calling process
(like access()).
[...]
AT_SYMLINK_NOFOLLOW
If pathname is a symbolic link, do not dereference it:
instead return information about the link itself."
"lstat() is identical to stat(), except that if pathname is a symbolic link, then
it returns information about the link itself, not the file that it refers to."
Testing
-------
1) symlink with existing target
2) symlink with non-existing target
3) non-existing symlink
4) existing file
5) non-existing file
Before:
$ touch file-found
$ ln -s file-found symlink-1
$ ./rename sym symbolic- symlink-1 # XPASS.
$ echo $?
0
$ ln -s file-not-found symlink-2
$ ./rename sym symbolic- symlink-2 # FAIL! REGRESSION.
rename: symlink-2: not accessible: No such file or directory
$ echo $?
1
$ ./rename sym symbolic- symlink-3 # XFAIL.
rename: symlink-3: not accessible: No such file or directory
$ echo $?
1
$ touch file-found
$ ./rename found existing file-found # XPASS.
$ echo $?
0
$ ./rename found existing file-not-found # XFAIL.
rename: file-not-found: not accessible: No such file or directory
$ echo $?
1
After:
$ touch file-found
$ ln -s file-found symlink-1
$ ./rename sym symbolic- symlink-1 # XPASS.
$ echo $?
0
$ ln -s file-not-found symlink-2
$ ./rename sym symbolic- symlink-2 # PASS! REGRESSION FIXED.
$ echo $?
0
$ ./rename sym symbolic- symlink-3 # XFAIL.
rename: symlink-3: not accessible: No such file or directory
$ echo $?
1
$ touch file-found
$ ./rename found existing file-found # XPASS.
$ echo $?
0
$ ./rename found existing file-not-found # XFAIL.
rename: file-not-found: not accessible: No such file or directory
$ echo $?
1
And to test/simulate faccessat()'s EINVAL for AT_SYMLINK_NOFOLLOW
for Mac OS X, per commit 826538bf64c5 ("rename: skip faccessat()
failure if AT_SYMLINK_NOFOLLOW is not a valid flag"), forced 'if'
to evaluate to false so that lstat() is taken.
It still fails early; the error messages are slightly different
('not accessible' vs. 'stat of ... failed') but still tell same
'No such file or directory'; exit code is the same as well.
$ ./rename sym symbolic- symlink-3 # XFAIL. DIFF MSG/SAME RC.
rename: stat of symlink-3 failed: No such file or directory
$ echo $?
1
$ ./rename found existing file-not-found # XFAIL. DIFF MSG/SAME RC.
rename: stat of file-not-found failed: No such file or directory
$ echo $?
1
Tested on commit 2b41c409e ("Merge branch 'blkd-err' of ...")
Signed-off-by: Mauricio Faria de Oliveira <mfo@canonical.com>
2020-07-07 13:49:13 -05:00
|
|
|
if (lstat(s, &sb) == -1) {
|
|
|
|
warn(_("stat of %s failed"), s);
|
|
|
|
return 2;
|
|
|
|
}
|
2015-06-22 12:26:22 -05:00
|
|
|
if (strchr(from, '/') == NULL && strchr(to, '/') == NULL)
|
|
|
|
file = strrchr(s, '/');
|
2014-05-25 18:41:06 -05:00
|
|
|
if (file == NULL)
|
|
|
|
file = s;
|
2018-04-09 07:44:52 -05:00
|
|
|
if (string_replace(from, to, file, s, &newname) != 0)
|
2014-05-25 18:41:06 -05:00
|
|
|
return 0;
|
2018-03-29 06:28:10 -05:00
|
|
|
|
2018-03-24 06:15:55 -05:00
|
|
|
if ((nooverwrite || interactive) && access(newname, F_OK) != 0)
|
|
|
|
nooverwrite = interactive = 0;
|
|
|
|
|
|
|
|
if (nooverwrite || (interactive && (noact || ask(newname) != 0))) {
|
2018-03-27 08:22:58 -05:00
|
|
|
if (verbose)
|
|
|
|
printf(_("Skipping existing file: `%s'\n"), newname);
|
2017-05-27 15:07:13 -05:00
|
|
|
ret = 0;
|
|
|
|
}
|
2017-02-13 06:44:38 -06:00
|
|
|
else if (!noact && rename(s, newname) != 0) {
|
2014-05-25 18:41:06 -05:00
|
|
|
warn(_("%s: rename to %s failed"), s, newname);
|
|
|
|
ret = 2;
|
|
|
|
}
|
2017-02-13 06:44:38 -06:00
|
|
|
if (verbose && (noact || ret == 1))
|
2014-05-25 18:41:06 -05:00
|
|
|
printf("`%s' -> `%s'\n", s, newname);
|
|
|
|
free(newname);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-19 13:52:50 -05:00
|
|
|
static void __attribute__((__noreturn__)) usage(void)
|
2011-06-11 16:21:31 -05:00
|
|
|
{
|
2017-06-19 13:52:50 -05:00
|
|
|
FILE *out = stdout;
|
2013-01-22 17:27:06 -06:00
|
|
|
fputs(USAGE_HEADER, out);
|
2011-06-11 16:21:31 -05:00
|
|
|
fprintf(out,
|
2014-07-22 15:56:27 -05:00
|
|
|
_(" %s [options] <expression> <replacement> <file>...\n"),
|
2011-06-11 16:21:31 -05:00
|
|
|
program_invocation_short_name);
|
2014-12-22 15:57:17 -06:00
|
|
|
|
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(_("Rename files.\n"), out);
|
|
|
|
|
2013-01-22 17:27:06 -06:00
|
|
|
fputs(USAGE_OPTIONS, out);
|
2017-06-03 15:40:47 -05:00
|
|
|
fputs(_(" -v, --verbose explain what is being done\n"), out);
|
|
|
|
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);
|
2018-03-24 06:15:55 -05:00
|
|
|
fputs(_(" -i, --interactive prompt before overwrite\n"), out);
|
2013-01-22 17:27:06 -06:00
|
|
|
fputs(USAGE_SEPARATOR, out);
|
2017-06-29 08:52:16 -05:00
|
|
|
printf(USAGE_HELP_OPTIONS(21));
|
|
|
|
printf(USAGE_MAN_TAIL("rename(1)"));
|
2017-06-19 13:52:50 -05:00
|
|
|
exit(EXIT_SUCCESS);
|
2011-06-11 16:21:31 -05:00
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2011-06-11 16:21:31 -05:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *from, *to;
|
2018-03-24 06:15:55 -05:00
|
|
|
int i, c, ret = 0, verbose = 0, noact = 0, nooverwrite = 0, interactive = 0;
|
2018-03-29 05:08:46 -05:00
|
|
|
struct termios tio;
|
2018-03-24 06:15:55 -05:00
|
|
|
int (*do_rename)(char *from, char *to, char *s, int verbose, int noact,
|
|
|
|
int nooverwrite, int interactive) = do_file;
|
2011-06-11 16:21:31 -05:00
|
|
|
|
|
|
|
static const struct option longopts[] = {
|
|
|
|
{"verbose", no_argument, NULL, 'v'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
2017-02-13 06:44:38 -06:00
|
|
|
{"no-act", no_argument, NULL, 'n'},
|
2017-06-03 15:40:47 -05:00
|
|
|
{"no-overwrite", no_argument, NULL, 'o'},
|
2018-03-24 06:15:55 -05:00
|
|
|
{"interactive", no_argument, NULL, 'i'},
|
2012-11-26 09:50:08 -06:00
|
|
|
{"symlink", no_argument, NULL, 's'},
|
2011-06-11 16:21:31 -05:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
2006-12-06 17:25:41 -06:00
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2019-04-16 08:14:13 -05:00
|
|
|
close_stdout_atexit();
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2018-03-24 06:15:55 -05:00
|
|
|
while ((c = getopt_long(argc, argv, "vsVhnoi", longopts, NULL)) != -1)
|
2011-06-11 16:21:31 -05:00
|
|
|
switch (c) {
|
2017-02-13 06:44:38 -06:00
|
|
|
case 'n':
|
|
|
|
noact = 1;
|
2018-03-29 05:58:44 -05:00
|
|
|
break;
|
2017-05-18 16:19:34 -05:00
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
2017-02-13 06:44:38 -06:00
|
|
|
break;
|
2018-03-29 05:58:44 -05:00
|
|
|
case 'o':
|
|
|
|
nooverwrite = 1;
|
2018-03-24 06:15:55 -05:00
|
|
|
interactive = 0;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
interactive = 1;
|
|
|
|
nooverwrite = 0;
|
2018-03-29 05:58:44 -05:00
|
|
|
break;
|
2012-11-26 09:50:08 -06:00
|
|
|
case 's':
|
2014-05-25 18:41:06 -05:00
|
|
|
do_rename = do_symlink;
|
2012-11-26 09:50:08 -06:00
|
|
|
break;
|
2019-04-16 08:14:13 -05:00
|
|
|
|
2011-06-11 16:21:31 -05:00
|
|
|
case 'V':
|
2019-04-16 08:14:13 -05:00
|
|
|
print_version(EXIT_SUCCESS);
|
2011-06-11 16:21:31 -05:00
|
|
|
case 'h':
|
2017-06-19 13:52:50 -05:00
|
|
|
usage();
|
2011-06-11 16:21:31 -05:00
|
|
|
default:
|
2016-12-19 06:13:34 -06:00
|
|
|
errtryhelp(EXIT_FAILURE);
|
2006-12-06 17:25:41 -06:00
|
|
|
}
|
2011-06-11 16:21:31 -05:00
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2006-12-06 17:25:41 -06:00
|
|
|
|
|
|
|
if (argc < 3) {
|
2012-02-06 09:11:27 -06:00
|
|
|
warnx(_("not enough arguments"));
|
2017-06-19 13:52:50 -05:00
|
|
|
errtryhelp(EXIT_FAILURE);
|
2006-12-06 17:25:41 -06:00
|
|
|
}
|
|
|
|
|
2011-06-11 16:21:31 -05:00
|
|
|
from = argv[0];
|
|
|
|
to = argv[1];
|
|
|
|
|
2017-05-20 04:41:23 -05:00
|
|
|
if (!strcmp(from, to))
|
|
|
|
return RENAME_EXIT_NOTHING;
|
|
|
|
|
2018-03-29 05:08:46 -05:00
|
|
|
tty_cbreak = 0;
|
|
|
|
if (interactive && isatty(STDIN_FILENO) != 0) {
|
|
|
|
if (tcgetattr(STDIN_FILENO, &tio) != 0)
|
|
|
|
warn(_("failed to get terminal attributes"));
|
|
|
|
else if (!(tio.c_lflag & ICANON) && tio.c_cc[VMIN] == 1)
|
|
|
|
tty_cbreak = 1;
|
|
|
|
}
|
|
|
|
|
2011-06-11 16:21:31 -05:00
|
|
|
for (i = 2; i < argc; i++)
|
2018-03-24 06:15:55 -05:00
|
|
|
ret |= do_rename(from, to, argv[i], verbose, noact, nooverwrite, interactive);
|
2014-06-28 12:42:49 -05:00
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case 0:
|
|
|
|
return RENAME_EXIT_NOTHING;
|
|
|
|
case 1:
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case 2:
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
case 3:
|
|
|
|
return RENAME_EXIT_SOMEOK;
|
|
|
|
default:
|
|
|
|
return RENAME_EXIT_UNEXPLAINED;
|
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
}
|