Replaced projects with products/projects.
[oweals/busybox.git] / ln.c
diff --git a/ln.c b/ln.c
index 16389a289711b54a5ba005408e92b02b89cfbb8c..ac1f68e0024034e84a0ad6ee17f9b1d97107f04c 100644 (file)
--- a/ln.c
+++ b/ln.c
@@ -50,7 +50,7 @@ static int followLinks = TRUE;
 
 extern int ln_main(int argc, char **argv)
 {
-       char *linkName;
+       char *linkName, *dirName=NULL;
        int linkIntoDirFlag;
        int stopIt = FALSE;
 
@@ -90,34 +90,25 @@ extern int ln_main(int argc, char **argv)
 
        linkName = argv[argc - 1];
 
-       if (strlen(linkName) > BUFSIZ) {
-               fprintf(stderr, name_too_long, "ln");
-               exit FALSE;
-       }
-
-       linkIntoDirFlag = isDirectory(linkName, TRUE, NULL);
-
+       linkIntoDirFlag = isDirectory(linkName, followLinks, NULL);
        if ((argc >= 3) && linkIntoDirFlag == FALSE) {
                fprintf(stderr, not_a_directory, "ln", linkName);
                exit FALSE;
        }
 
-       while (argc-- >= 2) {
-               char srcName[BUFSIZ + 1];
-               int nChars, status;
+       if (linkIntoDirFlag == TRUE)
+               dirName = linkName;
 
-               if (strlen(*argv) > BUFSIZ) {
-                       fprintf(stderr, name_too_long, "ln");
-                       exit FALSE;
-               }
-
-               if (followLinks == FALSE) {
-                       strcpy(srcName, *argv);
-               } else {
-                       /* Warning!  This can silently truncate if > BUFSIZ, but
-                          I don't think that there can be one > BUFSIZ anyway. */
-                       nChars = readlink(*argv, srcName, BUFSIZ);
-                       srcName[nChars] = '\0';
+       while (argc-- >= 2) {
+               int status;
+
+               if (linkIntoDirFlag == TRUE) {
+                       char *baseName = get_last_path_component(*argv);
+                       linkName = (char *)malloc(strlen(dirName)+strlen(baseName)+2);
+                       strcpy(linkName, dirName);
+                       if(dirName[strlen(dirName)-1] != '/')
+                               strcat(linkName, "/");
+                       strcat(linkName,baseName);
                }
 
                if (removeoldFlag == TRUE) {
@@ -136,6 +127,11 @@ extern int ln_main(int argc, char **argv)
                        perror(linkName);
                        exit FALSE;
                }
+
+               if (linkIntoDirFlag == TRUE)
+                       free(linkName);
+
+               argv++;
        }
        return( TRUE);
 }