rellns-sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/sh
  2. # rellns-sh - Simplified ln program to generate relative symbolic link.
  3. # Copyright (C) 1996-2019 Free Software Foundation, Inc.
  4. # Written by Ulrich Drepper <drepper@cygnus.com>, October 1996
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. # With -p, instead of creating the link print the computed relative link
  19. # name.
  20. do_print=false
  21. case $1 in
  22. -p)
  23. do_print=true
  24. shift
  25. ;;
  26. esac
  27. if test $# -ne 2; then
  28. echo "Usage: rellns [-p] SOURCE DEST" >&2
  29. exit 1
  30. fi
  31. # Make both paths absolute.
  32. if test -d $1; then
  33. to=`cd $1 && pwd -P`
  34. else
  35. temp=`echo $1 | sed 's%/*[^/]*$%%'`
  36. if test -z "$temp"; then
  37. to=`pwd -P`
  38. else
  39. to=`cd $temp && pwd -P`
  40. fi
  41. to="$to/`echo $1 | sed 's%.*/\([^/][^/]*\)$%\1%'`"
  42. fi
  43. to=`echo $to | sed 's%^/%%'`
  44. if test -d $2; then
  45. from=`echo $2 | sed 's%/*$%%'`
  46. else
  47. from=`echo $2 | sed 's%/*[^/]*$%%'`
  48. fi
  49. if test -z "$from"; then
  50. from=`pwd -P | sed 's%^/%%'`
  51. else
  52. from=`cd $from && pwd -P | sed 's%^/%%'`
  53. fi
  54. while test -n "$to" && test -n "$from"; do
  55. preto=`echo $to | sed 's%^\([^/]*\)/.*%\1%'`
  56. prefrom=`echo $from | sed 's%^\([^/]*\)/.*%\1%'`
  57. test "$preto" != "$prefrom" && break
  58. to=`echo $to | sed 's%^[^/]*/*\(.*\)$%\1%'`
  59. from=`echo $from | sed 's%^[^/]*/*\(.*\)$%\1%'`
  60. done
  61. while test -n "$from"; do
  62. rfrom="../$rfrom"
  63. from=`echo $from | sed 's%^[^/]*/*%%'`
  64. done
  65. if $do_print; then
  66. echo "$rfrom$to"
  67. else
  68. ln -s $rfrom$to $2
  69. fi