jfb2 writes in Bug 119:
[oweals/busybox.git] / coreutils / ln.c
index e81dbafaec49667f99cb70187ad8d7202928a466..885ba61dbd9c3febc70fbb51605135f8cd077042 100644 (file)
@@ -2,8 +2,7 @@
 /*
  * Mini ln implementation for busybox
  *
- * Copyright (C) 1999,2000 by Lineo, inc.
- * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  */
 
-#include "busybox.h"
-#define BB_DECLARE_EXTERN
-#define bb_need_not_a_directory
-#include "messages.c"
+/* BB_AUDIT SUSv3 compliant */
+/* BB_AUDIT GNU options missing: -b, -d, -F, -i, -S, and -v. */
+/* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */
 
-#include <stdio.h>
-#include <dirent.h>
-#include <errno.h>
+/* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
+ *
+ * Fixed bug involving -n option.  Essentially, -n was always in effect.
+ */
 
-#define LN_SYMLINK             1
-#define LN_FORCE                       2
-#define LN_NODEREFERENCE       4
+#include <stdlib.h>
+#include <unistd.h>
+#include "busybox.h"
 
-/*
- * linkDestName is where the link points to,
- * linkSrcName is the name of the link to be created.
- */
-static int fs_link(const char *link_DestName, const char *link_SrcName, const int flag)
-{
-       int status;
-       int srcIsDir;
-       char *srcName;
-
-       if (link_DestName==NULL)
-               return(FALSE);
-
-       srcName = (char *) malloc(strlen(link_SrcName)+strlen(link_DestName)+1);
-
-       if (link_SrcName==NULL)
-               strcpy(srcName, link_DestName);
-       else
-               strcpy(srcName, link_SrcName);
-
-       if (flag&LN_NODEREFERENCE)
-               srcIsDir = isDirectory(srcName, TRUE, NULL);
-       else
-               srcIsDir = isDirectory(srcName, FALSE, NULL);   
-       
-       if ((srcIsDir==TRUE)&&((flag&LN_NODEREFERENCE)==0)) {
-               strcat(srcName, "/");
-               strcat(srcName, link_DestName);
-       }
-       
-       if (flag&LN_FORCE)
-               unlink(srcName);
-               
-       if (flag&LN_SYMLINK)
-               status = symlink(link_DestName, srcName);
-       else
-               status = link(link_DestName, srcName);
-
-       if (status != 0) {
-               perror(srcName);
-               return(FALSE);
-       }
-       return(TRUE);
-}
+#define LN_SYMLINK          1
+#define LN_FORCE            2
+#define LN_NODEREFERENCE    4
 
 extern int ln_main(int argc, char **argv)
 {
        int status = EXIT_SUCCESS;
-       int flag = 0;
-       int opt;
-       
-       /* Parse any options */
-       while ((opt=getopt(argc, argv, "sfn")) != -1) {
-               switch(opt) {
-                       case 's':
-                               flag |= LN_SYMLINK;
-                               break;
-                       case 'f':
-                               flag |= LN_FORCE;
-                               break;
-                       case 'n':
-                               flag |= LN_NODEREFERENCE;
-                               break;
-                       default:
-                               usage(ln_usage);
-               }
+       int flag;
+       char *last;
+       char *src_name;
+       char *src;
+       struct stat statbuf;
+       int (*link_func)(const char *, const char *);
+
+       flag = bb_getopt_ulflags(argc, argv, "sfn");
+
+       if (argc == optind) {
+               bb_show_usage();
        }
-       while(optind<(argc-1)) {
-               if (fs_link(argv[optind], argv[argc-1], flag)==FALSE)
-                       status = EXIT_FAILURE;
-               optind++;
+
+       last = argv[argc - 1];
+       argv += optind;
+
+       if (argc == optind + 1) {
+               *--argv = last;
+               last = bb_get_last_path_component(bb_xstrdup(last));
        }
-       return(status);
-}
 
-/*
-Local Variables:
-c-file-style: "linux"
-c-basic-offset: 4
-tab-width: 4
-End:
-*/
+       do {
+               src_name = NULL;
+               src = last;
+
+               if (is_directory(src,
+                                                (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE,
+                                                NULL)) {
+                       src_name = bb_xstrdup(*argv);
+                       src = concat_path_file(src, bb_get_last_path_component(src_name));
+                       free(src_name);
+                       src_name = src;
+               }
+               if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) {
+                       bb_perror_msg(*argv);
+                       status = EXIT_FAILURE;
+                       free(src_name);
+                       continue;
+               }
+
+               if (flag & LN_FORCE) {
+                       unlink(src);
+               }
+
+               link_func = link;
+               if (flag & LN_SYMLINK) {
+                       link_func = symlink;
+               }
+
+               if (link_func(*argv, src) != 0) {
+                       bb_perror_msg(src);
+                       status = EXIT_FAILURE;
+               }
+
+               free(src_name);
+
+       } while ((++argv)[1]);
+
+       return status;
+}