- single KERNEL_VERSION(a,b,c) macro in platform.h
[oweals/busybox.git] / coreutils / ln.c
index d4fa47306b585164571b731091903b73b29301f0..229c682185ef029a0bb80deeacc05b30c23d453e 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 "internal.h"
-#define BB_DECLARE_EXTERN
-#define bb_need_name_too_long
-#define bb_need_not_a_directory
-#include "messages.c"
+/* BB_AUDIT SUSv3 compliant */
+/* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */
+/* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */
 
 #include <stdio.h>
-#include <dirent.h>
+#include <stdlib.h>
+#include <unistd.h>
 #include <errno.h>
+#include "busybox.h"
 
-static const char ln_usage[] =
-       "ln [OPTION] TARGET... LINK_NAME|DIRECTORY\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
-       "\nCreate a link named LINK_NAME or DIRECTORY to the specified TARGET\n\n"
-       "Options:\n"
-       "\t-s\tmake symbolic links instead of hard links\n"
+#define LN_SYMLINK          1
+#define LN_FORCE            2
+#define LN_NODEREFERENCE    4
+#define LN_BACKUP           8
+#define LN_SUFFIX           16
 
-       "\t-f\tremove existing destination files\n"
-       "\t-n\tno dereference symlinks - treat like normal file\n"
-#endif
-       ;
-
-static int symlinkFlag = FALSE;
-static int removeoldFlag = FALSE;
-static int followLinks = TRUE;
-
-extern int ln_main(int argc, char **argv)
+int ln_main(int argc, char **argv)
 {
-       char *linkName;
-       int linkIntoDirFlag;
-
-       if (argc < 3) {
-               usage(ln_usage);
-       }
-       argc--;
-       argv++;
-
-       /* Parse any options */
-       while (--argc >= 0 && *argv && **argv) {
-               while (**argv == '-') {
-                       while (*++(*argv))
-                               switch (**argv) {
-                                       case 's':
-                                               symlinkFlag = TRUE;
-                                               break;
-                                       case 'f':
-                                               removeoldFlag = TRUE;
-                                               break;
-                                       case 'n':
-                                               followLinks = FALSE;
-                                               break;
-                                       default:
-                                               usage(ln_usage);
-                               }
-               }
-               argv++;
-       }
-
-       if (argc < 1) {
-               fatalError("ln: missing file argument\n");
-       }
-
-       linkName = argv[argc - 1];
-
-       if (strlen(linkName) > BUFSIZ) {
-               fprintf(stderr, name_too_long, "ln");
-               exit FALSE;
+       int status = EXIT_SUCCESS;
+       int flag;
+       char *last;
+       char *src_name;
+       char *src;
+       char *suffix = "~";
+       struct stat statbuf;
+       int (*link_func)(const char *, const char *);
+
+       flag = bb_getopt_ulflags(argc, argv, "sfnbS:", &suffix);
+
+       if (argc == optind) {
+               bb_show_usage();
        }
 
-       linkIntoDirFlag = isDirectory(linkName, TRUE, NULL);
+       last = argv[argc - 1];
+       argv += optind;
 
-       if ((argc > 3) && !linkIntoDirFlag) {
-               fprintf(stderr, not_a_directory, "ln", linkName);
-               exit FALSE;
+       if (argc == optind + 1) {
+               *--argv = last;
+               last = bb_get_last_path_component(bb_xstrdup(last));
        }
 
-       while (argc-- >= 2) {
-               char srcName[BUFSIZ + 1];
-               int nChars, status;
-
-               if (strlen(*argv) > BUFSIZ) {
-                       fprintf(stderr, name_too_long, "ln");
-                       exit FALSE;
+       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("%s", *argv);
+                       status = EXIT_FAILURE;
+                       free(src_name);
+                       continue;
                }
 
-               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';
+               if (flag & LN_BACKUP) {
+                               char *backup;
+                               backup = bb_xasprintf("%s%s", src, suffix);
+                               if (rename(src, backup) < 0 && errno != ENOENT) {
+                                               bb_perror_msg("%s", src);
+                                               status = EXIT_FAILURE;
+                                               free(backup);
+                                               continue;
+                               }
+                               free(backup);
+                               /*
+                                * When the source and dest are both hard links to the same
+                                * inode, a rename may succeed even though nothing happened.
+                                * Therefore, always unlink().
+                                */
+                               unlink(src);
+               } else if (flag & LN_FORCE) {
+                       unlink(src);
                }
 
-               if (removeoldFlag == TRUE) {
-                       status = (unlink(linkName) && errno != ENOENT);
-                       if (status != 0) {
-                               perror(linkName);
-                               exit FALSE;
-                       }
+               link_func = link;
+               if (flag & LN_SYMLINK) {
+                       link_func = symlink;
                }
 
-               if (symlinkFlag == TRUE)
-                       status = symlink(*argv, linkName);
-               else
-                       status = link(*argv, linkName);
-               if (status != 0) {
-                       perror(linkName);
-                       exit FALSE;
+               if (link_func(*argv, src) != 0) {
+                       bb_perror_msg("%s", src);
+                       status = EXIT_FAILURE;
                }
-       }
-       exit TRUE;
-}
 
-/*
-Local Variables:
-c-file-style: "linux"
-c-basic-offset: 4
-tab-width: 4
-End:
-*/
+               free(src_name);
+
+       } while ((++argv)[1]);
+
+       return status;
+}