]> granicus.if.org Git - imagemagick/blob - winpath.sh
...
[imagemagick] / winpath.sh
1 #!/bin/sh
2 # Copyright (C) 1999-2019 ImageMagick Studio LLC
3 # Copyright (C) 2003 - 2008 GraphicsMagick Group
4 #
5 # This program is covered by multiple licenses, which are described in
6 # LICENSE. You should have received a copy of LICENSE with this
7 # package; otherwise see https://imagemagick.org/script/license.php.
8 #
9 # Convert the specified POSIX path to a Windows path under MinGW and Cygwin
10 # The optional second parameter specifies the level of backslash escaping
11 # to apply for each Windows backslash position in order to support varying
12 # levels of variable substitutions in scripts.
13 #
14 # Note that Cygwin includes the 'cygpath' utility, which already provides
15 # path translation capability.
16 #
17 # Written by Bob Friesenhahn, June 2002
18 #
19 arg="$1"
20 escapes=0
21 if test -n "$2"
22 then
23   escapes="$2"
24 fi
25 if test $escapes -gt 3
26 then
27   echo "$0: escape level must in range 0 - 3"
28   exit 1
29 fi
30 result=''
31 length=0
32 max_length=0
33 mount | sed -e 's:\\:/:g'  | (
34   IFS="\n"
35   while read mount_entry
36   do
37     win_mount_path=`echo "$mount_entry" | sed -e 's: .*::g'`
38     unix_mount_path=`echo "$mount_entry" | sed -e 's:.* on ::;s: type .*::'`
39     temp=`echo "$arg" | sed -e "s!^$unix_mount_path!$win_mount_path/!"`
40     if test "$temp" != "$arg"
41     then
42       candidate="$temp"
43       length=${#unix_mount_path}
44       if test $length -gt $max_length
45       then
46         result=$candidate
47         max_length=$length
48       fi
49     fi
50   done
51   if test -z "$result"
52   then
53     echo "$0: path \"$arg\" is not mounted"
54     exit 1
55   fi
56   case $escapes in
57     0)
58      echo "$result" | sed -e 's:/:\\:g'
59      ;;
60     1)
61      echo "$result" | sed -e 's:/:\\\\:g'
62      ;;
63     2)
64      echo "$result" | sed -e 's:/:\\\\\\\\:g'
65      ;;
66     3)
67      echo "$result" | sed -e 's:/:\\\\\\\\\\\\\\\\:g'
68      ;;
69   esac
70   exit 0;
71  )