<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>build/myftw.c Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.2.17 --> <center> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="modules.html">Modules</a> <a class="qindex" href="annotated.html">Data Structures</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Data Fields</a> <a class="qindex" href="globals.html">Globals</a> <a class="qindex" href="pages.html">Related Pages</a> </center> <hr><h1>build/myftw.c</h1><a href="myftw_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 00006 <span class="comment">/* Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc.</span> 00007 <span class="comment">This file is part of the GNU C Library.</span> 00008 <span class="comment">Contributed by Ian Lance Taylor (ian@airs.com).</span> 00009 <span class="comment"></span> 00010 <span class="comment">The GNU C Library is free software; you can redistribute it and/or</span> 00011 <span class="comment">modify it under the terms of the GNU Library General Public License as</span> 00012 <span class="comment">published by the Free Software Foundation; either version 2 of the</span> 00013 <span class="comment">License, or (at your option) any later version.</span> 00014 <span class="comment"></span> 00015 <span class="comment">The GNU C Library is distributed in the hope that it will be useful,</span> 00016 <span class="comment">but WITHOUT ANY WARRANTY; without even the implied warranty of</span> 00017 <span class="comment">MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span> 00018 <span class="comment">Library General Public License for more details.</span> 00019 <span class="comment"></span> 00020 <span class="comment">You should have received a copy of the GNU Library General Public</span> 00021 <span class="comment">License along with the GNU C Library; see the file COPYING.LIB. If</span> 00022 <span class="comment">not, write to the Free Software Foundation, Inc., 675 Mass Ave,</span> 00023 <span class="comment">Cambridge, MA 02139, USA. */</span> 00024 00025 <span class="preprocessor">#include "<a class="code" href="system_8h.html">system.h</a>"</span> 00026 00027 <span class="preprocessor">#include <<a class="code" href="rpmio_8h.html">rpmio.h</a>></span> 00028 00029 <span class="preprocessor">#ifndef NAMLEN</span> <a name="l00030"></a><a class="code" href="myftw_8c.html#a0">00030</a> <span class="preprocessor"></span><span class="preprocessor">#define NAMLEN(a) strlen((a)->d_name)</span> 00031 <span class="preprocessor"></span><span class="preprocessor">#endif</span> 00032 <span class="preprocessor"></span> 00033 <span class="preprocessor">#if !defined(__LCLINT__)</span> 00034 <span class="preprocessor"></span><span class="preprocessor">#ifndef PATH_MAX</span> 00035 <span class="preprocessor"></span><span class="preprocessor">#ifdef _POSIX_VERSION</span> 00036 <span class="preprocessor"></span><span class="preprocessor">#define PATH_MAX _POSIX_PATH_MAX</span> 00037 <span class="preprocessor"></span><span class="preprocessor">#else</span> 00038 <span class="preprocessor"></span><span class="preprocessor">#ifdef MAXPATHLEN</span> 00039 <span class="preprocessor"></span><span class="preprocessor">#define PATH_MAX MAXPATHLEN</span> 00040 <span class="preprocessor"></span><span class="preprocessor">#else</span> <a name="l00041"></a><a class="code" href="myftw_8c.html#a1">00041</a> <span class="preprocessor"></span><span class="preprocessor">#define PATH_MAX 1024</span> 00042 <span class="preprocessor"></span><span class="preprocessor">#endif</span> 00043 <span class="preprocessor"></span><span class="preprocessor">#endif</span> 00044 <span class="preprocessor"></span><span class="preprocessor">#endif</span> 00045 <span class="preprocessor"></span><span class="preprocessor">#endif </span><span class="comment">/* !__LCLINT */</span> 00046 00047 <span class="preprocessor">#include "<a class="code" href="myftw_8h.html">myftw.h</a>"</span> 00048 <span class="preprocessor">#include "<a class="code" href="debug_8h.html">debug.h</a>"</span> 00049 00050 <span class="comment">/* Traverse one level of a directory tree. */</span> 00051 00054 <span class="comment">/*@-nullstate@*/</span> 00055 <span class="keyword">static</span> <span class="keywordtype">int</span> <a name="l00056"></a><a class="code" href="myftw_8c.html#a2">00056</a> <a class="code" href="myftw_8c.html#a2">myftw_dir</a> (DIR **dirs, <span class="keywordtype">int</span> level, <span class="keywordtype">int</span> descriptors, 00057 <span class="keywordtype">char</span> *dir, size_t len, 00058 <a class="code" href="myftw_8h.html#a4">myftwFunc</a> func, 00059 <span class="keywordtype">void</span> *fl) 00060 <span class="comment">/*@globals errno, fileSystem @*/</span> 00061 <span class="comment">/*@modifies *dirs, *dir, errno, fileSystem @*/</span> 00062 { 00063 <span class="keywordtype">int</span> got; 00064 <span class="keyword">struct </span><a class="code" href="system_8h.html#a11">dirent</a> *entry; 00065 <span class="keywordtype">int</span> d_namlen; 00066 00067 got = 0; 00068 00069 <a class="code" href="system_8h.html#a29">errno</a> = 0; 00070 00071 <span class="keywordflow">while</span> ((entry = <a class="code" href="rpmrpc_8c.html#a52">Readdir</a> (dirs[level])) != NULL) 00072 { 00073 <span class="keyword">struct </span>stat s; 00074 <span class="keywordtype">int</span> flag, retval, newlev = 0; 00075 00076 ++got; 00077 00078 <span class="keywordflow">if</span> (entry->d_name[0] == <span class="charliteral">'.'</span> 00079 && (entry->d_name [1] == <span class="charliteral">'\0'</span> || 00080 (entry->d_name [2] == <span class="charliteral">'\0'</span> && entry->d_name[1] == <span class="charliteral">'.'</span>))) 00081 { 00082 <a class="code" href="system_8h.html#a29">errno</a> = 0; 00083 <span class="keywordflow">continue</span>; 00084 } 00085 00086 d_namlen = <a class="code" href="myftw_8c.html#a0">NAMLEN</a>(entry) + 1; 00087 <span class="keywordflow">if</span> (d_namlen + len > <a class="code" href="myftw_8c.html#a1">PATH_MAX</a>) 00088 { 00089 <span class="preprocessor">#ifdef ENAMETOOLONG</span> 00090 <span class="preprocessor"></span> <a class="code" href="system_8h.html#a29">errno</a> = ENAMETOOLONG; 00091 <span class="preprocessor">#else</span> 00092 <span class="preprocessor"></span> <a class="code" href="system_8h.html#a29">errno</a> = ENOMEM; 00093 <span class="preprocessor">#endif</span> 00094 <span class="preprocessor"></span> <span class="keywordflow">return</span> -1; 00095 } 00096 00097 dir[len] = <span class="charliteral">'/'</span>; 00098 memcpy ((<span class="keywordtype">void</span> *) (dir + len + 1), (<span class="keywordtype">void</span> *) entry->d_name, d_namlen); 00099 00100 <span class="keywordflow">if</span> (<a class="code" href="rpmrpc_8c.html#a46">Lstat</a> (dir, &s) < 0) 00101 { 00102 <span class="comment">/* Following POSIX.1 2.4 ENOENT is returned if the file cannot</span> 00103 <span class="comment"> * be stat'ed. This can happen for a file returned by Readdir</span> 00104 <span class="comment"> * if it's an unresolved symbolic link. This should be regarded</span> 00105 <span class="comment"> * as an forgivable error. -- Uli. */</span> 00106 <span class="keywordflow">if</span> (<a class="code" href="system_8h.html#a29">errno</a> != EACCES && <a class="code" href="system_8h.html#a29">errno</a> != ENOENT) 00107 <span class="keywordflow">return</span> -1; 00108 flag = <a class="code" href="myftw_8h.html#a3">MYFTW_NS</a>; 00109 } 00110 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (S_ISDIR (s.st_mode)) 00111 { 00112 newlev = (level + 1) % descriptors; 00113 00114 <span class="comment">/*@-unqualifiedtrans@*/</span> 00115 <span class="keywordflow">if</span> (dirs[newlev] != NULL) 00116 (void) <a class="code" href="rpmrpc_8c.html#a53">Closedir</a> (dirs[newlev]); 00117 <span class="comment">/*@=unqualifiedtrans@*/</span> 00118 00119 dirs[newlev] = <a class="code" href="rpmrpc_8c.html#a51">Opendir</a> (dir); 00120 <span class="keywordflow">if</span> (dirs[newlev] != NULL) 00121 flag = <a class="code" href="myftw_8h.html#a1">MYFTW_D</a>; 00122 <span class="keywordflow">else</span> 00123 { 00124 <span class="keywordflow">if</span> (<a class="code" href="system_8h.html#a29">errno</a> != EACCES) 00125 <span class="keywordflow">return</span> -1; 00126 flag = <a class="code" href="myftw_8h.html#a2">MYFTW_DNR</a>; 00127 } 00128 } 00129 <span class="keywordflow">else</span> 00130 flag = <a class="code" href="myftw_8h.html#a0">MYFTW_F</a>; 00131 00132 retval = (*func) (fl, dir, &s); 00133 00134 <span class="keywordflow">if</span> (flag == <a class="code" href="myftw_8h.html#a1">MYFTW_D</a>) 00135 { 00136 <span class="keywordflow">if</span> (retval == 0) 00137 retval = <a class="code" href="myftw_8c.html#a2">myftw_dir</a> (dirs, newlev, descriptors, dir, 00138 d_namlen + len, func, fl); 00139 <span class="keywordflow">if</span> (dirs[newlev] != NULL) 00140 { 00141 <span class="keywordtype">int</span> save; 00142 00143 save = <a class="code" href="system_8h.html#a29">errno</a>; 00144 (void) <a class="code" href="rpmrpc_8c.html#a53">Closedir</a> (dirs[newlev]); 00145 <a class="code" href="system_8h.html#a29">errno</a> = save; 00146 dirs[newlev] = NULL; 00147 } 00148 } 00149 00150 <span class="keywordflow">if</span> (retval != 0) 00151 <span class="keywordflow">return</span> retval; 00152 00153 <span class="keywordflow">if</span> (dirs[level] == NULL) 00154 { 00155 <span class="keywordtype">int</span> skip; 00156 00157 dir[len] = <span class="charliteral">'\0'</span>; 00158 dirs[level] = <a class="code" href="rpmrpc_8c.html#a51">Opendir</a> (dir); 00159 <span class="keywordflow">if</span> (dirs[level] == NULL) 00160 <span class="keywordflow">return</span> -1; 00161 skip = got; 00162 <span class="keywordflow">while</span> (skip-- != 0) 00163 { 00164 <a class="code" href="system_8h.html#a29">errno</a> = 0; 00165 <span class="keywordflow">if</span> (<a class="code" href="rpmrpc_8c.html#a52">Readdir</a> (dirs[level]) == NULL) 00166 <span class="keywordflow">return</span> <a class="code" href="system_8h.html#a29">errno</a> == 0 ? 0 : -1; 00167 } 00168 } 00169 00170 <a class="code" href="system_8h.html#a29">errno</a> = 0; 00171 } 00172 00173 <span class="keywordflow">return</span> <a class="code" href="system_8h.html#a29">errno</a> == 0 ? 0 : -1; 00174 } 00175 <span class="comment">/*@=nullstate@*/</span> 00176 00177 <span class="comment">/* Call a function on every element in a directory tree. */</span> 00178 <a name="l00179"></a><a class="code" href="myftw_8c.html#a3">00179</a> <span class="keywordtype">int</span> <a class="code" href="myftw_8c.html#a3">myftw</a> (<span class="keyword">const</span> <span class="keywordtype">char</span> *dir, 00180 <span class="keywordtype">int</span> descriptors, 00181 <a class="code" href="myftw_8h.html#a4">myftwFunc</a> func, 00182 <span class="keywordtype">void</span> *fl) 00183 { 00184 DIR **dirs; 00185 size_t len; 00186 <span class="keywordtype">char</span> buf[<a class="code" href="myftw_8c.html#a1">PATH_MAX</a> + 1]; 00187 <span class="keyword">struct </span>stat s; 00188 <span class="keywordtype">int</span> flag, retval; 00189 <span class="keywordtype">int</span> i; 00190 00191 <span class="keywordflow">if</span> (descriptors <= 0) 00192 descriptors = 1; 00193 00194 <span class="comment">/*@access DIR@*/</span> 00195 dirs = (DIR **) <a class="code" href="system_8h.html#a36">alloca</a> (descriptors * <span class="keyword">sizeof</span> (*dirs)); 00196 i = descriptors; 00197 <span class="keywordflow">while</span> (i-- > 0) 00198 dirs[i] = NULL; 00199 00200 <span class="keywordflow">if</span> (<a class="code" href="rpmrpc_8c.html#a46">Lstat</a> (dir, &s) < 0) 00201 { 00202 <span class="comment">/* Following POSIX.1 2.4 ENOENT is returned if the file cannot</span> 00203 <span class="comment"> * be stat'ed. This can happen for a file returned by Readdir</span> 00204 <span class="comment"> * if it's an unresolved symbolic link. This should be regarded</span> 00205 <span class="comment"> * as an forgivable error. -- Uli. */</span> 00206 <span class="keywordflow">if</span> (<a class="code" href="system_8h.html#a29">errno</a> != EACCES && <a class="code" href="system_8h.html#a29">errno</a> != ENOENT) 00207 <span class="keywordflow">return</span> -1; 00208 flag = <a class="code" href="myftw_8h.html#a3">MYFTW_NS</a>; 00209 } 00210 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (S_ISDIR (s.st_mode)) 00211 { 00212 dirs[0] = <a class="code" href="rpmrpc_8c.html#a51">Opendir</a> (dir); 00213 <span class="keywordflow">if</span> (dirs[0] != NULL) 00214 flag = <a class="code" href="myftw_8h.html#a1">MYFTW_D</a>; 00215 <span class="keywordflow">else</span> 00216 { 00217 <span class="keywordflow">if</span> (<a class="code" href="system_8h.html#a29">errno</a> != EACCES) 00218 <span class="keywordflow">return</span> -1; 00219 flag = <a class="code" href="myftw_8h.html#a2">MYFTW_DNR</a>; 00220 } 00221 } 00222 <span class="keywordflow">else</span> 00223 flag = <a class="code" href="myftw_8h.html#a0">MYFTW_F</a>; 00224 00225 len = strlen (dir); 00226 memcpy ((<span class="keywordtype">void</span> *) buf, (<span class="keywordtype">void</span> *) dir, len + 1); 00227 00228 retval = (*func) (fl, buf, &s); 00229 00230 <span class="keywordflow">if</span> (flag == <a class="code" href="myftw_8h.html#a1">MYFTW_D</a>) 00231 { 00232 <span class="keywordflow">if</span> (retval == 0) 00233 retval = <a class="code" href="myftw_8c.html#a2">myftw_dir</a> (dirs, 0, descriptors, buf, len, func, fl); 00234 <span class="keywordflow">if</span> (dirs[0] != NULL) 00235 { 00236 <span class="keywordtype">int</span> save; 00237 00238 save = <a class="code" href="system_8h.html#a29">errno</a>; 00239 (void) <a class="code" href="rpmrpc_8c.html#a53">Closedir</a> (dirs[0]); 00240 <span class="comment">/*@-mods@*/</span> 00241 <a class="code" href="system_8h.html#a29">errno</a> = save; 00242 <span class="comment">/*@=mods@*/</span> 00243 } 00244 } 00245 00246 <span class="keywordflow">return</span> retval; 00247 } </pre></div><hr><address style="align: right;"><small>Generated on Thu Sep 12 22:14:55 2002 for rpm by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.17 </small></address> </body> </html>