ln.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini ln implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. /* BB_AUDIT SUSv3 compliant */
  10. /* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */
  11. /* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */
  12. //usage:#define ln_trivial_usage
  13. //usage: "[OPTIONS] TARGET... LINK|DIR"
  14. //usage:#define ln_full_usage "\n\n"
  15. //usage: "Create a link LINK or DIR/TARGET to the specified TARGET(s)\n"
  16. //usage: "\n -s Make symlinks instead of hardlinks"
  17. //usage: "\n -f Remove existing destinations"
  18. //usage: "\n -n Don't dereference symlinks - treat like normal file"
  19. //usage: "\n -b Make a backup of the target (if exists) before link operation"
  20. //usage: "\n -S suf Use suffix instead of ~ when making backup files"
  21. //usage:
  22. //usage:#define ln_example_usage
  23. //usage: "$ ln -s BusyBox /tmp/ls\n"
  24. //usage: "$ ls -l /tmp/ls\n"
  25. //usage: "lrwxrwxrwx 1 root root 7 Apr 12 18:39 ls -> BusyBox*\n"
  26. #include "libbb.h"
  27. /* This is a NOEXEC applet. Be very careful! */
  28. #define LN_SYMLINK 1
  29. #define LN_FORCE 2
  30. #define LN_NODEREFERENCE 4
  31. #define LN_BACKUP 8
  32. #define LN_SUFFIX 16
  33. int ln_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  34. int ln_main(int argc, char **argv)
  35. {
  36. int status = EXIT_SUCCESS;
  37. int opts;
  38. char *last;
  39. char *src_name;
  40. char *src;
  41. char *suffix = (char*)"~";
  42. struct stat statbuf;
  43. int (*link_func)(const char *, const char *);
  44. opt_complementary = "-1"; /* min one arg */
  45. opts = getopt32(argv, "sfnbS:", &suffix);
  46. last = argv[argc - 1];
  47. argv += optind;
  48. if (!argv[1]) {
  49. /* "ln PATH/TO/FILE" -> "ln PATH/TO/FILE FILE" */
  50. *--argv = last;
  51. /* xstrdup is needed: "ln -s PATH/TO/FILE/" is equivalent to
  52. * "ln -s PATH/TO/FILE/ FILE", not "ln -s PATH/TO/FILE FILE"
  53. */
  54. last = bb_get_last_path_component_strip(xstrdup(last));
  55. }
  56. do {
  57. src_name = NULL;
  58. src = last;
  59. if (is_directory(src,
  60. (opts & LN_NODEREFERENCE) ^ LN_NODEREFERENCE
  61. )
  62. ) {
  63. src_name = xstrdup(*argv);
  64. src = concat_path_file(src, bb_get_last_path_component_strip(src_name));
  65. free(src_name);
  66. src_name = src;
  67. }
  68. if (!(opts & LN_SYMLINK) && stat(*argv, &statbuf)) {
  69. // coreutils: "ln dangling_symlink new_hardlink" works
  70. if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) {
  71. bb_simple_perror_msg(*argv);
  72. status = EXIT_FAILURE;
  73. free(src_name);
  74. continue;
  75. }
  76. }
  77. if (opts & LN_BACKUP) {
  78. char *backup;
  79. backup = xasprintf("%s%s", src, suffix);
  80. if (rename(src, backup) < 0 && errno != ENOENT) {
  81. bb_simple_perror_msg(src);
  82. status = EXIT_FAILURE;
  83. free(backup);
  84. continue;
  85. }
  86. free(backup);
  87. /*
  88. * When the source and dest are both hard links to the same
  89. * inode, a rename may succeed even though nothing happened.
  90. * Therefore, always unlink().
  91. */
  92. unlink(src);
  93. } else if (opts & LN_FORCE) {
  94. unlink(src);
  95. }
  96. link_func = link;
  97. if (opts & LN_SYMLINK) {
  98. link_func = symlink;
  99. }
  100. if (link_func(*argv, src) != 0) {
  101. bb_simple_perror_msg(src);
  102. status = EXIT_FAILURE;
  103. }
  104. free(src_name);
  105. } while ((++argv)[1]);
  106. return status;
  107. }