| 1 | #!/bin/sh |
| 2 | |
| 3 | # calcpath return a path which is $1/$2/$3 when $2 is relative and $2/$3 if absolute. |
| 4 | |
| 5 | # Copyright (C) 2021-2025 Free Software Foundation, Inc. |
| 6 | # Contributed by Gaius Mulley <[email protected]>. |
| 7 | # |
| 8 | # This file is part of GNU Modula-2. |
| 9 | # |
| 10 | # GNU Modula-2 is free software; you can redistribute it and/or modify it under |
| 11 | # the terms of the GNU General Public License as published by the Free |
| 12 | # Software Foundation; either version 3, or (at your option) any later |
| 13 | # version. |
| 14 | # |
| 15 | # GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY |
| 16 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 17 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 18 | # for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License |
| 21 | # along with GCC; see the file COPYING3. If not see |
| 22 | # <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | |
| 25 | Usage () { |
| 26 | cat<<EOF |
| 27 | Usage: $0 pathcomponent1 pathcomponent2 subdir |
| 28 | if pathcomponent2 is relative then pathcomponent1/pathcompinent2/subdir is returned |
| 29 | otherwise pathcomponent2/subdir is returned |
| 30 | the path is checked for legality in subdir. |
| 31 | EOF |
| 32 | } |
| 33 | |
| 34 | die () { |
| 35 | printf "calcpath: error: %s\n" "$1" >&2 |
| 36 | exit 1 |
| 37 | } |
| 38 | |
| 39 | if [ $# -eq 3 ]; then |
| 40 | case "$2" in |
| 41 | /*) the_path="$2/$3" ;; |
| 42 | *) the_path="$1/$2/$3" ;; |
| 43 | esac |
| 44 | cd "$3" || die "could not access $3" |
| 45 | if ( cd "$the_path" ); then |
| 46 | printf '%s\n' "${the_path}" |
| 47 | else |
| 48 | die "${the_path} is not a valid path in subdirectory $3" |
| 49 | fi |
| 50 | else |
| 51 | Usage |
| 52 | exit 1 |
| 53 | fi |