.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de) .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" Modified Wed Jul 21 22:35:42 1993 by Rik Faith (faith@cs.unc.edu) .\" Modified 18 Mar 1996 by Martin Schulze (joey@infodrom.north.de): .\" Corrected description of getwd(). .\" Modified Sat Aug 21 12:32:12 MET 1999 by aeb - applied fix by aj .\" Modified Mon Dec 11 13:32:51 MET 2000 by aeb .\" Modified Thu Apr 22 03:49:15 CEST 2002 by Roger Luethi .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 IMAMURA Nobutaka .\" all rights reserved. .\" Translated Fri Feb 14 12:40:22 JST 1997 .\" by IMAMURA Nobutaka .\" Modified Thu Dec 8 05:08:44 JST 1999 .\" by Kentaro Shirakata .\" Modified Sun Mar 12 21:08:44 JST 2000 .\" by HANATAKA Shinya .\" Updated 2001-01-29 by Kentaro Shirakata .\" Updated 2002-03-23 by Kentaro Shirakata .\" Updated 2002-10-17 by Kentaro Shirakata .\" Updated 2007-01-01 by Kentaro Shirakata .\" Updated 2008-08-21, Akihiro MOTOKI , LDP v3.07 .\" .TH GETCWD 3 2018\-04\-30 GNU "Linux Programmer's Manual" .SH 名前 getcwd, getwd, get_current_dir_name \- カレントワーキングディレクトリ名の取得 .SH 書式 .nf \fB#include \fP .PP \fBchar *getcwd(char *\fP\fIbuf\fP\fB, size_t \fP\fIsize\fP\fB);\fP .PP \fBchar *getwd(char *\fP\fIbuf\fP\fB);\fP .PP \fBchar *get_current_dir_name(void);\fP .fi .PP .RS -4 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .RE .PP \fBget_current_dir_name\fP(): .RS _GNU_SOURCE .RE .PP \fBgetwd\fP(): .ad l .RS 4 .PD 0 .TP 4 glibc 2.12 以降: .nf (_XOPEN_SOURCE\ >=\ 500) && ! (_POSIX_C_SOURCE\ >=\ 200809L) || /* Glibc 2.19 以降: */ _DEFAULT_SOURCE || /* Glibc 2.19 以前: */ _BSD_SOURCE .fi .TP 4 glibc 2.12 より前: .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 .PD .RE .ad b .SH 説明 これらの関数は、呼び出したプロセスのカレントワーキングディレクトリの 絶対パス名 (absolute pathname) が入った文字列を返す。 返される文字列はヌルで終端される。 パス名は関数の結果として返され、引数 \fIbuf\fP がある場合は \fIbuf\fP 経由でも返される。 .PP \fBgetcwd\fP() 関数はカレントワーキングディレクトリの絶対パス名を \fIbuf\fP で示された \fIsize\fP 長の配列にコピーする。 .PP 終端のヌルバイトも含めた、カレントワーキングディレクトリの 絶対パス名の長さが \fIsize\fP バイトを超えている場合は、返り値として NULL が返り \fIerrno\fP に \fBERANGE\fP がセットされる。 アプリケーションはこのエラーをチェックし、 必要に応じてより長いバッファーを用意すべきである。 .PP POSIX.1\-2001 標準の拡張として、 glibc では \fIbuf\fP が NULL の場合、 \fBgetcwd\fP() は必要なバッファーを \fBmalloc\fP(3) を用いて動的に割り当てる。 この場合、 \fIsize\fP が 0 の場合を除き、バッファーの長さは \fIsize\fP となる。 \fIsize\fP が 0 の場合には必要な大きさが確保される。 呼び出し側で、返されたバッファーを \fBfree\fP(3) すべきである。 .PP \fBget_current_dir_name\fP() はカレントワーキングディレクトリの絶対パス名を収めるのに 十分な大きさの配列を \fBmalloc\fP(3) で獲得する。環境変数 \fBPWD\fP が設定されておりその値が正しければ、その値が返される。 呼び出し側で、返されたバッファーを \fBfree\fP(3) すべきである。 .PP \fBgetwd\fP() は \fBmalloc\fP(3) によるメモリー獲得を一切行なわない。 \fIbuf\fP 引数は少なくとも \fBPATH_MAX\fP バイトの長さを持つ配列へのポインターである必要がある。 終端のヌルバイトも含めた、カレントワーキングディレクトリの 絶対パス名の長さが \fBPATH_MAX\fP バイトを超えている場合、 NULL が返され、 \fIerrno\fP に \fBENAMETOOLONG\fP が設定される。 (システムによっては、 \fBPATH_MAX\fP は必ずしもコンパイル時に決まる定数ではない点に注意すること。 また、ファイルシステムに依存する場合もある。 \fBpathconf\fP(3) を参照。) 移植性とセキュリティ上の理由から、 \fBgetwd\fP() の利用は推奨されない。 .SH 返り値 成功すると、これらの関数はカレントワーキングディレクトリの絶対パス名 が入った文字列へのポインターを返す。 \fBgetcwd\fP() と \fBgetwd\fP() の場合、返り値は \fIbuf\fP と同じ値になる。 .PP 失敗した場合、これらの関数は NULL を返し、 \fIerrno\fP にエラーを示す値を設定する。 \fIbuf\fP が指す配列の内容は未定義である。 .SH エラー .TP \fBEACCES\fP ファイル名の構成要素に対する読み込みあるいは検索の権限がない。 .TP \fBEFAULT\fP \fIbuf\fP が不正なアドレスを指している。 .TP \fBEINVAL\fP \fIsize\fP 引数が 0 かつ、 \fIbuf\fP 引数がヌルポインターでない。 .TP \fBEINVAL\fP \fBgetwd\fP(): \fIbuf\fP が NULL である。 .TP \fBENAMETOOLONG\fP \fBgetwd\fP(): 絶対パス名が入ったヌル終端された文字列の長さが \fBPATH_MAX\fP バイトを超えている。 .TP \fBENOENT\fP カレントワーキングディレクトリが削除されている。 .TP \fBENOMEM\fP メモリー不足。 .TP \fBERANGE\fP \fIsize\fP 引数の値がワーキングディレクトリの絶対パス名の長さより小さい。 長さには文字列の終端バイトも含まれる。 より大きい配列を確保してもう一度実行する必要がある。 .SH 属性 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。 .TS allbox; lbw22 lb lb l l l. インターフェース 属性 値 T{ \fBgetcwd\fP(), \fBgetwd\fP() T} Thread safety MT\-Safe T{ \fBget_current_dir_name\fP() T} Thread safety MT\-Safe env .TE .SH 準拠 \fBgetcwd\fP() は POSIX.1\-2001 に準拠している。 POSIX.1\-2001 は、 \fIbuf\fP が NULL の場合の \fBgetcwd\fP() の動作を規定しないままとしている。 .PP \fBgetwd\fP() は POSIX.1\-2001 に存在しているが、「過去の名残(LEGACY)」とされている。 POSIX.1\-2008 では、 \fBgetwd\fP() の仕様が削除されている。 代わりに \fBgetcwd\fP() を使うこと。 POSIX.1\-2001 は \fBgetwd\fP() に関するエラーを定義していない。 .PP \fBget_current_dir_name\fP() は GNU 拡張である。 .SH 注意 Under Linux, these functions make use of the \fBgetcwd\fP() system call (available since Linux 2.1.92). On older systems they would query \fI/proc/self/cwd\fP. If both system call and proc filesystem are missing, a generic implementation is called. Only in that case can these calls fail under Linux with \fBEACCES\fP. .PP .\" これらの関数はしばしばカレントワーキングディレクトリの位置を保存し、 後で戻ってくるために利用される。 未使用のファイルディスクリプターが十分ある場合は、 現在のディレクトリ (".") を開いて \fBfchdir\fP(2) を呼び出すほうが普通は高速で信頼性がある。 特に Linux 以外のプラットフォームの場合はそうである。 .SS "C ライブラリとカーネルの違い" .\" commit 3272c544da48f8915a0e34189182aed029bd0f2b On Linux, the kernel provides a \fBgetcwd\fP() system call, which the functions described in this page will use if possible. The system call takes the same arguments as the library function of the same name, but is limited to returning at most \fBPATH_MAX\fP bytes. (Before Linux 3.12, the limit on the size of the returned pathname was the system page size. On many architectures, \fBPATH_MAX\fP and the system page size are both 4096 bytes, but a few architectures have a larger page size.) If the length of the pathname of the current working directory exceeds this limit, then the system call fails with the error \fBENAMETOOLONG\fP. In this case, the library functions fall back to a (slower) alternative implementation that returns the full pathname. .PP .\" commit 8df9d1a4142311c084ffeeacb67cd34d190eff74 Following a change in Linux 2.6.36, the pathname returned by the \fBgetcwd\fP() system call will be prefixed with the string "(unreachable)" if the current directory is not below the root directory of the current process (e.g., because the process set a new filesystem root using \fBchroot\fP(2) without changing its current directory into the new root). Such behavior can also be caused by an unprivileged user by changing the current directory into another mount namespace. When dealing with pathname from untrusted sources, callers of the functions described in this page should consider checking whether the returned pathname starts with '/' or '(' to avoid misinterpreting an unreachable path as a relative pathname. .SH バグ Since the Linux 2.6.36 change that added "(unreachable)" in the circumstances described above, the glibc implementation of \fBgetcwd\fP() has failed to conform to POSIX and returned a relative pathname when the API contract requires an absolute pathname. With glibc 2.27 onwards this is corrected; calling \fBgetcwd\fP() from such a pathname will now result in failure with \fBENOENT\fP. .SH 関連項目 \fBpwd\fP(1), \fBchdir\fP(2), \fBfchdir\fP(2), \fBopen\fP(2), \fBunlink\fP(2), \fBfree\fP(3), \fBmalloc\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。