src/win32/dirent.c

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 Copyright (C) 2004  Armagetron Advanced Team (http://sourceforge.net/projects/armagetronad/)
00008 
00009 **************************************************************************
00010 
00011 This program is free software; you can redistribute it and/or
00012 modify it under the terms of the GNU General Public License
00013 as published by the Free Software Foundation; either version 2
00014 of the License, or (at your option) any later version.
00015 
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 GNU General Public License for more details.
00020 
00021 You should have received a copy of the GNU General Public License
00022 along with this program; if not, write to the Free Software
00023 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024   
00025 ***************************************************************************
00026 
00027 */
00028 
00029 /*
00030 
00031     Implementation of POSIX directory browsing functions and types for Win32.
00032 
00033     Author:  Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
00034     History: Created March 1997. Updated June 2003.
00035     Rights:  See end of file.
00036 
00037 */
00038 
00039 #include <dirent.h>
00040 #include <errno.h>
00041 #include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
00042 #include <stdlib.h>
00043 #include <string.h>
00044 
00045 #ifdef __cplusplus
00046 extern "C"
00047 {
00048 #endif
00049 
00050 struct DIR
00051 {
00052     long                handle; /* -1 for failed rewind */
00053     struct _finddata_t  info;
00054     struct dirent       result; /* d_name null iff first time */
00055     char                *name;  /* null-terminated char string */
00056 };
00057 
00058 DIR *opendir(const char *name)
00059 {
00060     DIR *dir = 0;
00061 
00062     if(name && name[0])
00063     {
00064         size_t base_length = strlen(name);
00065         const char *all = /* search pattern must end with suitable wildcard */
00066             strchr("/\\", name[base_length - 1]) ? "*" : "/*";
00067 
00068         if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
00069            (dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0)
00070         {
00071             strcat(strcpy(dir->name, name), all);
00072 
00073             if((dir->handle = (long) _findfirst(dir->name, &dir->info)) != -1)
00074             {
00075                 dir->result.d_name = 0;
00076             }
00077             else /* rollback */
00078             {
00079                 free(dir->name);
00080                 free(dir);
00081                 dir = 0;
00082             }
00083         }
00084         else /* rollback */
00085         {
00086             free(dir);
00087             dir   = 0;
00088             errno = ENOMEM;
00089         }
00090     }
00091     else
00092     {
00093         errno = EINVAL;
00094     }
00095 
00096     return dir;
00097 }
00098 
00099 int closedir(DIR *dir)
00100 {
00101     int result = -1;
00102 
00103     if(dir)
00104     {
00105         if(dir->handle != -1)
00106         {
00107             result = _findclose(dir->handle);
00108         }
00109 
00110         free(dir->name);
00111         free(dir);
00112     }
00113 
00114     if(result == -1) /* map all errors to EBADF */
00115     {
00116         errno = EBADF;
00117     }
00118 
00119     return result;
00120 }
00121 
00122 struct dirent *readdir(DIR *dir)
00123 {
00124     struct dirent *result = 0;
00125 
00126     if(dir && dir->handle != -1)
00127     {
00128         if(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
00129         {
00130             result         = &dir->result;
00131             result->d_name = dir->info.name;
00132         }
00133     }
00134     else
00135     {
00136         errno = EBADF;
00137     }
00138 
00139     return result;
00140 }
00141 
00142 void rewinddir(DIR *dir)
00143 {
00144     if(dir && dir->handle != -1)
00145     {
00146         _findclose(dir->handle);
00147         dir->handle = (long) _findfirst(dir->name, &dir->info);
00148         dir->result.d_name = 0;
00149     }
00150     else
00151     {
00152         errno = EBADF;
00153     }
00154 }
00155 
00156 #ifdef __cplusplus
00157 }
00158 #endif
00159 
00160 /*
00161 
00162     Copyright Kevlin Henney, 1997, 2003. All rights reserved.
00163 
00164     Permission to use, copy, modify, and distribute this software and its
00165     documentation for any purpose is hereby granted without fee, provided
00166     that this copyright and permissions notice appear in all copies and
00167     derivatives.
00168     
00169     This software is supplied "as is" without express or implied warranty.
00170 
00171     But that said, if there are any problems please get in touch.
00172 
00173 */
00174 

Generated on Sat Mar 15 22:56:14 2008 for Armagetron Advanced by  doxygen 1.5.4