/[LeafOK_CVS]/pvpgn-1.7.4/src/compat/pdir.c
ViewVC logotype

Contents of /pvpgn-1.7.4/src/compat/pdir.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Tue Jun 6 03:41:38 2006 UTC (19 years, 9 months ago) by sysadm
Branch: GNU, MAIN
CVS Tags: arelease, HEAD
Changes since 1.1: +0 -0 lines
Content type: text/x-csrc
Error occurred while calculating annotation data.
no message

1 /*
2 * Copyright (C) 2001 Dizzy (dizzy@roedu.net)
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #define PDIR_INTERNAL_ACCESS
19 #include "common/setup_before.h"
20 #ifdef HAVE_STDDEF_H
21 # include <stddef.h>
22 #else
23 # ifndef NULL
24 # define NULL ((void *)0)
25 # endif
26 #endif
27 #ifdef STDC_HEADERS
28 # include <stdlib.h>
29 #else
30 # ifdef HAVE_MALLOC_H
31 # include <malloc.h>
32 # endif
33 #endif
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 # include <string.h>
39 #else
40 # ifdef HAVE_STRINGS_H
41 # include <strings.h>
42 # endif
43 #endif
44 #include "compat/strdup.h"
45 #ifdef HAVE_DIRENT_H
46 # include <dirent.h>
47 #else
48 # ifdef HAVE_SYS_NDIR_H
49 # include <sys/ndir.h>
50 # endif
51 # if HAVE_SYS_DIR_H
52 # include <sys/dir.h>
53 # endif
54 # if HAVE_NDIR_H
55 # include <ndir.h>
56 # endif
57 # define dirent direct
58 #endif
59 #ifdef WIN32
60 # include <io.h> /* for _findfirst(), _findnext(), etc */
61 #endif
62 #include <errno.h>
63 #include "compat/strerror.h"
64 #include "common/eventlog.h"
65 #include "common/xalloc.h"
66 #include "pdir.h"
67 #include "common/setup_after.h"
68
69
70 extern t_pdir * p_opendir(const char * path) {
71 #ifdef WIN32
72 char npath[_MAX_PATH];
73 #endif
74 t_pdir * pdir;
75
76 if (path==NULL) {
77 eventlog(eventlog_level_error,__FUNCTION__,"got NULL path");
78 return NULL;
79 }
80 /* while(path[strlen(path)]=='/') path[strlen(path)]='\0'; */
81 /* win32 can use slash in addition to backslash */
82 pdir=xmalloc(sizeof(t_pdir));
83
84 #ifdef WIN32
85 if (strlen(path)+1+3+1>_MAX_PATH) {
86 eventlog(eventlog_level_error,__FUNCTION__,"WIN32: path too long");
87 xfree(pdir);
88 return NULL;
89 }
90 strcpy(npath, path);
91 strcat(npath, "/*.*");
92 pdir->path=xstrdup(npath);
93
94 pdir->status = 0;
95 memset(&pdir->fileinfo, 0, sizeof(pdir->fileinfo)); /* no need for compat because WIN32 always has memset() */
96 pdir->lFindHandle = _findfirst(npath, &pdir->fileinfo);
97 if (pdir->lFindHandle < 0) {
98 xfree((void *)pdir->path); /* avoid warning */
99 xfree(pdir);
100 return NULL;
101 }
102
103 #else /* POSIX style */
104
105 pdir->path=xstrdup(path);
106 if ((pdir->dir=opendir(path))==NULL) {
107 xfree((void *)pdir->path); /* avoid warning */
108 xfree(pdir);
109 return NULL;
110 }
111
112 #endif /* WIN32-POSIX */
113
114 return pdir;
115 }
116
117
118 extern int p_rewinddir(t_pdir * pdir) {
119 if (pdir==NULL) {
120 eventlog(eventlog_level_error,__FUNCTION__,"got NULL pdir");
121 return -1;
122 }
123 if (pdir->path==NULL) {
124 eventlog(eventlog_level_error,__FUNCTION__,"got pdir with NULL path");
125 return -1;
126 }
127 #ifdef WIN32
128 /* i dont have any win32 around so i dont know if io.h has any rewinddir equivalent */
129 /* FIXME: for the time being ill just close and reopen it */
130 if (pdir->status!=-1)
131 {
132 if (pdir->lFindHandle<0) {
133 eventlog(eventlog_level_error,__FUNCTION__,"WIN32: got negative lFindHandle");
134 return -1;
135 }
136 _findclose(pdir->lFindHandle);
137 }
138 pdir->status = 0;
139 memset(&pdir->fileinfo, 0, sizeof(pdir->fileinfo)); /* no need for compat because WIN32 always has memset() */
140 pdir->lFindHandle = _findfirst(pdir->path, &pdir->fileinfo);
141 if (pdir->lFindHandle < 0) {
142 pdir->status = -1;
143 return -1;
144 }
145 #else /* POSIX */
146 if (pdir->dir==NULL) {
147 eventlog(eventlog_level_error,__FUNCTION__,"POSIX: got pdir with NULL dir");
148 return -1;
149 }
150 rewinddir(pdir->dir);
151 #endif
152 return 0;
153 }
154
155
156 extern char const * p_readdir(t_pdir * pdir) {
157 if (pdir==NULL) {
158 eventlog(eventlog_level_error,__FUNCTION__,"got NULL pdir");
159 return NULL;
160 }
161 if (pdir->path==NULL) {
162 eventlog(eventlog_level_error,__FUNCTION__,"got pdir with NULL path");
163 return NULL;
164 }
165 #ifdef WIN32
166 switch (pdir->status)
167 {
168 default:
169 case -1: /* couldn't rewind */
170 eventlog(eventlog_level_error,__FUNCTION__,"got pdir with status -1");
171 return NULL;
172 case 0: /* freshly opened */
173 pdir->status = 1;
174 return pdir->fileinfo.name;
175 case 1: /* reading */
176 if (_findnext(pdir->lFindHandle, &pdir->fileinfo)<0)
177 {
178 pdir->status = 2;
179 return NULL;
180 }
181 else
182 return pdir->fileinfo.name;
183 break;
184 case 2: /* EOF */
185 return NULL;
186 }
187 #else /* POSIX */
188 {
189 struct dirent * dentry;
190
191 if (pdir->dir==NULL) {
192 eventlog(eventlog_level_error,__FUNCTION__,"POSIX: got pdir with NULL dir");
193 return NULL;
194 }
195 if ((dentry=readdir(pdir->dir))==NULL)
196 return NULL;
197 return dentry->d_name;
198 }
199 #endif /* WIN32-POSIX */
200 }
201
202
203 extern int p_closedir(t_pdir * pdir) {
204 int ret;
205
206 if (pdir==NULL) {
207 eventlog(eventlog_level_error,__FUNCTION__,"got NULL pdir");
208 return -1;
209 }
210 if (pdir->path==NULL) {
211 eventlog(eventlog_level_error,__FUNCTION__,"got pdir with NULL path");
212 return -1;
213 }
214
215 #ifdef WIN32
216 if (pdir->status!=-1)
217 {
218 if (pdir->lFindHandle<0) {
219 eventlog(eventlog_level_info,__FUNCTION__,"WIN32: got NULL findhandle");
220 return -1;
221 }
222 _findclose(pdir->lFindHandle); /* FIXME: what does _findclose() return on error? */
223 }
224 ret = 0;
225 #else /* POSIX */
226 if (pdir->dir==NULL) {
227 eventlog(eventlog_level_info,__FUNCTION__,"POSIX: got NULL dir");
228 return -1;
229 }
230 # ifdef CLOSEDIR_VOID
231 closedir(pdir->dir);
232 ret = 0;
233 # else
234 ret = closedir(pdir->dir);
235 # endif
236 #endif /* WIN32-POSIX */
237
238 xfree((void *)pdir->path); /* avoid warning */
239 xfree(pdir);
240 return ret;
241 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1