📄 posix.xs
字号:
#ifdef WIN32#define _POSIX_#endif#define PERL_NO_GET_CONTEXT#include "EXTERN.h"#define PERLIO_NOT_STDIO 1#include "perl.h"#include "XSUB.h"#if defined(PERL_OBJECT) || defined(PERL_CAPI) || defined(PERL_IMPLICIT_SYS)# undef signal# undef open# undef setmode# define open PerlLIO_open3#endif#include <ctype.h>#ifdef I_DIRENT /* XXX maybe better to just rely on perl.h? */#include <dirent.h>#endif#include <errno.h>#ifdef I_FLOAT#include <float.h>#endif#ifdef I_LIMITS#include <limits.h>#endif#include <locale.h>#include <math.h>#ifdef I_PWD#include <pwd.h>#endif#include <setjmp.h>#include <signal.h>#include <stdarg.h>#ifdef I_STDDEF#include <stddef.h>#endif/* XXX This comment is just to make I_TERMIO and I_SGTTY visible to metaconfig for future extension writers. We don't use them in POSIX. (This is really sneaky :-) --AD*/#if defined(I_TERMIOS)#include <termios.h>#endif#ifdef I_STDLIB#include <stdlib.h>#endif#include <string.h>#include <sys/stat.h>#include <sys/types.h>#include <time.h>#ifdef I_UNISTD#include <unistd.h>#endif#ifdef MACOS_TRADITIONAL#undef fdopen#endif#include <fcntl.h>#if defined(__VMS) && !defined(__POSIX_SOURCE)# include <libdef.h> /* LIB$_INVARG constant */# include <lib$routines.h> /* prototype for lib$ediv() */# include <starlet.h> /* prototype for sys$gettim() */# if DECC_VERSION < 50000000# define pid_t int /* old versions of DECC miss this in types.h */# endif# undef mkfifo# define mkfifo(a,b) (not_here("mkfifo"),-1)# define tzset() not_here("tzset")#if ((__VMS_VER >= 70000000) && (__DECC_VER >= 50200000)) || (__CRTL_VER >= 70000000)# define HAS_TZNAME /* shows up in VMS 7.0 or Dec C 5.6 */# include <utsname.h># endif /* __VMS_VER >= 70000000 or Dec C 5.6 */ /* The POSIX notion of ttyname() is better served by getname() under VMS */ static char ttnambuf[64];# define ttyname(fd) (isatty(fd) > 0 ? getname(fd,ttnambuf,0) : NULL) /* The non-POSIX CRTL times() has void return type, so we just get the current time directly */ clock_t vms_times(struct tms *bufptr) { dTHX; clock_t retval; /* Get wall time and convert to 10 ms intervals to * produce the return value that the POSIX standard expects */# if defined(__DECC) && defined (__ALPHA)# include <ints.h> uint64 vmstime; _ckvmssts(sys$gettim(&vmstime)); vmstime /= 100000; retval = vmstime & 0x7fffffff;# else /* (Older hw or ccs don't have an atomic 64-bit type, so we * juggle 32-bit ints (and a float) to produce a time_t result * with minimal loss of information.) */ long int vmstime[2],remainder,divisor = 100000; _ckvmssts(sys$gettim((unsigned long int *)vmstime)); vmstime[1] &= 0x7fff; /* prevent overflow in EDIV */ _ckvmssts(lib$ediv(&divisor,vmstime,(long int *)&retval,&remainder));# endif /* Fill in the struct tms using the CRTL routine . . .*/ times((tbuffer_t *)bufptr); return (clock_t) retval; }# define times(t) vms_times(t)#else#if defined (__CYGWIN__)# define tzname _tzname#endif#if defined (WIN32)# undef mkfifo# define mkfifo(a,b) not_here("mkfifo")# define ttyname(a) (char*)not_here("ttyname")# define sigset_t long# define pid_t long# ifdef __BORLANDC__# define tzname _tzname# endif# ifdef _MSC_VER# define mode_t short# endif# ifdef __MINGW32__# define mode_t short# ifndef tzset# define tzset() not_here("tzset")# endif# ifndef _POSIX_OPEN_MAX# define _POSIX_OPEN_MAX FOPEN_MAX /* XXX bogus ? */# endif# endif# define sigaction(a,b,c) not_here("sigaction")# define sigpending(a) not_here("sigpending")# define sigprocmask(a,b,c) not_here("sigprocmask")# define sigsuspend(a) not_here("sigsuspend")# define sigemptyset(a) not_here("sigemptyset")# define sigaddset(a,b) not_here("sigaddset")# define sigdelset(a,b) not_here("sigdelset")# define sigfillset(a) not_here("sigfillset")# define sigismember(a,b) not_here("sigismember")# define setuid(a) not_here("setuid")# define setgid(a) not_here("setgid")#else# ifndef HAS_MKFIFO# if defined(OS2) || defined(MACOS_TRADITIONAL)# define mkfifo(a,b) not_here("mkfifo")# else /* !( defined OS2 ) */ # ifndef mkfifo# define mkfifo(path, mode) (mknod((path), (mode) | S_IFIFO, 0))# endif# endif# endif /* !HAS_MKFIFO */# ifdef MACOS_TRADITIONAL# define ttyname(a) (char*)not_here("ttyname")# define tzset() not_here("tzset")# else# include <grp.h># include <sys/times.h># ifdef HAS_UNAME# include <sys/utsname.h># endif# include <sys/wait.h># endif# ifdef I_UTIME# include <utime.h># endif#endif /* WIN32 */#endif /* __VMS */typedef int SysRet;typedef long SysRetLong;typedef sigset_t* POSIX__SigSet;typedef HV* POSIX__SigAction;#ifdef I_TERMIOStypedef struct termios* POSIX__Termios;#else /* Define termios types to int, and call not_here for the functions.*/#define POSIX__Termios int#define speed_t int#define tcflag_t int#define cc_t int#define cfgetispeed(x) not_here("cfgetispeed")#define cfgetospeed(x) not_here("cfgetospeed")#define tcdrain(x) not_here("tcdrain")#define tcflush(x,y) not_here("tcflush")#define tcsendbreak(x,y) not_here("tcsendbreak")#define cfsetispeed(x,y) not_here("cfsetispeed")#define cfsetospeed(x,y) not_here("cfsetospeed")#define ctermid(x) (char *) not_here("ctermid")#define tcflow(x,y) not_here("tcflow")#define tcgetattr(x,y) not_here("tcgetattr")#define tcsetattr(x,y,z) not_here("tcsetattr")#endif/* Possibly needed prototypes */char *cuserid (char *);double strtod (const char *, char **);long strtol (const char *, char **, int);unsigned long strtoul (const char *, char **, int);#ifndef HAS_CUSERID#define cuserid(a) (char *) not_here("cuserid")#endif#ifndef HAS_DIFFTIME#ifndef difftime#define difftime(a,b) not_here("difftime")#endif#endif#ifndef HAS_FPATHCONF#define fpathconf(f,n) (SysRetLong) not_here("fpathconf")#endif#ifndef HAS_MKTIME#define mktime(a) not_here("mktime")#endif#ifndef HAS_NICE#define nice(a) not_here("nice")#endif#ifndef HAS_PATHCONF#define pathconf(f,n) (SysRetLong) not_here("pathconf")#endif#ifndef HAS_SYSCONF#define sysconf(n) (SysRetLong) not_here("sysconf")#endif#ifndef HAS_READLINK#define readlink(a,b,c) not_here("readlink")#endif#ifndef HAS_SETPGID#define setpgid(a,b) not_here("setpgid")#endif#ifndef HAS_SETSID#define setsid() not_here("setsid")#endif#ifndef HAS_STRCOLL#define strcoll(s1,s2) not_here("strcoll")#endif#ifndef HAS_STRTOD#define strtod(s1,s2) not_here("strtod")#endif#ifndef HAS_STRTOL#define strtol(s1,s2,b) not_here("strtol")#endif#ifndef HAS_STRTOUL#define strtoul(s1,s2,b) not_here("strtoul")#endif#ifndef HAS_STRXFRM#define strxfrm(s1,s2,n) not_here("strxfrm")#endif#ifndef HAS_TCGETPGRP#define tcgetpgrp(a) not_here("tcgetpgrp")#endif#ifndef HAS_TCSETPGRP#define tcsetpgrp(a,b) not_here("tcsetpgrp")#endif#ifndef HAS_TIMES#define times(a) not_here("times")#endif#ifndef HAS_UNAME#define uname(a) not_here("uname")#endif#ifndef HAS_WAITPID#define waitpid(a,b,c) not_here("waitpid")#endif#ifndef HAS_MBLEN#ifndef mblen#define mblen(a,b) not_here("mblen")#endif#endif#ifndef HAS_MBSTOWCS#define mbstowcs(s, pwcs, n) not_here("mbstowcs")#endif#ifndef HAS_MBTOWC#define mbtowc(pwc, s, n) not_here("mbtowc")#endif#ifndef HAS_WCSTOMBS#define wcstombs(s, pwcs, n) not_here("wcstombs")#endif#ifndef HAS_WCTOMB#define wctomb(s, wchar) not_here("wcstombs")#endif#if !defined(HAS_MBLEN) && !defined(HAS_MBSTOWCS) && !defined(HAS_MBTOWC) && !defined(HAS_WCSTOMBS) && !defined(HAS_WCTOMB)/* If we don't have these functions, then we wouldn't have gotten a typedef for wchar_t, the wide character type. Defining wchar_t allows the functions referencing it to compile. Its actual type is then meaningless, since without the above functions, all sections using it end up calling not_here() and croak. --Kaveh Ghazi (ghazi@noc.rutgers.edu) 9/18/94. */#ifndef wchar_t#define wchar_t char#endif#endif#ifndef HAS_LOCALECONV#define localeconv() not_here("localeconv")#endif#ifdef HAS_TZNAME# if !defined(WIN32) && !defined(__CYGWIN__)extern char *tzname[];# endif#else#if !defined(WIN32) || (defined(__MINGW32__) && !defined(tzname))char *tzname[] = { "" , "" };#endif#endif/* XXX struct tm on some systems (SunOS4/BSD) contains extra (non POSIX) * fields for which we don't have Configure support yet: * char *tm_zone; -- abbreviation of timezone name * long tm_gmtoff; -- offset from GMT in seconds * To workaround core dumps from the uninitialised tm_zone we get the * system to give us a reasonable struct to copy. This fix means that * strftime uses the tm_zone and tm_gmtoff values returned by * localtime(time()). That should give the desired result most of the * time. But probably not always! * * This is a temporary workaround to be removed once Configure * support is added and NETaa14816 is considered in full. * It does not address tzname aspects of NETaa14816. */#ifdef HAS_GNULIBC# ifndef STRUCT_TM_HASZONE# define STRUCT_TM_HASZONE# endif#endif#ifdef STRUCT_TM_HASZONEstatic voidinit_tm(struct tm *ptm) /* see mktime, strftime and asctime */{ Time_t now; (void)time(&now); Copy(localtime(&now), ptm, 1, struct tm);}#else# define init_tm(ptm)#endif/* * mini_mktime - normalise struct tm values without the localtime() * semantics (and overhead) of mktime(). */static voidmini_mktime(struct tm *ptm){ int yearday; int secs; int month, mday, year, jday; int odd_cent, odd_year;#define DAYS_PER_YEAR 365#define DAYS_PER_QYEAR (4*DAYS_PER_YEAR+1)#define DAYS_PER_CENT (25*DAYS_PER_QYEAR-1)#define DAYS_PER_QCENT (4*DAYS_PER_CENT+1)#define SECS_PER_HOUR (60*60)#define SECS_PER_DAY (24*SECS_PER_HOUR)/* parentheses deliberately absent on these two, otherwise they don't work */#define MONTH_TO_DAYS 153/5#define DAYS_TO_MONTH 5/153/* offset to bias by March (month 4) 1st between month/mday & year finding */#define YEAR_ADJUST (4*MONTH_TO_DAYS+1)/* as used here, the algorithm leaves Sunday as day 1 unless we adjust it */#define WEEKDAY_BIAS 6 /* (1+6)%7 makes Sunday 0 again *//* * Year/day algorithm notes: * * With a suitable offset for numeric value of the month, one can find * an offset into the year by considering months to have 30.6 (153/5) days, * using integer arithmetic (i.e., with truncation). To avoid too much * messing about with leap days, we consider January and February to be * the 13th and 14th month of the previous year. After that transformation, * we need the month index we use to be high by 1 from 'normal human' usage, * so the month index values we use run from 4 through 15. * * Given that, and the rules for the Gregorian calendar (leap years are those * divisible by 4 unless also divisible by 100, when they must be divisible * by 400 instead), we can simply calculate the number of days since some * arbitrary 'beginning of time' by futzing with the (adjusted) year number, * the days we derive from our month index, and adding in the day of the * month. The value used here is not adjusted for the actual origin which * it normally would use (1 January A.D. 1), since we're not exposing it. * We're only building the value so we can turn around and get the * normalised values for the year, month, day-of-month, and day-of-year. * * For going backward, we need to bias the value we're using so that we find * the right year value. (Basically, we don't want the contribution of * March 1st to the number to apply while deriving the year). Having done * that, we 'count up' the contribution to the year number by accounting for * full quadracenturies (400-year periods) with their extra leap days, plus * the contribution from full centuries (to avoid counting in the lost leap * days), plus the contribution from full quad-years (to count in the normal * leap days), plus the leftover contribution from any non-leap years. * At this point, if we were working with an actual leap day, we'll have 0 * days left over. This is also true for March 1st, however. So, we have * to special-case that result, and (earlier) keep track of the 'odd' * century and year contributions. If we got 4 extra centuries in a qcent, * or 4 extra years in a qyear, then it's a leap day and we call it 29 Feb. * Otherwise, we add back in the earlier bias we removed (the 123 from * figuring in March 1st), find the month index (integer division by 30.6), * and the remainder is the day-of-month. We then have to convert back to * 'real' months (including fixing January and February from being 14/15 in * the previous year to being in the proper year). After that, to get * tm_yday, we work with the normalised year and get a new yearday value for * January 1st, which we subtract from the yearday value we had earlier, * representing the date we've re-built. This is done from January 1 * because tm_yday is 0-origin. * * Since POSIX time routines are only guaranteed to work for times since the * UNIX epoch (00:00:00 1 Jan 1970 UTC), the fact that this algorithm * applies Gregorian calendar rules even to dates before the 16th century * doesn't bother me. Besides, you'd need cultural context for a given * date to know whether it was Julian or Gregorian calendar, and that's * outside the scope for this routine. Since we convert back based on the * same rules we used to build the yearday, you'll only get strange results * for input which needed normalising, or for the 'odd' century years which * were leap years in the Julian calander but not in the Gregorian one. * I can live with that. * * This algorithm also fails to handle years before A.D. 1 gracefully, but * that's still outside the scope for POSIX time manipulation, so I don't * care. */ year = 1900 + ptm->tm_year; month = ptm->tm_mon; mday = ptm->tm_mday; /* allow given yday with no month & mday to dominate the result */ if (ptm->tm_yday >= 0 && mday <= 0 && month <= 0) { month = 0; mday = 0; jday = 1 + ptm->tm_yday; } else { jday = 0; } if (month >= 2) month+=2; else month+=14, year--; yearday = DAYS_PER_YEAR * year + year/4 - year/100 + year/400; yearday += month*MONTH_TO_DAYS + mday + jday; /* * Note that we don't know when leap-seconds were or will be, * so we have to trust the user if we get something which looks * like a sensible leap-second. Wild values for seconds will * be rationalised, however. */ if ((unsigned) ptm->tm_sec <= 60) { secs = 0; } else { secs = ptm->tm_sec; ptm->tm_sec = 0; } secs += 60 * ptm->tm_min; secs += SECS_PER_HOUR * ptm->tm_hour; if (secs < 0) { if (secs-(secs/SECS_PER_DAY*SECS_PER_DAY) < 0) { /* got negative remainder, but need positive time */ /* back off an extra day to compensate */ yearday += (secs/SECS_PER_DAY)-1; secs -= SECS_PER_DAY * (secs/SECS_PER_DAY - 1); } else { yearday += (secs/SECS_PER_DAY); secs -= SECS_PER_DAY * (secs/SECS_PER_DAY); } } else if (secs >= SECS_PER_DAY) { yearday += (secs/SECS_PER_DAY); secs %= SECS_PER_DAY; } ptm->tm_hour = secs/SECS_PER_HOUR; secs %= SECS_PER_HOUR; ptm->tm_min = secs/60; secs %= 60; ptm->tm_sec += secs; /* done with time of day effects */ /* * The algorithm for yearday has (so far) left it high by 428. * To avoid mistaking a legitimate Feb 29 as Mar 1, we need to * bias it by 123 while trying to figure out what year it * really represents. Even with this tweak, the reverse * translation fails for years before A.D. 0001. * It would still fail for Feb 29, but we catch that one below. */ jday = yearday; /* save for later fixup vis-a-vis Jan 1 */ yearday -= YEAR_ADJUST; year = (yearday / DAYS_PER_QCENT) * 400; yearday %= DAYS_PER_QCENT; odd_cent = yearday / DAYS_PER_CENT; year += odd_cent * 100; yearday %= DAYS_PER_CENT; year += (yearday / DAYS_PER_QYEAR) * 4; yearday %= DAYS_PER_QYEAR; odd_year = yearday / DAYS_PER_YEAR; year += odd_year; yearday %= DAYS_PER_YEAR; if (!yearday && (odd_cent==4 || odd_year==4)) { /* catch Feb 29 */ month = 1; yearday = 29; } else { yearday += YEAR_ADJUST; /* recover March 1st crock */ month = yearday*DAYS_TO_MONTH; yearday -= month*MONTH_TO_DAYS; /* recover other leap-year adjustment */ if (month > 13) { month-=14; year++; } else { month-=2; } } ptm->tm_year = year - 1900; if (yearday) { ptm->tm_mday = yearday; ptm->tm_mon = month; } else { ptm->tm_mday = 31; ptm->tm_mon = month - 1; } /* re-build yearday based on Jan 1 to get tm_yday */ year--; yearday = year*DAYS_PER_YEAR + year/4 - year/100 + year/400; yearday += 14*MONTH_TO_DAYS + 1; ptm->tm_yday = jday - yearday; /* fix tm_wday if not overridden by caller */ if ((unsigned)ptm->tm_wday > 6) ptm->tm_wday = (jday + WEEKDAY_BIAS) % 7;}#ifdef HAS_LONG_DOUBLE# if LONG_DOUBLESIZE > NVSIZE# undef HAS_LONG_DOUBLE /* XXX until we figure out how to use them */# endif#endif#ifndef HAS_LONG_DOUBLE#ifdef LDBL_MAX#undef LDBL_MAX#endif#ifdef LDBL_MIN#undef LDBL_MIN#endif#ifdef LDBL_EPSILON#undef LDBL_EPSILON#endif#endifstatic intnot_here(char *s){ croak("POSIX::%s not implemented on this architecture", s); return -1;}staticNVconstant(char *name, int arg){ errno = 0; switch (*name) { case 'A': if (strEQ(name, "ARG_MAX"))#ifdef ARG_MAX return ARG_MAX;#else goto not_there;#endif break; case 'B': if (strEQ(name, "BUFSIZ"))#ifdef BUFSIZ return BUFSIZ;#else goto not_there;#endif if (strEQ(name, "BRKINT"))#ifdef BRKINT return BRKINT;#else goto not_there;#endif if (strEQ(name, "B9600"))#ifdef B9600 return B9600;#else goto not_there;#endif if (strEQ(name, "B19200"))#ifdef B19200 return B19200;#else goto not_there;#endif if (strEQ(name, "B38400"))#ifdef B38400 return B38400;#else goto not_there;#endif if (strEQ(name, "B0"))#ifdef B0 return B0;#else goto not_there;#endif if (strEQ(name, "B110"))#ifdef B110 return B110;#else goto not_there;#endif if (strEQ(name, "B1200"))#ifdef B1200 return B1200;#else goto not_there;#endif if (strEQ(name, "B134"))#ifdef B134 return B134;#else goto not_there;#endif if (strEQ(name, "B150"))#ifdef B150 return B150;#else goto not_there;#endif if (strEQ(name, "B1800"))#ifdef B1800 return B1800;#else goto not_there;#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -