Watt-32 tcp/ip  2.2 dev-rel.10
neterr.c
Go to the documentation of this file.
1 
38 #include "socket.h"
39 
40 char __syserr00[] = "No Error";
41 
42 #if defined(__WATCOMC__) /* data-modifier; near/far depending on model */
43  #define DATA_MOD _WCNEAR
44 #elif defined(_MSC_VER) && (_MSC_VER <= 800)
45  #define DATA_MOD _near /* even for large model */
46 #else
47  #define DATA_MOD
48 #endif
49 
50 #if defined(__TURBOC__) && (__TURBOC__ <= 0x410) /* TCC/BCC <= 3.1 */
51  #define SYS_ERRLIST sys_errlist
52  #define SYS_NERR sys_nerr
53  #include "borland/syserr.c" /* @NO_DEP */
54 
55 #elif defined(__BORLANDC__)
56  #define SYS_ERRLIST _sys_errlist
57  #define SYS_NERR _sys_nerr
58  #include "borland/syserr.c" /* @NO_DEP */
59 
60 #elif defined(__HIGHC__)
61  #define SYS_ERRLIST _sys_errlist
62  #define SYS_NERR _sys_nerr
63  #include "highc/syserr.c" /* @NO_DEP */
64 
65 #elif defined(__POCC__)
66  #define SYS_ERRLIST sys_errlist
67  #define SYS_NERR EILSEQ
68  #include "pellesc/syserr.c" /* @NO_DEP */
69 
70 #elif defined(__WATCOMC__)
71  #if 0
72  #if __WATCOMC__ > 1290
73  char * _WCDATA SYS_ERRLIST[] = {
74  #else
75  char *SYS_ERRLIST[] = {
76  #endif
77  #endif
78 
79  #if defined(WIN32)
80  #define SYS_ERRLIST _w32_sys_errlist
81  #define SYS_NERR _w32_sys_nerr
82  #else
83  #define SYS_ERRLIST _sys_errlist
84  #define SYS_NERR _sys_nerr
85  #endif
86 
87  #include "watcom/syserr.c" /* @NO_DEP */
88 
89 #elif defined(__DMC__)
90  #if defined(WIN32)
91  #define SYS_ERRLIST _w32_sys_errlist
92  #define SYS_NERR _w32_sys_nerr
93  #else
94  #define SYS_ERRLIST sys_errlist
95  #define SYS_NERR sys_nerr
96  #endif
97 
98  char __syserr37[] = "No Error"; /* !!fixme: isn't generated by dm_err.exe */
99  #include "digmars/syserr.c" /* @NO_DEP */
100 
101 #elif defined(_MSC_VER) && (_MSC_VER <= 800)
102  #define SYS_ERRLIST DATA_MOD sys_errlist
103  #define SYS_NERR sys_nerr
104  #include "quickc/syserr.c" /* @NO_DEP */
105 
106  #undef SYS_ERRLIST
107  #define SYS_ERRLIST sys_errlist
108 
109 #elif defined(_MSC_VER) && (_MSC_VER > 800) /* Visual C, assuming Win32 */
110  #define SYS_ERRLIST DATA_MOD _w32_sys_errlist
111  #define SYS_NERR _w32_sys_nerr
112  #include "visualc/syserr.c" /* @NO_DEP */
113 
114  #undef SYS_ERRLIST
115  #define SYS_ERRLIST sys_errlist
116 
117 #elif defined(__DJGPP__)
118  #define SYS_ERRLIST sys_errlist
119  #define SYS_NERR sys_nerr
120  #include "djgpp/syserr.c" /* @NO_DEP */
121 
122 #elif defined(__CCDL__)
123  #define SYS_ERRLIST sys_errlist
124  #define SYS_NERR sys_nerr
125  #include "ladsoft/syserr.c" /* @NO_DEP */
126 
127 #elif defined(__LCC__)
128  #define SYS_ERRLIST _w32_sys_errlist
129  #define SYS_NERR _w32_sys_nerr
130  #include "lcc/syserr.c" /* @NO_DEP */
131 
132 #elif defined(__MINGW64__)
133  #define SYS_ERRLIST _w32_sys_errlist
134  #define SYS_NERR _w32_sys_nerr
135  #include "MinGW64/syserr.c" /* @NO_DEP */
136 
137 #elif defined(__MINGW32__)
138  #define SYS_ERRLIST _w32_sys_errlist
139  #define SYS_NERR _w32_sys_nerr
140  #include "MinGW32/syserr.c" /* @NO_DEP */
141 
142 #elif defined(__CYGWIN__)
143  #define SYS_ERRLIST _sys_errlist
144  #define SYS_NERR _sys_nerr
145  // #include "CygWin/syserr.c" /* @NO_DEP */
146 
147 #else
148  #error Unknown target!
149 #endif
150 
151 /*
152  * A hack to make the linker use this sys_errlist[]
153  * and not the normal one in the C-library.
154  * Is there a better way?
155  */
156 int pull_neterr_module = 0;
157 
158 #if defined(__CYGWIN__)
159  /* Nothing to do here */
160 
161 #elif defined(__POCC__)
162  int _w32_sys_nerr = 120; /* pocc bug */
163 
164 #elif defined(WIN32) && !defined(__LCC__) && !defined(__BORLANDC__)
165  int _w32_sys_nerr = DIM(_w32_sys_errlist);
166  int *_sys_nerr_dll = &_w32_sys_nerr;
167  int *_imp___sys_nerr = &_w32_sys_nerr;
168 #else
169  int DATA_MOD SYS_NERR = DIM(SYS_ERRLIST);
170 #endif
171 
172 #if !defined(_MSC_VER) && defined(__MSDOS__)
173 void MS_CDECL perror (const char *str)
174 {
175  if (str && *str)
176  fprintf (stderr, "%s: ", str);
177 
178 #if defined(__GNUC__) /* Posix (?) dictates a newline */
179  fprintf (stderr, "%s\n", strerror(errno));
180 #else
181  fprintf (stderr, "%s", strerror(errno));
182 #endif
183 }
184 
185 /*
186  * For Win32/Win64, this doesn't replace the strerror() in the RTL,
187  * but replacing sys_nerr (*_sys_nerr_dll) should give the same effect (?).
188  */
189 char * MS_CDECL strerror (int errnum)
190 {
191  static char buf[70];
192 
193  if (errnum >= 0 && errnum < SYS_NERR)
194  _strlcpy (buf, SYS_ERRLIST[errnum], sizeof(buf));
195  else sprintf (buf, "Unknown error: %d", errnum);
196 
197 #if !defined(__DJGPP__) && !defined(__MINGW32__) && !defined(__MINGW64__)
198  /*
199  * Posix doesn't specify a newline at the end.
200  */
201  strcat (buf, "\n");
202 #endif
203 
204  return (buf);
205 }
206 #endif /* !_MSC_VER && __MSDOS__ */
207 
208 /*
209  * For non-Win32 errno == _w32_errno for all network calls
210  */
211 #if defined(WIN32)
212 /*
213  * Winsock compatibility.
214  */
215 int __stdcall WSAGetLastError (void)
216 {
217  int err = _w32_errno;
218  _w32_errno = 0;
219  return (err);
220 }
221 
222 void __stdcall WSASetLastError (int err)
223 {
224 #if defined(__POCC__)
225  #if defined(_DLL) || defined(_MT)
226  *__errno() = err;
227  #else
228  errno = err;
229  #endif
230 
231 #elif defined(_MSC_VER) || defined(__DMC__)
232  #if defined(_DLL) || defined(_MT)
233  *_errno() = err;
234  #else
235  errno = err;
236  #endif
237 
238 #elif defined(__MINGW32__) || defined(__MINGW64__)
239  *_errno() = err;
240 
241 #elif defined(__CYGWIN__)
242  *__errno() = err;
243 
244 #elif defined(__BORLANDC__)
245  int * _RTLENTRY _EXPFUNC __errno (void);
246  *__errno() = err;
247 
248 #elif defined(__LCC__)
249  #undef errno
250  *errno() = err;
251 
252 #elif defined(__WATCOMC__)
253  *__get_errno_ptr() = err;
254  errno = err;
255 
256 #else
257  #error Help me!
258 #endif
259 
260  _w32_errno = err;
261 }
262 #endif /* WIN32 */
263 
264 
265 #if defined(USE_DEBUG)
266 /*
267  * Return only the Exx part of strerror()
268  */
269 const char *short_strerror (int errnum)
270 {
271  static char buf[256];
272  char *l_par, *r_par;
273 
274  _strlcpy (buf, strerror(errnum), sizeof(buf));
275  l_par = strchr (buf, '(');
276  if (!l_par)
277  {
278  sprintf (buf, "errno %d", errnum);
279  return (buf);
280  }
281 
282  r_par = strchr (l_par, ')');
283  if (r_par)
284  *r_par = '\0';
285  return (l_par+1);
286 }
287 #endif
288 
289 
290 #if defined(__GNUC__) && 0
291  #define ALIAS(func) __attribute__((weak, alias(func)))
292 
293  char *strerror_s_ (int err) ALIAS(strerror);
294 #endif
295 
296 
297 /*
298  * Incase you have trouble with multiple defined symbols,
299  * make sure the following functions are used before
300  * perror/strerror/sys_errlist in your C-library.
301  */
302 void W32_CALL perror_s (const char *str)
303 {
304 #if defined(__LCC__)
305  perror ((char*)str);
306 #else
307  perror (str);
308 #endif
309 }
310 
311 char * W32_CALL strerror_s_ (int errnum)
312 {
313  return strerror (errnum);
314 }
315 
char * _strlcpy(char *dst, const char *src, size_t len)
Similar to strncpy(), but always returns 'dst' with 0-termination.
Definition: strings.c:226