#author("2017-06-07T13:11:49+09:00","","")
#navi(../)
* CygwinでWindowsのパスが必要になった時の対処方法・cygpath [#f4f636b8]

たとえば、Cygwinでsakuraエディタのaliasを設定しており、~
エディタで開くファイルを指定するとファイルが開けない状態となります。~
アプリケーションによってはエラーとなります。~
これは、WindowsアプリケーションがWindows形式のパス記述を求めているのに~
UNIX形式のパス記述で引数が渡されエラーとなっているためです。~

本資料は、cygpathコマンドを使用し、この問題を解決します。

#contents
#htmlinsertpcsp(win-top.html,win-sp.html)

* cygpathコマンドを使ってみる [#w7e3ca23]

以下のようにaliasでsakuraエディタを指定しています。
 sakura@win7pc ~
 $ alias sakura='/cygdrive/c/Program\ Files\ \(x86\)/sakura/sakura.exe'
 
 sakura@win7pc ~
 $ alias
 alias cls='clear'
 alias df='df -h'
 alias du='du -h'
  alias egrep='egrep --color=auto'
 alias fgrep='fgrep --color=auto'
 alias grep='grep --color'
 alias l='ls -CF'
 alias la='ls -A'
 alias ll='ls -l'
 alias sakura='/cygdrive/c/Program\ Files\ \(x86\)/sakura/sakura.exe'
 alias vi='vim'

テキストファイルを作成し開いてみます。
 sakura@win7pc ~
 $ echo hello > hello.txt
 
 sakura@win7pc ~
 $ sakura hello.txt &
#ref(01.png)
#br

上記の通り、カレントディレクトリにあるファイルであれば問題なく開く事ができます。

しかし以下のようにオペレーションをすると、何も表示されない状態となります。
 sakura@win7pc ~
 $ echo hello > /tmp/hello.txt
 
 sakura@win7pc ~
 $ sakura /tmp/hello.txt &

#ref(02.png)
#br

これは、sakuraエディタにUNIX形式のパスを与えたためだと思われます。~
このような場合、cygpathコマンドを使ってUNIX(Cygwinが管理してるパス)パスをWindows形式変換すれば解決します。

cygpathコマンドのヘルプは以下の通りです。

 $ cygpath --help
 Usage: cygpath (-d|-m|-u|-w|-t TYPE) [-f FILE] [OPTION]... NAME...
        cygpath [-c HANDLE]
        cygpath [-ADHOPSW]
        cygpath [-F ID]
 
 Convert Unix and Windows format paths, or output system path information
 
 Output type options:
 
   -d, --dos             print DOS (short) form of NAMEs (C:\PROGRA~1\)
   -m, --mixed           like --windows, but with regular slashes (C:/WINNT)
   -M, --mode            report on mode of file (binmode or textmode)
   -u, --unix            (default) print Unix form of NAMEs (/cygdrive/c/winnt)
   -w, --windows         print Windows form of NAMEs (C:\WINNT)
   -t, --type TYPE       print TYPE form: 'dos', 'mixed', 'unix', or 'windows'
 
 Path conversion options:
 
   -a, --absolute        output absolute path
   -l, --long-name       print Windows long form of NAMEs (with -w, -m only)
   -p, --path            NAME is a PATH list (i.e., '/bin:/usr/bin')
   -U, --proc-cygdrive   Emit /proc/cygdrive path instead of cygdrive prefix
                         when converting Windows path to UNIX path.
   -s, --short-name      print DOS (short) form of NAMEs (with -w, -m only)
   -C, --codepage CP     print DOS, Windows, or mixed pathname in Windows
                         codepage CP.  CP can be a numeric codepage identifier,
                         or one of the reserved words ANSI, OEM, or UTF8.
                         If this option is missing, cygpath defaults to the
                         character set defined by the current locale.
 
 System information:
 
   -A, --allusers        use `All Users' instead of current user for -D, -O, -P
   -D, --desktop         output `Desktop' directory and exit
   -H, --homeroot        output `Profiles' directory (home root) and exit
   -O, --mydocs          output `My Documents' directory and exit
   -P, --smprograms      output Start Menu `Programs' directory and exit
   -S, --sysdir          output system directory and exit
   -W, --windir          output `Windows' directory and exit
   -F, --folder ID       output special folder with numeric ID and exit
 
 Other options:
 
   -f, --file FILE       read FILE for input; use - to read from STDIN
   -o, --option          read options from FILE as well (for use with --file)
   -c, --close HANDLE    close HANDLE (for use in captured process)
   -i, --ignore          ignore missing argument
   -h, --help            output usage information and exit
   -V, --version         output version information and exit
 
上記のオペレーションを確認すると、-wと-pを使えば実現できることがわかります。

実際にcygpathをつかってみます。
 sakura@win7pc ~
 $ sakura $(cygpath -wp /tmp/hello.txt) &

#ref(03.png)

Windows形式のパスが展開され問題なく表示されているのが確認できます。

* cygpathの動作を見てみる [#ta6fd851]
上記でcygpathに与えた-wpオプションとパスがどのように動作しているか確認してみます。
 sakura@win7pc ~
 $ cygpath -wp /tmp/
 D:\cygwin\cygwin64\tmp\
 
 sakura@win7pc ~
 $ cygpath -wp /opt
 D:\cygwin\cygwin64\opt
 
 sakura@win7pc ~
 $ cygpath -wp /cygdrive/
 D:\cygwin\cygwin64\cygdrive\
 
 sakura@win7pc ~
 $ cygpath -wp /cygdrive/c
 C:\

上記のようにWindowsの絶対パスに変換されます。~
CygwinのパスがWindowsのどこになるの?と思われた場合もcygpathで簡単に調べることができます。

例えばカレントディレクトリを調べたい場合は以下のようになります。
 $ cygpath -wp $(pwd)
 D:\cygwin\cygwin64\home\sakura
 
 $ cygpath -wp `pwd`
 D:\cygwin\cygwin64\home\sakura

以上、CygwinでWindowsのパスが必要になった時の対処方法でした。

#htmlinsertpcsp(win-btm.html,win-sp.html)


トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS