aboutsummaryrefslogblamecommitdiffstats
path: root/doc/api-documentation/html/dirent_8cpp-source.html
blob: 415026e685c6d5c82bcdd054ad4bf01adc8281b3 (plain) (tree)
















































































































































                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
<!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>dirent.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="classes.html">Alphabetical List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; </center>
<hr><h1>dirent.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/*</font>
00002 <font class="comment"></font>
00003 <font class="comment">    Implementation of POSIX directory browsing functions and types for Win32.</font>
00004 <font class="comment"></font>
00005 <font class="comment">    Kevlin Henney (mailto:kevlin@acm.org), March 1997.</font>
00006 <font class="comment"></font>
00007 <font class="comment">    Copyright Kevlin Henney, 1997. All rights reserved.</font>
00008 <font class="comment"></font>
00009 <font class="comment">    Permission to use, copy, modify, and distribute this software and its</font>
00010 <font class="comment">    documentation for any purpose is hereby granted without fee, provided</font>
00011 <font class="comment">    that this copyright and permissions notice appear in all copies and</font>
00012 <font class="comment">    derivatives, and that no charge may be made for the software and its</font>
00013 <font class="comment">    documentation except to cover cost of distribution.</font>
00014 <font class="comment">    </font>
00015 <font class="comment">    This software is supplied "as is" without express or implied warranty.</font>
00016 <font class="comment"></font>
00017 <font class="comment">    But that said, if there are any problems please get in touch.</font>
00018 <font class="comment"></font>
00019 <font class="comment">*/</font>
00020 
00021 <font class="preprocessor">#include &lt;dirent.h&gt;</font>
00022 <font class="preprocessor">#include &lt;errno.h&gt;</font>
00023 <font class="preprocessor">#include &lt;io.h&gt;</font>
00024 <font class="preprocessor">#include &lt;stdlib.h&gt;</font>
00025 <font class="preprocessor">#include &lt;string.h&gt;</font>
00026 
00027 <font class="keyword">struct </font>DIR
00028 {
00029     <font class="keywordtype">long</font>                handle; <font class="comment">/* -1 for failed rewind */</font>
00030     <font class="keyword">struct </font>_finddata_t  info;
00031     <font class="keyword">struct </font>dirent       result; <font class="comment">/* d_name null iff first time */</font>
00032     <font class="keywordtype">char</font>                *name;  <font class="comment">/* NTBS */</font>
00033 };
00034 
00035 DIR *opendir(<font class="keyword">const</font> <font class="keywordtype">char</font> *name)
00036 {
00037     DIR *dir = 0;
00038 
00039     <font class="keywordflow">if</font>(name &amp;&amp; name[0])
00040     {
00041         size_t base_length = strlen(name);
00042         <font class="keyword">const</font> <font class="keywordtype">char</font> *all = <font class="comment">/* the root directory is a special case... */</font>
00043             strchr(<font class="stringliteral">"/\\"</font>, name[base_length - 1]) ? <font class="stringliteral">"*"</font> : <font class="stringliteral">"/*"</font>;
00044 
00045         <font class="keywordflow">if</font>((dir = (DIR *) malloc(<font class="keyword">sizeof</font> *dir)) != 0 &amp;&amp;
00046            (dir-&gt;name = (<font class="keywordtype">char</font> *) malloc(base_length + strlen(all) + 1)) != 0)
00047         {
00048             strcat(strcpy(dir-&gt;name, name), all);
00049 
00050             <font class="keywordflow">if</font>((dir-&gt;handle = _findfirst(dir-&gt;name, &amp;dir-&gt;info)) != -1)
00051             {
00052                 dir-&gt;result.d_name = 0;
00053             }
00054             <font class="keywordflow">else</font> <font class="comment">/* rollback */</font>
00055             {
00056                 free(dir-&gt;name);
00057                 free(dir);
00058                 dir = 0;
00059             }
00060         }
00061         <font class="keywordflow">else</font> <font class="comment">/* rollback */</font>
00062         {
00063             free(dir);
00064             dir   = 0;
00065             errno = ENOMEM;
00066         }
00067     }
00068     <font class="keywordflow">else</font>
00069     {
00070         errno = EINVAL;
00071     }
00072 
00073     <font class="keywordflow">return</font> dir;
00074 }
00075 
00076 <font class="keywordtype">int</font> closedir(DIR *dir)
00077 {
00078     <font class="keywordtype">int</font> result = -1;
00079 
00080     <font class="keywordflow">if</font>(dir)
00081     {
00082         <font class="keywordflow">if</font>(dir-&gt;handle != -1)
00083         {
00084             result = _findclose(dir-&gt;handle);
00085         }
00086 
00087         free(dir-&gt;name);
00088         free(dir);
00089     }
00090 
00091     <font class="keywordflow">if</font>(result == -1) <font class="comment">/* map all errors to EBADF */</font>
00092     {
00093         errno = EBADF;
00094     }
00095 
00096     <font class="keywordflow">return</font> result;
00097 }
00098 
00099 <font class="keyword">struct </font>dirent *readdir(DIR *dir)
00100 {
00101     <font class="keyword">struct </font>dirent *result = 0;
00102 
00103     <font class="keywordflow">if</font>(dir &amp;&amp; dir-&gt;handle != -1)
00104     {
00105         <font class="keywordflow">if</font>(!dir-&gt;result.d_name || _findnext(dir-&gt;handle, &amp;dir-&gt;info) != -1)
00106         {
00107             result         = &amp;dir-&gt;result;
00108             result-&gt;d_name = dir-&gt;info.name;
00109         }
00110     }
00111     <font class="keywordflow">else</font>
00112     {
00113         errno = EBADF;
00114     }
00115 
00116     <font class="keywordflow">return</font> result;
00117 }
00118 
00119 <font class="keywordtype">void</font> rewinddir(DIR *dir)
00120 {
00121     <font class="keywordflow">if</font>(dir &amp;&amp; dir-&gt;handle != -1)
00122     {
00123         _findclose(dir-&gt;handle);
00124         dir-&gt;handle = _findfirst(dir-&gt;name, &amp;dir-&gt;info);
00125         dir-&gt;result.d_name = 0;
00126     }
00127     <font class="keywordflow">else</font>
00128     {
00129         errno = EBADF;
00130     }
00131 }
</pre></div><hr><address align="right"><small>Generated on Thu Jun 20 22:12:58 2002 for The Sword Project 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.15 </small></address>
</body>
</html>