2007-09-02 [colin] 2.10.0cvs190
[claws.git] / src / common / w32_dirent.c
1 /* w32_dirent.c  - Posix emulation layer for Sylpheed (Claws)
2  *
3  * This file is part of w32lib.
4  *
5  * w32lib is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * w32lib is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * For more information and a list of changes, see w32lib.h
19  */
20
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <windows.h>
25 #include <dirent.h>
26
27 #include "w32lib.h"
28
29
30
31 DIR *
32 opendir ( const char *name )
33 {
34   DIR *dir;
35   
36   dir = calloc (1, sizeof *dir + strlen (name));
37   if (!dir)
38     return NULL;
39   strcpy (dir->dd_name, name);
40   return dir;
41 }
42
43 int 
44 closedir (DIR *dir)
45 {
46   FindClose( (HANDLE)dir->dd_handle );
47   free( dir );
48   return 0;
49 }
50
51 struct dirent *
52 readdir( DIR *dir )
53 {
54   WIN32_FIND_DATA fInfo;
55   struct dirent *xdirent;
56   int ret;
57   
58   if ( !dir->dd_handle )
59     {
60       char *dirname;
61       
62       if (*dir->dd_name) 
63         {
64           size_t n = strlen (dir->dd_name);
65           dirname = malloc (n + 4 + 1);
66           if (dirname) {
67             strcpy (dirname, dir->dd_name);
68             strcpy (dirname + n, "\\*.*");
69           }
70         }
71       else
72         dirname = strdup( "\\*.*" );
73       if (!dirname)
74         return NULL; /* Error. */
75
76       dir->dd_handle = (long)FindFirstFile( dirname, &fInfo );
77       free( dirname );
78       if ( !dir->dd_handle )
79         ret = 0;
80       else
81         ret = 1;
82     } 
83   else if ( dir->dd_handle != -1l )
84     {
85         ret = FindNextFile ((HANDLE)dir->dd_handle, &fInfo);
86     }
87   else
88     ret = 0;
89   if ( !ret ) 
90     return NULL;
91
92   xdirent = calloc ( 1, sizeof *xdirent);
93   if (xdirent)
94     {
95       strncpy (xdirent->d_name, fInfo.cFileName, FILENAME_MAX -1 );
96       xdirent->d_name[FILENAME_MAX-1] = 0;
97       xdirent->d_namlen = strlen( xdirent->d_name );
98     }
99   return xdirent;
100 }