OSDN Git Service

* miscfuncs.cc (check_iovec_for_read): Don't check buffer when zero length
[pf3gnuchains/sourceware.git] / winsup / cygwin / winsup.h
1 /* winsup.h: main Cygwin header file.
2
3    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #ifdef HAVE_CONFIG_H
12 # include "config.h"
13 #endif
14
15 #define __INSIDE_CYGWIN__
16
17 #define strlen __builtin_strlen
18 #define strcmp __builtin_strcmp
19 #define strcpy __builtin_strcpy
20 #define memcpy __builtin_memcpy
21 #define memcmp __builtin_memcmp
22 #ifdef HAVE_BUILTIN_MEMSET
23 # define memset __builtin_memset
24 #endif
25
26 #define NO_COPY __attribute__((nocommon)) __attribute__((section(".data_cygwin_nocopy")))
27
28 #ifdef __cplusplus
29
30 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ >= 199900L
31 #define NEW_MACRO_VARARGS
32 #endif
33
34 #include <sys/types.h>
35 #include <sys/strace.h>
36
37 extern const char case_folded_lower[];
38 #define cyg_tolower(c) (case_folded_lower[(unsigned char)(c)])
39 extern const char case_folded_upper[];
40 #define cyg_toupper(c) (case_folded_upper[(unsigned char)(c)])
41
42 #ifndef MALLOC_DEBUG
43 #define cfree newlib_cfree_dont_use
44 #endif
45
46 #define WIN32_LEAN_AND_MEAN 1
47 #define _WINGDI_H
48 #define _WINUSER_H
49 #define _WINNLS_H
50 #define _WINVER_H
51 #define _WINNETWK_H
52 #define _WINSVC_H
53 #include <windows.h>
54 #include <wincrypt.h>
55 #include <lmcons.h>
56 #undef _WINGDI_H
57 #undef _WINUSER_H
58 #undef _WINNLS_H
59 #undef _WINVER_H
60 #undef _WINNETWK_H
61 #undef _WINSVC_H
62
63 #include "wincap.h"
64
65 /* The one function we use from winuser.h most of the time */
66 extern "C" DWORD WINAPI GetLastError (void);
67
68 enum codepage_type {ansi_cp, oem_cp};
69 extern codepage_type current_codepage;
70
71 UINT get_cp ();
72
73 int __stdcall sys_wcstombs(char *, const WCHAR *, int)
74   __attribute__ ((regparm(3)));
75
76 int __stdcall sys_mbstowcs(WCHAR *, const char *, int)
77   __attribute__ ((regparm(3)));
78
79 /* Used to check if Cygwin DLL is dynamically loaded. */
80 extern int dynamically_loaded;
81
82 extern int cygserver_running;
83
84 #define TITLESIZE 1024
85
86 /* status bit manipulation */
87 #define __ISSETF(what, x, prefix) \
88   ((what)->status & prefix##_##x)
89 #define __SETF(what, x, prefix) \
90   ((what)->status |= prefix##_##x)
91 #define __CLEARF(what, x, prefix) \
92   ((what)->status &= ~prefix##_##x)
93 #define __CONDSETF(n, what, x, prefix) \
94   ((n) ? __SETF (what, x, prefix) : __CLEARF (what, x, prefix))
95
96 #include "debug.h"
97
98 /* Events/mutexes */
99 extern HANDLE title_mutex;
100
101 /**************************** Convenience ******************************/
102
103 /* Used when treating / and \ as equivalent. */
104 #define SLASH_P(ch) \
105     ({ \
106         char __c = (ch); \
107         ((__c) == '/' || (__c) == '\\'); \
108     })
109
110 /* Convert a signal to a signal mask */
111 #define SIGTOMASK(sig)  (1<<((sig) - signal_shift_subtract))
112 extern unsigned int signal_shift_subtract;
113
114 #ifdef NEW_MACRO_VARARGS
115 # define api_fatal(...) __api_fatal ("%P: *** " __VA_ARGS__)
116 #else
117 # define api_fatal(fmt, args...) __api_fatal ("%P: *** " fmt,## args)
118 #endif
119
120 #undef issep
121 #define issep(ch) (strchr (" \t\n\r", (ch)) != NULL)
122
123 #define isdirsep SLASH_P
124 #define isabspath(p) \
125   (isdirsep (*(p)) || (isalpha (*(p)) && (p)[1] == ':' && (!(p)[2] || isdirsep ((p)[2]))))
126
127 /******************** Initialization/Termination **********************/
128
129 class per_process;
130 /* cygwin .dll initialization */
131 void dll_crt0 (per_process *) __asm__ ("_dll_crt0__FP11per_process");
132 extern "C" void __stdcall _dll_crt0 ();
133
134 /* dynamically loaded dll initialization */
135 extern "C" int dll_dllcrt0 (HMODULE, per_process *);
136
137 /* dynamically loaded dll initialization for non-cygwin apps */
138 extern "C" int dll_noncygwin_dllcrt0 (HMODULE, per_process *);
139
140 /* exit the program */
141 extern "C" void __stdcall do_exit (int) __attribute__ ((noreturn));
142
143 /* UID/GID */
144 void uinfo_init (void);
145
146 #define ILLEGAL_UID16 ((__uid16_t)-1)
147 #define ILLEGAL_UID ((__uid32_t)-1)
148 #define ILLEGAL_GID16 ((__gid16_t)-1)
149 #define ILLEGAL_GID ((__gid32_t)-1)
150 #define ILLEGAL_SEEK ((__off64_t)-1)
151
152 #define uid16touid32(u16)  ((u16)==ILLEGAL_UID16?ILLEGAL_UID:(__uid32_t)(u16))
153 #define gid16togid32(g16)  ((g16)==ILLEGAL_GID16?ILLEGAL_GID:(__gid32_t)(g16))
154
155 extern "C" __uid32_t getuid32 (void);
156 extern "C" __uid32_t geteuid32 (void);
157 extern "C" struct passwd *getpwuid32 (__uid32_t);
158
159 /* various events */
160 void events_init (void);
161 void events_terminate (void);
162
163 void __stdcall close_all_files (void);
164 BOOL __stdcall check_pty_fds (void);
165
166 /* Invisible window initialization/termination. */
167 HWND __stdcall gethwnd (void);
168 void __stdcall window_terminate (void);
169
170 /* Globals that handle initialization of winsock in a child process. */
171 extern HANDLE wsock32_handle;
172 extern HANDLE ws2_32_handle;
173
174 /* Globals that handle initialization of netapi in a child process. */
175 extern HANDLE netapi32_handle;
176
177 /* debug_on_trap support. see exceptions.cc:try_to_debug() */
178 extern "C" void error_start_init (const char*);
179 extern "C" int try_to_debug (bool waitloop = 1);
180
181 void set_file_api_mode (codepage_type);
182
183 extern int cygwin_finished_initializing;
184
185 /**************************** Miscellaneous ******************************/
186
187 void __stdcall set_std_handle (int);
188 int __stdcall writable_directory (const char *file);
189 int __stdcall stat_dev (DWORD, int, unsigned long, struct __stat64 *);
190 extern BOOL allow_ntsec;
191
192 unsigned long __stdcall hash_path_name (unsigned long hash, const char *name) __attribute__ ((regparm(2)));
193 void __stdcall nofinalslash (const char *src, char *dst) __attribute__ ((regparm(2)));
194 extern "C" char *__stdcall rootdir (char *full_path) __attribute__ ((regparm(1)));
195
196 /* String manipulation */
197 extern "C" char *__stdcall strccpy (char *s1, const char **s2, char c);
198 extern "C" int __stdcall strcasematch (const char *s1, const char *s2) __attribute__ ((regparm(2)));
199 extern "C" int __stdcall strncasematch (const char *s1, const char *s2, size_t n) __attribute__ ((regparm(3)));
200 extern "C" char *__stdcall strcasestr (const char *searchee, const char *lookfor) __attribute__ ((regparm(2)));
201
202 /* Time related */
203 void __stdcall totimeval (struct timeval *dst, FILETIME * src, int sub, int flag);
204 long __stdcall to_time_t (FILETIME * ptr);
205 void __stdcall to_timestruc_t (FILETIME * ptr, timestruc_t * out);
206 void __stdcall time_as_timestruc_t (timestruc_t * out);
207
208 void __stdcall set_console_title (char *);
209 void early_stuff_init ();
210
211 int __stdcall check_null_str (const char *name) __attribute__ ((regparm(1)));
212 int __stdcall check_null_empty_str (const char *name) __attribute__ ((regparm(1)));
213 int __stdcall check_null_empty_str_errno (const char *name) __attribute__ ((regparm(1)));
214 int __stdcall check_null_str_errno (const char *name) __attribute__ ((regparm(1)));
215 int __stdcall __check_null_invalid_struct (void *s, unsigned sz) __attribute__ ((regparm(2)));
216 int __stdcall __check_null_invalid_struct_errno (void *s, unsigned sz) __attribute__ ((regparm(2)));
217 int __stdcall __check_invalid_read_ptr_errno (const void *s, unsigned sz) __attribute__ ((regparm(2)));
218
219 #define check_null_invalid_struct(s) \
220   __check_null_invalid_struct ((s), sizeof (*(s)))
221 #define check_null_invalid_struct_errno(s) \
222   __check_null_invalid_struct_errno ((s), sizeof (*(s)))
223
224 struct iovec;
225 ssize_t check_iovec_for_read (const struct iovec *, int) __attribute__ ((regparm(2)));
226 ssize_t check_iovec_for_write (const struct iovec *, int) __attribute__ ((regparm(2)));
227
228 #define set_winsock_errno() __set_winsock_errno (__FUNCTION__, __LINE__)
229 void __set_winsock_errno (const char *fn, int ln) __attribute__ ((regparm(2)));
230
231 extern bool wsock_started;
232
233 /* Printf type functions */
234 extern "C" void __api_fatal (const char *, ...) __attribute__ ((noreturn));
235 extern "C" int __small_sprintf (char *dst, const char *fmt, ...) /*__attribute__ ((regparm (2)))*/;
236 extern "C" int __small_vsprintf (char *dst, const char *fmt, va_list ap) /*__attribute__ ((regparm (3)))*/;
237 extern void multiple_cygwin_problem (const char *, unsigned, unsigned);
238
239 class path_conv;
240 int __stdcall stat_worker (const char *name, struct __stat64 *buf, int nofollow,
241                            path_conv *pc = NULL) __attribute__ ((regparm (3)));
242
243 /**************************** Exports ******************************/
244
245 extern "C" {
246 int cygwin_select (int , fd_set *, fd_set *, fd_set *,
247                    struct timeval *to);
248 int cygwin_gethostname (char *__name, size_t __len);
249
250 int kill_pgrp (pid_t, int);
251 int _kill (int, int);
252 int _raise (int sig);
253
254 extern DWORD binmode;
255 extern char _data_start__, _data_end__, _bss_start__, _bss_end__;
256 extern void (*__CTOR_LIST__) (void);
257 extern void (*__DTOR_LIST__) (void);
258 extern SYSTEM_INFO system_info;
259 };
260
261 /*************************** Unsorted ******************************/
262
263 #define WM_ASYNCIO      0x8000          // WM_APP
264
265 /* Note that MAX_PATH is defined in the windows headers */
266 /* There is also PATH_MAX and MAXPATHLEN.
267    PATH_MAX is from Posix and does *not* include the trailing NUL.
268    MAXPATHLEN is from Unix.
269
270    Thou shalt use MAX_PATH throughout.  It avoids the NUL vs no-NUL
271    issue and is neither of the Unixy ones [so we can punt on which
272    one is the right one to use].  */
273
274 #define STD_RBITS (S_IRUSR | S_IRGRP | S_IROTH)
275 #define STD_WBITS (S_IWUSR)
276 #define STD_XBITS (S_IXUSR | S_IXGRP | S_IXOTH)
277 #define NO_W ~(S_IWUSR | S_IWGRP | S_IWOTH)
278 #define NO_R ~(S_IRUSR | S_IRGRP | S_IROTH)
279 #define NO_X ~(S_IXUSR | S_IXGRP | S_IXOTH)
280
281 /* The title on program start. */
282 extern char *old_title;
283 extern BOOL display_title;
284
285 extern HANDLE hMainThread;
286 extern HANDLE hMainProc;
287
288 extern bool cygwin_testing;
289 extern unsigned _cygwin_testing_magic;
290 extern HMODULE cygwin_hmodule;
291
292 extern char almost_null[];
293
294 #define winsock2_active (wsadata.wVersion >= 512)
295 #define winsock_active (wsadata.wVersion < 512)
296 extern struct WSAData wsadata;
297
298 #endif /* defined __cplusplus */