File klibc-2.0.4-ln.patch of Package klibc

diff -Nurp klibc-2.0.4/usr/utils/ln.c klibc-2.0.4-ln/usr/utils/ln.c
--- klibc-2.0.4/usr/utils/ln.c	2014-07-26 03:15:07.000000000 +0300
+++ klibc-2.0.4-ln/usr/utils/ln.c	2014-09-22 05:45:32.272428689 +0300
@@ -9,68 +9,60 @@
 
 int main(int argc, char *argv[])
 {
-	int c, s, f;
-	char *p;
+	int (*linkfunc)(const char *target, const char *linkpath);
+	int c, s, f, a;
 	struct stat sb;
 
-	s = f = 0;
-	do {
+	for (linkfunc = link, f = 0;;) {
 		c = getopt(argc, argv, "sf");
-		if (c == EOF)
-			break;
-
 		switch (c) {
-
-		case 's':
-			s = 1;
+		case EOF:
 			break;
+		case 's':
+			linkfunc = symlink;
+			continue;
 		case 'f':
 			f = 1;
-			break;
+			continue;
 		case '?':
 			fprintf(stderr, "%s: invalid option -%c\n",
 				argv[0], optopt);
 			return 1;
 		}
-
-	} while (1);
+		break;
+	}
 
 	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;
-		}
+	a = argc - 1;
+	s = !stat(argv[a], &sb) && S_ISDIR(sb.st_mode);
+	if (argc - optind > 2 && !s) {
+		fprintf(stderr, "multiple targets and %s is not a directory\n",
+		argv[a]);
+		return 1;
 	}
 
-	for (c = optind; c < argc - 1; c++) {
+	for (c = optind; c < a; c++) {
 		char target[PATH_MAX];
 
-		p = strrchr(argv[c], '/');
-		p++;
+		strncpy(target, argv[a], PATH_MAX - 1);
+		if (s) {
+			size_t l = strlen(target);
+			char *p = strrchr(argv[c], '/');
 
-		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 (target[l - 1] != '/')
+				target[l++] = '/';
+			strncpy(target + l, p ? p + 1 : argv[c], PATH_MAX - 1 - l);
+		}
 
 		if (f)
 			unlink(target);
 
-		if (s) {
-			if (symlink(argv[c], target) == -1)
-				perror(target);
-		} else {
-			if (link(argv[c], target) == -1)
-				perror(target);
-		}
+		if (linkfunc(argv[c], target))
+			perror(target);
 	}
 
 	return 0;
openSUSE Build Service is sponsored by