aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormaximilian attems <max@stro.at>2011-06-09 10:23:38 +0200
committermaximilian attems <max@stro.at>2011-06-12 11:11:33 +0200
commitd16b1b15a14bfeac86125b250aa72831d2081f00 (patch)
tree88a2ade657ec23aee308cab9265cb1e7d10cfded
parentdd624e26a21aefaf3876c0dcfeb21c0391727395 (diff)
downloadklibc-d16b1b15a14bfeac86125b250aa72831d2081f00.tar.gz
[klibc] utils add minimal mv
mv is needed these days in initramfs. One needs to move all the data of initramfs /run to the real rootfs. This minimal mv utiliy originates from the existing ln. Signed-off-by: maximilian attems <max@stro.at>
-rw-r--r--usr/utils/Kbuild4
-rw-r--r--usr/utils/mv.c69
2 files changed, 72 insertions, 1 deletions
diff --git a/usr/utils/Kbuild b/usr/utils/Kbuild
index 57e2d080cc3ed..05aa794f481d3 100644
--- a/usr/utils/Kbuild
+++ b/usr/utils/Kbuild
@@ -3,7 +3,7 @@
#
progs := chroot dd mkdir mkfifo mknod mount pivot_root umount
-progs += true false sleep ln nuke minips cat ls losetup
+progs += true false sleep ln mv nuke minips cat ls losetup
progs += uname halt kill readlink cpio sync dmesg
static-y := $(addprefix static/, $(progs))
@@ -38,6 +38,8 @@ static/ln-y := ln.o
shared/ln-y := ln.o
static/ls-y := ls.o
shared/ls-y := ls.o
+static/mv-y := mv.o
+shared/mv-y := mv.o
static/nuke-y := nuke.o
shared/nuke-y := nuke.o
static/minips-y := minips.o
diff --git a/usr/utils/mv.c b/usr/utils/mv.c
new file mode 100644
index 0000000000000..e3f38ed9ea551
--- /dev/null
+++ b/usr/utils/mv.c
@@ -0,0 +1,69 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <linux/limits.h>
+
+int main(int argc, char *argv[])
+{
+ int c, f;
+ char *p;
+ struct stat sb;
+
+ f = 0;
+ do {
+ c = getopt(argc, argv, "f");
+ if (c == EOF)
+ break;
+
+ switch (c) {
+
+ case 'f':
+ f = 1;
+ break;
+ case '?':
+ fprintf(stderr, "%s: invalid option -%c\n",
+ argv[0], optopt);
+ return 1;
+ }
+
+ } while (1);
+
+ if (optind == argc) {
+ fprintf(stderr, "Usage: %s [-f] source dest\n", argv[0]);
+ return 1;
+ }
+
+ memset(&sb, 0, sizeof(struct stat));
+ if (stat(argv[argc - 1], &sb) < 0 && argc - optind > 2) {
+ if (!(S_ISDIR(sb.st_mode))) {
+ fprintf(stderr,
+ "multiple targets and %s is not a directory\n",
+ argv[argc - 1]);
+ return 1;
+ }
+ }
+
+ for (c = optind; c < argc - 1; c++) {
+ char target[PATH_MAX];
+
+ p = strrchr(argv[c], '/');
+ p++;
+
+ if (S_ISDIR(sb.st_mode))
+ snprintf(target, PATH_MAX, "%s/%s", argv[argc - 1], p);
+ else
+ snprintf(target, PATH_MAX, "%s", argv[argc - 1]);
+
+ if (f)
+ unlink(target);
+
+ if (rename(argv[c], target) == -1)
+ perror(target);
+ }
+
+ return 0;
+}