OSDN Git Service

154fdad7c998970ef39e05f030a9e0e100c4c7af
[linuxjm/LDP_man-pages.git] / draft / man3 / ftw.3
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>
5 .\"
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.
10 .\"
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.
15 .\"
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.
20 .\"
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,
24 .\" USA.
25 .\"
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.
34 .\"
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
40 .\"
41 .\" WORD:       file descriptor         ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¡¼
42 .\"
43 .TH FTW 3 2010-09-20 "Linux" "Linux Programmer's Manual"
44 .\"O .SH NAME
45 .SH Ì¾Á°
46 .\"O ftw, nftw \- file tree walk
47 ftw, nftw \- ¥Õ¥¡¥¤¥ë¥Ä¥ê¡¼¤òÊ⤭¤Þ¤ï¤ë
48 .\"O .SH SYNOPSIS
49 .SH ½ñ¼°
50 .nf
51 .B #include <ftw.h>
52 .sp
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 );
57 .sp
58 .B #define _XOPEN_SOURCE 500
59 .B #include <ftw.h>
60 .sp
61 .BI "int nftw(const char *" dirpath ,
62 .BI "        int (*" fn ") (const char *" fpath ", const struct stat *" sb ,
63 .BI "                   int " typeflag ", struct FTW *" ftwbuf ),
64 .BI "        int " nopenfd ", int " flags );
65 .fi
66 .\"O .SH DESCRIPTION
67 .SH ÀâÌÀ
68 .\"O .BR ftw ()
69 .\"O walks through the directory tree that is
70 .\"O located under the directory \fIdirpath\fP,
71 .\"O and calls \fIfn\fP() once for each entry in the tree.
72 .\"O By default, directories are handled before the files and
73 .\"O subdirectories they contain (preorder traversal).
74 .BR ftw ()
75 ¤Ï¡¢
76 .I dirpath
77 ¤Ç»ØÄꤵ¤ì¤¿¥Ç¥£¥ì¥¯¥È¥ê°Ê²¼¤Î¥Ç¥£¥ì¥¯¥È¥ê¥Ä¥ê¡¼Á´ÂΤòÊ⤭¤Þ¤ï¤ê¡¢
78 ¥Ä¥ê¡¼Ãæ¤Ç¥¨¥ó¥È¥ê¤¬¸«ÉÕ¤«¤ë¤´¤È¤Ë¡¢
79 .IR fn ()
80 ¤ò¸Æ¤Ó½Ð¤¹¡£
81 ¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï¡¢¥Ç¥£¥ì¥¯¥È¥ê¤½¤Î¤â¤Î¤¬¡¢¤½¤Î¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤¢¤ë¥Õ¥¡¥¤¥ë¤ä
82 ¥µ¥Ö¥Ç¥£¥ì¥¯¥È¥ê¤è¤ê¤âÀè¤Ë½èÍý¤µ¤ì¤ë (¹Ô¤­¤¬¤±½çõº÷; preorder traversal)¡£
83
84 .\"O To avoid using up all of the calling process's file descriptors,
85 .\"O \fInopenfd\fP specifies the maximum number of directories that
86 .\"O .BR ftw ()
87 .\"O will hold open simultaneously.
88 .\"O When
89 .\"O the search depth exceeds this,
90 .\"O .BR ftw ()
91 .\"O will become slower because
92 .\"O directories have to be closed and reopened.
93 .\"O .BR ftw ()
94 .\"O uses at most
95 .\"O one file descriptor for each level in the directory tree.
96 ¸Æ¤Ó½Ð¤·¸µ¥×¥í¥»¥¹¤¬ÍøÍѲÄǽ¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò»È¤¤ÀڤäÆ
97 ¤·¤Þ¤ï¤Ê¤¤¤è¤¦¤Ë¤¹¤ë¤¿¤á¡¢
98 .BR ftw ()
99 ¤¬Æ±»þ¤Ë¥ª¡¼¥×¥ó¤¹¤ë¥Ç¥£¥ì¥¯¥È¥ê¤ÎºÇÂç¿ô¤ò
100 .I nopenfd
101 ¤Ç»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
102 õº÷¤Î¿¼¤µ¤¬¤³¤ÎÃͤò±Û¤¨¤ë¤È¡¢
103 °ì¤Ä¤Î¥Ç¥£¥ì¥¯¥È¥ê¤òÊĤ¸¤Æ¤«¤é¾¤Î¥Ç¥£¥ì¥¯¥È¥ê¤ò¥ª¡¼¥×¥ó¤·Ä¾¤¹¤³¤È
104 ¤Ë¤Ê¤ë¤Î¤Ç¡¢
105 .BR ftw ()
106 ¤ÎÆ°ºî¤ÏÃÙ¤¯¤Ê¤ë¡£
107 .BR ftw ()
108 ¤Ï¡¢¥Ç¥£¥ì¥¯¥È¥ê¥Ä¥ê¡¼¤Î³¬ÁØ 1 ¥ì¥Ù¥ë¤Ë¤Ä¤­¡¢
109 ºÇÂç¤Ç¤â°ì¤Ä¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤·¤«»ÈÍѤ·¤Ê¤¤¡£
110
111 .\"O For each entry found in the tree,
112 .\"O .BR ftw ()
113 .\"O calls
114 .\"O \fIfn\fP() with three arguments:
115 .\"O .IR fpath ,
116 .\"O .IR sb ,
117 .\"O and
118 .\"O .IR typeflag .
119 ¥Ç¥£¥ì¥¯¥È¥ê¥Ä¥ê¡¼¤Ç¸«¤Ä¤«¤Ã¤¿¥¨¥ó¥È¥êËè¤Ë¡¢
120 .BR ftw ()
121 ¤Ï
122 .IR fpath ,
123 .IR sb ,
124 .I typeflag
125 ¤Î 3¤Ä¤ò°ú¤­¿ô¤È¤·¤Æ
126 .IR fn ()
127 ¤ò¸Æ¤Ó½Ð¤¹¡£
128 .\"O .I fpath
129 .\"O is the pathname of the entry,
130 .\"O and is expressed either as a pathname relative to the calling process's
131 .\"O current working directory at the time of the call to
132 .\"O .BR ftw (),
133 .\"O if
134 .\"O .IR dirpath
135 .\"O was expressed as a relative pathname,
136 .\"O or as an absolute pathname, if
137 .\"O .I dirpath
138 .\"O was expressed as an absolute pathname.
139 .I fpath
140 ¤Ï¥¨¥ó¥È¥ê¤Î¥Ñ¥¹Ì¾¤Ç¤¢¤ë¡£
141 .I dirpath
142 ¤¬ÁêÂХѥ¹Ì¾¤Ç»ØÄꤵ¤ì¤¿¾ì¹ç¤Ë¤Ï¡¢
143 .I fpath
144 ¤Ï
145 .BR ftw ()
146 ¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿»þÅÀ¤Î¸Æ¤Ó½Ð¤·¸µ¥×¥í¥»¥¹¤Î¥«¥ì¥ó¥È¥ï¡¼¥­¥ó¥°¥Ç¥£¥ì¥¯¥È¥ê
147 ¤«¤é¤ÎÁêÂХѥ¹Ì¾¤È¤Ê¤ë¡£
148 .I dirpath
149 ¤¬ÀäÂХѥ¹Ì¾¤Ç»ØÄꤵ¤ì¤¿¾ì¹ç¤Ë¤Ï¡¢
150 .I fpath
151 ¤ÏÀäÂХѥ¹Ì¾¤È¤Ê¤ë¡£
152 .\"O .I sb
153 .\"O is a pointer to the
154 .\"O .I stat
155 .\"O structure returned by a call to
156 .\"O .BR stat (2)
157 .\"O for
158 .\"O .IR fpath .
159 .I sb
160 ¤Ï
161 .I fpath
162 ¤ËÂФ¹¤ë
163 .BR stat (2)
164 ¤Î¸Æ¤Ó½Ð¤·¤ÇÊÖ¤µ¤ì¤ë
165 .I stat
166 ¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤Ç¤¢¤ë¡£
167 .\"O .I typeflag
168 .\"O is an integer that has one of the following values:
169 .I typeflag
170 ¤ÏÀ°¿ô¤Ç¡¢°Ê²¼¤ÎÃͤΤ¤¤º¤ì¤«°ì¤Ä¤Ç¤¢¤ë:
171 .TP
172 .B FTW_F
173 .\"O .I fpath
174 .\"O is a regular file.
175 .I fpath
176 ¤¬Ä̾ï¤Î¥Õ¥¡¥¤¥ë¤Ç¤¢¤ë
177 .TP
178 .B FTW_D
179 .\"O .I fpath
180 .\"O is a directory.
181 .I fpath
182 ¤¬¥Ç¥£¥ì¥¯¥È¥ê¤Ç¤¢¤ë
183 .TP
184 .B FTW_DNR
185 .\"O .I fpath
186 .\"O is a directory which can't be read.
187 .I fpath
188 ¤¬Æɤ߹þ¤ß¤Ç¤­¤Ê¤¤¥Ç¥£¥ì¥¯¥È¥ê¤Ç¤¢¤ë
189 .TP
190 .B FTW_NS
191 .\"O The
192 .\"O .BR stat (2)
193 .\"O call failed on
194 .\"O .IR fpath ,
195 .\"O which is not a symbolic link.
196 ¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯¤Ç¤Ï¤Ê¤¤
197 .I fpath
198 ¤ËÂФ¹¤ë
199 .BR stat (2)
200 ¸Æ¤Ó½Ð¤·¤¬¼ºÇÔ¤·¤¿¡£
201 .sp
202 .\"O If
203 .\"O .I fpath
204 .\"O is a symbolic link and
205 .\"O .BR stat (2)
206 .\"O failed, POSIX.1-2001 states
207 .\"O that it is undefined whether \fBFTW_NS\fP or \fBFTW_SL\fP (see below)
208 .\"O is passed in
209 .\"O .IR typeflag .
210 .I fpath
211 ¤¬¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯¤Ç¡¢¤«¤Ä
212 .BR stat (2)
213 ¤¬¼ºÇÔ¤·¤¿¾ì¹ç¡¢
214 .B FTW_NS
215 ¤È
216 .B FTW_SL
217 (¸å½Ò) ¤Î¤É¤Á¤é¤¬
218 .I typeflag
219 ¤ËÅϤµ¤ì¤ë¤«¤Ï̤ÄêµÁ¤Ç¤¢¤ë¤È¡¢POSIX.1-2001 ¤Ë¤Ï½ñ¤«¤ì¤Æ¤¤¤ë¡£
220 .PP
221 .\"O To stop the tree walk, \fIfn\fP() returns a nonzero value; this
222 .\"O value will become the return value of
223 .\"O .BR ftw ().
224 .\"O As long as \fIfn\fP() returns 0,
225 .\"O .BR ftw ()
226 .\"O will continue either until it has traversed the entire tree,
227 .\"O in which case it will return zero,
228 .\"O or until it encounters an error (such as a
229 .\"O .BR malloc (3)
230 .\"O failure), in which case it will return \-1.
231 ¥Ä¥ê¡¼¤Îõº÷¤ò»ß¤á¤¿¤¤¾ì¹ç¤Ï¡¢
232 .IR fn ()
233 ¤¬ 0 °Ê³°¤ÎÃͤòÊÖ¤»¤ÐÎɤ¤
234 (¤³¤ÎÃͤÏ
235 .BR ftw ()
236 ¼«¿È¤ÎÌá¤êÃͤȤʤë)¡£
237 ¤½¤ì°Ê³°¤Î¾ì¹ç¤Ï
238 .BR ftw ()
239 ¤Ï¥Ä¥ê¡¼Á´ÂΤÎõº÷¤ò³¤±¡¢¤¹¤Ù¤Æ¤Î¥Ä¥ê¡¼¤òõº÷¤·½ª¤¨¤¿¤È¤³¤í¤Ç
240 0 ¤òÊÖ¤¹¡£Ãµº÷Ãæ¤Ë
241 .RB ( malloc (3)
242 ¤Î¼ºÇԤʤɤÎ) ¥¨¥é¡¼¤¬µ¯¤³¤ë¤È \-1 ¤òÊÖ¤¹¡£
243 .PP
244 .\"O Because
245 .\"O .BR ftw ()
246 .\"O uses dynamic data structures, the only safe way to
247 .\"O exit out of a tree walk is to return a nonzero value from \fIfn\fP().
248 .\"O To allow a signal to terminate the walk without causing a memory leak,
249 .\"O have the handler set a global flag that is checked by \fIfn\fP().
250 .\"O \fIDon't\fP use
251 .\"O .BR longjmp (3)
252 .\"O unless the program is going to terminate.
253 .BR ftw ()
254 ¤ÏưŪ¤Ê¥Ç¡¼¥¿¹½Â¤¤òÍѤ¤¤ë¤Î¤Ç¡¢¥Ä¥ê¡¼Ãµº÷¤ò°ÂÁ´¤ËÃæÃǤ¹¤ëÍ£°ì¤ÎÊýË¡¤Ï
255 0 °Ê³°¤ÎÃͤò
256 .IR fn ()
257 ¤ÎÊÖ¤êÃͤȤ¹¤ë¤³¤È¤Ç¤¢¤ë¡£³ä¤ê¹þ¤ß¤ò°·¤¦¤Ë¤Ï¡¢
258 Î㤨¤ÐȯÀ¸¤·¤¿³ä¤ê¹þ¤ß¤ò¥Þ¡¼¥¯¤·¤Æ¤ª¤¤¤Æ¡¢ 0 °Ê³°¤ÎÃͤòÊÖ¤¹¤è¤¦¤Ë¤¹¤ë
259 ¥·¥°¥Ê¥ë¤Ë¤è¤ê¥á¥â¥ê¥ê¡¼¥¯¤òµ¯¤³¤µ¤º¤Ëõº÷¤ò½ªÎ»¤Ç¤­¤ë¤è¤¦¤Ë¤¹¤ë¤Ë¤Ï¡¢
260 ¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ç
261 .IR fn ()
262 ¤¬¥Á¥§¥Ã¥¯¤¹¤ë¥°¥í¡¼¥Ð¥ë¤Ê¥Õ¥é¥°¤ò¥»¥Ã¥È¤¹¤ë¤è¤¦¤Ë¤¹¤ì¤Ð¤è¤¤¡£
263 ¥×¥í¥°¥é¥à¤ò½ªÎ»¤µ¤»¤ë¾ì¹ç°Ê³°¤Ï¡¢
264 .BR longjmp (3)
265 ¤ò»ÈÍѤ·¤Ê¤¤¤³¤È¡£
266 .SS nftw()
267 .\"O The function \fBnftw\fP() is the same as
268 .\"O .BR ftw (),
269 .\"O except that it has one additional argument, \fIflags\fP,
270 .\"O and calls \fIfn\fP() with one more argument, \fIftwbuf\fP.
271 ´Ø¿ô
272 .BR nftw ()
273 ¤Ï
274 .BR ftw ()
275 ¤ÈƱ¤¸¤À¤¬¡¢°ú¤­¿ô
276 .I flags
277 ¤¬Äɲ䵤ì¤ëÅÀ¤È¡¢
278 .IR fn ()
279 ¤Î°ú¤­¿ô¤Ë
280 .I ftwbuf
281 ¤¬Äɲ䵤ì¤ëÅÀ¤¬°Û¤Ê¤ë¡£
282
283 .\"O This \fIflags\fP argument is formed by ORing zero or more of the
284 .\"O following flags:
285 ¤³¤Î
286 .I flags
287 °ú¤­¿ô¤Ï²¼µ­¤Î¥Õ¥é¥°¤Î 0 ¸Ä°Ê¾å¤ÎÏÀÍýϤò¼è¤Ã¤¿¤â¤Î¤Ç¤¢¤ë:
288 .TP
289 .\"O .BR FTW_ACTIONRETVAL " (since glibc 2.3.3)"
290 .BR FTW_ACTIONRETVAL " (glibc 2.3.3 °Ê¹ß)"
291 .\"O If this glibc-specific flag is set, then
292 .\"O .BR nftw ()
293 .\"O handles the return value from
294 .\"O .IR fn ()
295 .\"O differently.
296 .\"O .IR fn ()
297 .\"O should return one of the following values:
298 ¤³¤Î¥Õ¥é¥°¤Ï glibc ¸ÇÍ­¤Ç¤¢¤ë¡£
299 ¤³¤Î¥Õ¥é¥°¤ò¥»¥Ã¥È¤¹¤ë¤È¡¢
300 .BR nftw ()
301 ¤Î
302 .IR fn ()
303 ¤ÎÊÖ¤êÃͤΰ·¤¤¤¬ÊѤï¤ë¡£
304 .IR fn ()
305 ¤Ï°Ê²¼¤ÎÃͤΤ¤¤º¤ì¤«°ì¤Ä¤òÊÖ¤¹É¬Íפ¬¤¢¤ë¡£
306 .RS
307 .TP
308 .B FTW_CONTINUE
309 .\"O Instructs
310 .\"O .BR nftw ()
311 .\"O to continue normally.
312 .BR nftw ()
313 ¤ÏÄ̾ïÄ̤ê½èÍý¤ò³¤±¤ë¡£
314 .TP
315 .B FTW_SKIP_SIBLINGS
316 .\"O If \fIfn\fP() returns this value, then
317 .\"O siblings of the current entry will be skipped,
318 .\"O and processing continues in the parent.
319 .IR fn ()
320 ¤¬¤³¤ÎÃͤòÊÖ¤·¤¿¾ì¹ç¡¢½èÍýÃæ¤Î¥¨¥ó¥È¥ê¤Î·»Äï (Ʊ¤¸³¬ÁؤΥ¨¥ó¥È¥ê)
321 ¤Î½èÍý¤Ï¥¹¥­¥Ã¥×¤µ¤ì¡¢¿Æ¥Ç¥£¥ì¥¯¥È¥ê¤Ç³¤­¤Î½èÍý¤¬¹Ô¤ï¤ì¤ë¡£
322 .\" If \fBFTW_DEPTH\fP
323 .\" is set, the entry's parent directory is processed next (with
324 .\" \fIflag\fP set to \fBFTW_DP\fP).
325 .TP
326 .B FTW_SKIP_SUBTREE
327 .\"O If \fIfn\fP() is called with an entry that is a directory
328 .\"O (\fItypeflag\fP is \fBFTW_D\fP), this return
329 .\"O value will prevent objects within that directory from being passed as
330 .\"O arguments to \fIfn\fP().
331 .\"O .BR nftw ()
332 .\"O continues processing with the next sibling of the directory.
333 .IR fn ()
334 ¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿¥¨¥ó¥È¥ê¤¬¥Ç¥£¥ì¥¯¥È¥ê
335 .RI ( typeflag
336 ¤¬
337 .BR FTW_D )
338 ¤Î¾ì¹ç¤Ë¡¢¤³¤ÎÃͤòÊÖ¤¹¤È
339 .IR fn ()
340 ¤Î°ú¤­¿ô¤È¤·¤ÆÅϤµ¤ì¤¿¥Ç¥£¥ì¥¯¥È¥êÆâ¤Î¥¨¥ó¥È¥ê¤Î½èÍý¤¬¹Ô¤ï¤ì¤Ê¤¯¤Ê¤ë¡£
341 .BR nftw ()
342 ¤Ï½èÍýÃæ¤Î¥Ç¥£¥ì¥¯¥È¥ê¤Î·»Äï (Ʊ¤¸³¬ÁؤΥ¨¥ó¥È¥ê) ¤«¤é½èÍý¤ò³¤±¤ë¡£
343 .TP
344 .B FTW_STOP
345 .\"O Causes
346 .\"O .BR nftw ()
347 .\"O to return immediately with the return value
348 .\"O \fBFTW_STOP\fP.
349 .B nftw ()
350 ¤Ï¡¢ÊÖ¤êÃÍ
351 .B FTW_STOP
352 ¤Ç¤¹¤°¤ËÉüµ¢¤¹¤ë¡£
353 .PP
354 .\"O Other return values could be associated with new actions in the future;
355 .\"O \fIfn\fP() should not return values other than those listed above.
356 ¾¤ÎÊÖ¤êÃͤϾ­Íè¿·¤·¤¤Æ°ºî¤ËÂбþÉÕ¤±¤é¤ì¤ë²ÄǽÀ­¤¬¤¢¤ë¡£
357 .IR fn ()
358 ¤Ï¾åµ­¤Î¥ê¥¹¥È¤Ë¤¢¤ëÃͰʳ°¤òÊÖ¤µ¤Ê¤¤¤è¤¦¤Ë¤¹¤Ù¤­¤Ç¤¢¤ë¡£
359
360 .\"O The feature test macro
361 .\"O .B _GNU_SOURCE
362 .\"O must be defined
363 .\"O (before including
364 .\"O .I any
365 .\"O header files)
366 .\"O in order to
367 .\"O obtain the definition of \fBFTW_ACTIONRETVAL\fP from \fI<ftw.h>\fP.
368 .I <ftw.h>
369 ¤Ç
370 .B FTW_ACTIONRETVAL
371 ¤ÎÄêµÁ¤¬Í­¸ú¤Ë¤¹¤ë¤¿¤á¤Ë¤Ï¡¢
372 (¡Ö¤É¤Î¡×¥Ø¥Ã¥À¥Õ¥¡¥¤¥ë¤ò¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ë¤è¤ê¤âÁ°¤Ë)
373 µ¡Ç½¸¡ºº¥Þ¥¯¥í
374 .B _GNU_SOURCE
375 ¤òÄêµÁ¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
376 .RE
377 .TP
378 .B FTW_CHDIR
379 .\"O If set, do a
380 .\"O .BR chdir (2)
381 .\"O to each directory before handling its contents.
382 .\"O This is useful if the program needs to perform some action
383 .\"O in the directory in which \fIfpath\fP resides.
384 ¥»¥Ã¥È¤µ¤ì¤ë¤È¡¢¥Ç¥£¥ì¥¯¥È¥ê¤ÎÆâÍƤò½èÍý¤¹¤ëÁ°¤Ë
385 ¤½¤Î¥Ç¥£¥ì¥¯¥È¥ê¤Ë
386 .BR chdir (2)
387 ¤¹¤ë¡£¤³¤Î¥Õ¥é¥°¤Ï¡¢
388 .I fpath
389 ¤¬Â°¤¹¥Ç¥£¥ì¥¯¥È¥ê¤Ç²¿¤é¤«¤ÎÆ°ºî¤ò¼Â¹Ô¤¹¤ëɬÍפ¬¤¢¤ë¾ì¹ç¤Ë
390 ÊØÍø¤Ç¤¢¤ë¡£
391 .TP
392 .B FTW_DEPTH
393 .\"O If set, do a post-order traversal, that is, call \fIfn\fP() for
394 .\"O the directory itself \fIafter\fP handling the contents of the directory
395 .\"O and its subdirectories.
396 .\"O (By default, each directory is handled \fIbefore\fP its contents.)
397 ¥»¥Ã¥È¤µ¤ì¤ë¤È¡¢µ¢¤ê¤¬¤±½çõº÷ (post-order traversal) ¤ò¹Ô¤¦¡£
398 ¤Ä¤Þ¤ê¡¢¥Ç¥£¥ì¥¯¥È¥ê¤½¤Î¤â¤Î¤ò°ú¤­¿ô¤È¤·¤¿
399 .IR fn ()
400 ¸Æ½Ð¤·¤Ï¡¢¤½¤Î¥Ç¥£¥ì¥¯¥È¥ê¤Ë´Þ¤Þ¤ì¤ë¥Õ¥¡¥¤¥ë¤È¥µ¥Ö¥Ç¥£¥ì¥¯¥È¥ê¤Ë
401 ÂФ¹¤ë½èÍý¤Î¡Ö¸å¤Ç¡×¹Ô¤ï¤ì¤ë
402 (¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï¡¢¥Ç¥£¥ì¥¯¥È¥ê¼«¿È¤Î½èÍý¤Ï¥Ç¥£¥ì¥¯¥È¥êÆâ¤Î¥¨¥ó¥È¥ê
403 ¤è¤ê¡ÖÁ°¤Ë¡×¹Ô¤Ê¤ï¤ì¤ë)¡£
404 .TP
405 .B FTW_MOUNT
406 .\"O If set, stay within the same file system
407 .\"O (i.e., do not cross mount points).
408 ¥»¥Ã¥È¤µ¤ì¤ë¤È¡¢Æ±¤¸¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à¤ÎÃæ¤À¤±¤òõº÷ÂоݤȤ¹¤ë
409 (¤Ä¤Þ¤ê¡¢¥Þ¥¦¥ó¥È¥Ý¥¤¥ó¥È¤ò¤Þ¤¿¤°¤³¤È¤Ï¤Ê¤¤)¡£
410 .TP
411 .B FTW_PHYS
412 .\"O If set, do not follow symbolic links.
413 .\"O (This is what you want.)
414 .\"O If not set, symbolic links are followed, but no file is reported twice.
415 ¥»¥Ã¥È¤µ¤ì¤ë¤È¡¢¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯¤òé¤é¤Ê¤¤ (¤ª¤½¤é¤¯¤³¤Á¤é¤¬
416 Ä̾ï˾¤Þ¤·¤¤Æ°ºî¤À¤í¤¦)¡£¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤Ê¤¤¤È¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯¤ò
417 é¤ë¤¬¡¢Æ±¤¸¥Õ¥¡¥¤¥ë¤¬Æó²óÊó¹ð¤µ¤ì¤ë¤³¤È¤Ï¤Ê¤¤¡£
418 .sp
419 .\"O If \fBFTW_PHYS\fP is not set, but \fBFTW_DEPTH\fP is set,
420 .\"O then the function
421 .\"O .IR fn ()
422 .\"O is never called for a directory that would be a descendant of itself.
423 .B FTW_PHYS
424 ¤¬¥»¥Ã¥È¤µ¤ì¤º¤Ë
425 .B FTW_DEPTH
426 ¤¬¥»¥Ã¥È¤µ¤ì¤ë¤È¡¢¼«Ê¬¼«¿È¤ËÂФ¹¤ë¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯¤òÇÛ²¼¤Ë»ý¤Ä
427 ¥Ç¥£¥ì¥¯¥È¥ê¤ËÂФ·¤Æ
428 .IR fn ()
429 ¤¬¸Æ¤Ó½Ð¤µ¤ì¤ë¤³¤È¤Ï·è¤·¤Æ¤Ê¤¤¡£
430 .LP
431 .\"O For each entry in the directory tree,
432 .\"O .BR nftw ()
433 .\"O calls
434 .\"O .IR fn ()
435 .\"O with four arguments.
436 .\"O .I fpath
437 .\"O and
438 .\"O .I sb
439 .\"O are as for
440 .\"O .BR ftw ().
441 .\"O .I typeflag
442 .\"O may receive any of the same values as with
443 .\"O .BR ftw (),
444 .\"O or any of the following values:
445 ¥Ç¥£¥ì¥¯¥È¥ê¥Ä¥ê¡¼¤Î¥¨¥ó¥È¥êËè¤Ë¡¢
446 .BR nftw ()
447 ¤Ï 4¤Ä¤Î°ú¤­¿ô¤Ç
448 .IR fn ()
449 ¤ò¸Æ¤Ó½Ð¤¹¡£
450 .I fpath
451 ¤È
452 .I sb
453 ¤Ï
454 .BR ftw ()
455 ¤ÈƱ¤¸¤Ç¤¢¤ë¡£
456 .I typeflag
457 ¤Ë¤Ï¡¢
458 .BR ftw ()
459 ¤Ç¼è¤êÆÀ¤ëÃͤΤ¤¤º¤ì¤«¡¢¤Þ¤¿¤Ï°Ê²¼¤ÎÃͤΤ¤¤º¤ì¤«¤¬ÅϤµ¤ì¤ë:
460 .TP
461 .B FTW_DP
462 .\"O .I fpath
463 .\"O is a directory, and \fBFTW_DEPTH\fP was specified in \fIflags\fP.
464 .\"O All of the files
465 .\"O and subdirectories within \fIfpath\fP have been processed.
466 .I fpath
467 ¤¬¥Ç¥£¥ì¥¯¥È¥ê¤Ç¡¢¤«¤Ä
468 .I flags
469 ¤Ç
470 .B FTW_DEPTH
471 ¤¬»ØÄꤵ¤ì¤Æ¤¤¤¿¡£
472 .I fpath
473 ÇÛ²¼¤Î¥Õ¥¡¥¤¥ë¤È¥µ¥Ö¥Ç¥£¥ì¥¯¥È¥ê¤ÏÁ´¤Æ½èÍý¤¬½ª¤ï¤Ã¤Æ¤¤¤ë¡£
474 .TP
475 .B FTW_SL
476 .\"O .I fpath
477 .\"O is a symbolic link, and \fBFTW_PHYS\fP was set in \fIflags\fP.
478 .I fpath
479 ¤¬¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯¤Ç¡¢¤«¤Ä \fBFTW_PHYS\fP ¤¬ \fIflags\fP ¤Ë
480 ¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤¿¡£
481 .\" To obtain the definition of this constant from
482 .\" .IR <ftw.h> ,
483 .\" either
484 .\" .B _BSD_SOURCE
485 .\" must be defined, or
486 .\" .BR _XOPEN_SOURCE
487 .\" must be defined with a value of 500 or more.
488 .TP
489 .B FTW_SLN
490 .\"O .I fpath
491 .\"O is a symbolic link pointing to a nonexistent file.
492 .\"O (This occurs only if \fBFTW_PHYS\fP is not set.)
493 .I fpath
494 ¤¬¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯¤Ç¡¢Â¸ºß¤·¤Ê¤¤¥Õ¥¡¥¤¥ë¤ò»Ø¤·¤Æ¤¤¤ë
495 (¤³¤ì¤¬¥»¥Ã¥È¤µ¤ì¤ë¤Î¤Ï
496 .B FTW_PHYS
497 ¤¬¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤À¤±¤Ç¤¢¤ë)¡£
498 .LP
499 .\"O The fourth argument that
500 .\"O .BR nftw ()
501 .\"O supplies when calling
502 .\"O \fIfn\fP()
503 .\"O is a structure of type \fIFTW\fP:
504 .BR nftw ()
505 ¤¬
506 .IR fn ()
507 ¤ò¸Æ¤Ó½Ð¤¹ºÝ¤ËÅϤ¹ 4¤Ä¤á¤Î°ú¤­¿ô¤Ï
508 .I FTW
509 ·¿¤Î¹½Â¤ÂΤǤ¢¤ë¡£
510 .in +4n
511 .nf
512
513 struct FTW {
514     int base;
515     int level;
516 };
517
518 .fi
519 .in
520 .\"O .I base
521 .\"O is the offset of the filename (i.e., basename component)
522 .\"O in the pathname given in
523 .\"O .IR fpath .
524 .\"O .I level
525 .\"O is the depth of
526 .\"O .I fpath
527 .\"O in the directory tree, relative to the root of the tree
528 .\"O .RI ( dirpath ,
529 .\"O which has depth 0).
530 .I base
531 ¤Ï¡¢¥Õ¥¡¥¤¥ë̾ (basename Í×ÁÇ) ¤Î¡¢
532 .I fpath
533 ¤ÇÅϤµ¤ì¤ë¥Ñ¥¹Ì¾¤ÎÃæ¤Ç¤Î¥ª¥Õ¥»¥Ã¥È¤Ç¤¢¤ë¡£
534 .I level
535 ¤Ï¥Ç¥£¥ì¥¯¥È¥ê¥Ä¥ê¡¼¤Ç¤Î
536 .I fpath
537 ¤Î¿¼¤µ¤ò¼¨¤¹¡£¿¼¤µ¤Ï¥Ç¥£¥ì¥¯¥È¥ê¥Ä¥ê¡¼¤Î¥È¥Ã¥× (root) ¤«¤é¤Î
538 ÁêÂÐÃͤǤ¢¤ë
539 .RI ( dirpath
540 ¤Ï¿¼¤µ 0 ¤Ç¤¢¤ë)¡£
541 .\"O .SH "RETURN VALUE"
542 .SH ÊÖ¤êÃÍ
543 .\"O These functions return 0 on success, and \-1 if an error occurs.
544 ¤³¤ì¤é¤Î´Ø¿ô¤Ï¡¢À®¸ù¤¹¤ë¤È 0 ¤ò¡¢¥¨¥é¡¼¤¬È¯À¸¤¹¤ë¤È \-1 ¤òÊÖ¤¹¡£
545
546 .\"O If \fIfn\fP() returns nonzero,
547 .\"O then the tree walk is terminated and the value returned by \fIfn\fP()
548 .\"O is returned as the result of \fBftw\fP() or
549 .\"O .BR nftw ().
550 .IR fn ()
551 ¤¬ 0 °Ê³°¤òÊÖ¤·¤¿¾ì¹ç¡¢¥Ç¥£¥ì¥¯¥È¥ê¥Ä¥ê¡¼¤Îõº÷¤ò½ªÎ»¤·¡¢
552 .IR fn ()
553 ¤¬ÊÖ¤·¤¿Ãͤò
554 .BR ftw ()
555 ¤ä
556 .BR nftw ()
557 ¤Î·ë²Ì¤È¤·¤ÆÊÖ¤¹¡£
558
559 .\"O If
560 .\"O .BR nftw ()
561 .\"O is called with the \fBFTW_ACTIONRETVAL\fP flag,
562 .\"O then the only nonzero value that should be used by \fIfn\fP()
563 .\"O to terminate the tree walk is \fBFTW_STOP\fP,
564 .\"O and that value is returned as the result of
565 .\"O .BR nftw ().
566 .BR nftw ()
567 ¤¬
568 .B FTW_ACTIONRETVAL
569 ¥Õ¥é¥°ÉÕ¤­¤Ç¸Æ¤Ð¤ì¤¿¾ì¹ç¡¢¥Ä¥ê¡¼¤Îõº÷¤ò½ªÎ»¤µ¤»¤ë¤¿¤á¤Ë
570 .IR fn ()
571 ¤¬»ÈÍѤǤ­¤ë¡¢Èó 0 ¤ÎÃͤÏ
572 .B FTW_STOP
573 ¤À¤±¤Ç¤¢¤ê¡¢
574 ¤³¤ÎÃͤÏ
575 .BR nftw ()
576 ¤ÎÊÖ¤êÃͤȤ·¤ÆÊÖ¤µ¤ì¤ë¡£
577 .\"O .SH "CONFORMING TO"
578 .SH ½àµò
579 POSIX.1-2001, SVr4, SUSv1.
580 .\"O POSIX.1-2008 marks
581 .\"O .BR ftw ()
582 .\"O as obsolete.
583 POSIX.1-2008 ¤Ï
584 .BR ftw ()
585 ¤òÇÑ»ßͽÄê¤È¤·¤Æ¤¤¤ë¡£
586 .\"O .SH NOTES
587 .SH Ãí°Õ
588 .\"O POSIX.1-2001 note that the results are unspecified if
589 .\"O .I fn
590 .\"O does not preserve the current working directory.
591 POSIX.1-2001 ¤ÎÃíµ­¤Ë¤è¤ë¤È¡¢
592 .I fn
593 ¤¬¥«¥ì¥ó¥È¥ï¡¼¥­¥ó¥°¥Ç¥£¥ì¥¯¥È¥ê¤òÊÝ»ý¤·¤Ê¤«¤Ã¤¿¾ì¹ç¤Î
594 ·ë²Ì¤Ïµ¬Äꤵ¤ì¤Æ¤¤¤Ê¤¤¤È¤µ¤ì¤Æ¤¤¤ë¡£
595 .PP
596 .\"O The function
597 .\"O .BR nftw ()
598 .\"O and the use of \fBFTW_SL\fP with
599 .\"O .BR ftw ()
600 .\"O were introduced in SUSv1.
601 .BR nftw ()
602 ´Ø¿ô¤È¡¢
603 .BR ftw ()
604 ¤Ë¤ª¤±¤ë
605 .B FTW_SL
606 ¤Ï¡¢SUSv1 ¤ÇƳÆþ¤µ¤ì¤¿¡£
607 .LP
608 .\"O On some systems
609 .\"O .BR ftw ()
610 .\"O will never use \fBFTW_SL\fP, on other systems \fBFTW_SL\fP occurs only
611 .\"O for symbolic links that do not point to an existing file,
612 .\"O and again on other systems
613 .\"O .BR ftw ()
614 .\"O will use \fBFTW_SL\fP for each symbolic link.
615 .\"O For predictable control, use
616 .\"O .BR nftw ().
617 .BR ftw ()
618 ¤Ç
619 .B FTW_SL
620 ¤ò°ìÀڻȤï¤Ê¤¤¥·¥¹¥Æ¥à¤ä¡¢
621 ¸ºß¤·¤Ê¤¤¥Õ¥¡¥¤¥ë¤ò»Ø¤·¤Æ¤¤¤ë¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯¤Î¾ì¹ç¤Ë¤Î¤ß
622 .B FTW_SL
623 ¤ò»È¤¦¥·¥¹¥Æ¥à¡¢¤Þ¤¿
624 .BR ftw ()
625 ¤¬Á´¤Æ¤Î¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯¤ËÂФ·¤Æ
626 .B FTW_SL
627 ¤ò»È¤¦¥·¥¹¥Æ¥à¤â¤¢¤ë¡£
628 ͽ¬²Äǽ¤ÊÆ°ºî¤ò¤µ¤»¤ë¤¿¤á¤Ë¤Ï¡¢
629 .BR nftw ()
630 ¤ò»È¤¦¤³¤È¡£
631 .LP
632 .\"O Under Linux, libc4 and libc5 and glibc 2.0.6 will
633 .\"O use \fBFTW_F\fP for all objects (files, symbolic links, FIFOs, etc.)
634 .\"O that can be stat'ed but are not a directory.
635 Linux ¤Ç¤Ï¡¢ libc4, libc5, glibc 2.0.6 ¤Ï
636 ¡Östat ¤Ç¤­¤ë¤¬¥Ç¥£¥ì¥¯¥È¥ê¤Ç¤Ï¤Ê¤¤¥ª¥Ö¥¸¥§¥¯¥È¡×
637 (¥Õ¥¡¥¤¥ë, ¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯, fifo Åù)
638 ¤ËÂФ·¤Æ¤Ï¤¹¤Ù¤Æ
639 .B FTW_F
640 ¤ò»È¤¦¡£
641
642 .\"O The function
643 .\"O .BR nftw ()
644 .\"O is available since glibc 2.1.
645 .BR nftw ()
646 ´Ø¿ô¤Ï glibc 2.1 °Ê¹ß¤ÇÍøÍѤǤ­¤ë¡£
647
648 .\"O \fBFTW_ACTIONRETVAL\fP is glibc-specific.
649 .B FTW_ACTIONRETVAL
650 ¤Ï glibc ¸ÇÍ­¤Ç¤¢¤ë¡£
651 .\"O .SH EXAMPLE
652 .SH Îã
653 .\"O The following program traverses the directory tree under the path named
654 .\"O in its first command-line argument, or under the current directory
655 .\"O if no argument is supplied.
656 .\"O It displays various information about each file.
657 .\"O The second command-line argument can be used to specify characters that
658 .\"O control the value assigned to the \fIflags\fP
659 .\"O argument when calling
660 .\"O .BR nftw ().
661 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ï¡¢°ì¤ÄÌܤΥ³¥Þ¥ó¥É¥é¥¤¥ó°ú¤­¿ô¤ò̾Á°¤Ë»ý¤Ä¥Ñ¥¹°Ê²¼¤Î
662 ¥Ç¥£¥ì¥¯¥È¥ê¥Ä¥ê¡¼¤òõº÷¤¹¤ë¡£°ú¤­¿ô¤¬»ØÄꤵ¤ì¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï¡¢
663 ¥«¥ì¥ó¥È¥Ç¥£¥ì¥¯¥È¥ê°Ê²¼¤òõº÷¤¹¤ë¡£
664 ³Æ¡¹¤Î¥Õ¥¡¥¤¥ë¤Ë¤Ä¤¤¤ÆÍÍ¡¹¤Î¾ðÊó¤¬É½¼¨¤µ¤ì¤ë¡£
665 ÆóÈÖÌܤΥ³¥Þ¥ó¥É¥é¥¤¥ó°ú¤­¿ô¤Ëʸ»ú¤ò»ØÄꤹ¤ë¤³¤È¤Ç¡¢
666 .BR nftw ()
667 ¤ò¸Æ¤Ó½Ð¤¹ºÝ¤Ë
668 .I flags
669 °ú¤­¿ô¤ËÅϤ¹ÃͤòÀ©¸æ¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
670 .nf
671
672 #define _XOPEN_SOURCE 500
673 #include <ftw.h>
674 #include <stdio.h>
675 #include <stdlib.h>
676 #include <string.h>
677 #include <stdint.h>
678
679 static int
680 display_info(const char *fpath, const struct stat *sb,
681              int tflag, struct FTW *ftwbuf)
682 {
683     printf("%\-3s %2d %7jd   %\-40s %d %s\\n",
684         (tflag == FTW_D) ?   "d"   : (tflag == FTW_DNR) ? "dnr" :
685         (tflag == FTW_DP) ?  "dp"  : (tflag == FTW_F) ?   "f" :
686         (tflag == FTW_NS) ?  "ns"  : (tflag == FTW_SL) ?  "sl" :
687         (tflag == FTW_SLN) ? "sln" : "???",
688         ftwbuf\->level, (intmax_t) sb\->st_size,
689         fpath, ftwbuf\->base, fpath + ftwbuf\->base);
690     return 0;           /* To tell nftw() to continue */
691 }
692
693 int
694 main(int argc, char *argv[])
695 {
696     int flags = 0;
697
698     if (argc > 2 && strchr(argv[2], \(aqd\(aq) != NULL)
699         flags |= FTW_DEPTH;
700     if (argc > 2 && strchr(argv[2], \(aqp\(aq) != NULL)
701         flags |= FTW_PHYS;
702
703     if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags)
704             == \-1) {
705         perror("nftw");
706         exit(EXIT_FAILURE);
707     }
708     exit(EXIT_SUCCESS);
709 }
710 .fi
711 .\"O .SH "SEE ALSO"
712 .SH ´ØÏ¢¹àÌÜ
713 .BR stat (2),
714 .BR fts (3),
715 .BR readdir (3),
716 .BR feature_test_macros (7)