2 * Copyright 1997-2003 by The XFree86 Project, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the names of Orest Zborowski and David Wexelblat
9 * not be used in advertising or publicity pertaining to distribution of
10 * the software without specific, written prior permission. Orest Zborowski
11 * and David Wexelblat make no representations about the suitability of this
12 * software for any purpose. It is provided "as is" without express or
15 * THE XFREE86 PROJECT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD
16 * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL OREST ZBOROWSKI OR DAVID WEXELBLAT BE LIABLE
18 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #ifdef HAVE_XORG_CONFIG_H
26 #include <xorg-config.h>
29 #if defined(linux) && !defined(__GLIBC__)
30 #undef __STRICT_ANSI__
35 #include <sys/types.h>
48 #include <X11/Xfuncproto.h>
55 #include <sys/ioctl.h>
56 #ifdef HAS_SVR3_MMAPDRV
58 #ifdef SELF_CONTAINED_WRAPPER
59 #include <sys/at_ansi.h>
61 #include <sys/sysmacros.h>
62 #if !defined(_NEED_SYSI86)
63 # include <sys/immu.h>
64 # include <sys/region.h>
67 struct kd_memloc MapDSC
;
70 extern struct kd_memloc MapDSC
;
77 #define MAP_FAILED ((caddr_t)-1)
84 #define NEED_XF86_TYPES 1
85 #define NEED_XF86_PROTOTYPES 1
86 #define DONT_DEFINE_WRAPPERS
87 #include "xf86_ansic.h"
89 #ifndef SELF_CONTAINED_WRAPPER
92 #define NO_OSLIB_PROTOTYPES
94 #define HAVE_WRAPPER_DECLS
95 #include "xf86_OSlib.h"
97 void xf86WrapperInit(void);
112 #define dirent direct
117 typedef struct dirent DIRENTRY
;
120 #include <sys/types.h>
121 #define WIFEXITED(a) ((a & 0x00ff) == 0) /* LSB will be 0 */
122 #define WEXITSTATUS(a) ((a & 0xff00) >> 8)
123 #define WIFSIGNALED(a) ((a & 0xff00) == 0) /* MSB will be 0 */
124 #define WTERMSIG(a) (a & 0x00ff)
126 #if defined(ISC) && !defined(_POSIX_SOURCE)
127 #define _POSIX_SOURCE
128 #include <sys/types.h>
129 #include <sys/wait.h>
132 #if (defined(ISC) && defined(_POSIX_SOURCE)) || defined(Lynx) || (defined (__alpha__) && defined(linux))
133 #include <sys/types.h>
135 #include <sys/wait.h>
139 #if !defined(S_IFIFO) && defined(S_IFFIFO)
140 #define S_IFIFO S_IFFIFO
144 /* For xf86getpagesize() */
146 #define HAS_SC_PAGESIZE
147 #define HAS_GETPAGESIZE
148 #elif defined(CSRG_BASED)
149 #define HAS_GETPAGESIZE
151 #define HAS_GETPAGESIZE
152 #elif defined(sun) && !defined(SVR4)
153 #define HAS_GETPAGESIZE
164 #if defined(setjmp) && defined(__GNU_LIBRARY__) && \
165 (!defined(__GLIBC__) || (__GLIBC__ < 2) || \
166 ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 3)))
167 #define HAS_GLIBC_SIGSETJMP 1
171 #define SETBUF_RETURNS_INT
174 _X_EXPORT
double xf86HUGE_VAL
;
176 #ifndef SELF_CONTAINED_WRAPPERS
177 extern void xf86DisableIO(void);
181 * This file contains the XFree86 wrappers for libc functions that can be
182 * called by loadable modules
186 xf86hypot(double x
, double y
)
192 xf86qsort(void *base
, xf86size_t nmemb
, xf86size_t size
,
193 int (*comp
)(const void *, const void *))
195 qsort(base
, nmemb
, size
, comp
);
198 /* string functions */
201 xf86strcat(char* dest
, const char* src
)
203 return(strcat(dest
,src
));
207 xf86strchr(const char* s
, int c
)
213 xf86strcmp(const char* s1
, const char* s2
)
215 return strcmp(s1
,s2
);
218 /* Just like the BSD version. It assumes that tolower() is ANSI-compliant */
220 xf86strcasecmp(const char* s1
, const char* s2
)
222 const unsigned char *us1
= (const unsigned char *)s1
;
223 const unsigned char *us2
= (const unsigned char *)s2
;
225 while (tolower(*us1
) == tolower(*us2
++))
229 return tolower(*us1
) - tolower(*--us2
);
233 xf86strcpy(char* dest
, const char* src
)
235 return strcpy(dest
,src
);
239 xf86strcspn(const char* s1
, const char* s2
)
241 return (xf86size_t
)strcspn(s1
,s2
);
245 xf86strlen(const char* s
)
247 return (xf86size_t
)strlen(s
);
251 xf86strlcat(char *dest
, const char *src
, xf86size_t size
)
253 return(strlcat(dest
, src
, size
));
257 xf86strlcpy(char *dest
, const char *src
, xf86size_t size
)
259 return strlcpy(dest
, src
, size
);
263 xf86strncat(char* dest
, const char* src
, xf86size_t n
)
265 return strncat(dest
,src
,(size_t)n
);
269 xf86strncmp(const char* s1
, const char* s2
, xf86size_t n
)
271 return strncmp(s1
,s2
,(size_t)n
);
274 /* Just like the BSD version. It assumes that tolower() is ANSI-compliant */
276 xf86strncasecmp(const char* s1
, const char* s2
, xf86size_t n
)
279 const unsigned char *us1
= (const unsigned char *)s1
;
280 const unsigned char *us2
= (const unsigned char *)s2
;
283 if (tolower(*us1
) != tolower(*us2
++))
284 return tolower(*us1
) - tolower(*--us2
);
293 xf86strncpy(char* dest
, const char* src
, xf86size_t n
)
295 return strncpy(dest
,src
,(size_t)n
);
299 xf86strpbrk(const char* s1
, const char* s2
)
301 return strpbrk(s1
,s2
);
305 xf86strrchr(const char* s
, int c
)
311 xf86strspn(const char* s1
, const char* s2
)
313 return strspn(s1
,s2
);
317 xf86strstr(const char* s1
, const char* s2
)
319 return strstr(s1
,s2
);
323 xf86strtok(char* s1
, const char* s2
)
325 return strtok(s1
,s2
);
329 xf86strdup(const char* s
)
335 xf86sprintf(char *s
, const char *format
, ...)
339 va_start(args
, format
);
340 ret
= vsprintf(s
, format
, args
);
346 xf86snprintf(char *s
, xf86size_t len
, const char *format
, ...)
350 va_start(args
, format
);
351 ret
= vsnprintf(s
, (size_t)len
, format
, args
);
357 xf86bzero(void* s
, unsigned int n
)
364 xf86sscanf(char *s
, const char *format
, ...)
367 xf86sscanf(char *s
, const char *format
, char *a0
, char *a1
, char *a2
,
368 char *a3
, char *a4
, char *a5
, char *a6
, char *a7
, char *a8
,
369 char *a9
) /* limit of ten args */
375 va_start(args
, format
);
377 ret
= vsscanf(s
,format
,args
);
381 return sscanf(s
, format
, a0
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
);
387 _X_EXPORT
int xf86errno
;
389 /* XXX This is not complete */
392 xfToOsOpenFlags(int xfflags
)
396 /* XXX This assumes O_RDONLY is 0 */
397 if (xfflags
& XF86_O_WRONLY
)
399 if (xfflags
& XF86_O_RDWR
)
401 if (xfflags
& XF86_O_CREAT
)
408 xf86open(const char *path
, int flags
, ...)
414 flags
= xfToOsOpenFlags(flags
);
415 if (flags
& O_CREAT
) {
416 /* can't request a mode_t directly on systems where mode_t
417 is an unsigned short */
418 mode_t mode
= (mode_t
)va_arg(ap
, unsigned int);
419 fd
= open(path
, flags
, mode
);
421 fd
= open(path
, flags
);
424 xf86errno
= xf86GetErrno();
432 int status
= close(fd
);
434 xf86errno
= xf86GetErrno();
439 xf86lseek(int fd
, long offset
, int whence
)
452 return (long)lseek(fd
, (off_t
)offset
, whence
);
456 xf86ioctl(int fd
, unsigned long request
, pointer argp
)
458 int status
= ioctl(fd
, request
, argp
);
460 xf86errno
= xf86GetErrno();
464 _X_EXPORT xf86ssize_t
465 xf86read(int fd
, void *buf
, xf86size_t nbytes
)
467 xf86ssize_t n
= read(fd
, buf
, (size_t)nbytes
);
469 xf86errno
= xf86GetErrno();
473 _X_EXPORT xf86ssize_t
474 xf86write(int fd
, const void *buf
, xf86size_t nbytes
)
476 xf86ssize_t n
= write(fd
, buf
, (size_t)nbytes
);
478 xf86errno
= xf86GetErrno();
483 xf86mmap(void *start
, xf86size_t length
, int prot
,
484 int flags
, int fd
, xf86size_t
/* off_t */ offset
)
490 if (flags
& XF86_MAP_FIXED
) f
|= MAP_FIXED
;
491 if (flags
& XF86_MAP_SHARED
) f
|= MAP_SHARED
;
492 if (flags
& XF86_MAP_PRIVATE
) f
|= MAP_PRIVATE
;
493 #if defined(__amd64__) && defined(linux)
494 if (flags
& XF86_MAP_32BIT
) f
|= MAP_32BIT
;
496 if (prot
& XF86_PROT_EXEC
) p
|= PROT_EXEC
;
497 if (prot
& XF86_PROT_READ
) p
|= PROT_READ
;
498 if (prot
& XF86_PROT_WRITE
) p
|= PROT_WRITE
;
499 if (prot
& XF86_PROT_NONE
) p
|= PROT_NONE
;
501 rc
= mmap(start
,(size_t)length
,p
,f
,fd
,(off_t
)offset
);
503 xf86errno
= xf86GetErrno();
504 if (rc
== MAP_FAILED
)
505 return XF86_MAP_FAILED
;
509 #ifdef HAS_SVR3_MMAPDRV
511 #ifdef SELF_CONTAINED_WRAPPER
513 if ((mmapFd
= open("/dev/mmap", O_RDWR
)) == -1) {
514 ErrorF("Warning: failed to open /dev/mmap \n");
515 xf86errno
= xf86_ENOSYS
;
516 return XF86_MAP_FAILED
;
520 MapDSC
.vaddr
= (char *)start
;
521 MapDSC
.physaddr
= (char *)offset
;
522 MapDSC
.length
= length
;
525 rc
= (pointer
)ioctl(mmapFd
, MAP
, &MapDSC
);
526 xf86errno
= xf86GetErrno();
528 return XF86_MAP_FAILED
;
532 ErrorF("Warning: mmap() is not supported on this platform\n");
533 xf86errno
= xf86_ENOSYS
;
534 return XF86_MAP_FAILED
;
540 xf86munmap(void *start
, xf86size_t length
)
543 int rc
= munmap(start
,(size_t)length
);
545 xf86errno
= xf86GetErrno();
548 #ifdef HAS_SVR3_MMAPDRV
549 int rc
= ioctl(mmapFd
, UNMAPRM
, start
);
551 xf86errno
= xf86GetErrno();
554 ErrorF("Warning: munmap() is not supported on this platform\n");
555 xf86errno
= xf86_ENOSYS
;
562 xf86stat(const char *file_name
, struct xf86stat
*xfst
)
567 rc
= stat(file_name
, &st
);
568 xf86errno
= xf86GetErrno();
569 xfst
->st_rdev
= st
.st_rdev
; /* Not much is currently supported */
574 xf86fstat(int fd
, struct xf86stat
*xfst
)
580 xf86errno
= xf86GetErrno();
581 xfst
->st_rdev
= st
.st_rdev
; /* Not much is currently supported */
586 xfToOsAccessMode(int xfmode
)
589 case XF86_R_OK
: return R_OK
;
590 case XF86_W_OK
: return W_OK
;
591 case XF86_X_OK
: return X_OK
;
592 case XF86_F_OK
: return F_OK
;
598 xf86access(const char *pathname
, int mode
)
602 mode
= xfToOsAccessMode(mode
);
603 rc
= access(pathname
, mode
);
604 xf86errno
= xf86GetErrno();
610 /* limited stdio support */
612 #define XF86FILE_magic 0x58464856 /* "XFHV" */
614 typedef struct _xf86_file_
{
621 static XF86FILE_priv stdhnd
[3] = {
622 { 0, XF86FILE_magic
, NULL
, "$stdinp$" },
623 { 0, XF86FILE_magic
, NULL
, "$stdout$" },
624 { 0, XF86FILE_magic
, NULL
, "$stderr$" }
627 _X_EXPORT XF86FILE
* xf86stdin
= (XF86FILE
*)&stdhnd
[0];
628 _X_EXPORT XF86FILE
* xf86stdout
= (XF86FILE
*)&stdhnd
[1];
629 _X_EXPORT XF86FILE
* xf86stderr
= (XF86FILE
*)&stdhnd
[2];
634 if (stdhnd
[0].filehnd
== NULL
)
635 stdhnd
[0].filehnd
= stdin
;
636 if (stdhnd
[1].filehnd
== NULL
)
637 stdhnd
[1].filehnd
= stdout
;
638 if (stdhnd
[2].filehnd
== NULL
)
639 stdhnd
[2].filehnd
= stderr
;
640 xf86HUGE_VAL
= HUGE_VAL
;
644 xf86fopen(const char* fn
, const char* mode
)
647 FILE *f
= fopen(fn
,mode
);
648 xf86errno
= xf86GetErrno();
651 fp
= xalloc(sizeof(XF86FILE_priv
));
652 fp
->magic
= XF86FILE_magic
;
654 fp
->fileno
= fileno(f
);
655 fp
->fname
= xf86strdup(fn
);
657 ErrorF("xf86fopen(%s,%s) yields FILE %p XF86FILE %p\n",
660 return (XF86FILE
*)fp
;
663 static void _xf86checkhndl(XF86FILE_priv
* f
,const char *func
)
665 if (!f
|| f
->magic
!= XF86FILE_magic
||
666 !f
->filehnd
|| !f
->fname
) {
667 FatalError("libc_wrapper error: passed invalid FILE handle to %s",
674 xf86fclose(XF86FILE
* f
)
676 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
679 _xf86checkhndl(fp
,"xf86fclose");
681 /* somewhat bad check */
682 if (fp
->fileno
< 3 && fp
->fname
[0]=='$') {
683 /* assume this is stdin/out/err, don't dispose */
684 ret
= fclose(fp
->filehnd
);
686 ret
= fclose(fp
->filehnd
);
687 fp
->magic
= 0; /* invalidate */
695 xf86printf(const char *format
, ...)
699 va_start(args
, format
);
701 ret
= printf(format
,args
);
707 xf86fprintf(XF86FILE
* f
, const char *format
, ...)
709 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
713 va_start(args
, format
);
716 ErrorF("xf86fprintf for XF86FILE %p\n", fp
);
718 _xf86checkhndl(fp
,"xf86fprintf");
720 ret
= vfprintf(fp
->filehnd
,format
,args
);
726 xf86vfprintf(XF86FILE
* f
, const char *format
, va_list ap
)
728 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
731 ErrorF("xf86vfprintf for XF86FILE %p\n", fp
);
733 _xf86checkhndl(fp
,"xf86vfprintf");
735 return vfprintf(fp
->filehnd
,format
,ap
);
739 xf86vsprintf(char *s
, const char *format
, va_list ap
)
741 return vsprintf(s
, format
, ap
);
745 xf86vsnprintf(char *s
, xf86size_t len
, const char *format
, va_list ap
)
747 return vsnprintf(s
, (size_t)len
, format
, ap
);
752 xf86fscanf(XF86FILE
* f
, const char *format
, ...)
755 xf86fscanf(XF86FILE
* f
, const char *format
, char *a0
, char *a1
, char *a2
,
756 char *a3
, char *a4
, char *a5
, char *a6
, char *a7
, char *a8
,
757 char *a9
) /* limit of ten args */
760 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
765 va_start(args
, format
);
767 _xf86checkhndl(fp
,"xf86fscanf");
769 ret
= vfscanf(fp
->filehnd
,format
,args
);
773 _xf86checkhndl(fp
,"xf86fscanf");
774 return fscanf(fp
->filehnd
, format
, a0
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
);
779 xf86fgets(char *buf
, INT32 n
, XF86FILE
* f
)
781 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
783 _xf86checkhndl(fp
,"xf86fgets");
784 return fgets(buf
,(int)n
,fp
->filehnd
);
788 xf86fputs(const char *buf
, XF86FILE
* f
)
790 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
792 _xf86checkhndl(fp
,"xf86fputs");
793 return fputs(buf
,fp
->filehnd
);
797 xf86getc(XF86FILE
* f
)
799 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
801 _xf86checkhndl(fp
,"xf86getc");
802 return getc(fp
->filehnd
);
806 xf86fgetc(XF86FILE
* f
)
808 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
810 _xf86checkhndl(fp
,"xf86fgetc");
811 return fgetc(fp
->filehnd
);
815 xf86fputc(int c
,XF86FILE
* f
)
817 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
819 _xf86checkhndl(fp
,"xf86fputc");
820 return fputc(c
,fp
->filehnd
);
824 xf86fflush(XF86FILE
* f
)
826 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
828 _xf86checkhndl(fp
,"xf86fflush");
829 return fflush(fp
->filehnd
);
833 xf86fread(void* buf
, xf86size_t sz
, xf86size_t cnt
, XF86FILE
* f
)
835 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
838 ErrorF("xf86fread for XF86FILE %p\n", fp
);
840 _xf86checkhndl(fp
,"xf86fread");
841 return fread(buf
,(size_t)sz
,(size_t)cnt
,fp
->filehnd
);
845 xf86fwrite(const void* buf
, xf86size_t sz
, xf86size_t cnt
, XF86FILE
* f
)
847 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
849 _xf86checkhndl(fp
,"xf86fwrite");
850 return fwrite(buf
,(size_t)sz
,(size_t)cnt
,fp
->filehnd
);
854 xf86fseek(XF86FILE
* f
, long offset
, int whence
)
856 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
858 _xf86checkhndl(fp
,"xf86fseek");
870 return fseek(fp
->filehnd
,offset
,whence
);
874 xf86ftell(XF86FILE
* f
)
876 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
878 _xf86checkhndl(fp
,"xf86ftell");
879 return ftell(fp
->filehnd
);
882 #define mapnum(e) case (xf86_##e): err = e; break;
891 case 0: err
= 0; break;
900 mapnum (ELOOP
); /* not POSIX 1 */
902 mapnum (ENAMETOOLONG
);
910 mapnum (ETXTBSY
); /* not POSIX 1 */
958 return strerror(err
);
964 /* required for portable fgetpos/fsetpos,
966 * XF86fpos_t* pos = xalloc(xf86fpossize());
971 return sizeof(fpos_t);
975 xf86fgetpos(XF86FILE
* f
,XF86fpos_t
* pos
)
977 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
978 fpos_t *ppos
= (fpos_t*)pos
;
980 _xf86checkhndl(fp
,"xf86fgetpos");
982 return fgetpos(fp
->filehnd
,ppos
);
984 *ppos
= ftell(fp
->filehnd
);
992 xf86fsetpos(XF86FILE
* f
,const XF86fpos_t
* pos
)
994 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
995 fpos_t *ppos
= (fpos_t*)pos
;
997 /* XXX need to handle xf86errno here */
998 _xf86checkhndl(fp
,"xf86fsetpos");
1000 return fsetpos(fp
->filehnd
,ppos
);
1007 return fseek(fp
->filehnd
, *ppos
, SEEK_SET
);
1012 xf86perror(const char *s
)
1018 xf86remove(const char *s
)
1020 #ifdef _POSIX_SOURCE
1028 xf86rename(const char *old
, const char *new)
1030 #ifdef _POSIX_SOURCE
1031 return rename(old
,new);
1033 int ret
= link(old
,new);
1036 if (ret
) unlink(new);
1044 xf86rewind(XF86FILE
* f
)
1046 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
1048 _xf86checkhndl(fp
,"xf86fsetpos");
1049 rewind(fp
->filehnd
);
1053 xf86clearerr(XF86FILE
* f
)
1055 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
1057 _xf86checkhndl(fp
,"xf86clearerr");
1058 clearerr(fp
->filehnd
);
1062 xf86feof(XF86FILE
* f
)
1064 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
1066 _xf86checkhndl(fp
,"xf86feof");
1067 return feof(fp
->filehnd
);
1071 xf86ferror(XF86FILE
* f
)
1073 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
1075 _xf86checkhndl(fp
,"xf86ferror");
1076 return ferror(fp
->filehnd
);
1080 xf86freopen(const char* fname
,const char* mode
,XF86FILE
* fold
)
1082 XF86FILE_priv
* fp
= (XF86FILE_priv
*)fold
;
1085 _xf86checkhndl(fp
,"xf86freopen");
1086 fnew
= freopen(fname
,mode
,fp
->filehnd
);
1087 xf86errno
= xf86GetErrno();
1089 xf86fclose(fold
); /* discard old XF86FILE structure */
1092 /* recycle the old XF86FILE structure */
1093 fp
->magic
= XF86FILE_magic
;
1095 fp
->fileno
= fileno(fnew
);
1096 fp
->fname
= xf86strdup(fname
);
1098 ErrorF("xf86freopen(%s,%s,%p) yields FILE %p XF86FILE %p\n",
1099 fname
,mode
,fold
,fnew
,fp
);
1101 return (XF86FILE
*)fp
;
1105 xf86setbuf(XF86FILE
* f
, char *buf
)
1107 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
1109 _xf86checkhndl(fp
,"xf86fsetbuf");
1110 #ifdef SETBUF_RETURNS_INT
1111 return setbuf(fp
->filehnd
, buf
);
1113 setbuf(fp
->filehnd
, buf
);
1119 xf86setvbuf(XF86FILE
* f
, char *buf
, int mode
, xf86size_t size
)
1121 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
1124 _xf86checkhndl(fp
,"xf86fsetvbuf");
1137 FatalError("libc_wrapper error: mode in setvbuf incorrect");
1141 return setvbuf(fp
->filehnd
,buf
,vbufmode
,(size_t)size
);
1148 return xf86fopen(tmpnam((char*)0),"w+");
1151 FILE *f
= tmpfile();
1152 xf86errno
= xf86GetErrno();
1155 fp
= xalloc(sizeof(XF86FILE_priv
));
1156 fp
->magic
= XF86FILE_magic
;
1158 fp
->fileno
= fileno(f
);
1159 fp
->fname
= xf86strdup("*tmpfile*"); /* so that it can be xfree()'d */
1161 ErrorF("xf86tmpfile() yields FILE %p XF86FILE %p\n",f
,fp
);
1163 return (XF86FILE
*)fp
;
1165 #endif /* HAS_TMPFILE */
1169 xf86ungetc(int c
,XF86FILE
* f
)
1171 XF86FILE_priv
* fp
= (XF86FILE_priv
*)f
;
1173 _xf86checkhndl(fp
,"xf86ungetc");
1174 return ungetc(c
,fp
->filehnd
);
1177 /* Misc functions. Some are ANSI C, some are not. */
1183 #if (defined(SYSV) || defined(SVR4)) && !defined(sun)
1184 syscall(3112, (usec
) / 1000 + 1);
1191 xf86getsecs(long * secs
, long * usecs
)
1195 X_GETTIMEOFDAY(&tv
);
1208 if (mask
== 0) return 0;
1209 for (n
= 1; (mask
& 1)==0; n
++)
1215 xf86getenv(const char * a
)
1217 /* Only allow this when the real and effective uids are the same */
1218 if (getuid() != geteuid())
1225 xf86bsearch(const void *key
, const void *base
, xf86size_t nmemb
,
1226 xf86size_t size
, int (*compar
)(const void *, const void *))
1228 return bsearch(key
, base
, (size_t)nmemb
, (size_t)size
, compar
);
1232 xf86execl(const char *pathname
, const char *arg
, ...)
1239 va_start(args
, arg
);
1240 arglist
[0] = (char*)&args
;
1242 while (i
< 5 && (arglist
[i
++] = va_arg(args
, char *)) != NULL
)
1246 if ((pid
= fork()) < 0) {
1247 ErrorF("Fork failed (%s)\n", strerror(errno
));
1249 } else if (pid
== 0) { /* child */
1251 * Make sure that the child doesn't inherit any I/O permissions it
1252 * shouldn't have. It's better to put constraints on the development
1253 * of a clock program than to give I/O permissions to a bogus program
1254 * in someone's XF86Config file
1256 #ifndef SELF_CONTAINED_WRAPPER
1259 if (setuid(getuid()) == -1) {
1260 ErrorF("xf86Execl: setuid() failed: %s\n", strerror(errno
));
1263 #if !defined(SELF_CONTAINED_WRAPPER)
1264 /* set stdin, stdout to the consoleFD, and leave stderr alone */
1265 for (i
= 0; i
< 2; i
++)
1267 if (xf86Info
.consoleFd
!= i
)
1270 dup(xf86Info
.consoleFd
);
1275 execv(pathname
, arglist
);
1276 ErrorF("Exec failed for command \"%s\" (%s)\n",
1277 pathname
, strerror(errno
));
1283 if (WIFEXITED(exit_status
))
1285 switch (WEXITSTATUS(exit_status
))
1289 case 255: /* exec() failed */
1291 default: /* bad exit status */
1292 ErrorF("Program \"%s\" had bad exit status %d\n",
1293 pathname
, WEXITSTATUS(exit_status
));
1294 return(WEXITSTATUS(exit_status
));
1297 else if (WIFSIGNALED(exit_status
))
1299 ErrorF("Program \"%s\" died on signal %d\n",
1300 pathname
, WTERMSIG(exit_status
));
1301 return(WTERMSIG(exit_status
));
1304 else if (WIFSTOPPED(exit_status
))
1306 ErrorF("Program \"%s\" stopped by signal %d\n",
1307 pathname
, WSTOPSIG(exit_status
));
1308 return(WSTOPSIG(exit_status
));
1311 else /* should never get to this point */
1313 ErrorF("Program \"%s\" has unknown exit condition\n",
1322 ErrorF("Module called abort() function\n");
1329 ErrorF("Module called exit() function with value=%d\n",ex
);
1333 /* directory handling functions */
1334 #define XF86DIR_magic 0x78666876 /* "xfhv" */
1336 typedef struct _xf86_dir_
{
1343 _xf86checkdirhndl(XF86DIR_priv
* f
,const char *func
)
1345 if (!f
|| f
->magic
!= XF86DIR_magic
|| !f
->dir
|| !f
->dirent
) {
1346 FatalError("libc_wrapper error: passed invalid DIR handle to %s",
1353 xf86opendir(const char *name
)
1358 dirp
= opendir(name
);
1362 dp
= xalloc(sizeof(XF86DIR_priv
));
1363 dp
->magic
= XF86DIR_magic
; /* This time I have this, Dirk! :-) */
1365 dp
->dirent
= xalloc(sizeof(struct _xf86dirent
));
1367 return (XF86DIR
*)dp
;
1370 _X_EXPORT XF86DIRENT
*
1371 xf86readdir(XF86DIR
* dirp
)
1373 XF86DIR_priv
* dp
= (XF86DIR_priv
*)dirp
;
1378 _xf86checkdirhndl(dp
,"xf86readdir");
1380 de
= readdir(dp
->dir
);
1382 return (XF86DIRENT
*)0;
1384 sz
= strlen(de
->d_name
);
1385 strncpy(xde
->d_name
,de
->d_name
, sz
>_XF86NAMELEN
? (_XF86NAMELEN
+1) : (sz
+1));
1386 xde
->d_name
[_XF86NAMELEN
] = '\0'; /* be sure to have a 0 byte */
1391 xf86rewinddir(XF86DIR
* dirp
)
1393 XF86DIR_priv
* dp
= (XF86DIR_priv
*)dirp
;
1395 _xf86checkdirhndl(dp
,"xf86readdir");
1400 xf86closedir(XF86DIR
* dir
)
1402 XF86DIR_priv
* dp
= (XF86DIR_priv
*)dir
;
1405 _xf86checkdirhndl(dp
,"xf86readdir");
1407 n
= closedir(dp
->dir
);
1416 xfToOsChmodMode(xf86mode_t xfmode
)
1420 if (xfmode
& XF86_S_ISUID
) mode
|= S_ISUID
;
1421 if (xfmode
& XF86_S_ISGID
) mode
|= S_ISGID
;
1422 if (xfmode
& XF86_S_ISVTX
) mode
|= S_ISVTX
;
1423 if (xfmode
& XF86_S_IRUSR
) mode
|= S_IRUSR
;
1424 if (xfmode
& XF86_S_IWUSR
) mode
|= S_IWUSR
;
1425 if (xfmode
& XF86_S_IXUSR
) mode
|= S_IXUSR
;
1426 if (xfmode
& XF86_S_IRGRP
) mode
|= S_IRGRP
;
1427 if (xfmode
& XF86_S_IWGRP
) mode
|= S_IWGRP
;
1428 if (xfmode
& XF86_S_IXGRP
) mode
|= S_IXGRP
;
1429 if (xfmode
& XF86_S_IROTH
) mode
|= S_IROTH
;
1430 if (xfmode
& XF86_S_IWOTH
) mode
|= S_IWOTH
;
1431 if (xfmode
& XF86_S_IXOTH
) mode
|= S_IXOTH
;
1437 xf86chmod(const char *path
, xf86mode_t xfmode
)
1439 mode_t mode
= xfToOsChmodMode(xfmode
);
1440 int rc
= chmod(path
, mode
);
1442 xf86errno
= xf86GetErrno();
1447 xf86chown(const char *path
, xf86uid_t owner
, xf86gid_t group
)
1449 int rc
= chown(path
, owner
, group
);
1450 xf86errno
= xf86GetErrno();
1473 xfToOsMknodMode(xf86mode_t xfmode
)
1475 mode_t mode
= xfToOsChmodMode(xfmode
);
1477 if (xfmode
& XF86_S_IFREG
) mode
|= S_IFREG
;
1478 if (xfmode
& XF86_S_IFCHR
) mode
|= S_IFCHR
;
1479 if (xfmode
& XF86_S_IFBLK
) mode
|= S_IFBLK
;
1480 if (xfmode
& XF86_S_IFIFO
) mode
|= S_IFIFO
;
1485 _X_EXPORT
int xf86mknod(const char *pathname
, xf86mode_t xfmode
, xf86dev_t dev
)
1487 mode_t mode
= xfToOsMknodMode(xfmode
);
1488 int rc
= mknod(pathname
, mode
, dev
);
1489 xf86errno
= xf86GetErrno();
1493 _X_EXPORT
unsigned int xf86sleep(unsigned int seconds
)
1495 return sleep(seconds
);
1498 _X_EXPORT
int xf86mkdir(const char *pathname
, xf86mode_t xfmode
)
1500 mode_t mode
= xfToOsChmodMode(xfmode
);
1501 int rc
= mkdir(pathname
, mode
);
1503 xf86errno
= xf86GetErrno();
1508 /* Several math functions */
1535 xf86atan2(double x
,double y
)
1541 xf86atof(const char* s
)
1547 xf86atoi(const char* s
)
1553 xf86atol(const char* s
)
1583 xf86finite(double x
)
1588 /* XXX Replace this with something that really works. */
1600 xf86fmod(double x
,double y
)
1612 xf86ldexp(double x
, int exp
)
1614 return ldexp(x
, exp
);
1630 xf86modf(double x
,double* y
)
1636 xf86pow(double x
, double y
)
1654 xf86strtod(const char *s
, char **end
)
1656 return strtod(s
,end
);
1660 xf86strtol(const char *s
, char **end
, int radix
)
1662 return strtol(s
,end
,radix
);
1665 _X_EXPORT
unsigned long
1666 xf86strtoul(const char *s
, char **end
,int radix
)
1668 return strtoul(s
,end
,radix
);
1677 /* memory functions */
1679 xf86memchr(const void* s
, int c
, xf86size_t n
)
1681 return memchr(s
,c
,(size_t)n
);
1685 xf86memcmp(const void* s1
, const void* s2
, xf86size_t n
)
1687 return(memcmp(s1
,s2
,(size_t)n
));
1691 xf86memcpy(void* dest
, const void* src
, xf86size_t n
)
1693 return(memcpy(dest
,src
,(size_t)n
));
1697 xf86memmove(void* dest
, const void* src
, xf86size_t n
)
1699 return(memmove(dest
,src
,(size_t)n
));
1703 xf86memset(void* s
, int c
, xf86size_t n
)
1705 return(memset(s
,c
,(size_t)n
));
1708 /* ctype functions */
1713 return isalnum(c
) ? 1 : 0;
1719 return isalpha(c
) ? 1 : 0;
1725 return iscntrl(c
) ? 1 : 0;
1731 return isdigit(c
) ? 1 : 0;
1737 return isgraph(c
) ? 1 : 0;
1743 return islower(c
) ? 1 : 0;
1749 return isprint(c
) ? 1 : 0;
1755 return ispunct(c
) ? 1 : 0;
1761 return isspace(c
) ? 1 : 0;
1767 return isupper(c
) ? 1 : 0;
1773 return isxdigit(c
) ? 1 : 0;
1788 /* memory allocation functions */
1790 xf86calloc(xf86size_t sz
,xf86size_t n
)
1792 return xcalloc(sz
, n
);
1802 xf86frexp(double x
, int *exp
)
1804 return frexp(x
, exp
);
1808 xf86malloc(xf86size_t n
)
1814 xf86realloc(void* p
, xf86size_t n
)
1816 return xrealloc(p
,n
);
1820 * XXX This probably doesn't belong here.
1825 static int pagesize
= -1;
1830 #if defined(_SC_PAGESIZE) || defined(HAS_SC_PAGESIZE)
1831 pagesize
= sysconf(_SC_PAGESIZE
);
1833 #ifdef _SC_PAGE_SIZE
1835 pagesize
= sysconf(_SC_PAGE_SIZE
);
1837 #ifdef HAS_GETPAGESIZE
1839 pagesize
= getpagesize();
1843 pagesize
= PAGE_SIZE
;
1846 FatalError("xf86getpagesize: Cannot determine page size");
1852 #define mapnum(e) case (e): return (xf86_##e)
1868 mapnum (ELOOP
); /* not POSIX 1 */
1870 mapnum (ENAMETOOLONG
);
1878 mapnum (ETXTBSY
); /* not POSIX 1 */
1923 return (xf86_UNKNOWN
);
1931 #ifdef HAVE_SYSV_IPC
1934 xf86shmget(xf86key_t key
, int size
, int xf86shmflg
)
1939 /* This copies the permissions (SHM_R, SHM_W for u, g, o). */
1940 shmflg
= xf86shmflg
& 0777;
1942 if (key
== XF86IPC_PRIVATE
) key
= IPC_PRIVATE
;
1944 if (xf86shmflg
& XF86IPC_CREAT
) shmflg
|= IPC_CREAT
;
1945 if (xf86shmflg
& XF86IPC_EXCL
) shmflg
|= IPC_EXCL
;
1946 if (xf86shmflg
& XF86IPC_NOWAIT
) shmflg
|= IPC_NOWAIT
;
1947 ret
= shmget((key_t
) key
, size
, shmflg
);
1950 xf86errno
= xf86GetErrno();
1956 xf86shmat(int id
, char *addr
, int xf86shmflg
)
1962 if (xf86shmflg
& XF86SHM_RDONLY
) shmflg
|= SHM_RDONLY
;
1965 if (xf86shmflg
& XF86SHM_RND
) shmflg
|= SHM_RND
;
1968 if (xf86shmflg
& XF86SHM_REMAP
) shmflg
|= SHM_REMAP
;
1971 ret
= shmat(id
,addr
,shmflg
);
1973 if (ret
== (pointer
) -1)
1974 xf86errno
= xf86GetErrno();
1980 xf86shmdt(char *addr
)
1987 xf86errno
= xf86GetErrno();
1993 * for now only implement the rmid command.
1996 xf86shmctl(int id
, int xf86cmd
, pointer buf
)
2009 ret
= shmctl(id
, cmd
, buf
);
2012 xf86errno
= xf86GetErrno();
2019 xf86shmget(xf86key_t key
, int size
, int xf86shmflg
)
2027 xf86shmat(int id
, char *addr
, int xf86shmflg
)
2035 xf86shmctl(int id
, int xf86cmd
, pointer buf
)
2043 xf86shmdt(char *addr
)
2049 #endif /* HAVE_SYSV_IPC */
2054 #ifdef HAS_GLIBC_SIGSETJMP
2061 #ifdef HAS_GLIBC_SIGSETJMP
2064 xf86setjmp(xf86jmp_buf env
)
2066 #if defined(__GLIBC__) && (__GLIBC__ >= 2)
2067 return __sigsetjmp((void *)env
, xf86setjmp1_arg2());
2069 return xf86setjmp1(env
, xf86setjmp1_arg2());
2074 xf86setjmp0(xf86jmp_buf env
)
2076 FatalError("setjmp: type 0 called instead of type %d", xf86getjmptype());
2079 #if !defined(__GLIBC__) || (__GLIBC__ < 2) /* libc5 */
2082 xf86setjmp1(xf86jmp_buf env
, int arg2
)
2084 __sigjmp_save((void *)env
, arg2
);
2085 return __setjmp((void *)env
);
2090 #else /* HAS_GLIBC_SIGSETJMP */
2093 xf86setjmp1(xf86jmp_buf env
, int arg2
)
2095 FatalError("setjmp: type 1 called instead of type %d", xf86getjmptype());
2099 xf86setjmp0(xf86jmp_buf env
)
2101 return setjmp((void *)env
);
2104 #endif /* HAS_GLIBC_SIGSETJMP */
2113 xf86setjmperror(xf86jmp_buf env
)
2115 FatalError("setjmp: don't know how to handle setjmp() type %d",