Debian Codegraph @

unix/tclUnixPort.h

raw
1 /*
2 * tclUnixPort.h --
3 *
4 * This header file handles porting issues that occur because of
5 * differences between systems. It reads in UNIX-related header files and
6 * sets up UNIX-related macros for Tcl's UNIX core. It should be the only
7 * file that contains #ifdefs to handle different flavors of UNIX. This
8 * file sets up the union of all UNIX-related things needed by any of the
9 * Tcl core files. This file depends on configuration #defines such as
10 * NO_DIRENT_H that are set up by the "configure" script.
11 *
12 * Much of the material in this file was originally contributed by Karl
13 * Lehenbauer, Mark Diekhans and Peter da Silva.
14 *
15 * Copyright (c) 1991-1994 The Regents of the University of California.
16 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
17 *
18 * See the file "license.terms" for information on usage and redistribution of
19 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
20 */
21
22 #ifndef _TCLUNIXPORT
23 #define _TCLUNIXPORT
24
25 /*
26 *---------------------------------------------------------------------------
27 * The following sets of #includes and #ifdefs are required to get Tcl to
28 * compile under the various flavors of unix.
29 *---------------------------------------------------------------------------
30 */
31
32 #include <errno.h>
33 #include <fcntl.h>
34 #ifdef HAVE_NET_ERRNO_H
35 # include <net/errno.h>
36 #endif
37 #include <pwd.h>
38 #include <signal.h>
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
41 #endif
42 #include <sys/types.h>
43 #ifdef USE_DIRENT2_H
44 # include "../compat/dirent2.h"
45 #else
46 #ifdef NO_DIRENT_H
47 # include "../compat/dirent.h"
48 #else
49 # include <dirent.h>
50 #endif
51 #endif
52
53 /*
54 *---------------------------------------------------------------------------
55 * Parameterize for 64-bit filesystem support.
56 *---------------------------------------------------------------------------
57 */
58
59 #ifdef HAVE_STRUCT_DIRENT64
60 typedef struct dirent64 Tcl_DirEntry;
61 # define TclOSreaddir readdir64
62 #else
63 typedef struct dirent Tcl_DirEntry;
64 # define TclOSreaddir readdir
65 #endif
66 #ifdef HAVE_DIR64
67 typedef DIR64 TclDIR;
68 # define TclOSopendir opendir64
69 # define TclOSrewinddir rewinddir64
70 # define TclOSclosedir closedir64
71 #else
72 typedef DIR TclDIR;
73 # define TclOSopendir opendir
74 # define TclOSrewinddir rewinddir
75 # define TclOSclosedir closedir
76 #endif
77
78 #ifdef HAVE_TYPE_OFF64_T
79 typedef off64_t Tcl_SeekOffset;
80 # define TclOSseek lseek64
81 # define TclOSopen open64
82 #else
83 typedef off_t Tcl_SeekOffset;
84 # define TclOSseek lseek
85 # define TclOSopen open
86 #endif
87
88 #ifdef __CYGWIN__
89 #ifdef __cplusplus
90 extern "C" {
91 #endif
92 /* Make some symbols available without including <windows.h> */
93 # define DWORD unsigned int
94 # define CP_UTF8 65001
95 # define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 0x00000004
96 # define HANDLE void *
97 # define HINSTANCE void *
98 # define SOCKET unsigned int
99 # define WSAEWOULDBLOCK 10035
100 typedef unsigned short WCHAR;
101 __declspec(dllimport) extern int GetModuleHandleExW(unsigned int, const void *, void *);
102 __declspec(dllimport) extern int GetModuleFileNameW(void *, const void *, int);
103 __declspec(dllimport) extern int WideCharToMultiByte(int, int, const void *, int,
104 char *, int, const char *, void *);
105 __declspec(dllimport) extern int MultiByteToWideChar(int, int, const char *, int,
106 WCHAR *, int);
107 __declspec(dllimport) extern void OutputDebugStringW(const WCHAR *);
108 __declspec(dllimport) extern int IsDebuggerPresent(void);
109 __declspec(dllimport) extern int GetLastError(void);
110 __declspec(dllimport) extern int GetFileAttributesW(const WCHAR *);
111 __declspec(dllimport) extern int SetFileAttributesW(const WCHAR *, int);
112 __declspec(dllimport) extern int cygwin_conv_path(int, const void *, void *, int);
113 # define timezone _timezone
114 extern int TclOSstat(const char *name, void *statBuf);
115 extern int TclOSlstat(const char *name, void *statBuf);
116 #ifdef __cplusplus
117 }
118 #endif
119 #elif defined(HAVE_STRUCT_STAT64) && !defined(__APPLE__)
120 # define TclOSstat(name, buf) stat64(name, (struct stat64 *)buf)
121 # define TclOSlstat(name,buf) lstat64(name, (struct stat64 *)buf)
122 #else
123 # define TclOSstat(name, buf) stat(name, (struct stat *)buf)
124 # define TclOSlstat(name, buf) lstat(name, (struct stat *)buf)
125 #endif
126
127 /*
128 *---------------------------------------------------------------------------
129 * Miscellaneous includes that might be missing.
130 *---------------------------------------------------------------------------
131 */
132
133 #include <sys/file.h>
134 #ifdef HAVE_SYS_SELECT_H
135 # include <sys/select.h>
136 #endif
137 #include <sys/stat.h>
138 #ifdef TIME_WITH_SYS_TIME
139 # include <sys/time.h>
140 # include <time.h>
141 #else
142 #ifdef HAVE_SYS_TIME_H
143 # include <sys/time.h>
144 #else
145 # include <time.h>
146 #endif
147 #endif
148 #ifndef NO_SYS_WAIT_H
149 # include <sys/wait.h>
150 #endif
151 #ifdef HAVE_INTTYPES_H
152 # include <inttypes.h>
153 #endif
154 #include <limits.h>
155 #ifdef HAVE_STDINT_H
156 # include <stdint.h>
157 #endif
158 #ifdef HAVE_UNISTD_H
159 # include <unistd.h>
160 #else
161 # include "../compat/unistd.h"
162 #endif
163
164 extern int TclUnixSetBlockingMode(int fd, int mode);
165
166 #include <utime.h>
167
168 /*
169 *---------------------------------------------------------------------------
170 * Socket support stuff: This likely needs more work to parameterize for each
171 * system.
172 *---------------------------------------------------------------------------
173 */
174
175 #include <sys/socket.h> /* struct sockaddr, SOCK_STREAM, ... */
176 #ifndef NO_UNAME
177 # include <sys/utsname.h> /* uname system call. */
178 #endif
179 #include <netinet/in.h> /* struct in_addr, struct sockaddr_in */
180 #include <arpa/inet.h> /* inet_ntoa() */
181 #include <netdb.h> /* getaddrinfo() */
182 #ifdef NEED_FAKE_RFC2553
183 # include "../compat/fake-rfc2553.h"
184 #endif
185
186 /*
187 *---------------------------------------------------------------------------
188 * Some platforms (e.g. SunOS) don't define FLT_MAX and FLT_MIN, so we look
189 * for an alternative definition. If no other alternative is available we use
190 * a reasonable guess.
191 *---------------------------------------------------------------------------
192 */
193
194 #ifndef NO_FLOAT_H
195 # include <float.h>
196 #else
197 #ifndef NO_VALUES_H
198 # include <values.h>
199 #endif
200 #endif
201
202 #ifndef FLT_MAX
203 # ifdef MAXFLOAT
204 # define FLT_MAX MAXFLOAT
205 # else
206 # define FLT_MAX 3.402823466E+38F
207 # endif
208 #endif
209 #ifndef FLT_MIN
210 # ifdef MINFLOAT
211 # define FLT_MIN MINFLOAT
212 # else
213 # define FLT_MIN 1.175494351E-38F
214 # endif
215 #endif
216
217 /*
218 *---------------------------------------------------------------------------
219 * NeXT doesn't define O_NONBLOCK, so #define it here if necessary.
220 *---------------------------------------------------------------------------
221 */
222
223 #ifndef O_NONBLOCK
224 # define O_NONBLOCK 0x80
225 #endif
226
227 /*
228 *---------------------------------------------------------------------------
229 * The type of the status returned by wait varies from UNIX system to UNIX
230 * system. The macro below defines it:
231 *---------------------------------------------------------------------------
232 */
233
234 #ifdef _AIX
235 # define WAIT_STATUS_TYPE pid_t
236 #else
237 #ifndef NO_UNION_WAIT
238 # define WAIT_STATUS_TYPE union wait
239 #else
240 # define WAIT_STATUS_TYPE int
241 #endif
242 #endif
243
244 /*
245 *---------------------------------------------------------------------------
246 * Supply definitions for macros to query wait status, if not already defined
247 * in header files above.
248 *---------------------------------------------------------------------------
249 */
250
251 #ifndef WIFEXITED
252 # define WIFEXITED(stat) (((*((int *) &(stat))) & 0xFF) == 0)
253 #endif
254
255 #ifndef WEXITSTATUS
256 # define WEXITSTATUS(stat) (((*((int *) &(stat))) >> 8) & 0xFF)
257 #endif
258
259 #ifndef WIFSIGNALED
260 # define WIFSIGNALED(stat) \
261 (((*((int *) &(stat)))) && ((*((int *) &(stat))) \
262 == ((*((int *) &(stat))) & 0x00FF)))
263 #endif
264
265 #ifndef WTERMSIG
266 # define WTERMSIG(stat) ((*((int *) &(stat))) & 0x7F)
267 #endif
268
269 #ifndef WIFSTOPPED
270 # define WIFSTOPPED(stat) (((*((int *) &(stat))) & 0xFF) == 0177)
271 #endif
272
273 #ifndef WSTOPSIG
274 # define WSTOPSIG(stat) (((*((int *) &(stat))) >> 8) & 0xFF)
275 #endif
276
277 /*
278 *---------------------------------------------------------------------------
279 * Define constants for waitpid() system call if they aren't defined by a
280 * system header file.
281 *---------------------------------------------------------------------------
282 */
283
284 #ifndef WNOHANG
285 # define WNOHANG 1
286 #endif
287 #ifndef WUNTRACED
288 # define WUNTRACED 2
289 #endif
290
291 /*
292 *---------------------------------------------------------------------------
293 * Supply macros for seek offsets, if they're not already provided by an
294 * include file.
295 *---------------------------------------------------------------------------
296 */
297
298 #ifndef SEEK_SET
299 # define SEEK_SET 0
300 #endif
301 #ifndef SEEK_CUR
302 # define SEEK_CUR 1
303 #endif
304 #ifndef SEEK_END
305 # define SEEK_END 2
306 #endif
307
308 /*
309 *---------------------------------------------------------------------------
310 * The stuff below is needed by the "time" command. If this system has no
311 * gettimeofday call, then must use times() instead.
312 *---------------------------------------------------------------------------
313 */
314
315 #ifdef NO_GETTOD
316 # include <sys/times.h>
317 #else
318 # ifdef HAVE_BSDGETTIMEOFDAY
319 # define gettimeofday BSDgettimeofday
320 # endif
321 #endif
322
323 #ifdef GETTOD_NOT_DECLARED
324 extern int gettimeofday(struct timeval *tp,
325 struct timezone *tzp);
326 #endif
327
328 /*
329 *---------------------------------------------------------------------------
330 * Define access mode constants if they aren't already defined.
331 *---------------------------------------------------------------------------
332 */
333
334 #ifndef F_OK
335 # define F_OK 00
336 #endif
337 #ifndef X_OK
338 # define X_OK 01
339 #endif
340 #ifndef W_OK
341 # define W_OK 02
342 #endif
343 #ifndef R_OK
344 # define R_OK 04
345 #endif
346
347 /*
348 *---------------------------------------------------------------------------
349 * Define FD_CLOEEXEC (the close-on-exec flag bit) if it isn't already
350 * defined.
351 *---------------------------------------------------------------------------
352 */
353
354 #ifndef FD_CLOEXEC
355 # define FD_CLOEXEC 1
356 #endif
357
358 /*
359 *---------------------------------------------------------------------------
360 * On systems without symbolic links (i.e. S_IFLNK isn't defined) define
361 * "lstat" to use "stat" instead.
362 *---------------------------------------------------------------------------
363 */
364
365 #ifndef S_IFLNK
366 # undef TclOSlstat
367 # define lstat stat
368 # define lstat64 stat64
369 # define TclOSlstat TclOSstat
370 #endif
371
372 /*
373 *---------------------------------------------------------------------------
374 * Define macros to query file type bits, if they're not already defined.
375 *---------------------------------------------------------------------------
376 */
377
378 #ifndef S_ISREG
379 # ifdef S_IFREG
380 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
381 # else
382 # define S_ISREG(m) 0
383 # endif
384 #endif /* !S_ISREG */
385 #ifndef S_ISDIR
386 # ifdef S_IFDIR
387 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
388 # else
389 # define S_ISDIR(m) 0
390 # endif
391 #endif /* !S_ISDIR */
392 #ifndef S_ISCHR
393 # ifdef S_IFCHR
394 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
395 # else
396 # define S_ISCHR(m) 0
397 # endif
398 #endif /* !S_ISCHR */
399
400 #ifndef S_ISBLK
401 # ifdef S_IFBLK
402 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
403 # else
404 # define S_ISBLK(m) 0
405 # endif
406 #endif /* !S_ISBLK */
407
408 #ifndef S_ISFIFO
409 # ifdef S_IFIFO
410 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
411 # else
412 # define S_ISFIFO(m) 0
413 # endif
414 #endif /* !S_ISFIFO */
415
416 #ifndef S_ISLNK
417 # ifdef S_IFLNK
418 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
419 # else
420 # define S_ISLNK(m) 0
421 # endif
422 #endif /* !S_ISLNK */
423
424 #ifndef S_ISSOCK
425 # ifdef S_IFSOCK
426 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
427 # else
428 # define S_ISSOCK(m) 0
429 # endif
430 #endif /* !S_ISSOCK */
431
432 /*
433 *---------------------------------------------------------------------------
434 * Make sure that MAXPATHLEN and MAXNAMLEN are defined.
435 *---------------------------------------------------------------------------
436 */
437
438 #ifndef MAXPATHLEN
439 # ifdef PATH_MAX
440 # define MAXPATHLEN PATH_MAX
441 # else
442 # define MAXPATHLEN 2048
443 # endif
444 #endif
445
446 #ifndef MAXNAMLEN
447 # ifdef NAME_MAX
448 # define MAXNAMLEN NAME_MAX
449 # else
450 # define MAXNAMLEN 255
451 # endif
452 #endif
453
454 /*
455 *---------------------------------------------------------------------------
456 * The following macro defines the type of the mask arguments to select:
457 *---------------------------------------------------------------------------
458 */
459
460 #ifndef NO_FD_SET
461 # define SELECT_MASK fd_set
462 #else /* NO_FD_SET */
463 # ifndef _AIX
464 typedef long fd_mask;
465 # endif /* !AIX */
466 # if defined(_IBMR2)
467 # define SELECT_MASK void
468 # else /* !defined(_IBMR2) */
469 # define SELECT_MASK int
470 # endif /* defined(_IBMR2) */
471 #endif /* !NO_FD_SET */
472
473 /*
474 *---------------------------------------------------------------------------
475 * Define "NBBY" (number of bits per byte) if it's not already defined.
476 *---------------------------------------------------------------------------
477 */
478
479 #ifndef NBBY
480 # define NBBY 8
481 #endif
482
483 /*
484 *---------------------------------------------------------------------------
485 * The following macro defines the number of fd_masks in an fd_set:
486 *---------------------------------------------------------------------------
487 */
488
489 #ifndef FD_SETSIZE
490 # ifdef OPEN_MAX
491 # define FD_SETSIZE OPEN_MAX
492 # else
493 # define FD_SETSIZE 256
494 # endif
495 #endif /* FD_SETSIZE */
496
497 #ifndef howmany
498 # define howmany(x, y) (((x)+((y)-1))/(y))
499 #endif /* !defined(howmany) */
500
501 #ifndef NFDBITS
502 # define NFDBITS NBBY*sizeof(fd_mask)
503 #endif /* NFDBITS */
504
505 #define MASK_SIZE howmany(FD_SETSIZE, NFDBITS)
506
507 /*
508 *---------------------------------------------------------------------------
509 * Not all systems declare the errno variable in errno.h, so this file does it
510 * explicitly. The list of system error messages also isn't generally declared
511 * in a header file anywhere.
512 *---------------------------------------------------------------------------
513 */
514
515 #ifdef NO_ERRNO
516 extern int errno;
517 #endif /* NO_ERRNO */
518
519 /*
520 *---------------------------------------------------------------------------
521 * Not all systems declare all the errors that Tcl uses! Provide some
522 * work-arounds...
523 *---------------------------------------------------------------------------
524 */
525
526 #ifndef EOVERFLOW
527 # ifdef EFBIG
528 # define EOVERFLOW EFBIG
529 # else /* !EFBIG */
530 # define EOVERFLOW EINVAL
531 # endif /* EFBIG */
532 #endif /* EOVERFLOW */
533
534 /*
535 *---------------------------------------------------------------------------
536 * Variables provided by the C library:
537 *---------------------------------------------------------------------------
538 */
539
540 #if defined(__APPLE__) && defined(__DYNAMIC__)
541 # include <crt_externs.h>
542 # define environ (*_NSGetEnviron())
543 # define USE_PUTENV 1
544 #else
545 # if defined(_sgi) || defined(__sgi)
546 # define environ _environ
547 # endif
548 extern char ** environ;
549 #endif
550
551 /*
552 *---------------------------------------------------------------------------
553 * Darwin specifc configure overrides.
554 *---------------------------------------------------------------------------
555 */
556
557 #ifdef __APPLE__
558
559 /*
560 *---------------------------------------------------------------------------
561 * Support for fat compiles: configure runs only once for multiple architectures
562 *---------------------------------------------------------------------------
563 */
564
565 # if defined(__LP64__) && defined (NO_COREFOUNDATION_64)
566 # undef HAVE_COREFOUNDATION
567 # endif /* __LP64__ && NO_COREFOUNDATION_64 */
568 # include <sys/cdefs.h>
569 # ifdef __DARWIN_UNIX03
570 # if __DARWIN_UNIX03
571 # undef HAVE_PUTENV_THAT_COPIES
572 # else
573 # define HAVE_PUTENV_THAT_COPIES 1
574 # endif
575 # endif /* __DARWIN_UNIX03 */
576
577 /*
578 *---------------------------------------------------------------------------
579 * Include AvailabilityMacros.h here (when available) to ensure any symbolic
580 * MAC_OS_X_VERSION_* constants passed on the command line are translated.
581 *---------------------------------------------------------------------------
582 */
583
584 # ifdef HAVE_AVAILABILITYMACROS_H
585 # include <AvailabilityMacros.h>
586 # endif
587
588 /*
589 *---------------------------------------------------------------------------
590 * Support for weak import.
591 *---------------------------------------------------------------------------
592 */
593
594 # ifdef HAVE_WEAK_IMPORT
595 # if !defined(HAVE_AVAILABILITYMACROS_H) || !defined(MAC_OS_X_VERSION_MIN_REQUIRED)
596 # undef HAVE_WEAK_IMPORT
597 # else
598 # ifndef WEAK_IMPORT_ATTRIBUTE
599 # define WEAK_IMPORT_ATTRIBUTE __attribute__((weak_import))
600 # endif
601 # endif
602 # endif /* HAVE_WEAK_IMPORT */
603
604 /*
605 *---------------------------------------------------------------------------
606 * Support for MAC_OS_X_VERSION_MAX_ALLOWED define from AvailabilityMacros.h:
607 * only use API available in the indicated OS version or earlier.
608 *---------------------------------------------------------------------------
609 */
610
611 # ifdef MAC_OS_X_VERSION_MAX_ALLOWED
612 # if MAC_OS_X_VERSION_MAX_ALLOWED < 1050 && defined(__LP64__)
613 # undef HAVE_COREFOUNDATION
614 # endif
615 # if MAC_OS_X_VERSION_MAX_ALLOWED < 1040
616 # undef HAVE_OSSPINLOCKLOCK
617 # undef HAVE_PTHREAD_ATFORK
618 # undef HAVE_COPYFILE
619 # endif
620 # if MAC_OS_X_VERSION_MAX_ALLOWED < 1030
621 # ifdef TCL_THREADS
622 /* prior to 10.3, realpath is not threadsafe, c.f. bug 711232 */
623 # define NO_REALPATH 1
624 # endif
625 # undef HAVE_LANGINFO
626 # endif
627 # endif /* MAC_OS_X_VERSION_MAX_ALLOWED */
628 # if defined(HAVE_COREFOUNDATION) && defined(__LP64__) && \
629 defined(HAVE_WEAK_IMPORT) && MAC_OS_X_VERSION_MIN_REQUIRED < 1050
630 # warning "Weak import of 64-bit CoreFoundation is not supported, will not run on Mac OS X < 10.5."
631 # endif
632 /*
633 * For now, test exec-17.1 fails (I/O setup after closing stdout) with
634 * posix_spawnp(), but the classic implementation (based on fork()+execvp())
635 * works well under macOS.
636 */
637 # undef HAVE_POSIX_SPAWNP
638 # undef HAVE_VFORK
639 #endif /* __APPLE__ */
640
641 /*
642 *---------------------------------------------------------------------------
643 * The following macros and declarations represent the interface between
644 * generic and unix-specific parts of Tcl. Some of the macros may override
645 * functions declared in tclInt.h.
646 *---------------------------------------------------------------------------
647 */
648
649 /*
650 * The default platform eol translation on Unix is TCL_TRANSLATE_LF.
651 */
652
653 #ifdef DJGPP
654 #define TCL_PLATFORM_TRANSLATION TCL_TRANSLATE_CRLF
655 typedef int socklen_t;
656 #else
657 #define TCL_PLATFORM_TRANSLATION TCL_TRANSLATE_LF
658 #endif
659
660 /*
661 *---------------------------------------------------------------------------
662 * The following macros have trivial definitions, allowing generic code to
663 * address platform-specific issues.
664 *---------------------------------------------------------------------------
665 */
666
667 #define TclpReleaseFile(file) /* Nothing. */
668
669 /*
670 *---------------------------------------------------------------------------
671 * The following defines wrap the system memory allocation routines.
672 *---------------------------------------------------------------------------
673 */
674
675 #define TclpSysAlloc(size, isBin) malloc((size_t)(size))
676 #define TclpSysFree(ptr) free((char *)(ptr))
677 #define TclpSysRealloc(ptr, size) realloc((char *)(ptr), (size_t)(size))
678
679 /*
680 *---------------------------------------------------------------------------
681 * The following macros and declaration wrap the C runtime library functions.
682 *---------------------------------------------------------------------------
683 */
684
685 #define TclpExit exit
686
687 #ifdef TCL_THREADS
688 # include <pthread.h>
689 #endif /* TCL_THREADS */
690
691 /* FIXME - Hyper-enormous platform assumption! */
692 #ifndef AF_INET6
693 # define AF_INET6 10
694 #endif
695
696 /*
697 *---------------------------------------------------------------------------
698 * Set of MT-safe implementations of some known-to-be-MT-unsafe library calls.
699 * Instead of returning pointers to the static storage, those return pointers
700 * to the TSD data.
701 *---------------------------------------------------------------------------
702 */
703
704 #include <pwd.h>
705 #include <grp.h>
706
707 extern struct passwd * TclpGetPwNam(const char *name);
708 extern struct group * TclpGetGrNam(const char *name);
709 extern struct passwd * TclpGetPwUid(uid_t uid);
710 extern struct group * TclpGetGrGid(gid_t gid);
711 extern struct hostent * TclpGetHostByName(const char *name);
712 extern struct hostent * TclpGetHostByAddr(const char *addr,
713 int length, int type);
714 extern void *TclpMakeTcpClientChannelMode(
715 void *tcpSocket, int mode);
716
717 #endif /* _TCLUNIXPORT */
718
719 /*
720 * Local Variables:
721 * mode: c
722 * c-basic-offset: 4
723 * fill-column: 78
724 * End:
725 */