[klibc] [PATCH] ln for klibc utils

Olaf Hering olh at suse.de
Sun Feb 22 01:54:31 PST 2004


this implements a ln(1) with -f and -s support.


diff -p -purN klibc-0.112/utils/Makefile klibc-0.112.utils/utils/Makefile
--- klibc-0.112/utils/Makefile	2004-02-21 23:49:26.000000000 +0100
+++ klibc-0.112.utils/utils/Makefile	2004-02-22 01:23:17.000000000 +0100
@@ -6,7 +6,7 @@ MAKEDEPS     = -Wp,-MD,.$(subst /,-,$*).
 CFLAGS       = $(MAKEDEPS) $(OPTFLAGS) $(REQFLAGS) -W -Wall
 LIBS         = $(KLIBC) $(LIBGCC)
 PROGS       := chroot dd fstype mkdir mkfifo mount pivot_root umount \
-	       true false sleep
+	       true false sleep ln
 STATICPROGS := $(patsubst %,static/%,$(PROGS))
 SHAREDPROGS := $(patsubst %,shared/%,$(PROGS))
 LIBOBJS	     = file_mode.o
diff -p -purN klibc-0.112/utils/ln.c klibc-0.112.utils/utils/ln.c
--- klibc-0.112/utils/ln.c	1970-01-01 01:00:00.000000000 +0100
+++ klibc-0.112.utils/utils/ln.c	2004-02-22 01:15:39.000000000 +0100
@@ -0,0 +1,84 @@
+#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, s, f;
+	char *p, *target;
+	struct stat sb;
+
+	s = f = 0;
+	do {
+		c = getopt(argc, argv, "sf");
+		if (c == EOF)
+			break;
+
+		switch (c) {
+
+		case 's':
+			s = 1;
+			break;
+		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 [-s] [-f] target link\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;
+		}
+	}
+
+	target = malloc(PATH_MAX);
+	if (!target) {
+		perror("malloc");
+		return 1;
+	}
+
+	for (c = optind; c < argc - 1; c++) {
+
+		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 (s) {
+			if (symlink(argv[c], target) == -1)
+				perror(target);
+		} else {
+			if (link(argv[c], target) == -1)
+				perror(target);
+		}
+	}
+
+	return 0;
+}
-- 
USB is for mice, FireWire is for men!

sUse lINUX ag, nÜRNBERG



More information about the klibc mailing list