Initial commit
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / os-support / shared / libc_wrapper.c
blob95942411045ad03220225b890603ae5b5e977304
1 /*
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
13 * implied warranty.
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>
27 #endif
29 #if defined(linux) && !defined(__GLIBC__)
30 #undef __STRICT_ANSI__
31 #endif
32 #include <X11/X.h>
33 #include <X11/Xmd.h>
34 #include <X11/Xos.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #if defined(__bsdi__)
38 #undef _POSIX_SOURCE
39 #undef _ANSI_SOURCE
40 #endif
41 #include <sys/time.h>
42 #include <math.h>
43 #ifdef sun
44 #include <ieeefp.h>
45 #endif
46 #include <stdarg.h>
47 #include <fcntl.h>
48 #include <X11/Xfuncproto.h>
49 #include "os.h"
50 #include <ctype.h>
51 #include <unistd.h>
52 #include <string.h>
53 #include <errno.h>
54 #include <stdio.h>
55 #include <sys/ioctl.h>
56 #ifdef HAS_SVR3_MMAPDRV
57 #define NO_MMAP
58 #ifdef SELF_CONTAINED_WRAPPER
59 #include <sys/at_ansi.h>
60 #include <sys/kd.h>
61 #include <sys/sysmacros.h>
62 #if !defined(_NEED_SYSI86)
63 # include <sys/immu.h>
64 # include <sys/region.h>
65 #endif
66 #include <sys/mmap.h>
67 struct kd_memloc MapDSC;
68 int mmapFd = -2;
69 #else
70 extern struct kd_memloc MapDSC;
71 extern int mmapFd;
72 #endif
73 #endif
74 #ifndef NO_MMAP
75 #include <sys/mman.h>
76 #ifndef MAP_FAILED
77 #define MAP_FAILED ((caddr_t)-1)
78 #endif
79 #endif
80 #if !defined(ISC)
81 #include <stdlib.h>
82 #endif
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
90 #include "xf86.h"
91 #include "xf86Priv.h"
92 #define NO_OSLIB_PROTOTYPES
93 #define XF86_OS_PRIVS
94 #define HAVE_WRAPPER_DECLS
95 #include "xf86_OSlib.h"
96 #else
97 void xf86WrapperInit(void);
98 #endif
101 #ifndef X_NOT_POSIX
102 #include <dirent.h>
103 #else
104 #ifdef SYSV
105 #include <dirent.h>
106 #else
107 #ifdef USG
108 #include <dirent.h>
109 #else
110 #include <sys/dir.h>
111 #ifndef dirent
112 #define dirent direct
113 #endif
114 #endif
115 #endif
116 #endif
117 typedef struct dirent DIRENTRY;
119 #ifdef ISC202
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)
125 #else
126 #if defined(ISC) && !defined(_POSIX_SOURCE)
127 #define _POSIX_SOURCE
128 #include <sys/types.h>
129 #include <sys/wait.h>
130 #undef _POSIX_SOURCE
131 #else
132 #if (defined(ISC) && defined(_POSIX_SOURCE)) || defined(Lynx) || (defined (__alpha__) && defined(linux))
133 #include <sys/types.h>
134 #endif
135 #include <sys/wait.h>
136 #endif
137 #endif
138 #ifdef Lynx
139 #if !defined(S_IFIFO) && defined(S_IFFIFO)
140 #define S_IFIFO S_IFFIFO
141 #endif
142 #endif
144 /* For xf86getpagesize() */
145 #if defined(linux)
146 #define HAS_SC_PAGESIZE
147 #define HAS_GETPAGESIZE
148 #elif defined(CSRG_BASED)
149 #define HAS_GETPAGESIZE
150 #elif defined(DGUX)
151 #define HAS_GETPAGESIZE
152 #elif defined(sun) && !defined(SVR4)
153 #define HAS_GETPAGESIZE
154 #endif
155 #ifdef XNO_SYSCONF
156 #undef _SC_PAGESIZE
157 #endif
158 #ifdef HAVE_SYSV_IPC
159 #include <sys/ipc.h>
160 #include <sys/shm.h>
161 #endif
162 #include <setjmp.h>
164 #if defined(setjmp) && defined(__GNU_LIBRARY__) && \
165 (!defined(__GLIBC__) || (__GLIBC__ < 2) || \
166 ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 3)))
167 #define HAS_GLIBC_SIGSETJMP 1
168 #endif
170 #if 0
171 #define SETBUF_RETURNS_INT
172 #endif
174 _X_EXPORT double xf86HUGE_VAL;
176 #ifndef SELF_CONTAINED_WRAPPERS
177 extern void xf86DisableIO(void);
178 #endif
181 * This file contains the XFree86 wrappers for libc functions that can be
182 * called by loadable modules
185 _X_EXPORT double
186 xf86hypot(double x, double y)
188 return(hypot(x,y));
191 _X_EXPORT void
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 */
200 _X_EXPORT char*
201 xf86strcat(char* dest, const char* src)
203 return(strcat(dest,src));
206 _X_EXPORT char*
207 xf86strchr(const char* s, int c)
209 return strchr(s,c);
212 _X_EXPORT int
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 */
219 _X_EXPORT int
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++))
226 if (*us1++ == '\0')
227 return 0;
229 return tolower(*us1) - tolower(*--us2);
232 _X_EXPORT char*
233 xf86strcpy(char* dest, const char* src)
235 return strcpy(dest,src);
238 _X_EXPORT xf86size_t
239 xf86strcspn(const char* s1, const char* s2)
241 return (xf86size_t)strcspn(s1,s2);
244 _X_EXPORT xf86size_t
245 xf86strlen(const char* s)
247 return (xf86size_t)strlen(s);
250 _X_EXPORT xf86size_t
251 xf86strlcat(char *dest, const char *src, xf86size_t size)
253 return(strlcat(dest, src, size));
256 _X_EXPORT xf86size_t
257 xf86strlcpy(char *dest, const char *src, xf86size_t size)
259 return strlcpy(dest, src, size);
262 _X_EXPORT char*
263 xf86strncat(char* dest, const char* src, xf86size_t n)
265 return strncat(dest,src,(size_t)n);
268 _X_EXPORT int
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 */
275 _X_EXPORT int
276 xf86strncasecmp(const char* s1, const char* s2, xf86size_t n)
278 if (n != 0) {
279 const unsigned char *us1 = (const unsigned char *)s1;
280 const unsigned char *us2 = (const unsigned char *)s2;
282 do {
283 if (tolower(*us1) != tolower(*us2++))
284 return tolower(*us1) - tolower(*--us2);
285 if (*us1++ == '\0')
286 break;
287 } while (--n != 0);
289 return 0;
292 _X_EXPORT char*
293 xf86strncpy(char* dest, const char* src, xf86size_t n)
295 return strncpy(dest,src,(size_t)n);
298 _X_EXPORT char*
299 xf86strpbrk(const char* s1, const char* s2)
301 return strpbrk(s1,s2);
304 _X_EXPORT char*
305 xf86strrchr(const char* s, int c)
307 return strrchr(s,c);
310 _X_EXPORT xf86size_t
311 xf86strspn(const char* s1, const char* s2)
313 return strspn(s1,s2);
316 _X_EXPORT char*
317 xf86strstr(const char* s1, const char* s2)
319 return strstr(s1,s2);
322 _X_EXPORT char*
323 xf86strtok(char* s1, const char* s2)
325 return strtok(s1,s2);
328 _X_EXPORT char*
329 xf86strdup(const char* s)
331 return xstrdup(s);
334 _X_EXPORT int
335 xf86sprintf(char *s, const char *format, ...)
337 int ret;
338 va_list args;
339 va_start(args, format);
340 ret = vsprintf(s, format, args);
341 va_end(args);
342 return ret;
345 _X_EXPORT int
346 xf86snprintf(char *s, xf86size_t len, const char *format, ...)
348 int ret;
349 va_list args;
350 va_start(args, format);
351 ret = vsnprintf(s, (size_t)len, format, args);
352 va_end(args);
353 return ret;
356 _X_EXPORT void
357 xf86bzero(void* s, unsigned int n)
359 memset(s, 0, n);
362 #ifdef HAVE_VSSCANF
363 _X_EXPORT int
364 xf86sscanf(char *s, const char *format, ...)
365 #else
366 _X_EXPORT int
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 */
370 #endif
372 #ifdef HAVE_VSSCANF
373 int ret;
374 va_list args;
375 va_start(args, format);
377 ret = vsscanf(s,format,args);
378 va_end(args);
379 return ret;
380 #else
381 return sscanf(s, format, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
382 #endif
385 /* Basic I/O */
387 _X_EXPORT int xf86errno;
389 /* XXX This is not complete */
391 static int
392 xfToOsOpenFlags(int xfflags)
394 int flags = 0;
396 /* XXX This assumes O_RDONLY is 0 */
397 if (xfflags & XF86_O_WRONLY)
398 flags |= O_WRONLY;
399 if (xfflags & XF86_O_RDWR)
400 flags |= O_RDWR;
401 if (xfflags & XF86_O_CREAT)
402 flags |= O_CREAT;
404 return flags;
407 _X_EXPORT int
408 xf86open(const char *path, int flags, ...)
410 int fd;
411 va_list ap;
413 va_start(ap, 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);
420 } else {
421 fd = open(path, flags);
423 va_end(ap);
424 xf86errno = xf86GetErrno();
426 return fd;
429 _X_EXPORT int
430 xf86close(int fd)
432 int status = close(fd);
434 xf86errno = xf86GetErrno();
435 return status;
438 _X_EXPORT long
439 xf86lseek(int fd, long offset, int whence)
441 switch (whence) {
442 case XF86_SEEK_SET:
443 whence = SEEK_SET;
444 break;
445 case XF86_SEEK_CUR:
446 whence = SEEK_CUR;
447 break;
448 case XF86_SEEK_END:
449 whence = SEEK_END;
450 break;
452 return (long)lseek(fd, (off_t)offset, whence);
455 _X_EXPORT int
456 xf86ioctl(int fd, unsigned long request, pointer argp)
458 int status = ioctl(fd, request, argp);
460 xf86errno = xf86GetErrno();
461 return status;
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();
470 return n;
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();
479 return n;
482 _X_EXPORT void*
483 xf86mmap(void *start, xf86size_t length, int prot,
484 int flags, int fd, xf86size_t /* off_t */ offset)
486 #ifndef NO_MMAP
487 int p=0, f=0;
488 void *rc;
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;
495 #endif
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;
506 else
507 return rc;
508 #else
509 #ifdef HAS_SVR3_MMAPDRV
510 void *rc;
511 #ifdef SELF_CONTAINED_WRAPPER
512 if(mmapFd < 0) {
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;
519 #endif
520 MapDSC.vaddr = (char *)start;
521 MapDSC.physaddr = (char *)offset;
522 MapDSC.length = length;
523 MapDSC.ioflg = 1;
525 rc = (pointer)ioctl(mmapFd, MAP, &MapDSC);
526 xf86errno = xf86GetErrno();
527 if (rc == NULL)
528 return XF86_MAP_FAILED;
529 else
530 return rc;
531 #else
532 ErrorF("Warning: mmap() is not supported on this platform\n");
533 xf86errno = xf86_ENOSYS;
534 return XF86_MAP_FAILED;
535 #endif
536 #endif
539 _X_EXPORT int
540 xf86munmap(void *start, xf86size_t length)
542 #ifndef NO_MMAP
543 int rc = munmap(start,(size_t)length);
545 xf86errno = xf86GetErrno();
546 return rc;
547 #else
548 #ifdef HAS_SVR3_MMAPDRV
549 int rc = ioctl(mmapFd, UNMAPRM , start);
551 xf86errno = xf86GetErrno();
552 return rc;
553 #else
554 ErrorF("Warning: munmap() is not supported on this platform\n");
555 xf86errno = xf86_ENOSYS;
556 return -1;
557 #endif
558 #endif
561 _X_EXPORT int
562 xf86stat(const char *file_name, struct xf86stat *xfst)
564 int rc;
565 struct stat st;
567 rc = stat(file_name, &st);
568 xf86errno = xf86GetErrno();
569 xfst->st_rdev = st.st_rdev; /* Not much is currently supported */
570 return rc;
573 _X_EXPORT int
574 xf86fstat(int fd, struct xf86stat *xfst)
576 int rc;
577 struct stat st;
579 rc = fstat(fd, &st);
580 xf86errno = xf86GetErrno();
581 xfst->st_rdev = st.st_rdev; /* Not much is currently supported */
582 return rc;
585 static int
586 xfToOsAccessMode(int xfmode)
588 switch(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;
594 return 0;
597 _X_EXPORT int
598 xf86access(const char *pathname, int mode)
600 int rc;
602 mode = xfToOsAccessMode(mode);
603 rc = access(pathname, mode);
604 xf86errno = xf86GetErrno();
605 return rc;
610 /* limited stdio support */
612 #define XF86FILE_magic 0x58464856 /* "XFHV" */
614 typedef struct _xf86_file_ {
615 INT32 fileno;
616 INT32 magic;
617 FILE* filehnd;
618 char* fname;
619 } XF86FILE_priv;
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];
631 void
632 xf86WrapperInit()
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;
643 _X_EXPORT XF86FILE*
644 xf86fopen(const char* fn, const char* mode)
646 XF86FILE_priv* fp;
647 FILE *f = fopen(fn,mode);
648 xf86errno = xf86GetErrno();
649 if (!f) return 0;
651 fp = xalloc(sizeof(XF86FILE_priv));
652 fp->magic = XF86FILE_magic;
653 fp->filehnd = f;
654 fp->fileno = fileno(f);
655 fp->fname = xf86strdup(fn);
656 #ifdef DEBUG
657 ErrorF("xf86fopen(%s,%s) yields FILE %p XF86FILE %p\n",
658 fn,mode,f,fp);
659 #endif
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",
668 func);
669 exit(42);
673 _X_EXPORT int
674 xf86fclose(XF86FILE* f)
676 XF86FILE_priv* fp = (XF86FILE_priv*)f;
677 int ret;
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);
685 } else {
686 ret = fclose(fp->filehnd);
687 fp->magic = 0; /* invalidate */
688 xfree(fp->fname);
689 xfree(fp);
691 return ret ? -1 : 0;
694 _X_EXPORT int
695 xf86printf(const char *format, ...)
697 int ret;
698 va_list args;
699 va_start(args, format);
701 ret = printf(format,args);
702 va_end(args);
703 return ret;
706 _X_EXPORT int
707 xf86fprintf(XF86FILE* f, const char *format, ...)
709 XF86FILE_priv* fp = (XF86FILE_priv*)f;
711 int ret;
712 va_list args;
713 va_start(args, format);
715 #ifdef DEBUG
716 ErrorF("xf86fprintf for XF86FILE %p\n", fp);
717 #endif
718 _xf86checkhndl(fp,"xf86fprintf");
720 ret = vfprintf(fp->filehnd,format,args);
721 va_end(args);
722 return ret;
725 _X_EXPORT int
726 xf86vfprintf(XF86FILE* f, const char *format, va_list ap)
728 XF86FILE_priv* fp = (XF86FILE_priv*)f;
730 #ifdef DEBUG
731 ErrorF("xf86vfprintf for XF86FILE %p\n", fp);
732 #endif
733 _xf86checkhndl(fp,"xf86vfprintf");
735 return vfprintf(fp->filehnd,format,ap);
738 _X_EXPORT int
739 xf86vsprintf(char *s, const char *format, va_list ap)
741 return vsprintf(s, format, ap);
744 _X_EXPORT int
745 xf86vsnprintf(char *s, xf86size_t len, const char *format, va_list ap)
747 return vsnprintf(s, (size_t)len, format, ap);
750 #ifdef HAVE_VFSCANF
751 _X_EXPORT int
752 xf86fscanf(XF86FILE* f, const char *format, ...)
753 #else
754 _X_EXPORT int
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 */
758 #endif
760 XF86FILE_priv* fp = (XF86FILE_priv*)f;
762 #ifdef HAVE_VFSCANF
763 int ret;
764 va_list args;
765 va_start(args, format);
767 _xf86checkhndl(fp,"xf86fscanf");
769 ret = vfscanf(fp->filehnd,format,args);
770 va_end(args);
771 return ret;
772 #else
773 _xf86checkhndl(fp,"xf86fscanf");
774 return fscanf(fp->filehnd, format, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
775 #endif
778 _X_EXPORT char *
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);
787 _X_EXPORT int
788 xf86fputs(const char *buf, XF86FILE* f)
790 XF86FILE_priv* fp = (XF86FILE_priv*)f;
792 _xf86checkhndl(fp,"xf86fputs");
793 return fputs(buf,fp->filehnd);
796 _X_EXPORT int
797 xf86getc(XF86FILE* f)
799 XF86FILE_priv* fp = (XF86FILE_priv*)f;
801 _xf86checkhndl(fp,"xf86getc");
802 return getc(fp->filehnd);
805 _X_EXPORT int
806 xf86fgetc(XF86FILE* f)
808 XF86FILE_priv* fp = (XF86FILE_priv*)f;
810 _xf86checkhndl(fp,"xf86fgetc");
811 return fgetc(fp->filehnd);
814 _X_EXPORT int
815 xf86fputc(int c,XF86FILE* f)
817 XF86FILE_priv* fp = (XF86FILE_priv*)f;
819 _xf86checkhndl(fp,"xf86fputc");
820 return fputc(c,fp->filehnd);
823 _X_EXPORT int
824 xf86fflush(XF86FILE* f)
826 XF86FILE_priv* fp = (XF86FILE_priv*)f;
828 _xf86checkhndl(fp,"xf86fflush");
829 return fflush(fp->filehnd);
832 _X_EXPORT xf86size_t
833 xf86fread(void* buf, xf86size_t sz, xf86size_t cnt, XF86FILE* f)
835 XF86FILE_priv* fp = (XF86FILE_priv*)f;
837 #ifdef DEBUG
838 ErrorF("xf86fread for XF86FILE %p\n", fp);
839 #endif
840 _xf86checkhndl(fp,"xf86fread");
841 return fread(buf,(size_t)sz,(size_t)cnt,fp->filehnd);
844 _X_EXPORT xf86size_t
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);
853 _X_EXPORT int
854 xf86fseek(XF86FILE* f, long offset, int whence)
856 XF86FILE_priv* fp = (XF86FILE_priv*)f;
858 _xf86checkhndl(fp,"xf86fseek");
859 switch (whence) {
860 case XF86_SEEK_SET:
861 whence = SEEK_SET;
862 break;
863 case XF86_SEEK_CUR:
864 whence = SEEK_CUR;
865 break;
866 case XF86_SEEK_END:
867 whence = SEEK_END;
868 break;
870 return fseek(fp->filehnd,offset,whence);
873 _X_EXPORT long
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;
884 _X_EXPORT char*
885 xf86strerror(int n)
887 int err;
889 switch (n)
891 case 0: err = 0; break;
892 mapnum (EACCES);
893 mapnum (EAGAIN);
894 mapnum (EBADF);
895 mapnum (EEXIST);
896 mapnum (EFAULT);
897 mapnum (EINTR);
898 mapnum (EINVAL);
899 mapnum (EISDIR);
900 mapnum (ELOOP); /* not POSIX 1 */
901 mapnum (EMFILE);
902 mapnum (ENAMETOOLONG);
903 mapnum (ENFILE);
904 mapnum (ENOENT);
905 mapnum (ENOMEM);
906 mapnum (ENOSPC);
907 mapnum (ENOTDIR);
908 mapnum (EPIPE);
909 mapnum (EROFS);
910 mapnum (ETXTBSY); /* not POSIX 1 */
911 mapnum (ENOTTY);
912 #ifdef ENOSYS
913 mapnum (ENOSYS);
914 #endif
915 mapnum (EBUSY);
916 mapnum (ENODEV);
917 mapnum (EIO);
918 #ifdef ESRCH
919 mapnum (ESRCH);
920 #endif
921 #ifdef ENXIO
922 mapnum (ENXIO);
923 #endif
924 #ifdef E2BIG
925 mapnum (E2BIG);
926 #endif
927 #ifdef ENOEXEC
928 mapnum (ENOEXEC);
929 #endif
930 #ifdef ECHILD
931 mapnum (ECHILD);
932 #endif
933 #ifdef ENOTBLK
934 mapnum (ENOTBLK);
935 #endif
936 #ifdef EXDEV
937 mapnum (EXDEV);
938 #endif
939 #ifdef EFBIG
940 mapnum (EFBIG);
941 #endif
942 #ifdef ESPIPE
943 mapnum (ESPIPE);
944 #endif
945 #ifdef EMLINK
946 mapnum (EMLINK);
947 #endif
948 #ifdef EDOM
949 mapnum (EDOM);
950 #endif
951 #ifdef ERANGE
952 mapnum (ERANGE);
953 #endif
955 default:
956 err = 999;
958 return strerror(err);
961 #undef mapnum
964 /* required for portable fgetpos/fsetpos,
965 * use as
966 * XF86fpos_t* pos = xalloc(xf86fpossize());
968 _X_EXPORT long
969 xf86fpossize()
971 return sizeof(fpos_t);
974 _X_EXPORT int
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");
981 #ifndef ISC
982 return fgetpos(fp->filehnd,ppos);
983 #else
984 *ppos = ftell(fp->filehnd);
985 if (*ppos < 0L)
986 return(-1);
987 return(0);
988 #endif
991 _X_EXPORT int
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");
999 #ifndef ISC
1000 return fsetpos(fp->filehnd,ppos);
1001 #else
1002 if (ppos == NULL)
1004 errno = EINVAL;
1005 return EOF;
1007 return fseek(fp->filehnd, *ppos, SEEK_SET);
1008 #endif
1011 _X_EXPORT void
1012 xf86perror(const char *s)
1014 perror(s);
1017 _X_EXPORT int
1018 xf86remove(const char *s)
1020 #ifdef _POSIX_SOURCE
1021 return remove(s);
1022 #else
1023 return unlink(s);
1024 #endif
1027 _X_EXPORT int
1028 xf86rename(const char *old, const char *new)
1030 #ifdef _POSIX_SOURCE
1031 return rename(old,new);
1032 #else
1033 int ret = link(old,new);
1034 if (!ret) {
1035 ret = unlink(old);
1036 if (ret) unlink(new);
1037 } else
1038 ret = unlink(new);
1039 return ret;
1040 #endif
1043 _X_EXPORT void
1044 xf86rewind(XF86FILE* f)
1046 XF86FILE_priv* fp = (XF86FILE_priv*)f;
1048 _xf86checkhndl(fp,"xf86fsetpos");
1049 rewind(fp->filehnd);
1052 _X_EXPORT void
1053 xf86clearerr(XF86FILE* f)
1055 XF86FILE_priv* fp = (XF86FILE_priv*)f;
1057 _xf86checkhndl(fp,"xf86clearerr");
1058 clearerr(fp->filehnd);
1061 _X_EXPORT int
1062 xf86feof(XF86FILE* f)
1064 XF86FILE_priv* fp = (XF86FILE_priv*)f;
1066 _xf86checkhndl(fp,"xf86feof");
1067 return feof(fp->filehnd);
1070 _X_EXPORT int
1071 xf86ferror(XF86FILE* f)
1073 XF86FILE_priv* fp = (XF86FILE_priv*)f;
1075 _xf86checkhndl(fp,"xf86ferror");
1076 return ferror(fp->filehnd);
1079 _X_EXPORT XF86FILE*
1080 xf86freopen(const char* fname,const char* mode,XF86FILE* fold)
1082 XF86FILE_priv* fp = (XF86FILE_priv*)fold;
1083 FILE *fnew;
1085 _xf86checkhndl(fp,"xf86freopen");
1086 fnew = freopen(fname,mode,fp->filehnd);
1087 xf86errno = xf86GetErrno();
1088 if (!fnew) {
1089 xf86fclose(fold); /* discard old XF86FILE structure */
1090 return 0;
1092 /* recycle the old XF86FILE structure */
1093 fp->magic = XF86FILE_magic;
1094 fp->filehnd = fnew;
1095 fp->fileno = fileno(fnew);
1096 fp->fname = xf86strdup(fname);
1097 #ifdef DEBUG
1098 ErrorF("xf86freopen(%s,%s,%p) yields FILE %p XF86FILE %p\n",
1099 fname,mode,fold,fnew,fp);
1100 #endif
1101 return (XF86FILE*)fp;
1104 _X_EXPORT int
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);
1112 #else
1113 setbuf(fp->filehnd, buf);
1114 return 0;
1115 #endif
1118 _X_EXPORT int
1119 xf86setvbuf(XF86FILE* f, char *buf, int mode, xf86size_t size)
1121 XF86FILE_priv* fp = (XF86FILE_priv*)f;
1122 int vbufmode;
1124 _xf86checkhndl(fp,"xf86fsetvbuf");
1126 switch (mode) {
1127 case XF86_IONBF:
1128 vbufmode = _IONBF;
1129 break;
1130 case XF86_IOLBF:
1131 vbufmode = _IOFBF;
1132 break;
1133 case XF86_IOFBF:
1134 vbufmode = _IOLBF;
1135 break;
1136 default:
1137 FatalError("libc_wrapper error: mode in setvbuf incorrect");
1138 exit(42);
1141 return setvbuf(fp->filehnd,buf,vbufmode,(size_t)size);
1144 _X_EXPORT XF86FILE*
1145 xf86tmpfile(void)
1147 #ifdef NEED_TMPFILE
1148 return xf86fopen(tmpnam((char*)0),"w+");
1149 #else
1150 XF86FILE_priv* fp;
1151 FILE *f = tmpfile();
1152 xf86errno = xf86GetErrno();
1153 if (!f) return 0;
1155 fp = xalloc(sizeof(XF86FILE_priv));
1156 fp->magic = XF86FILE_magic;
1157 fp->filehnd = f;
1158 fp->fileno = fileno(f);
1159 fp->fname = xf86strdup("*tmpfile*"); /* so that it can be xfree()'d */
1160 #ifdef DEBUG
1161 ErrorF("xf86tmpfile() yields FILE %p XF86FILE %p\n",f,fp);
1162 #endif
1163 return (XF86FILE*)fp;
1165 #endif /* HAS_TMPFILE */
1168 _X_EXPORT int
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. */
1179 _X_EXPORT void
1180 xf86usleep(usec)
1181 unsigned long usec;
1183 #if (defined(SYSV) || defined(SVR4)) && !defined(sun)
1184 syscall(3112, (usec) / 1000 + 1);
1185 #else
1186 usleep(usec);
1187 #endif
1190 _X_EXPORT void
1191 xf86getsecs(long * secs, long * usecs)
1193 struct timeval tv;
1195 X_GETTIMEOFDAY(&tv);
1196 if (secs)
1197 *secs = tv.tv_sec;
1198 if (usecs)
1199 *usecs= tv.tv_usec;
1201 return;
1204 _X_EXPORT int
1205 xf86ffs(int mask)
1207 int n;
1208 if (mask == 0) return 0;
1209 for (n = 1; (mask & 1)==0; n++)
1210 mask >>= 1;
1211 return n;
1214 _X_EXPORT char *
1215 xf86getenv(const char * a)
1217 /* Only allow this when the real and effective uids are the same */
1218 if (getuid() != geteuid())
1219 return NULL;
1220 else
1221 return(getenv(a));
1224 _X_EXPORT void *
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);
1231 _X_EXPORT int
1232 xf86execl(const char *pathname, const char *arg, ...)
1234 int i;
1235 pid_t pid;
1236 int exit_status;
1237 char *arglist[5];
1238 va_list args;
1239 va_start(args, arg);
1240 arglist[0] = (char*)&args;
1241 i = 1;
1242 while (i < 5 && (arglist[i++] = va_arg(args, char *)) != NULL)
1244 va_end(args);
1246 if ((pid = fork()) < 0) {
1247 ErrorF("Fork failed (%s)\n", strerror(errno));
1248 return -1;
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
1257 xf86DisableIO();
1258 #endif
1259 if (setuid(getuid()) == -1) {
1260 ErrorF("xf86Execl: setuid() failed: %s\n", strerror(errno));
1261 exit(255);
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)
1269 close(i);
1270 dup(xf86Info.consoleFd);
1273 #endif
1275 execv(pathname, arglist);
1276 ErrorF("Exec failed for command \"%s\" (%s)\n",
1277 pathname, strerror(errno));
1278 exit(255);
1281 /* parent */
1282 wait(&exit_status);
1283 if (WIFEXITED(exit_status))
1285 switch (WEXITSTATUS(exit_status))
1287 case 0: /* OK */
1288 return 0;
1289 case 255: /* exec() failed */
1290 return(255);
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));
1303 #ifdef WIFSTOPPED
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));
1310 #endif
1311 else /* should never get to this point */
1313 ErrorF("Program \"%s\" has unknown exit condition\n",
1314 pathname);
1315 return(1);
1319 _X_EXPORT void
1320 xf86abort(void)
1322 ErrorF("Module called abort() function\n");
1323 abort();
1326 _X_EXPORT void
1327 xf86exit(int ex)
1329 ErrorF("Module called exit() function with value=%d\n",ex);
1330 exit(ex);
1333 /* directory handling functions */
1334 #define XF86DIR_magic 0x78666876 /* "xfhv" */
1336 typedef struct _xf86_dir_ {
1337 DIR *dir;
1338 INT32 magic;
1339 XF86DIRENT *dirent;
1340 } XF86DIR_priv;
1342 static void
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",
1347 func);
1348 exit(42);
1352 _X_EXPORT XF86DIR *
1353 xf86opendir(const char *name)
1355 XF86DIR_priv *dp;
1356 DIR *dirp;
1358 dirp = opendir(name);
1359 if (!dirp)
1360 return (XF86DIR*)0;
1362 dp = xalloc(sizeof(XF86DIR_priv));
1363 dp->magic = XF86DIR_magic; /* This time I have this, Dirk! :-) */
1364 dp->dir = dirp;
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;
1374 DIRENTRY *de;
1375 XF86DIRENT* xde;
1376 int sz;
1378 _xf86checkdirhndl(dp,"xf86readdir");
1380 de = readdir(dp->dir);
1381 if (!de)
1382 return (XF86DIRENT*)0;
1383 xde = dp->dirent;
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 */
1387 return xde;
1390 _X_EXPORT void
1391 xf86rewinddir(XF86DIR* dirp)
1393 XF86DIR_priv* dp = (XF86DIR_priv*)dirp;
1395 _xf86checkdirhndl(dp,"xf86readdir");
1396 rewinddir(dp->dir);
1399 _X_EXPORT int
1400 xf86closedir(XF86DIR* dir)
1402 XF86DIR_priv* dp = (XF86DIR_priv*)dir;
1403 int n;
1405 _xf86checkdirhndl(dp,"xf86readdir");
1407 n = closedir(dp->dir);
1408 dp->magic = 0;
1409 xfree(dp->dirent);
1410 xfree(dp);
1412 return n;
1415 static mode_t
1416 xfToOsChmodMode(xf86mode_t xfmode)
1418 mode_t mode = 0;
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;
1433 return mode;
1436 _X_EXPORT int
1437 xf86chmod(const char *path, xf86mode_t xfmode)
1439 mode_t mode = xfToOsChmodMode(xfmode);
1440 int rc = chmod(path, mode);
1442 xf86errno = xf86GetErrno();
1443 return rc;
1446 _X_EXPORT int
1447 xf86chown(const char *path, xf86uid_t owner, xf86gid_t group)
1449 int rc = chown(path, owner, group);
1450 xf86errno = xf86GetErrno();
1451 return rc;
1454 _X_EXPORT xf86uid_t
1455 xf86geteuid(void)
1457 return geteuid();
1460 _X_EXPORT xf86gid_t
1461 xf86getegid(void)
1463 return getegid();
1466 _X_EXPORT int
1467 xf86getpid(void)
1469 return getpid();
1472 static mode_t
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;
1482 return mode;
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();
1490 return rc;
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();
1504 return rc;
1508 /* Several math functions */
1510 _X_EXPORT int
1511 xf86abs(int x)
1513 return abs(x);
1516 _X_EXPORT double
1517 xf86acos(double x)
1519 return acos(x);
1522 _X_EXPORT double
1523 xf86asin(double x)
1525 return asin(x);
1528 _X_EXPORT double
1529 xf86atan(double x)
1531 return atan(x);
1534 _X_EXPORT double
1535 xf86atan2(double x,double y)
1537 return atan2(x,y);
1540 _X_EXPORT double
1541 xf86atof(const char* s)
1543 return atof(s);
1546 _X_EXPORT int
1547 xf86atoi(const char* s)
1549 return atoi(s);
1552 _X_EXPORT long
1553 xf86atol(const char* s)
1555 return atol(s);
1558 _X_EXPORT double
1559 xf86ceil(double x)
1561 return ceil(x);
1564 _X_EXPORT double
1565 xf86cos(double x)
1567 return(cos(x));
1570 _X_EXPORT double
1571 xf86exp(double x)
1573 return(exp(x));
1576 _X_EXPORT double
1577 xf86fabs(double x)
1579 return(fabs(x));
1582 _X_EXPORT int
1583 xf86finite(double x)
1585 #ifndef QNX4
1586 return(finite(x));
1587 #else
1588 /* XXX Replace this with something that really works. */
1589 return 1;
1590 #endif
1593 _X_EXPORT double
1594 xf86floor(double x)
1596 return floor(x);
1599 _X_EXPORT double
1600 xf86fmod(double x,double y)
1602 return fmod(x,y);
1605 _X_EXPORT long
1606 xf86labs(long x)
1608 return labs(x);
1611 _X_EXPORT double
1612 xf86ldexp(double x, int exp)
1614 return ldexp(x, exp);
1617 _X_EXPORT double
1618 xf86log(double x)
1620 return(log(x));
1623 _X_EXPORT double
1624 xf86log10(double x)
1626 return(log10(x));
1629 _X_EXPORT double
1630 xf86modf(double x,double* y)
1632 return modf(x,y);
1635 _X_EXPORT double
1636 xf86pow(double x, double y)
1638 return(pow(x,y));
1641 _X_EXPORT double
1642 xf86sin(double x)
1644 return sin(x);
1647 _X_EXPORT double
1648 xf86sqrt(double x)
1650 return(sqrt(x));
1653 _X_EXPORT double
1654 xf86strtod(const char *s, char **end)
1656 return strtod(s,end);
1659 _X_EXPORT long
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);
1671 _X_EXPORT double
1672 xf86tan(double x)
1674 return tan(x);
1677 /* memory functions */
1678 _X_EXPORT void*
1679 xf86memchr(const void* s, int c, xf86size_t n)
1681 return memchr(s,c,(size_t)n);
1684 _X_EXPORT int
1685 xf86memcmp(const void* s1, const void* s2, xf86size_t n)
1687 return(memcmp(s1,s2,(size_t)n));
1690 _X_EXPORT void*
1691 xf86memcpy(void* dest, const void* src, xf86size_t n)
1693 return(memcpy(dest,src,(size_t)n));
1696 _X_EXPORT void*
1697 xf86memmove(void* dest, const void* src, xf86size_t n)
1699 return(memmove(dest,src,(size_t)n));
1702 _X_EXPORT void*
1703 xf86memset(void* s, int c, xf86size_t n)
1705 return(memset(s,c,(size_t)n));
1708 /* ctype functions */
1710 _X_EXPORT int
1711 xf86isalnum(int c)
1713 return isalnum(c) ? 1 : 0;
1716 _X_EXPORT int
1717 xf86isalpha(int c)
1719 return isalpha(c) ? 1 : 0;
1722 _X_EXPORT int
1723 xf86iscntrl(int c)
1725 return iscntrl(c) ? 1 : 0;
1728 _X_EXPORT int
1729 xf86isdigit(int c)
1731 return isdigit(c) ? 1 : 0;
1734 _X_EXPORT int
1735 xf86isgraph(int c)
1737 return isgraph(c) ? 1 : 0;
1740 _X_EXPORT int
1741 xf86islower(int c)
1743 return islower(c) ? 1 : 0;
1746 _X_EXPORT int
1747 xf86isprint(int c)
1749 return isprint(c) ? 1 : 0;
1752 _X_EXPORT int
1753 xf86ispunct(int c)
1755 return ispunct(c) ? 1 : 0;
1758 _X_EXPORT int
1759 xf86isspace(int c)
1761 return isspace(c) ? 1 : 0;
1764 _X_EXPORT int
1765 xf86isupper(int c)
1767 return isupper(c) ? 1 : 0;
1770 _X_EXPORT int
1771 xf86isxdigit(int c)
1773 return isxdigit(c) ? 1 : 0;
1776 _X_EXPORT int
1777 xf86tolower(int c)
1779 return tolower(c);
1782 _X_EXPORT int
1783 xf86toupper(int c)
1785 return toupper(c);
1788 /* memory allocation functions */
1789 _X_EXPORT void*
1790 xf86calloc(xf86size_t sz,xf86size_t n)
1792 return xcalloc(sz, n);
1795 _X_EXPORT void
1796 xf86free(void* p)
1798 xfree(p);
1801 _X_EXPORT double
1802 xf86frexp(double x, int *exp)
1804 return frexp(x, exp);
1807 _X_EXPORT void*
1808 xf86malloc(xf86size_t n)
1810 return xalloc(n);
1813 _X_EXPORT void*
1814 xf86realloc(void* p, xf86size_t n)
1816 return xrealloc(p,n);
1820 * XXX This probably doesn't belong here.
1822 _X_EXPORT int
1823 xf86getpagesize()
1825 static int pagesize = -1;
1827 if (pagesize != -1)
1828 return pagesize;
1830 #if defined(_SC_PAGESIZE) || defined(HAS_SC_PAGESIZE)
1831 pagesize = sysconf(_SC_PAGESIZE);
1832 #endif
1833 #ifdef _SC_PAGE_SIZE
1834 if (pagesize == -1)
1835 pagesize = sysconf(_SC_PAGE_SIZE);
1836 #endif
1837 #ifdef HAS_GETPAGESIZE
1838 if (pagesize == -1)
1839 pagesize = getpagesize();
1840 #endif
1841 #ifdef PAGE_SIZE
1842 if (pagesize == -1)
1843 pagesize = PAGE_SIZE;
1844 #endif
1845 if (pagesize == -1)
1846 FatalError("xf86getpagesize: Cannot determine page size");
1848 return pagesize;
1852 #define mapnum(e) case (e): return (xf86_##e)
1854 _X_EXPORT int
1855 xf86GetErrno ()
1857 switch (errno)
1859 case 0: return 0;
1860 mapnum (EACCES);
1861 mapnum (EAGAIN);
1862 mapnum (EBADF);
1863 mapnum (EEXIST);
1864 mapnum (EFAULT);
1865 mapnum (EINTR);
1866 mapnum (EINVAL);
1867 mapnum (EISDIR);
1868 mapnum (ELOOP); /* not POSIX 1 */
1869 mapnum (EMFILE);
1870 mapnum (ENAMETOOLONG);
1871 mapnum (ENFILE);
1872 mapnum (ENOENT);
1873 mapnum (ENOMEM);
1874 mapnum (ENOSPC);
1875 mapnum (ENOTDIR);
1876 mapnum (EPIPE);
1877 mapnum (EROFS);
1878 mapnum (ETXTBSY); /* not POSIX 1 */
1879 mapnum (ENOTTY);
1880 #ifdef ENOSYS
1881 mapnum (ENOSYS);
1882 #endif
1883 mapnum (EBUSY);
1884 mapnum (ENODEV);
1885 mapnum (EIO);
1886 #ifdef ESRCH
1887 mapnum (ESRCH);
1888 #endif
1889 #ifdef ENXIO
1890 mapnum (ENXIO);
1891 #endif
1892 #ifdef E2BIG
1893 mapnum (E2BIG);
1894 #endif
1895 #ifdef ENOEXEC
1896 mapnum (ENOEXEC);
1897 #endif
1898 #ifdef ECHILD
1899 mapnum (ECHILD);
1900 #endif
1901 #ifdef ENOTBLK
1902 mapnum (ENOTBLK);
1903 #endif
1904 #ifdef EXDEV
1905 mapnum (EXDEV);
1906 #endif
1907 #ifdef EFBIG
1908 mapnum (EFBIG);
1909 #endif
1910 #ifdef ESPIPE
1911 mapnum (ESPIPE);
1912 #endif
1913 #ifdef EMLINK
1914 mapnum (EMLINK);
1915 #endif
1916 #ifdef EDOM
1917 mapnum (EDOM);
1918 #endif
1919 #ifdef ERANGE
1920 mapnum (ERANGE);
1921 #endif
1922 default:
1923 return (xf86_UNKNOWN);
1927 #undef mapnum
1931 #ifdef HAVE_SYSV_IPC
1933 _X_EXPORT int
1934 xf86shmget(xf86key_t key, int size, int xf86shmflg)
1936 int shmflg;
1937 int ret;
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);
1949 if (ret == -1)
1950 xf86errno = xf86GetErrno();
1952 return ret;
1955 _X_EXPORT char *
1956 xf86shmat(int id, char *addr, int xf86shmflg)
1958 int shmflg = 0;
1959 pointer ret;
1961 #ifdef SHM_RDONLY
1962 if (xf86shmflg & XF86SHM_RDONLY) shmflg |= SHM_RDONLY;
1963 #endif
1964 #ifdef SHM_RND
1965 if (xf86shmflg & XF86SHM_RND) shmflg |= SHM_RND;
1966 #endif
1967 #ifdef SHM_REMAP
1968 if (xf86shmflg & XF86SHM_REMAP) shmflg |= SHM_REMAP;
1969 #endif
1971 ret = shmat(id,addr,shmflg);
1973 if (ret == (pointer) -1)
1974 xf86errno = xf86GetErrno();
1976 return ret;
1979 _X_EXPORT int
1980 xf86shmdt(char *addr)
1982 int ret;
1984 ret = shmdt(addr);
1986 if (ret == -1)
1987 xf86errno = xf86GetErrno();
1989 return ret;
1993 * for now only implement the rmid command.
1995 _X_EXPORT int
1996 xf86shmctl(int id, int xf86cmd, pointer buf)
1998 int cmd;
1999 int ret;
2001 switch (xf86cmd) {
2002 case XF86IPC_RMID:
2003 cmd = IPC_RMID;
2004 break;
2005 default:
2006 return 0;
2009 ret = shmctl(id, cmd, buf);
2011 if (ret == -1)
2012 xf86errno = xf86GetErrno();
2014 return ret;
2016 #else
2019 xf86shmget(xf86key_t key, int size, int xf86shmflg)
2021 xf86errno = ENOSYS;
2023 return -1;
2026 char *
2027 xf86shmat(int id, char *addr, int xf86shmflg)
2029 xf86errno = ENOSYS;
2031 return (char *)-1;
2035 xf86shmctl(int id, int xf86cmd, pointer buf)
2037 xf86errno = ENOSYS;
2039 return -1;
2043 xf86shmdt(char *addr)
2045 xf86errno = ENOSYS;
2047 return -1;
2049 #endif /* HAVE_SYSV_IPC */
2051 _X_EXPORT int
2052 xf86getjmptype()
2054 #ifdef HAS_GLIBC_SIGSETJMP
2055 return 1;
2056 #else
2057 return 0;
2058 #endif
2061 #ifdef HAS_GLIBC_SIGSETJMP
2063 _X_EXPORT int
2064 xf86setjmp(xf86jmp_buf env)
2066 #if defined(__GLIBC__) && (__GLIBC__ >= 2)
2067 return __sigsetjmp((void *)env, xf86setjmp1_arg2());
2068 #else
2069 return xf86setjmp1(env, xf86setjmp1_arg2());
2070 #endif
2073 _X_EXPORT int
2074 xf86setjmp0(xf86jmp_buf env)
2076 FatalError("setjmp: type 0 called instead of type %d", xf86getjmptype());
2079 #if !defined(__GLIBC__) || (__GLIBC__ < 2) /* libc5 */
2081 _X_EXPORT int
2082 xf86setjmp1(xf86jmp_buf env, int arg2)
2084 __sigjmp_save((void *)env, arg2);
2085 return __setjmp((void *)env);
2088 #endif
2090 #else /* HAS_GLIBC_SIGSETJMP */
2093 xf86setjmp1(xf86jmp_buf env, int arg2)
2095 FatalError("setjmp: type 1 called instead of type %d", xf86getjmptype());
2098 int
2099 xf86setjmp0(xf86jmp_buf env)
2101 return setjmp((void *)env);
2104 #endif /* HAS_GLIBC_SIGSETJMP */
2106 _X_EXPORT int
2107 xf86setjmp1_arg2()
2109 return 1;
2112 _X_EXPORT int
2113 xf86setjmperror(xf86jmp_buf env)
2115 FatalError("setjmp: don't know how to handle setjmp() type %d",
2116 xf86getjmptype());
2119 long
2120 xf86random()
2122 return random();