2008-11-26 [colin] 3.6.1cvs44
[claws.git] / src / common / w32lib.h
1 /* w32lib.h  - 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  * The code has been taken from the package
19  *    http://claws.sylpheed.org/win32/sylpheed/w32lib-dev-2004.2.3.0.zip
20  * on 2005-11-17 by Werner Koch <wk@gnupg.org>. There are no regular
21  * copyright notices but the file version.rc from the ZIP archive
22  * claims:
23  *
24  *     #define FILEVER "2004.2.3.0\0"
25  *     #define PRODVER "2004.2.3\0"
26  *
27  *     [...]
28  *      VALUE "FileDescription", "Posix emulation layer for Sylpheed (Claws)\0"
29  *      VALUE "FileVersion", FILEVER
30  *      VALUE "ProductVersion", PRODVER
31  *      VALUE "LegalCopyright", "GPL\0"
32  *      VALUE "CompanyName", "GNU / Free Software Foundation\0"
33  *      VALUE "ProductName", "w32lib\0"
34  *
35  * Along with the fact that Sylpheed is under the GPL we can assume
36  * that this code is under the GPL.  No author information or
37  * changelogs have been found.
38  * Files taken form the package are:
39  *    w32_dirent.c w32_reg.c w32_stat.c w32_stdlib.c w32_time.c w32_wait.c
40  *    w32_gettext.c w32_signal.c w32_stdio.c w32_string.c w32_unistd.c
41  */
42
43 /* Changes are:
44
45 2007-05-21  Werner Koch  <wk@g10code.com>
46
47         * src/common/w32_account.c: New.
48
49         * src/common/w32lib.h: Undef "interface".
50
51 2005-11-17  Werner Koch  <wk@g10code.com>
52
53         Add boilerplate text to all files and explain legal status.
54
55         * w32_reg.c: Replaced g_free and g_strdup by regular C functions.
56         (get_content_type_from_registry_with_ext): Ditto.
57         * w32_dirent.c (readdir): Ditto. 
58         (opendir): Ditto.
59         (closedir): Reformatted.
60         (readdir): Reformatted, replaced use of g_strdup_printf and other
61         g-style malloc function by regular ones.  Use DIR structure from mingw.
62         * w32lib.h: Don't define finddata_t for mingw. Replaced replacement
63         DIR structure by the one form mingw.  Allocate filename in dirent
64         statically to match the defintion ussed by mingw.
65         * w32_reg.c (read_w32_registry_string): Return error for invalid root
66         key.
67
68   */
69
70
71 #ifndef _W32LIB_H_
72 #define _W32LIB_H_
73
74 #include <windows.h>
75 #include <io.h>
76 #include <stdio.h>
77
78 #ifdef __MINGW32__
79 #include <_mingw.h>
80 #define MINGW32_VERSION (__MINGW32_MAJOR_VERSION * 100 \
81                          + __MINGW32_MINOR_VERSION)
82 #include <wchar.h>
83 #include <dirent.h>
84 #include <sys/time.h>
85 #endif
86
87 /* Mingw32 3.4.4 defines interface to struct and thus breaks our own
88    use of that symbol.  Undef it here. */
89 #if defined(_BASETYPS_H) && defined(interface) 
90 #undef interface
91 #endif
92
93
94 /* types */
95 /*** ??? ***/
96 #ifndef __MINGW32__
97 typedef long int off_t;
98 typedef int pid_t;
99 typedef unsigned char u_char;
100 #endif /* __MINGW32__ */
101 typedef unsigned int uid_t;
102
103 #ifndef __MINGW32__
104 /*** stat ***/
105 #define S_IRUSR _S_IREAD
106 #define S_IWUSR _S_IWRITE
107 #define S_IXUSR _S_IEXEC
108 #define S_IRWXU (_S_IREAD|_S_IWRITE|_S_IEXEC)
109 #endif /* __MINGW32__ */
110
111
112 /* (signal?) */
113 #define SIGPIPE _S_IFIFO
114
115
116 /* (directory) */
117 #define __S_ISTYPE(mode, mask)  (((mode) & _S_IFMT) == (mask))
118 #ifndef __MINGW32__
119 #define S_ISFIFO(mode)  __S_ISTYPE((mode), _S_IFIFO)
120 #define S_ISDIR(mode)   __S_ISTYPE((mode), _S_IFDIR)
121 #define S_ISREG(mode)   __S_ISTYPE((mode), _S_IFREG)
122 #endif /* __MINGW32__ */
123
124 /* functions */
125 /*** str ***/
126 int strcasecmp( const char *s1, const char *s2 );
127
128 int strncasecmp( const char *s1, const char *s2, size_t n );
129
130 /*** dir ***/
131 #ifndef __MINGW32__
132 typedef void * HANDLE;
133
134 #ifndef _FINDDATA_T_DEFINED
135 typedef unsigned long _fsize_t; /* Could be 64 bits for Win32 */
136 struct _finddata_t {
137     unsigned    attrib;
138     time_t      time_create;    /* -1 for FAT file systems */
139     time_t      time_access;    /* -1 for FAT file systems */
140     time_t      time_write;
141     _fsize_t    size;
142     char        name[260];
143 };
144 #endif /* !_FINDDATA_T_DEFINED */
145
146 struct dirent {
147         long d_ino;
148         unsigned short d_reclen;
149         unsigned short d_namlen;
150         char d_name[FILENAME_MAX];
151 };
152
153 typedef struct
154 {
155         struct _finddata_t      dd_dta;
156         struct dirent           dd_dir;
157         long                    dd_handle;
158         int                     dd_stat;
159         char                    dd_name[1];
160 } DIR;
161
162 #endif /* !__MINGW32__ */
163
164
165 DIR *opendir( const char *name );
166 int closedir( DIR *dir );
167 struct dirent *readdir( DIR *dir );
168
169 #if defined (__MINGW32__) && MINGW32_VERSION < 312
170 struct timezone {
171   int tz_minuteswest;
172   int tz_dsttime;
173 };
174 #endif
175
176 /*** stat ***/
177 int lstat( const char *file_name, struct stat *buf );
178
179 /*** sys/wait ***/
180 pid_t waitpid( pid_t pid, int *status, int options );
181
182 /*** sys/time ***/
183 #if ! defined (__MINGW32__) || MINGW32_VERSION < 312
184 int gettimeofday( struct timeval *tv, struct timezone *tz );
185 #endif
186
187 /*** unistd ***/
188 uid_t getuid( void );
189 int setpgid( pid_t pid, pid_t pgid );
190 pid_t getppid( void );
191 pid_t fork( void );
192 unsigned int sleep( unsigned int seconds );
193 int link( const char *oldpath, const char *newpath );
194 int usleep( unsigned long usec );
195
196 /*** stdlib ***/
197 long int random( void );
198 void srandom( unsigned int seed );
199 int truncate( const char *path, off_t length );
200
201 /*** signal ***/
202 int kill( pid_t pid, int sig );
203
204 /*** stdio ***/
205 FILE *popen( const char *command, const char *type );
206 int pclose( FILE *stream );
207
208 /*** w32_account.c ***/
209 int w32_is_administrator (void);
210
211 /*** misc ***/
212 int write_w32_registry_string( char *parent, char *section, char *value, char *data );
213 int write_w32_registry_dword( char *parent, char *section, char *value, int data );
214 char *read_w32_registry_string( char *parent, char *section, char *key );
215 char *get_content_type_from_registry_with_ext( char *ext );
216
217 \f
218 /* Simulate thread-safe versions of some functions.  */
219 #define gmtime_r(time, resultp) gmtime (time)
220 #define localtime_r(time, resultp) localtime (time)
221 #define asctime_r(time, buffer) asctime (time)
222 #define ctime_r(time, buffer) ctime (time)
223
224 #endif