[klibc] [PATCH] utils add minimal mv

maximilian attems max at stro.at
Sat Jun 11 00:10:18 PDT 2011


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 at stro.at>
---
 usr/utils/Kbuild |    4 ++-
 usr/utils/mv.c   |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 1 deletions(-)
 create mode 100644 usr/utils/mv.c

diff --git a/usr/utils/Kbuild b/usr/utils/Kbuild
index 57e2d08..05aa794 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 0000000..e3f38ed
--- /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;
+}
-- 
1.7.5.3



More information about the klibc mailing list