* This shell script will do renames of files, but may fail
* in cases involving special characters. Here a C version.
*/
+#include <limits.h>
#include <stdio.h>
#ifdef HAVE_STDIO_EXT_H
# include <stdio_ext.h>
static int do_symlink(char *from, char *to, char *s, int verbose, int noact,
int nooverwrite, int interactive)
{
- char *newname = NULL, *target = NULL;
+ char *newname = NULL;
+ char target[PATH_MAX];
int ret = 1;
ssize_t ssz;
struct stat sb;
warnx(_("%s: not a symbolic link"), s);
return 2;
}
- target = xmalloc(sb.st_size + 1);
- ssz = readlink(s, target, sb.st_size + 1);
+ ssz = readlink(s, target, PATH_MAX - 1);
if (ssz < 0) {
warn(_("%s: readlink failed"), s);
- free(target);
return 2;
}
target[ssz] = '\0';
if (verbose && (noact || ret == 1))
printf("%s: `%s' -> `%s'\n", s, target, newname);
free(newname);
- free(target);
return ret;
}
static int do_copy(char *from, char *to, char *s, int verbose, int noact,
int nooverwrite, int interactive)
{
- char *newname = NULL, *target = NULL;
+ char *newname = NULL;
+ char target[PATH_MAX];
int ret = 1, res;
int src_fd = -1, dst_fd = -1;
ssize_t ssz;
ret = 2;
goto done;
}
- target = xmalloc(sb.st_size + 1);
- ssz = readlink(s, target, sb.st_size + 1);
+
+ ssz = readlink(s, target, PATH_MAX - 1);
if (ssz < 0) {
warn(_("%s: readlink failed"), s);
ret = 2;
done:
if (verbose && (noact || ret == 1))
printf("`%s' -> `%s'\n", s, newname);
- free(target);
free(newname);
if (src_fd >= 0)
close(src_fd);