initial load of upstream version 1.06.32
[xmlrpc-c] / lib / abyss / src / file.c
1 /******************************************************************************
2 **
3 ** file.c
4 **
5 ** This file is part of the ABYSS Web server project.
6 **
7 ** Copyright (C) 2000 by Moez Mahfoudh <mmoez@bigfoot.com>.
8 ** All rights reserved.
9 **
10 ** Redistribution and use in source and binary forms, with or without
11 ** modification, are permitted provided that the following conditions
12 ** are met:
13 ** 1. Redistributions of source code must retain the above copyright
14 **    notice, this list of conditions and the following disclaimer.
15 ** 2. Redistributions in binary form must reproduce the above copyright
16 **    notice, this list of conditions and the following disclaimer in the
17 **    documentation and/or other materials provided with the distribution.
18 ** 3. The name of the author may not be used to endorse or promote products
19 **    derived from this software without specific prior written permission.
20 ** 
21 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 ** SUCH DAMAGE.
32 **
33 ******************************************************************************/
34
35 #include <string.h>
36
37 #ifdef WIN32
38 #include <io.h>
39 #endif
40
41 #ifndef WIN32
42 #include <dirent.h>
43 #include <sys/stat.h>
44 #endif
45
46 #include "xmlrpc-c/abyss.h"
47 #include "file.h"
48
49 /*********************************************************************
50 ** File
51 *********************************************************************/
52
53 abyss_bool FileOpen(TFile *f, const char *name,uint32_t attrib)
54 {
55 #if defined( WIN32 ) && !defined( __BORLANDC__ )
56     return ((*f=_open(name,attrib))!=(-1));
57 #else
58     return ((*f=open(name,attrib))!=(-1));
59 #endif
60 }
61
62 abyss_bool FileOpenCreate(TFile *f, const char *name, uint32_t attrib)
63 {
64 #if defined( WIN32 ) && !defined( __BORLANDC__ )
65     return ((*f=_open(name,attrib | O_CREAT,_S_IWRITE | _S_IREAD))!=(-1));
66 #else
67     return ((*f=open(name,attrib | O_CREAT,S_IWRITE | S_IREAD))!=(-1));
68 #endif
69 }
70
71 abyss_bool
72 FileWrite(TFile *      const f,
73           const void * const buffer,
74           uint32_t     const len) {
75 #if defined( WIN32 ) && !defined( __BORLANDC__ )
76     return (_write(*f,buffer,len)==(int32_t)len);
77 #else
78     return (write(*f,buffer,len)==(int32_t)len);
79 #endif
80 }
81
82 int32_t FileRead(TFile *f, void *buffer, uint32_t len)
83 {
84 #if defined( WIN32 ) && !defined( __BORLANDC__ )
85     return (_read(*f,buffer,len));
86 #else
87     return (read(*f,buffer,len));
88 #endif
89 }
90
91 abyss_bool FileSeek(TFile *f, uint64_t pos, uint32_t attrib)
92 {
93 #if defined( WIN32 ) && !defined( __BORLANDC__ )
94     return (_lseek(*f,pos,attrib)!=(-1));
95 #else
96     return (lseek(*f,pos,attrib)!=(-1));
97 #endif
98 }
99
100 uint64_t FileSize(TFile *f)
101 {
102 #if defined( WIN32 ) && !defined( __BORLANDC__ )
103     return (_filelength(*f));
104 #else
105     struct stat fs;
106
107     fstat(*f,&fs);
108     return (fs.st_size);
109 #endif  
110 }
111
112 abyss_bool FileClose(TFile *f)
113 {
114 #if defined( WIN32 ) && !defined( __BORLANDC__ )
115     return (_close(*f)!=(-1));
116 #else
117     return (close(*f)!=(-1));
118 #endif
119 }
120
121
122
123 abyss_bool
124 FileStat(const char * const filename,
125          TFileStat *  const filestat) {
126 #if defined( WIN32 ) && !defined( __BORLANDC__ )
127     return (_stati64(filename,filestat)!=(-1));
128 #else
129     return (stat(filename,filestat)!=(-1));
130 #endif
131 }
132
133
134
135 abyss_bool
136 FileFindFirst(TFileFind *  const filefind,
137               const char * const path,
138               TFileInfo *  const fileinfo) {
139 #ifdef WIN32
140     abyss_bool ret;
141     char *p=path+strlen(path);
142
143     *p='\\';
144     *(p+1)='*';
145     *(p+2)='\0';
146 #ifndef __BORLANDC__
147     ret=(((*filefind)=_findfirst(path,fileinfo))!=(-1));
148 #else
149     *filefind = FindFirstFile( path, &fileinfo->data );
150    ret = *filefind != NULL;
151    if( ret )
152    {
153       LARGE_INTEGER li;
154       li.LowPart = fileinfo->data.nFileSizeLow;
155       li.HighPart = fileinfo->data.nFileSizeHigh;
156       strcpy( fileinfo->name, fileinfo->data.cFileName );
157        fileinfo->attrib = fileinfo->data.dwFileAttributes;
158        fileinfo->size   = li.QuadPart;
159       fileinfo->time_write = fileinfo->data.ftLastWriteTime.dwLowDateTime;
160    }
161 #endif
162     *p='\0';
163     return ret;
164 #else  /* WIN32 */
165     strncpy(filefind->path,path,NAME_MAX);
166     filefind->path[NAME_MAX]='\0';
167     filefind->handle=opendir(path);
168     if (filefind->handle)
169         return FileFindNext(filefind,fileinfo);
170
171     return FALSE;
172 #endif /* WIN32 */
173 }
174
175
176
177 abyss_bool FileFindNext(TFileFind *filefind,TFileInfo *fileinfo)
178 {
179 #ifdef WIN32
180
181 #ifndef __BORLANDC__
182     return (_findnext(*filefind,fileinfo)!=(-1));
183 #else
184    abyss_bool ret = FindNextFile( *filefind, &fileinfo->data );
185    if( ret )
186    {
187       LARGE_INTEGER li;
188       li.LowPart = fileinfo->data.nFileSizeLow;
189       li.HighPart = fileinfo->data.nFileSizeHigh;
190       strcpy( fileinfo->name, fileinfo->data.cFileName );
191        fileinfo->attrib = fileinfo->data.dwFileAttributes;
192        fileinfo->size   = li.QuadPart;
193       fileinfo->time_write = fileinfo->data.ftLastWriteTime.dwLowDateTime;
194    }
195     return ret;
196 #endif
197
198 #else  /* WIN32 */
199     struct dirent *de;
200     /****** Must be changed ***/
201     char z[NAME_MAX+1];
202
203     de=readdir(filefind->handle);
204     if (de)
205     {
206         struct stat fs;
207
208         strcpy(fileinfo->name,de->d_name);
209         strcpy(z,filefind->path);
210         strncat(z,"/",NAME_MAX);
211         strncat(z,fileinfo->name,NAME_MAX);
212         z[NAME_MAX]='\0';
213         
214         stat(z,&fs);
215
216         if (fs.st_mode & S_IFDIR)
217             fileinfo->attrib=A_SUBDIR;
218         else
219             fileinfo->attrib=0;
220
221         fileinfo->size=fs.st_size;
222         fileinfo->time_write=fs.st_mtime;
223         
224         return TRUE;
225     };
226
227     return FALSE;
228 #endif /* WIN32 */
229 }
230
231 void FileFindClose(TFileFind *filefind)
232 {
233 #ifdef WIN32
234
235 #ifndef __BORLANDC__
236     _findclose(*filefind);
237 #else
238    FindClose( *filefind );
239 #endif
240
241 #else  /* WIN32 */
242     closedir(filefind->handle);
243 #endif /* WIN32 */
244 }