ln.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* BB_AUDIT SUSv3 compliant */
  23. /* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */
  24. /* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include "busybox.h"
  30. #define LN_SYMLINK 1
  31. #define LN_FORCE 2
  32. #define LN_NODEREFERENCE 4
  33. #define LN_BACKUP 8
  34. #define LN_SUFFIX 16
  35. int ln_main(int argc, char **argv)
  36. {
  37. int status = EXIT_SUCCESS;
  38. int flag;
  39. char *last;
  40. char *src_name;
  41. char *src;
  42. char *suffix = "~";
  43. struct stat statbuf;
  44. int (*link_func)(const char *, const char *);
  45. flag = bb_getopt_ulflags(argc, argv, "sfnbS:", &suffix);
  46. if (argc == optind) {
  47. bb_show_usage();
  48. }
  49. last = argv[argc - 1];
  50. argv += optind;
  51. if (argc == optind + 1) {
  52. *--argv = last;
  53. last = bb_get_last_path_component(bb_xstrdup(last));
  54. }
  55. do {
  56. src_name = NULL;
  57. src = last;
  58. if (is_directory(src,
  59. (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE,
  60. NULL)) {
  61. src_name = bb_xstrdup(*argv);
  62. src = concat_path_file(src, bb_get_last_path_component(src_name));
  63. free(src_name);
  64. src_name = src;
  65. }
  66. if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) {
  67. bb_perror_msg("%s", *argv);
  68. status = EXIT_FAILURE;
  69. free(src_name);
  70. continue;
  71. }
  72. if (flag & LN_BACKUP) {
  73. char *backup;
  74. backup = bb_xasprintf("%s%s", src, suffix);
  75. if (rename(src, backup) < 0 && errno != ENOENT) {
  76. bb_perror_msg("%s", src);
  77. status = EXIT_FAILURE;
  78. free(backup);
  79. continue;
  80. }
  81. free(backup);
  82. /*
  83. * When the source and dest are both hard links to the same
  84. * inode, a rename may succeed even though nothing happened.
  85. * Therefore, always unlink().
  86. */
  87. unlink(src);
  88. } else if (flag & LN_FORCE) {
  89. unlink(src);
  90. }
  91. link_func = link;
  92. if (flag & LN_SYMLINK) {
  93. link_func = symlink;
  94. }
  95. if (link_func(*argv, src) != 0) {
  96. bb_perror_msg("%s", src);
  97. status = EXIT_FAILURE;
  98. }
  99. free(src_name);
  100. } while ((++argv)[1]);
  101. return status;
  102. }