Linewrapping MUAs, grumble.
[p5sagit/p5-mst-13.2.git] / ext / POSIX / POSIX.xs
index 1bf8521..292c6a9 100644 (file)
@@ -1,6 +1,4 @@
-#ifdef WIN32
-#define _POSIX_
-#endif
+#define PERL_EXT_POSIX
 
 #ifdef NETWARE
        #define _POSIX_
@@ -19,7 +17,7 @@
 #define PERLIO_NOT_STDIO 1
 #include "perl.h"
 #include "XSUB.h"
-#if defined(PERL_OBJECT) || defined(PERL_CAPI) || defined(PERL_IMPLICIT_SYS)
+#if defined(PERL_IMPLICIT_SYS)
 #  undef signal
 #  undef open
 #  undef setmode
 #include <fcntl.h>
 
 #ifdef HAS_TZNAME
-#  if !defined(WIN32) && !defined(__CYGWIN__) && !defined(NETWARE)
+#  if !defined(WIN32) && !defined(__CYGWIN__) && !defined(NETWARE) && defined(__UWIN__)
 extern char *tzname[];
 #  endif
 #else
-#if !defined(WIN32) || (defined(__MINGW32__) && !defined(tzname))
+#if !defined(WIN32) && !defined(__UWIN__) || (defined(__MINGW32__) && defined(tzname))
 char *tzname[] = { "" , "" };
 #endif
 #endif
@@ -168,6 +166,8 @@ char *tzname[] = { "" , "" };
 #  define sigfillset(a)                not_here("sigfillset")
 #  define sigismember(a,b)     not_here("sigismember")
 #ifndef NETWARE
+#  undef setuid
+#  undef setgid
 #  define setuid(a)            not_here("setuid")
 #  define setgid(a)            not_here("setgid")
 #endif /* NETWARE */
@@ -226,9 +226,11 @@ typedef struct termios* POSIX__Termios;
 
 /* Possibly needed prototypes */
 char *cuserid (char *);
+#ifndef WIN32
 double strtod (const char *, char **);
 long strtol (const char *, char **, int);
 unsigned long strtoul (const char *, char **, int);
+#endif
 
 #ifndef HAS_CUSERID
 #define cuserid(a) (char *) not_here("cuserid")
@@ -345,6 +347,20 @@ unsigned long strtoul (const char *, char **, int);
 #endif
 #endif
 
+/* Background: in most systems the low byte of the wait status
+ * is the signal (the lowest 7 bits) and the coredump flag is
+ * the eight bit, and the second lowest byte is the exit status.
+ * BeOS bucks the trend and has the bytes in different order.
+ * See beos/beos.c for how the reality is bent even in BeOS
+ * to follow the traditional.  However, to make the POSIX
+ * wait W*() macros to work in BeOS, we need to unbend the
+ * reality back in place. --jhi */
+#ifdef __BEOS__
+#    define WMUNGE(x) (((x) & 0xFF00) >> 8 | ((x) & 0x00FF) << 8)
+#else
+#    define WMUNGE(x) (x)
+#endif
+
 static int
 not_here(char *s)
 {
@@ -352,7 +368,7 @@ not_here(char *s)
     return -1;
 }
 
-#include "constants.c"
+#include "const-c.inc"
 
 /* These were implemented in the old "constant" subroutine. They are actually
    macros that take an integer argument and return an integer result.  */
@@ -437,7 +453,8 @@ __END__
       if (memEQ(name, "WSTOPSIG", 8)) {
       /*                  ^          */
 #ifdef WSTOPSIG
-        *arg_result = WSTOPSIG(*arg_result);
+        int i = *arg_result;
+        *arg_result = WSTOPSIG(WMUNGE(i));
         return PERL_constant_ISIV;
 #else
         return PERL_constant_NOTDEF;
@@ -448,7 +465,8 @@ __END__
       if (memEQ(name, "WTERMSIG", 8)) {
       /*                  ^          */
 #ifdef WTERMSIG
-        *arg_result = WTERMSIG(*arg_result);
+        int i = *arg_result;
+        *arg_result = WTERMSIG(WMUNGE(i));
         return PERL_constant_ISIV;
 #else
         return PERL_constant_NOTDEF;
@@ -471,7 +489,8 @@ __END__
   case 9:
     if (memEQ(name, "WIFEXITED", 9)) {
 #ifdef WIFEXITED
-      *arg_result = WIFEXITED(*arg_result);
+      int i = *arg_result;
+      *arg_result = WIFEXITED(WMUNGE(i));
       return PERL_constant_ISIV;
 #else
       return PERL_constant_NOTDEF;
@@ -481,7 +500,8 @@ __END__
   case 10:
     if (memEQ(name, "WIFSTOPPED", 10)) {
 #ifdef WIFSTOPPED
-      *arg_result = WIFSTOPPED(*arg_result);
+      int i = *arg_result;
+      *arg_result = WIFSTOPPED(WMUNGE(i));
       return PERL_constant_ISIV;
 #else
       return PERL_constant_NOTDEF;
@@ -497,7 +517,8 @@ __END__
       if (memEQ(name, "WEXITSTATUS", 11)) {
       /*                ^                */
 #ifdef WEXITSTATUS
-        *arg_result = WEXITSTATUS(*arg_result);
+       int i = *arg_result;
+        *arg_result = WEXITSTATUS(WMUNGE(i));
         return PERL_constant_ISIV;
 #else
         return PERL_constant_NOTDEF;
@@ -508,7 +529,8 @@ __END__
       if (memEQ(name, "WIFSIGNALED", 11)) {
       /*                ^                */
 #ifdef WIFSIGNALED
-        *arg_result = WIFSIGNALED(*arg_result);
+       int i = *arg_result;
+        *arg_result = WIFSIGNALED(WMUNGE(i));
         return PERL_constant_ISIV;
 #else
         return PERL_constant_NOTDEF;
@@ -522,14 +544,15 @@ __END__
 }
 
 static void
-restore_sigmask(sigset_t *ossetp)
+restore_sigmask(pTHX_ SV *osset_sv)
 {
-           /* Fortunately, restoring the signal mask can't fail, because
-            * there's nothing we can do about it if it does -- we're not
-            * supposed to return -1 from sigaction unless the disposition
-            * was unaffected.
-            */
-           (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
+     /* Fortunately, restoring the signal mask can't fail, because
+      * there's nothing we can do about it if it does -- we're not
+      * supposed to return -1 from sigaction unless the disposition
+      * was unaffected.
+      */
+     sigset_t *ossetp = (sigset_t *) SvPV_nolen( osset_sv );
+     (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
 }
 
 MODULE = SigSet                PACKAGE = POSIX::SigSet         PREFIX = sig
@@ -771,7 +794,7 @@ setcc(termios_ref, ccix, cc)
 
 MODULE = POSIX         PACKAGE = POSIX
 
-INCLUDE: constants.xs
+INCLUDE: const-xs.inc
 
 void
 int_macro_int(sv, iv)
@@ -820,7 +843,7 @@ isalnum(charstring)
        unsigned char * charstring
     CODE:
        unsigned char *s = charstring;
-       unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
+       unsigned char *e = s + SvCUR(ST(0));
        for (RETVAL = 1; RETVAL && s < e; s++)
            if (!isalnum(*s))
                RETVAL = 0;
@@ -832,7 +855,7 @@ isalpha(charstring)
        unsigned char * charstring
     CODE:
        unsigned char *s = charstring;
-       unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
+       unsigned char *e = s + SvCUR(ST(0));
        for (RETVAL = 1; RETVAL && s < e; s++)
            if (!isalpha(*s))
                RETVAL = 0;
@@ -844,7 +867,7 @@ iscntrl(charstring)
        unsigned char * charstring
     CODE:
        unsigned char *s = charstring;
-       unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
+       unsigned char *e = s + SvCUR(ST(0));
        for (RETVAL = 1; RETVAL && s < e; s++)
            if (!iscntrl(*s))
                RETVAL = 0;
@@ -856,7 +879,7 @@ isdigit(charstring)
        unsigned char * charstring
     CODE:
        unsigned char *s = charstring;
-       unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
+       unsigned char *e = s + SvCUR(ST(0));
        for (RETVAL = 1; RETVAL && s < e; s++)
            if (!isdigit(*s))
                RETVAL = 0;
@@ -868,7 +891,7 @@ isgraph(charstring)
        unsigned char * charstring
     CODE:
        unsigned char *s = charstring;
-       unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
+       unsigned char *e = s + SvCUR(ST(0));
        for (RETVAL = 1; RETVAL && s < e; s++)
            if (!isgraph(*s))
                RETVAL = 0;
@@ -880,7 +903,7 @@ islower(charstring)
        unsigned char * charstring
     CODE:
        unsigned char *s = charstring;
-       unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
+       unsigned char *e = s + SvCUR(ST(0));
        for (RETVAL = 1; RETVAL && s < e; s++)
            if (!islower(*s))
                RETVAL = 0;
@@ -892,7 +915,7 @@ isprint(charstring)
        unsigned char * charstring
     CODE:
        unsigned char *s = charstring;
-       unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
+       unsigned char *e = s + SvCUR(ST(0));
        for (RETVAL = 1; RETVAL && s < e; s++)
            if (!isprint(*s))
                RETVAL = 0;
@@ -904,7 +927,7 @@ ispunct(charstring)
        unsigned char * charstring
     CODE:
        unsigned char *s = charstring;
-       unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
+       unsigned char *e = s + SvCUR(ST(0));
        for (RETVAL = 1; RETVAL && s < e; s++)
            if (!ispunct(*s))
                RETVAL = 0;
@@ -916,7 +939,7 @@ isspace(charstring)
        unsigned char * charstring
     CODE:
        unsigned char *s = charstring;
-       unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
+       unsigned char *e = s + SvCUR(ST(0));
        for (RETVAL = 1; RETVAL && s < e; s++)
            if (!isspace(*s))
                RETVAL = 0;
@@ -928,7 +951,7 @@ isupper(charstring)
        unsigned char * charstring
     CODE:
        unsigned char *s = charstring;
-       unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
+       unsigned char *e = s + SvCUR(ST(0));
        for (RETVAL = 1; RETVAL && s < e; s++)
            if (!isupper(*s))
                RETVAL = 0;
@@ -940,7 +963,7 @@ isxdigit(charstring)
        unsigned char * charstring
     CODE:
        unsigned char *s = charstring;
-       unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
+       unsigned char *e = s + SvCUR(ST(0));
        for (RETVAL = 1; RETVAL && s < e; s++)
            if (!isxdigit(*s))
                RETVAL = 0;
@@ -1185,6 +1208,7 @@ sigaction(sig, optaction, oldaction = 0)
            struct sigaction act;
            struct sigaction oact;
            sigset_t sset;
+           SV *osset_sv;
            sigset_t osset;
            POSIX__SigSet sigset;
            SV** svp;
@@ -1215,7 +1239,9 @@ sigaction(sig, optaction, oldaction = 0)
                XSRETURN_UNDEF;
            ENTER;
            /* Restore signal mask no matter how we exit this block. */
-           SAVEDESTRUCTOR(restore_sigmask, &osset);
+           osset_sv = newSVpv((char *)(&osset), sizeof(sigset_t));
+           SAVEFREESV( osset_sv );
+           SAVEDESTRUCTOR_X(restore_sigmask, osset_sv);
 
            RETVAL=-1; /* In case both oldaction and action are 0. */
 
@@ -1348,15 +1374,29 @@ dup2(fd1, fd2)
        int             fd1
        int             fd2
 
-SysRetLong
+SV *
 lseek(fd, offset, whence)
        int             fd
        Off_t           offset
        int             whence
+    CODE:
+       Off_t pos = PerlLIO_lseek(fd, offset, whence);
+       RETVAL = sizeof(Off_t) > sizeof(IV)
+                ? newSVnv((NV)pos) : newSViv((IV)pos);
+    OUTPUT:
+       RETVAL
 
-SysRet
+SV *
 nice(incr)
        int             incr
+    PPCODE:
+       errno = 0;
+       if ((incr = nice(incr)) != -1 || errno == 0) {
+           if (incr == 0)
+               XPUSHs(sv_2mortal(newSVpvn("0 but true", 10)));
+           else
+               XPUSHs(sv_2mortal(newSViv(incr)));
+       }
 
 void
 pipe()
@@ -1544,7 +1584,7 @@ strxfrm(src)
           STRLEN dstlen;
           char *p = SvPV(src,srclen);
           srclen++;
-          ST(0) = sv_2mortal(NEWSV(800,srclen));
+          ST(0) = sv_2mortal(NEWSV(800,srclen*4+1));
           dstlen = strxfrm(SvPVX(ST(0)), p, (size_t)srclen);
           if (dstlen > srclen) {
               dstlen++;