1 .\" Copyright (c) 1993 Michael Haardt (u31b3hs@pool.informatik.rwth-aachen.de)
2 .\" and copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
3 .\" and copyright (c) 2006 Justin Pryzby <justinpryzby@users.sf.net>
4 .\" and copyright (c) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
11 .\" The GNU General Public License's references to "object code"
12 .\" and "executables" are to be interpreted as the output of any
13 .\" document formatting or typesetting system, including
14 .\" intermediate and printed output.
16 .\" This manual is distributed in the hope that it will be useful,
17 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 .\" GNU General Public License for more details.
21 .\" You should have received a copy of the GNU General Public
22 .\" License along with this manual; if not, write to the Free
23 .\" Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
26 .\" Modified Sun Jul 25 11:02:22 1993 by Rik Faith (faith@cs.unc.edu)
27 .\" 2006-05-24, Justin Pryzby <justinpryzby@users.sf.net>
28 .\" document FTW_ACTIONRETVAL; include .SH "RETURN VALUE";
29 .\" 2006-05-24, Justin Pryzby <justinpryzby@users.sf.net> and
30 .\" Michael Kerrisk <mtk.manpages@gmail.com>
31 .\" reorganized and rewrote much of the page
32 .\" 2006-05-24, Michael Kerrisk <mtk.manpages@gmail.com>
33 .\" Added an example program.
35 .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
36 .\" Translated 1998-04-28, NAKANO Takeo <nakano@apm.seikei.ac.jp>
37 .\" Updated & Modified 1999-09-14, NAKANO Takeo <nakano@apm.seikei.ac.jp>
38 .\" Updated & Modified 2005-11-04, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
39 .\" Updated 2006-07-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
41 .\" WORD: file descriptor ファイルディスクリプター
43 .TH FTW 3 2010-09-20 "Linux" "Linux Programmer's Manual"
46 .\"O ftw, nftw \- file tree walk
47 ftw, nftw \- ファイルツリーを歩きまわる
53 .BI "int ftw(const char *" dirpath ,
54 .BI " int (*" fn ") (const char *" fpath ", const struct stat *" sb ,
55 .BI " int " typeflag ),
56 .BI " int " nopenfd );
58 .\"O .BR "#define _XOPEN_SOURCE 500" " /* See feature_test_macros(7) */"
59 .BR "#define _XOPEN_SOURCE 500" " /* feature_test_macros(7) 参照 */"
60 .B #define _XOPEN_SOURCE 500
63 .BI "int nftw(const char *" dirpath ,
64 .BI " int (*" fn ") (const char *" fpath ", const struct stat *" sb ,
65 .BI " int " typeflag ", struct FTW *" ftwbuf ),
66 .BI " int " nopenfd ", int " flags );
71 .\"O walks through the directory tree that is
72 .\"O located under the directory \fIdirpath\fP,
73 .\"O and calls \fIfn\fP() once for each entry in the tree.
74 .\"O By default, directories are handled before the files and
75 .\"O subdirectories they contain (preorder traversal).
79 で指定されたディレクトリ以下のディレクトリツリー全体を歩きまわり、
83 デフォルトでは、ディレクトリそのものが、そのディレクトリにあるファイルや
84 サブディレクトリよりも先に処理される (行きがけ順探索; preorder traversal)。
86 .\"O To avoid using up all of the calling process's file descriptors,
87 .\"O \fInopenfd\fP specifies the maximum number of directories that
89 .\"O will hold open simultaneously.
91 .\"O the search depth exceeds this,
93 .\"O will become slower because
94 .\"O directories have to be closed and reopened.
97 .\"O one file descriptor for each level in the directory tree.
98 呼び出し元プロセスが利用可能なファイルディスクリプタを使い切って
101 が同時にオープンするディレクトリの最大数を
105 一つのディレクトリを閉じてから他のディレクトリをオープンし直すこと
110 は、ディレクトリツリーの階層 1 レベルにつき、
111 最大でも一つのファイルディスクリプタしか使用しない。
113 .\"O For each entry found in the tree,
116 .\"O \fIfn\fP() with three arguments:
121 ディレクトリツリーで見つかったエントリ毎に、
131 .\"O is the pathname of the entry,
132 .\"O and is expressed either as a pathname relative to the calling process's
133 .\"O current working directory at the time of the call to
137 .\"O was expressed as a relative pathname,
138 .\"O or as an absolute pathname, if
140 .\"O was expressed as an absolute pathname.
148 が呼び出された時点の呼び出し元プロセスのカレントワーキングディレクトリ
155 .\"O is a pointer to the
157 .\"O structure returned by a call to
170 .\"O is an integer that has one of the following values:
176 .\"O is a regular file.
188 .\"O is a directory which can't be read.
197 .\"O which is not a symbolic link.
206 .\"O is a symbolic link and
208 .\"O failed, POSIX.1-2001 states
209 .\"O that it is undefined whether \fBFTW_NS\fP or \fBFTW_SL\fP (see below)
221 に渡されるかは未定義であると、POSIX.1-2001 には書かれている。
223 .\"O To stop the tree walk, \fIfn\fP() returns a nonzero value; this
224 .\"O value will become the return value of
226 .\"O As long as \fIfn\fP() returns 0,
228 .\"O will continue either until it has traversed the entire tree,
229 .\"O in which case it will return zero,
230 .\"O or until it encounters an error (such as a
232 .\"O failure), in which case it will return \-1.
241 はツリー全体の探索を続け、すべてのツリーを探索し終えたところで
244 の失敗などの) エラーが起こると \-1 を返す。
248 .\"O uses dynamic data structures, the only safe way to
249 .\"O exit out of a tree walk is to return a nonzero value from \fIfn\fP().
250 .\"O To allow a signal to terminate the walk without causing a memory leak,
251 .\"O have the handler set a global flag that is checked by \fIfn\fP().
254 .\"O unless the program is going to terminate.
256 は動的なデータ構造を用いるので、ツリー探索を安全に中断する唯一の方法は
259 の返り値とすることである。割り込みを扱うには、
260 例えば発生した割り込みをマークしておいて、 0 以外の値を返すようにする
261 シグナルによりメモリリークを起こさずに探索を終了できるようにするには、
264 がチェックするグローバルなフラグをセットするようにすればよい。
269 .\"O The function \fBnftw\fP() is the same as
271 .\"O except that it has one additional argument, \fIflags\fP,
272 .\"O and calls \fIfn\fP() with one more argument, \fIftwbuf\fP.
285 .\"O This \fIflags\fP argument is formed by ORing zero or more of the
286 .\"O following flags:
289 引き数は下記のフラグの 0 個以上の論理和を取ったものである:
291 .\"O .BR FTW_ACTIONRETVAL " (since glibc 2.3.3)"
292 .BR FTW_ACTIONRETVAL " (glibc 2.3.3 以降)"
293 .\"O If this glibc-specific flag is set, then
295 .\"O handles the return value from
299 .\"O should return one of the following values:
307 は以下の値のいずれか一つを返す必要がある。
313 .\"O to continue normally.
318 .\"O If \fIfn\fP() returns this value, then
319 .\"O siblings of the current entry will be skipped,
320 .\"O and processing continues in the parent.
322 がこの値を返した場合、処理中のエントリの兄弟 (同じ階層のエントリ)
323 の処理はスキップされ、親ディレクトリで続きの処理が行われる。
324 .\" If \fBFTW_DEPTH\fP
325 .\" is set, the entry's parent directory is processed next (with
326 .\" \fIflag\fP set to \fBFTW_DP\fP).
329 .\"O If \fIfn\fP() is called with an entry that is a directory
330 .\"O (\fItypeflag\fP is \fBFTW_D\fP), this return
331 .\"O value will prevent objects within that directory from being passed as
332 .\"O arguments to \fIfn\fP().
334 .\"O continues processing with the next sibling of the directory.
342 の引き数として渡されたディレクトリ内のエントリの処理が行われなくなる。
344 は処理中のディレクトリの兄弟 (同じ階層のエントリ) から処理を続ける。
349 .\"O to return immediately with the return value
356 .\"O Other return values could be associated with new actions in the future;
357 .\"O \fIfn\fP() should not return values other than those listed above.
358 他の返り値は将来新しい動作に対応付けられる可能性がある。
360 は上記のリストにある値以外を返さないようにすべきである。
362 .\"O The feature test macro
365 .\"O (before including
369 .\"O obtain the definition of \fBFTW_ACTIONRETVAL\fP from \fI<ftw.h>\fP.
374 (「どの」ヘッダファイルをインクルードするよりも前に)
383 .\"O to each directory before handling its contents.
384 .\"O This is useful if the program needs to perform some action
385 .\"O in the directory in which \fIfpath\fP resides.
386 セットされると、ディレクトリの内容を処理する前に
391 が属すディレクトリで何らかの動作を実行する必要がある場合に
395 .\"O If set, do a post-order traversal, that is, call \fIfn\fP() for
396 .\"O the directory itself \fIafter\fP handling the contents of the directory
397 .\"O and its subdirectories.
398 .\"O (By default, each directory is handled \fIbefore\fP its contents.)
399 セットされると、帰りがけ順探索 (post-order traversal) を行う。
400 つまり、ディレクトリそのものを引き数とした
402 呼出しは、そのディレクトリに含まれるファイルとサブディレクトリに
404 (デフォルトでは、ディレクトリ自身の処理はディレクトリ内のエントリ
408 .\"O If set, stay within the same file system
409 .\"O (i.e., do not cross mount points).
410 セットされると、同じファイルシステムの中だけを探索対象とする
411 (つまり、マウントポイントをまたぐことはない)。
414 .\"O If set, do not follow symbolic links.
415 .\"O (This is what you want.)
416 .\"O If not set, symbolic links are followed, but no file is reported twice.
417 セットされると、シンボリックリンクを辿らない (おそらくこちらが
418 通常望ましい動作だろう)。セットされていないとシンボリックリンクを
419 辿るが、同じファイルが二回報告されることはない。
421 .\"O If \fBFTW_PHYS\fP is not set, but \fBFTW_DEPTH\fP is set,
422 .\"O then the function
424 .\"O is never called for a directory that would be a descendant of itself.
428 がセットされると、自分自身に対するシンボリックリンクを配下に持つ
433 .\"O For each entry in the directory tree,
437 .\"O with four arguments.
444 .\"O may receive any of the same values as with
446 .\"O or any of the following values:
461 で取り得る値のいずれか、または以下の値のいずれかが渡される:
465 .\"O is a directory, and \fBFTW_DEPTH\fP was specified in \fIflags\fP.
466 .\"O All of the files
467 .\"O and subdirectories within \fIfpath\fP have been processed.
475 配下のファイルとサブディレクトリは全て処理が終わっている。
479 .\"O is a symbolic link, and \fBFTW_PHYS\fP was set in \fIflags\fP.
481 がシンボリックリンクで、かつ \fBFTW_PHYS\fP が \fIflags\fP に
483 .\" To obtain the definition of this constant from
487 .\" must be defined, or
488 .\" .BR _XOPEN_SOURCE
489 .\" must be defined with a value of 500 or more.
493 .\"O is a symbolic link pointing to a nonexistent file.
494 .\"O (This occurs only if \fBFTW_PHYS\fP is not set.)
496 がシンボリックリンクで、存在しないファイルを指している
501 .\"O The fourth argument that
503 .\"O supplies when calling
505 .\"O is a structure of type \fIFTW\fP:
523 .\"O is the offset of the filename (i.e., basename component)
524 .\"O in the pathname given in
529 .\"O in the directory tree, relative to the root of the tree
531 .\"O which has depth 0).
533 は、ファイル名 (basename 要素) の、
535 で渡されるパス名の中でのオフセットである。
539 の深さを示す。深さはディレクトリツリーのトップ (root) からの
543 .\"O .SH "RETURN VALUE"
545 .\"O These functions return 0 on success, and \-1 if an error occurs.
546 これらの関数は、成功すると 0 を、エラーが発生すると \-1 を返す。
548 .\"O If \fIfn\fP() returns nonzero,
549 .\"O then the tree walk is terminated and the value returned by \fIfn\fP()
550 .\"O is returned as the result of \fBftw\fP() or
553 が 0 以外を返した場合、ディレクトリツリーの探索を終了し、
563 .\"O is called with the \fBFTW_ACTIONRETVAL\fP flag,
564 .\"O then the only nonzero value that should be used by \fIfn\fP()
565 .\"O to terminate the tree walk is \fBFTW_STOP\fP,
566 .\"O and that value is returned as the result of
571 フラグ付きで呼ばれた場合、ツリーの探索を終了させるために
579 .\"O .SH "CONFORMING TO"
581 POSIX.1-2001, SVr4, SUSv1.
582 .\"O POSIX.1-2008 marks
590 .\"O POSIX.1-2001 note that the results are unspecified if
592 .\"O does not preserve the current working directory.
593 POSIX.1-2001 の注記によると、
595 がカレントワーキングディレクトリを保持しなかった場合の
600 .\"O and the use of \fBFTW_SL\fP with
602 .\"O were introduced in SUSv1.
612 .\"O will never use \fBFTW_SL\fP, on other systems \fBFTW_SL\fP occurs only
613 .\"O for symbolic links that do not point to an existing file,
614 .\"O and again on other systems
616 .\"O will use \fBFTW_SL\fP for each symbolic link.
617 .\"O For predictable control, use
623 存在しないファイルを指しているシンボリックリンクの場合にのみ
634 .\"O Under Linux, libc4 and libc5 and glibc 2.0.6 will
635 .\"O use \fBFTW_F\fP for all objects (files, symbolic links, FIFOs, etc.)
636 .\"O that can be stat'ed but are not a directory.
637 Linux では、 libc4, libc5, glibc 2.0.6 は
638 「stat できるがディレクトリではないオブジェクト」
639 (ファイル, シンボリックリンク, fifo 等)
646 .\"O is available since glibc 2.1.
648 関数は glibc 2.1 以降で利用できる。
650 .\"O \fBFTW_ACTIONRETVAL\fP is glibc-specific.
655 .\"O The following program traverses the directory tree under the path named
656 .\"O in its first command-line argument, or under the current directory
657 .\"O if no argument is supplied.
658 .\"O It displays various information about each file.
659 .\"O The second command-line argument can be used to specify characters that
660 .\"O control the value assigned to the \fIflags\fP
661 .\"O argument when calling
663 以下のプログラムは、一つ目のコマンドライン引き数を名前に持つパス以下の
664 ディレクトリツリーを探索する。引き数が指定されなかった場合は、
666 各々のファイルについて様々の情報が表示される。
667 二番目のコマンドライン引き数に文字を指定することで、
674 #define _XOPEN_SOURCE 500
682 display_info(const char *fpath, const struct stat *sb,
683 int tflag, struct FTW *ftwbuf)
685 printf("%\-3s %2d %7jd %\-40s %d %s\\n",
686 (tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
687 (tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" :
688 (tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" :
689 (tflag == FTW_SLN) ? "sln" : "???",
690 ftwbuf\->level, (intmax_t) sb\->st_size,
691 fpath, ftwbuf\->base, fpath + ftwbuf\->base);
692 return 0; /* To tell nftw() to continue */
696 main(int argc, char *argv[])
700 if (argc > 2 && strchr(argv[2], \(aqd\(aq) != NULL)
702 if (argc > 2 && strchr(argv[2], \(aqp\(aq) != NULL)
705 if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags)