rellns-sh 1.9 KB

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