3 * Copyright (C) 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * But only a short way ahead its floor and the walls on either side were
13 * cloven by a great fissure, out of which the red glare came, now leaping
14 * up, now dying down into darkness; and all the while far below there was
15 * a rumour and a trouble as of great engines throbbing and labouring.
18 /* This file contains system pp ("push/pop") functions that
19 * execute the opcodes that make up a perl program. A typical pp function
20 * expects to find its arguments on the stack, and usually pushes its
21 * results onto the stack, hence the 'pp' terminology. Each OP structure
22 * contains a pointer to the relevant pp_foo() function.
24 * By 'system', we mean ops which interact with the OS, such as pp_open().
28 #define PERL_IN_PP_SYS_C
32 /* Shadow password support for solaris - pdo@cs.umd.edu
33 * Not just Solaris: at least HP-UX, IRIX, Linux.
34 * The API is from SysV.
36 * There are at least two more shadow interfaces,
37 * see the comments in pp_gpwent().
41 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
42 * and another MAXINT from "perl.h" <- <sys/param.h>. */
49 # include <sys/wait.h>
53 # include <sys/resource.h>
62 # include <sys/select.h>
66 /* XXX Configure test needed.
67 h_errno might not be a simple 'int', especially for multi-threaded
68 applications, see "extern int errno in perl.h". Creating such
69 a test requires taking into account the differences between
70 compiling multithreaded and singlethreaded ($ccflags et al).
71 HOST_NOT_FOUND is typically defined in <netdb.h>.
73 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
82 struct passwd *getpwnam (char *);
83 struct passwd *getpwuid (Uid_t);
88 struct passwd *getpwent (void);
89 #elif defined (VMS) && defined (my_getpwent)
90 struct passwd *Perl_my_getpwent (void);
99 struct group *getgrnam (char *);
100 struct group *getgrgid (Gid_t);
104 struct group *getgrent (void);
110 # if defined(_MSC_VER) || defined(__MINGW32__)
111 # include <sys/utime.h>
118 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
121 # define my_chsize PerlLIO_chsize
126 #else /* no flock() */
128 /* fcntl.h might not have been included, even if it exists, because
129 the current Configure only sets I_FCNTL if it's needed to pick up
130 the *_OK constants. Make sure it has been included before testing
131 the fcntl() locking constants. */
132 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
136 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
137 # define FLOCK fcntl_emulate_flock
138 # define FCNTL_EMULATE_FLOCK
139 # else /* no flock() or fcntl(F_SETLK,...) */
141 # define FLOCK lockf_emulate_flock
142 # define LOCKF_EMULATE_FLOCK
144 # endif /* no flock() or fcntl(F_SETLK,...) */
147 static int FLOCK (int, int);
150 * These are the flock() constants. Since this sytems doesn't have
151 * flock(), the values of the constants are probably not available.
165 # endif /* emulating flock() */
167 #endif /* no flock() */
170 static char zero_but_true[ZBTLEN + 1] = "0 but true";
172 #if defined(I_SYS_ACCESS) && !defined(R_OK)
173 # include <sys/access.h>
176 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
177 # define FD_CLOEXEC 1 /* NeXT needs this */
183 /* Missing protos on LynxOS */
184 void sethostent(int);
185 void endhostent(void);
187 void endnetent(void);
188 void setprotoent(int);
189 void endprotoent(void);
190 void setservent(int);
191 void endservent(void);
194 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
195 #undef PERL_EFF_ACCESS_W_OK
196 #undef PERL_EFF_ACCESS_X_OK
198 /* F_OK unused: if stat() cannot find it... */
200 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
201 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
202 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
203 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
204 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
207 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
208 # ifdef I_SYS_SECURITY
209 # include <sys/security.h>
213 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
214 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
215 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
218 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
219 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
220 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
224 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
226 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
227 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
228 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
231 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
232 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
233 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
236 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
238 Uid_t ruid = getuid();
239 Uid_t euid = geteuid();
240 Gid_t rgid = getgid();
241 Gid_t egid = getegid();
245 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
246 Perl_croak(aTHX_ "switching effective uid is not implemented");
249 if (setreuid(euid, ruid))
252 if (setresuid(euid, ruid, (Uid_t)-1))
255 Perl_croak(aTHX_ "entering effective uid failed");
258 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
259 Perl_croak(aTHX_ "switching effective gid is not implemented");
262 if (setregid(egid, rgid))
265 if (setresgid(egid, rgid, (Gid_t)-1))
268 Perl_croak(aTHX_ "entering effective gid failed");
271 res = access(path, mode);
274 if (setreuid(ruid, euid))
277 if (setresuid(ruid, euid, (Uid_t)-1))
280 Perl_croak(aTHX_ "leaving effective uid failed");
283 if (setregid(rgid, egid))
286 if (setresgid(rgid, egid, (Gid_t)-1))
289 Perl_croak(aTHX_ "leaving effective gid failed");
294 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
295 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
296 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
299 #if !defined(PERL_EFF_ACCESS_R_OK)
300 /* With it or without it: anyway you get a warning: either that
301 it is unused, or it is declared static and never defined.
304 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
306 Perl_croak(aTHX_ "switching effective uid is not implemented");
319 const char *mode = "r";
322 if (PL_op->op_private & OPpOPEN_IN_RAW)
324 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
326 fp = PerlProc_popen(tmps, (char *)mode);
328 const char *type = NULL;
329 if (PL_curcop->cop_io) {
330 type = SvPV_nolen(PL_curcop->cop_io);
333 PerlIO_apply_layers(aTHX_ fp,mode,type);
335 if (gimme == G_VOID) {
337 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
341 else if (gimme == G_SCALAR) {
344 PL_rs = &PL_sv_undef;
345 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
346 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
358 if (sv_gets(sv, fp, 0) == Nullch) {
362 XPUSHs(sv_2mortal(sv));
363 if (SvLEN(sv) - SvCUR(sv) > 20) {
364 SvLEN_set(sv, SvCUR(sv)+1);
365 Renew(SvPVX(sv), SvLEN(sv), char);
370 STATUS_NATIVE_SET(PerlProc_pclose(fp));
371 TAINT; /* "I believe that this is not gratuitous!" */
374 STATUS_NATIVE_SET(-1);
375 if (gimme == G_SCALAR)
385 tryAMAGICunTARGET(iter, -1);
387 /* Note that we only ever get here if File::Glob fails to load
388 * without at the same time croaking, for some reason, or if
389 * perl was built with PERL_EXTERNAL_GLOB */
396 * The external globbing program may use things we can't control,
397 * so for security reasons we must assume the worst.
400 taint_proper(PL_no_security, "glob");
404 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
405 PL_last_in_gv = (GV*)*PL_stack_sp--;
407 SAVESPTR(PL_rs); /* This is not permanent, either. */
408 PL_rs = sv_2mortal(newSVpvn("\000", 1));
411 *SvPVX(PL_rs) = '\n';
415 result = do_readline();
422 PL_last_in_gv = cGVOP_gv;
423 return do_readline();
432 if (SP - MARK != 1) {
434 do_join(TARG, &PL_sv_no, MARK, SP);
441 tmps = SvPV(tmpsv, len);
442 if ((!tmps || !len) && PL_errgv) {
444 (void)SvUPGRADE(error, SVt_PV);
445 if (SvPOK(error) && SvCUR(error))
446 sv_catpv(error, "\t...caught");
448 tmps = SvPV(tmpsv, len);
451 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
453 Perl_warn(aTHX_ "%"SVf, tmpsv);
465 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
467 if (SP - MARK != 1) {
469 do_join(TARG, &PL_sv_no, MARK, SP);
471 tmps = SvPV(tmpsv, len);
477 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
481 (void)SvUPGRADE(error, SVt_PV);
482 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
484 SvSetSV(error,tmpsv);
485 else if (sv_isobject(error)) {
486 HV *stash = SvSTASH(SvRV(error));
487 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
489 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
490 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
497 call_sv((SV*)GvCV(gv),
498 G_SCALAR|G_EVAL|G_KEEPERR);
499 sv_setsv(error,*PL_stack_sp--);
505 if (SvPOK(error) && SvCUR(error))
506 sv_catpv(error, "\t...propagated");
508 tmps = SvPV(tmpsv, len);
512 tmpsv = sv_2mortal(newSVpvn("Died", 4));
514 DIE(aTHX_ "%"SVf, tmpsv);
534 DIE(aTHX_ PL_no_usym, "filehandle");
535 if ((io = GvIOp(gv)))
536 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
538 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
539 /* Method's args are same as ours ... */
540 /* ... except handle is replaced by the object */
541 *MARK-- = SvTIED_obj((SV*)io, mg);
545 call_method("OPEN", G_SCALAR);
558 tmps = SvPV(sv, len);
559 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
562 PUSHi( (I32)PL_forkprocess );
563 else if (PL_forkprocess == 0) /* we are a new child */
582 if (gv && (io = GvIO(gv))
583 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
586 XPUSHs(SvTIED_obj((SV*)io, mg));
589 call_method("CLOSE", G_SCALAR);
595 PUSHs(boolSV(do_close(gv, TRUE)));
615 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
616 DIE(aTHX_ PL_no_usym, "filehandle");
621 do_close(rgv, FALSE);
623 do_close(wgv, FALSE);
625 if (PerlProc_pipe(fd) < 0)
628 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
629 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
630 IoOFP(rstio) = IoIFP(rstio);
631 IoIFP(wstio) = IoOFP(wstio);
632 IoTYPE(rstio) = IoTYPE_RDONLY;
633 IoTYPE(wstio) = IoTYPE_WRONLY;
635 if (!IoIFP(rstio) || !IoOFP(wstio)) {
636 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
637 else PerlLIO_close(fd[0]);
638 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
639 else PerlLIO_close(fd[1]);
642 #if defined(HAS_FCNTL) && defined(F_SETFD)
643 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
644 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
651 DIE(aTHX_ PL_no_func, "pipe");
667 if (gv && (io = GvIO(gv))
668 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
671 XPUSHs(SvTIED_obj((SV*)io, mg));
674 call_method("FILENO", G_SCALAR);
680 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
681 /* Can't do this because people seem to do things like
682 defined(fileno($foo)) to check whether $foo is a valid fh.
683 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
684 report_evil_fh(gv, io, PL_op->op_type);
689 PUSHi(PerlIO_fileno(fp));
700 anum = PerlLIO_umask(0);
701 (void)PerlLIO_umask(anum);
704 anum = PerlLIO_umask(POPi);
705 TAINT_PROPER("umask");
708 /* Only DIE if trying to restrict permissions on `user' (self).
709 * Otherwise it's harmless and more useful to just return undef
710 * since 'group' and 'other' concepts probably don't exist here. */
711 if (MAXARG >= 1 && (POPi & 0700))
712 DIE(aTHX_ "umask not implemented");
713 XPUSHs(&PL_sv_undef);
735 if (gv && (io = GvIO(gv))
736 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
739 XPUSHs(SvTIED_obj((SV*)io, mg));
744 call_method("BINMODE", G_SCALAR);
751 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
752 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
753 report_evil_fh(gv, io, PL_op->op_type);
754 SETERRNO(EBADF,RMS_IFI);
759 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
760 (discp) ? SvPV_nolen(discp) : Nullch)) {
761 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
762 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
763 mode_from_discipline(discp),
764 (discp) ? SvPV_nolen(discp) : Nullch)) {
786 I32 markoff = MARK - PL_stack_base;
787 const char *methname;
788 int how = PERL_MAGIC_tied;
792 switch(SvTYPE(varsv)) {
794 methname = "TIEHASH";
795 HvEITER((HV *)varsv) = Null(HE *);
798 methname = "TIEARRAY";
801 #ifdef GV_UNIQUE_CHECK
802 if (GvUNIQUE((GV*)varsv)) {
803 Perl_croak(aTHX_ "Attempt to tie unique GV");
806 methname = "TIEHANDLE";
807 how = PERL_MAGIC_tiedscalar;
808 /* For tied filehandles, we apply tiedscalar magic to the IO
809 slot of the GP rather than the GV itself. AMS 20010812 */
811 GvIOp(varsv) = newIO();
812 varsv = (SV *)GvIOp(varsv);
815 methname = "TIESCALAR";
816 how = PERL_MAGIC_tiedscalar;
820 if (sv_isobject(*MARK)) {
822 PUSHSTACKi(PERLSI_MAGIC);
824 EXTEND(SP,(I32)items);
828 call_method(methname, G_SCALAR);
831 /* Not clear why we don't call call_method here too.
832 * perhaps to get different error message ?
834 stash = gv_stashsv(*MARK, FALSE);
835 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
836 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
840 PUSHSTACKi(PERLSI_MAGIC);
842 EXTEND(SP,(I32)items);
846 call_sv((SV*)GvCV(gv), G_SCALAR);
852 if (sv_isobject(sv)) {
853 sv_unmagic(varsv, how);
854 /* Croak if a self-tie on an aggregate is attempted. */
855 if (varsv == SvRV(sv) &&
856 (SvTYPE(varsv) == SVt_PVAV ||
857 SvTYPE(varsv) == SVt_PVHV))
859 "Self-ties of arrays and hashes are not supported");
860 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
863 SP = PL_stack_base + markoff;
873 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
874 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
876 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
879 if ((mg = SvTIED_mg(sv, how))) {
880 SV *obj = SvRV(SvTIED_obj(sv, mg));
884 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
885 isGV(gv) && (cv = GvCV(gv))) {
887 XPUSHs(SvTIED_obj((SV*)gv, mg));
888 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
891 call_sv((SV *)cv, G_VOID);
895 else if (ckWARN(WARN_UNTIE)) {
896 if (mg && SvREFCNT(obj) > 1)
897 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
898 "untie attempted while %"UVuf" inner references still exist",
899 (UV)SvREFCNT(obj) - 1 ) ;
903 sv_unmagic(sv, how) ;
912 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
913 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
915 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
918 if ((mg = SvTIED_mg(sv, how))) {
919 SV *osv = SvTIED_obj(sv, mg);
920 if (osv == mg->mg_obj)
921 osv = sv_mortalcopy(osv);
939 sv = sv_mortalcopy(&PL_sv_no);
940 sv_setpv(sv, "AnyDBM_File");
941 stash = gv_stashsv(sv, FALSE);
942 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
944 require_pv("AnyDBM_File.pm");
946 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
947 DIE(aTHX_ "No dbm on this machine");
957 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
959 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
962 call_sv((SV*)GvCV(gv), G_SCALAR);
965 if (!sv_isobject(TOPs)) {
970 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
973 call_sv((SV*)GvCV(gv), G_SCALAR);
977 if (sv_isobject(TOPs)) {
978 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
979 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
1001 struct timeval timebuf;
1002 struct timeval *tbuf = &timebuf;
1006 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1011 # if BYTEORDER & 0xf0000
1012 # define ORDERBYTE (0x88888888 - BYTEORDER)
1014 # define ORDERBYTE (0x4444 - BYTEORDER)
1020 for (i = 1; i <= 3; i++) {
1028 /* little endians can use vecs directly */
1029 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1036 masksize = NFDBITS / NBBY;
1038 masksize = sizeof(long); /* documented int, everyone seems to use long */
1040 Zero(&fd_sets[0], 4, char*);
1043 # if SELECT_MIN_BITS == 1
1044 growsize = sizeof(fd_set);
1046 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1047 # undef SELECT_MIN_BITS
1048 # define SELECT_MIN_BITS __FD_SETSIZE
1050 /* If SELECT_MIN_BITS is greater than one we most probably will want
1051 * to align the sizes with SELECT_MIN_BITS/8 because for example
1052 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1053 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1054 * on (sets/tests/clears bits) is 32 bits. */
1055 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1063 timebuf.tv_sec = (long)value;
1064 value -= (NV)timebuf.tv_sec;
1065 timebuf.tv_usec = (long)(value * 1000000.0);
1068 tbuf = Null(struct timeval*);
1070 for (i = 1; i <= 3; i++) {
1076 else if (!SvPOK(sv))
1077 SvPV_force(sv,n_a); /* force string conversion */
1080 Sv_Grow(sv, growsize);
1084 while (++j <= growsize) {
1088 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1090 New(403, fd_sets[i], growsize, char);
1091 for (offset = 0; offset < growsize; offset += masksize) {
1092 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1093 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1096 fd_sets[i] = SvPVX(sv);
1100 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1101 /* Can't make just the (void*) conditional because that would be
1102 * cpp #if within cpp macro, and not all compilers like that. */
1103 nfound = PerlSock_select(
1105 (Select_fd_set_t) fd_sets[1],
1106 (Select_fd_set_t) fd_sets[2],
1107 (Select_fd_set_t) fd_sets[3],
1108 (void*) tbuf); /* Workaround for compiler bug. */
1110 nfound = PerlSock_select(
1112 (Select_fd_set_t) fd_sets[1],
1113 (Select_fd_set_t) fd_sets[2],
1114 (Select_fd_set_t) fd_sets[3],
1117 for (i = 1; i <= 3; i++) {
1120 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1122 for (offset = 0; offset < growsize; offset += masksize) {
1123 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1124 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1126 Safefree(fd_sets[i]);
1133 if (GIMME == G_ARRAY && tbuf) {
1134 value = (NV)(timebuf.tv_sec) +
1135 (NV)(timebuf.tv_usec) / 1000000.0;
1136 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1137 sv_setnv(sv, value);
1141 DIE(aTHX_ "select not implemented");
1146 Perl_setdefout(pTHX_ GV *gv)
1149 (void)SvREFCNT_inc(gv);
1151 SvREFCNT_dec(PL_defoutgv);
1158 GV *newdefout, *egv;
1161 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1163 egv = GvEGV(PL_defoutgv);
1168 XPUSHs(&PL_sv_undef);
1170 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1171 if (gvp && *gvp == egv) {
1172 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1176 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1181 if (!GvIO(newdefout))
1182 gv_IOadd(newdefout);
1183 setdefout(newdefout);
1201 if (gv && (io = GvIO(gv))
1202 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1204 I32 gimme = GIMME_V;
1206 XPUSHs(SvTIED_obj((SV*)io, mg));
1209 call_method("GETC", gimme);
1212 if (gimme == G_SCALAR)
1213 SvSetMagicSV_nosteal(TARG, TOPs);
1216 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1217 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1218 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1219 report_evil_fh(gv, io, PL_op->op_type);
1220 SETERRNO(EBADF,RMS_IFI);
1224 sv_setpv(TARG, " ");
1225 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1226 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1227 /* Find out how many bytes the char needs */
1228 Size_t len = UTF8SKIP(SvPVX(TARG));
1231 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1232 SvCUR_set(TARG,1+len);
1242 return pp_sysread();
1246 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1248 register PERL_CONTEXT *cx;
1249 I32 gimme = GIMME_V;
1254 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1256 cx->blk_sub.retop = retop;
1257 PAD_SET_CUR(CvPADLIST(cv), 1);
1259 setdefout(gv); /* locally select filehandle so $% et al work */
1292 SV *tmpsv = sv_newmortal();
1293 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1294 name = SvPV_nolen(tmpsv);
1297 DIE(aTHX_ "Undefined format \"%s\" called", name);
1298 DIE(aTHX_ "Not a format reference");
1301 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1303 IoFLAGS(io) &= ~IOf_DIDTOP;
1304 return doform(cv,gv,PL_op->op_next);
1310 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1311 register IO *io = GvIOp(gv);
1312 PerlIO *ofp = IoOFP(io);
1316 register PERL_CONTEXT *cx;
1318 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1319 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1322 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1323 PL_formtarget != PL_toptarget)
1327 if (!IoTOP_GV(io)) {
1331 if (!IoTOP_NAME(io)) {
1332 if (!IoFMT_NAME(io))
1333 IoFMT_NAME(io) = savepv(GvNAME(gv));
1334 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1335 topgv = gv_fetchsv(topname, FALSE, SVt_PVFM);
1336 if ((topgv && GvFORM(topgv)) ||
1337 !gv_fetchpv("top",FALSE,SVt_PVFM))
1338 IoTOP_NAME(io) = savesvpv(topname);
1340 IoTOP_NAME(io) = savepvn("top", 3);
1342 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1343 if (!topgv || !GvFORM(topgv)) {
1344 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1347 IoTOP_GV(io) = topgv;
1349 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1350 I32 lines = IoLINES_LEFT(io);
1351 char *s = SvPVX(PL_formtarget);
1352 if (lines <= 0) /* Yow, header didn't even fit!!! */
1354 while (lines-- > 0) {
1355 s = strchr(s, '\n');
1361 STRLEN save = SvCUR(PL_formtarget);
1362 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1363 do_print(PL_formtarget, ofp);
1364 SvCUR_set(PL_formtarget, save);
1365 sv_chop(PL_formtarget, s);
1366 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1369 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1370 do_print(PL_formfeed, ofp);
1371 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1373 PL_formtarget = PL_toptarget;
1374 IoFLAGS(io) |= IOf_DIDTOP;
1377 DIE(aTHX_ "bad top format reference");
1382 SV *sv = sv_newmortal();
1383 gv_efullname4(sv, fgv, Nullch, FALSE);
1384 name = SvPV_nolen(sv);
1387 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1390 DIE(aTHX_ "Undefined top format called");
1394 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1395 return doform(cv,gv,PL_op);
1399 POPBLOCK(cx,PL_curpm);
1405 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1407 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1408 else if (ckWARN(WARN_CLOSED))
1409 report_evil_fh(gv, io, PL_op->op_type);
1414 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1415 if (ckWARN(WARN_IO))
1416 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1418 if (!do_print(PL_formtarget, fp))
1421 FmLINES(PL_formtarget) = 0;
1422 SvCUR_set(PL_formtarget, 0);
1423 *SvEND(PL_formtarget) = '\0';
1424 if (IoFLAGS(io) & IOf_FLUSH)
1425 (void)PerlIO_flush(fp);
1430 PL_formtarget = PL_bodytarget;
1432 return cx->blk_sub.retop;
1437 dSP; dMARK; dORIGMARK;
1444 if (PL_op->op_flags & OPf_STACKED)
1449 if (gv && (io = GvIO(gv))
1450 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1452 if (MARK == ORIGMARK) {
1455 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1459 *MARK = SvTIED_obj((SV*)io, mg);
1462 call_method("PRINTF", G_SCALAR);
1465 MARK = ORIGMARK + 1;
1472 if (!(io = GvIO(gv))) {
1473 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1474 report_evil_fh(gv, io, PL_op->op_type);
1475 SETERRNO(EBADF,RMS_IFI);
1478 else if (!(fp = IoOFP(io))) {
1479 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1481 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1482 else if (ckWARN(WARN_CLOSED))
1483 report_evil_fh(gv, io, PL_op->op_type);
1485 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1489 do_sprintf(sv, SP - MARK, MARK + 1);
1490 if (!do_print(sv, fp))
1493 if (IoFLAGS(io) & IOf_FLUSH)
1494 if (PerlIO_flush(fp) == EOF)
1505 PUSHs(&PL_sv_undef);
1526 /* Need TIEHANDLE method ? */
1528 tmps = SvPV(sv, len);
1529 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1530 IoLINES(GvIOp(gv)) = 0;
1534 PUSHs(&PL_sv_undef);
1541 dSP; dMARK; dORIGMARK; dTARGET;
1548 Sock_size_t bufsize;
1557 bool charstart = FALSE;
1558 STRLEN charskip = 0;
1562 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1563 && gv && (io = GvIO(gv))
1564 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1569 *MARK = SvTIED_obj((SV*)io, mg);
1571 call_method("READ", G_SCALAR);
1584 sv_setpvn(bufsv, "", 0);
1585 length = SvIVx(*++MARK);
1588 offset = SvIVx(*++MARK);
1592 if (!io || !IoIFP(io)) {
1593 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1594 report_evil_fh(gv, io, PL_op->op_type);
1595 SETERRNO(EBADF,RMS_IFI);
1598 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1599 buffer = SvPVutf8_force(bufsv, blen);
1600 /* UTF-8 may not have been set if they are all low bytes */
1605 buffer = SvPV_force(bufsv, blen);
1606 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1609 DIE(aTHX_ "Negative length");
1617 if (PL_op->op_type == OP_RECV) {
1618 char namebuf[MAXPATHLEN];
1619 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1620 bufsize = sizeof (struct sockaddr_in);
1622 bufsize = sizeof namebuf;
1624 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1628 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1629 /* 'offset' means 'flags' here */
1630 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1631 (struct sockaddr *)namebuf, &bufsize);
1635 /* Bogus return without padding */
1636 bufsize = sizeof (struct sockaddr_in);
1638 SvCUR_set(bufsv, count);
1639 *SvEND(bufsv) = '\0';
1640 (void)SvPOK_only(bufsv);
1644 /* This should not be marked tainted if the fp is marked clean */
1645 if (!(IoFLAGS(io) & IOf_UNTAINT))
1646 SvTAINTED_on(bufsv);
1648 sv_setpvn(TARG, namebuf, bufsize);
1653 if (PL_op->op_type == OP_RECV)
1654 DIE(aTHX_ PL_no_sock_func, "recv");
1656 if (DO_UTF8(bufsv)) {
1657 /* offset adjust in characters not bytes */
1658 blen = sv_len_utf8(bufsv);
1661 if (-offset > (int)blen)
1662 DIE(aTHX_ "Offset outside string");
1665 if (DO_UTF8(bufsv)) {
1666 /* convert offset-as-chars to offset-as-bytes */
1667 if (offset >= (int)blen)
1668 offset += SvCUR(bufsv) - blen;
1670 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1673 bufsize = SvCUR(bufsv);
1674 /* Allocating length + offset + 1 isn't perfect in the case of reading
1675 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1677 (should be 2 * length + offset + 1, or possibly something longer if
1678 PL_encoding is true) */
1679 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1680 if (offset > bufsize) { /* Zero any newly allocated space */
1681 Zero(buffer+bufsize, offset-bufsize, char);
1683 buffer = buffer + offset;
1685 read_target = bufsv;
1687 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1688 concatenate it to the current buffer. */
1690 /* Truncate the existing buffer to the start of where we will be
1692 SvCUR_set(bufsv, offset);
1694 read_target = sv_newmortal();
1695 SvUPGRADE(read_target, SVt_PV);
1696 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1699 if (PL_op->op_type == OP_SYSREAD) {
1700 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1701 if (IoTYPE(io) == IoTYPE_SOCKET) {
1702 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1708 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1713 #ifdef HAS_SOCKET__bad_code_maybe
1714 if (IoTYPE(io) == IoTYPE_SOCKET) {
1715 char namebuf[MAXPATHLEN];
1716 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1717 bufsize = sizeof (struct sockaddr_in);
1719 bufsize = sizeof namebuf;
1721 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1722 (struct sockaddr *)namebuf, &bufsize);
1727 count = PerlIO_read(IoIFP(io), buffer, length);
1728 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1729 if (count == 0 && PerlIO_error(IoIFP(io)))
1733 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1734 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1737 SvCUR_set(read_target, count+(buffer - SvPVX(read_target)));
1738 *SvEND(read_target) = '\0';
1739 (void)SvPOK_only(read_target);
1740 if (fp_utf8 && !IN_BYTES) {
1741 /* Look at utf8 we got back and count the characters */
1742 char *bend = buffer + count;
1743 while (buffer < bend) {
1745 skip = UTF8SKIP(buffer);
1748 if (buffer - charskip + skip > bend) {
1749 /* partial character - try for rest of it */
1750 length = skip - (bend-buffer);
1751 offset = bend - SvPVX(bufsv);
1763 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1764 provided amount read (count) was what was requested (length)
1766 if (got < wanted && count == length) {
1767 length = wanted - got;
1768 offset = bend - SvPVX(bufsv);
1771 /* return value is character count */
1775 else if (buffer_utf8) {
1776 /* Let svcatsv upgrade the bytes we read in to utf8.
1777 The buffer is a mortal so will be freed soon. */
1778 sv_catsv_nomg(bufsv, read_target);
1781 /* This should not be marked tainted if the fp is marked clean */
1782 if (!(IoFLAGS(io) & IOf_UNTAINT))
1783 SvTAINTED_on(bufsv);
1796 int items = (SP - PL_stack_base) - TOPMARK;
1800 sv = sv_2mortal(newSViv(sv_len(*SP)));
1809 dSP; dMARK; dORIGMARK; dTARGET;
1820 if (PL_op->op_type == OP_SYSWRITE
1821 && gv && (io = GvIO(gv))
1822 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1827 *MARK = SvTIED_obj((SV*)io, mg);
1829 call_method("WRITE", G_SCALAR);
1840 #if Size_t_size > IVSIZE
1841 length = (Size_t)SvNVx(*++MARK);
1843 length = (Size_t)SvIVx(*++MARK);
1845 if ((SSize_t)length < 0)
1846 DIE(aTHX_ "Negative length");
1849 if (!io || !IoIFP(io)) {
1851 if (ckWARN(WARN_CLOSED))
1852 report_evil_fh(gv, io, PL_op->op_type);
1853 SETERRNO(EBADF,RMS_IFI);
1857 if (PerlIO_isutf8(IoIFP(io))) {
1858 if (!SvUTF8(bufsv)) {
1859 bufsv = sv_2mortal(newSVsv(bufsv));
1860 buffer = sv_2pvutf8(bufsv, &blen);
1862 buffer = SvPV(bufsv, blen);
1865 if (DO_UTF8(bufsv)) {
1866 /* Not modifying source SV, so making a temporary copy. */
1867 bufsv = sv_2mortal(newSVsv(bufsv));
1868 sv_utf8_downgrade(bufsv, FALSE);
1870 buffer = SvPV(bufsv, blen);
1873 if (PL_op->op_type == OP_SYSWRITE) {
1875 if (DO_UTF8(bufsv)) {
1876 /* length and offset are in chars */
1877 blen = sv_len_utf8(bufsv);
1880 offset = SvIVx(*++MARK);
1882 if (-offset > (IV)blen)
1883 DIE(aTHX_ "Offset outside string");
1885 } else if (offset >= (IV)blen && blen > 0)
1886 DIE(aTHX_ "Offset outside string");
1889 if (length > blen - offset)
1890 length = blen - offset;
1891 if (DO_UTF8(bufsv)) {
1892 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1893 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1896 buffer = buffer+offset;
1898 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1899 if (IoTYPE(io) == IoTYPE_SOCKET) {
1900 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1906 /* See the note at doio.c:do_print about filesize limits. --jhi */
1907 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1912 else if (SP > MARK) {
1915 sockbuf = SvPVx(*++MARK, mlen);
1916 /* length is really flags */
1917 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1918 length, (struct sockaddr *)sockbuf, mlen);
1921 /* length is really flags */
1922 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1925 DIE(aTHX_ PL_no_sock_func, "send");
1931 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1932 #if Size_t_size > IVSIZE
1946 return pp_sysread();
1957 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1959 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1961 if (io && !IoIFP(io)) {
1962 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1964 IoFLAGS(io) &= ~IOf_START;
1965 do_open(gv, (char *)"-", 1, FALSE, O_RDONLY, 0, Nullfp);
1966 sv_setpvn(GvSV(gv), "-", 1);
1967 SvSETMAGIC(GvSV(gv));
1969 else if (!nextargv(gv))
1974 gv = PL_last_in_gv; /* eof */
1977 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1979 if (gv && (io = GvIO(gv))
1980 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1983 XPUSHs(SvTIED_obj((SV*)io, mg));
1986 call_method("EOF", G_SCALAR);
1992 PUSHs(boolSV(!gv || do_eof(gv)));
2006 gv = PL_last_in_gv = (GV*)POPs;
2008 if (gv && (io = GvIO(gv))
2009 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2012 XPUSHs(SvTIED_obj((SV*)io, mg));
2015 call_method("TELL", G_SCALAR);
2021 #if LSEEKSIZE > IVSIZE
2022 PUSHn( do_tell(gv) );
2024 PUSHi( do_tell(gv) );
2031 return pp_sysseek();
2040 #if LSEEKSIZE > IVSIZE
2041 Off_t offset = (Off_t)SvNVx(POPs);
2043 Off_t offset = (Off_t)SvIVx(POPs);
2047 gv = PL_last_in_gv = (GV*)POPs;
2049 if (gv && (io = GvIO(gv))
2050 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2053 XPUSHs(SvTIED_obj((SV*)io, mg));
2054 #if LSEEKSIZE > IVSIZE
2055 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2057 XPUSHs(sv_2mortal(newSViv(offset)));
2059 XPUSHs(sv_2mortal(newSViv(whence)));
2062 call_method("SEEK", G_SCALAR);
2068 if (PL_op->op_type == OP_SEEK)
2069 PUSHs(boolSV(do_seek(gv, offset, whence)));
2071 Off_t sought = do_sysseek(gv, offset, whence);
2073 PUSHs(&PL_sv_undef);
2076 #if LSEEKSIZE > IVSIZE
2081 : newSVpvn(zero_but_true, ZBTLEN);
2082 PUSHs(sv_2mortal(sv));
2091 /* There seems to be no consensus on the length type of truncate()
2092 * and ftruncate(), both off_t and size_t have supporters. In
2093 * general one would think that when using large files, off_t is
2094 * at least as wide as size_t, so using an off_t should be okay. */
2095 /* XXX Configure probe for the length type of *truncate() needed XXX */
2098 #if Off_t_size > IVSIZE
2103 /* Checking for length < 0 is problematic as the type might or
2104 * might not be signed: if it is not, clever compilers will moan. */
2105 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2112 if (PL_op->op_flags & OPf_SPECIAL) {
2113 tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
2122 TAINT_PROPER("truncate");
2123 if (!(fp = IoIFP(io))) {
2129 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2131 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2142 if (SvTYPE(sv) == SVt_PVGV) {
2143 tmpgv = (GV*)sv; /* *main::FRED for example */
2144 goto do_ftruncate_gv;
2146 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2147 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2148 goto do_ftruncate_gv;
2150 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2151 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2152 goto do_ftruncate_io;
2155 name = SvPV(sv, n_a);
2156 TAINT_PROPER("truncate");
2158 if (truncate(name, len) < 0)
2164 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2167 if (my_chsize(tmpfd, len) < 0)
2169 PerlLIO_close(tmpfd);
2178 SETERRNO(EBADF,RMS_IFI);
2192 unsigned int func = POPu;
2193 const int optype = PL_op->op_type;
2197 IO *io = gv ? GvIOn(gv) : 0;
2199 if (!io || !argsv || !IoIFP(io)) {
2200 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2201 report_evil_fh(gv, io, PL_op->op_type);
2202 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2206 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2209 s = SvPV_force(argsv, len);
2210 need = IOCPARM_LEN(func);
2212 s = Sv_Grow(argsv, need + 1);
2213 SvCUR_set(argsv, need);
2216 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2219 retval = SvIV(argsv);
2220 s = INT2PTR(char*,retval); /* ouch */
2223 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2225 if (optype == OP_IOCTL)
2227 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2229 DIE(aTHX_ "ioctl is not implemented");
2233 DIE(aTHX_ "fcntl is not implemented");
2235 #if defined(OS2) && defined(__EMX__)
2236 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2238 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2242 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2244 if (s[SvCUR(argsv)] != 17)
2245 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2247 s[SvCUR(argsv)] = 0; /* put our null back */
2248 SvSETMAGIC(argsv); /* Assume it has changed */
2257 PUSHp(zero_but_true, ZBTLEN);
2278 if (gv && (io = GvIO(gv)))
2285 (void)PerlIO_flush(fp);
2286 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2289 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2290 report_evil_fh(gv, io, PL_op->op_type);
2292 SETERRNO(EBADF,RMS_IFI);
2297 DIE(aTHX_ PL_no_func, "flock()");
2309 int protocol = POPi;
2315 io = gv ? GvIOn(gv) : NULL;
2318 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2319 report_evil_fh(gv, io, PL_op->op_type);
2321 do_close(gv, FALSE);
2322 SETERRNO(EBADF,LIB_INVARG);
2327 do_close(gv, FALSE);
2329 TAINT_PROPER("socket");
2330 fd = PerlSock_socket(domain, type, protocol);
2333 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2334 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2335 IoTYPE(io) = IoTYPE_SOCKET;
2336 if (!IoIFP(io) || !IoOFP(io)) {
2337 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2338 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2339 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2342 #if defined(HAS_FCNTL) && defined(F_SETFD)
2343 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2347 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2352 DIE(aTHX_ PL_no_sock_func, "socket");
2358 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2364 int protocol = POPi;
2371 io1 = gv1 ? GvIOn(gv1) : NULL;
2372 io2 = gv2 ? GvIOn(gv2) : NULL;
2373 if (!gv1 || !gv2 || !io1 || !io2) {
2374 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2376 report_evil_fh(gv1, io1, PL_op->op_type);
2378 report_evil_fh(gv1, io2, PL_op->op_type);
2381 do_close(gv1, FALSE);
2383 do_close(gv2, FALSE);
2388 do_close(gv1, FALSE);
2390 do_close(gv2, FALSE);
2392 TAINT_PROPER("socketpair");
2393 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2395 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2396 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2397 IoTYPE(io1) = IoTYPE_SOCKET;
2398 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2399 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2400 IoTYPE(io2) = IoTYPE_SOCKET;
2401 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2402 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2403 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2404 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2405 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2406 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2407 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2410 #if defined(HAS_FCNTL) && defined(F_SETFD)
2411 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2412 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2417 DIE(aTHX_ PL_no_sock_func, "socketpair");
2425 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2426 extern void GETPRIVMODE();
2427 extern void GETUSERMODE();
2432 register IO *io = GvIOn(gv);
2436 int mpeprivmode = 0;
2439 if (!io || !IoIFP(io))
2442 addr = SvPV(addrsv, len);
2443 TAINT_PROPER("bind");
2444 #ifdef MPE /* Deal with MPE bind() peculiarities */
2445 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2446 /* The address *MUST* stupidly be zero. */
2447 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2448 /* PRIV mode is required to bind() to ports < 1024. */
2449 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2450 ((struct sockaddr_in *)addr)->sin_port > 0) {
2451 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2456 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2457 (struct sockaddr *)addr, len) >= 0)
2460 #ifdef MPE /* Switch back to USER mode */
2471 if (ckWARN(WARN_CLOSED))
2472 report_evil_fh(gv, io, PL_op->op_type);
2473 SETERRNO(EBADF,SS_IVCHAN);
2476 DIE(aTHX_ PL_no_sock_func, "bind");
2487 register IO *io = GvIOn(gv);
2490 if (!io || !IoIFP(io))
2493 addr = SvPV(addrsv, len);
2494 TAINT_PROPER("connect");
2495 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2501 if (ckWARN(WARN_CLOSED))
2502 report_evil_fh(gv, io, PL_op->op_type);
2503 SETERRNO(EBADF,SS_IVCHAN);
2506 DIE(aTHX_ PL_no_sock_func, "connect");
2516 register IO *io = gv ? GvIOn(gv) : NULL;
2518 if (!gv || !io || !IoIFP(io))
2521 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2527 if (ckWARN(WARN_CLOSED))
2528 report_evil_fh(gv, io, PL_op->op_type);
2529 SETERRNO(EBADF,SS_IVCHAN);
2532 DIE(aTHX_ PL_no_sock_func, "listen");
2544 char namebuf[MAXPATHLEN];
2545 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2546 Sock_size_t len = sizeof (struct sockaddr_in);
2548 Sock_size_t len = sizeof namebuf;
2561 if (!gstio || !IoIFP(gstio))
2565 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2569 do_close(ngv, FALSE);
2570 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2571 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2572 IoTYPE(nstio) = IoTYPE_SOCKET;
2573 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2574 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2575 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2576 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2579 #if defined(HAS_FCNTL) && defined(F_SETFD)
2580 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2584 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2585 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2587 #ifdef __SCO_VERSION__
2588 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2591 PUSHp(namebuf, len);
2595 if (ckWARN(WARN_CLOSED))
2596 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2597 SETERRNO(EBADF,SS_IVCHAN);
2603 DIE(aTHX_ PL_no_sock_func, "accept");
2613 register IO *io = GvIOn(gv);
2615 if (!io || !IoIFP(io))
2618 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2622 if (ckWARN(WARN_CLOSED))
2623 report_evil_fh(gv, io, PL_op->op_type);
2624 SETERRNO(EBADF,SS_IVCHAN);
2627 DIE(aTHX_ PL_no_sock_func, "shutdown");
2634 return pp_ssockopt();
2636 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2644 int optype = PL_op->op_type;
2647 unsigned int optname;
2653 if (optype == OP_GSOCKOPT)
2654 sv = sv_2mortal(NEWSV(22, 257));
2657 optname = (unsigned int) POPi;
2658 lvl = (unsigned int) POPi;
2662 if (!io || !IoIFP(io))
2665 fd = PerlIO_fileno(IoIFP(io));
2669 (void)SvPOK_only(sv);
2673 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2688 aint = (int)SvIV(sv);
2692 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2701 if (ckWARN(WARN_CLOSED))
2702 report_evil_fh(gv, io, optype);
2703 SETERRNO(EBADF,SS_IVCHAN);
2708 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2715 return pp_getpeername();
2717 DIE(aTHX_ PL_no_sock_func, "getsockname");
2725 int optype = PL_op->op_type;
2729 register IO *io = GvIOn(gv);
2732 if (!io || !IoIFP(io))
2735 sv = sv_2mortal(NEWSV(22, 257));
2736 (void)SvPOK_only(sv);
2740 fd = PerlIO_fileno(IoIFP(io));
2742 case OP_GETSOCKNAME:
2743 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2746 case OP_GETPEERNAME:
2747 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2749 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2751 static const char nowhere[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
2752 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2753 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2754 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2755 sizeof(u_short) + sizeof(struct in_addr))) {
2762 #ifdef BOGUS_GETNAME_RETURN
2763 /* Interactive Unix, getpeername() and getsockname()
2764 does not return valid namelen */
2765 if (len == BOGUS_GETNAME_RETURN)
2766 len = sizeof(struct sockaddr);
2774 if (ckWARN(WARN_CLOSED))
2775 report_evil_fh(gv, io, optype);
2776 SETERRNO(EBADF,SS_IVCHAN);
2781 DIE(aTHX_ PL_no_sock_func, "getpeername");
2800 if (PL_op->op_flags & OPf_REF) {
2802 if (PL_op->op_type == OP_LSTAT) {
2803 if (gv != PL_defgv) {
2804 if (ckWARN(WARN_IO))
2805 Perl_warner(aTHX_ packWARN(WARN_IO),
2806 "lstat() on filehandle %s", GvENAME(gv));
2807 } else if (PL_laststype != OP_LSTAT)
2808 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2812 if (gv != PL_defgv) {
2813 PL_laststype = OP_STAT;
2815 sv_setpv(PL_statname, "");
2816 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2817 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2819 if (PL_laststatval < 0) {
2820 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2821 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2827 if (SvTYPE(sv) == SVt_PVGV) {
2831 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2833 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2834 Perl_warner(aTHX_ packWARN(WARN_IO),
2835 "lstat() on filehandle %s", GvENAME(gv));
2838 sv_setpv(PL_statname, SvPV(sv,n_a));
2840 PL_laststype = PL_op->op_type;
2841 if (PL_op->op_type == OP_LSTAT)
2842 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2844 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2845 if (PL_laststatval < 0) {
2846 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2847 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2853 if (gimme != G_ARRAY) {
2854 if (gimme != G_VOID)
2855 XPUSHs(boolSV(max));
2861 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2862 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2863 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2864 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2865 #if Uid_t_size > IVSIZE
2866 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2868 # if Uid_t_sign <= 0
2869 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2871 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2874 #if Gid_t_size > IVSIZE
2875 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2877 # if Gid_t_sign <= 0
2878 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2880 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2883 #ifdef USE_STAT_RDEV
2884 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2886 PUSHs(sv_2mortal(newSVpvn("", 0)));
2888 #if Off_t_size > IVSIZE
2889 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2891 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2894 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2895 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2896 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2898 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2899 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2900 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2902 #ifdef USE_STAT_BLOCKS
2903 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2904 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2906 PUSHs(sv_2mortal(newSVpvn("", 0)));
2907 PUSHs(sv_2mortal(newSVpvn("", 0)));
2913 /* This macro is used by the stacked filetest operators :
2914 * if the previous filetest failed, short-circuit and pass its value.
2915 * Else, discard it from the stack and continue. --rgs
2917 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2918 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2919 else { (void)POPs; PUTBACK; } \
2926 STACKED_FTEST_CHECK;
2927 #if defined(HAS_ACCESS) && defined(R_OK)
2928 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2930 result = access(POPpx, R_OK);
2945 if (cando(S_IRUSR, 0, &PL_statcache))
2954 STACKED_FTEST_CHECK;
2955 #if defined(HAS_ACCESS) && defined(W_OK)
2956 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2958 result = access(POPpx, W_OK);
2973 if (cando(S_IWUSR, 0, &PL_statcache))
2982 STACKED_FTEST_CHECK;
2983 #if defined(HAS_ACCESS) && defined(X_OK)
2984 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2986 result = access(POPpx, X_OK);
3001 if (cando(S_IXUSR, 0, &PL_statcache))
3010 STACKED_FTEST_CHECK;
3011 #ifdef PERL_EFF_ACCESS_R_OK
3012 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3014 result = PERL_EFF_ACCESS_R_OK(POPpx);
3029 if (cando(S_IRUSR, 1, &PL_statcache))
3038 STACKED_FTEST_CHECK;
3039 #ifdef PERL_EFF_ACCESS_W_OK
3040 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3042 result = PERL_EFF_ACCESS_W_OK(POPpx);
3057 if (cando(S_IWUSR, 1, &PL_statcache))
3066 STACKED_FTEST_CHECK;
3067 #ifdef PERL_EFF_ACCESS_X_OK
3068 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3070 result = PERL_EFF_ACCESS_X_OK(POPpx);
3085 if (cando(S_IXUSR, 1, &PL_statcache))
3094 STACKED_FTEST_CHECK;
3104 return pp_ftrowned();
3111 STACKED_FTEST_CHECK;
3116 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3126 STACKED_FTEST_CHECK;
3131 if (PL_statcache.st_size == 0)
3140 STACKED_FTEST_CHECK;
3145 #if Off_t_size > IVSIZE
3146 PUSHn(PL_statcache.st_size);
3148 PUSHi(PL_statcache.st_size);
3157 STACKED_FTEST_CHECK;
3162 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3170 STACKED_FTEST_CHECK;
3175 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3183 STACKED_FTEST_CHECK;
3188 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3196 STACKED_FTEST_CHECK;
3201 if (S_ISSOCK(PL_statcache.st_mode))
3210 STACKED_FTEST_CHECK;
3215 if (S_ISCHR(PL_statcache.st_mode))
3224 STACKED_FTEST_CHECK;
3229 if (S_ISBLK(PL_statcache.st_mode))
3238 STACKED_FTEST_CHECK;
3243 if (S_ISREG(PL_statcache.st_mode))
3252 STACKED_FTEST_CHECK;
3257 if (S_ISDIR(PL_statcache.st_mode))
3266 STACKED_FTEST_CHECK;
3271 if (S_ISFIFO(PL_statcache.st_mode))
3278 I32 result = my_lstat();
3282 if (S_ISLNK(PL_statcache.st_mode))
3292 STACKED_FTEST_CHECK;
3297 if (PL_statcache.st_mode & S_ISUID)
3308 STACKED_FTEST_CHECK;
3313 if (PL_statcache.st_mode & S_ISGID)
3324 STACKED_FTEST_CHECK;
3329 if (PL_statcache.st_mode & S_ISVTX)
3342 STACKED_FTEST_CHECK;
3344 if (PL_op->op_flags & OPf_REF)
3346 else if (isGV(TOPs))
3348 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3349 gv = (GV*)SvRV(POPs);
3351 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
3353 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3354 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3355 else if (tmpsv && SvOK(tmpsv)) {
3357 char *tmps = SvPV(tmpsv, n_a);
3365 if (PerlLIO_isatty(fd))
3370 #if defined(atarist) /* this will work with atariST. Configure will
3371 make guesses for other systems. */
3372 # define FILE_base(f) ((f)->_base)
3373 # define FILE_ptr(f) ((f)->_ptr)
3374 # define FILE_cnt(f) ((f)->_cnt)
3375 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3385 register STDCHAR *s;
3392 STACKED_FTEST_CHECK;
3394 if (PL_op->op_flags & OPf_REF)
3396 else if (isGV(TOPs))
3398 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3399 gv = (GV*)SvRV(POPs);
3405 if (gv == PL_defgv) {
3407 io = GvIO(PL_statgv);
3410 goto really_filename;
3415 PL_laststatval = -1;
3416 sv_setpv(PL_statname, "");
3417 io = GvIO(PL_statgv);
3419 if (io && IoIFP(io)) {
3420 if (! PerlIO_has_base(IoIFP(io)))
3421 DIE(aTHX_ "-T and -B not implemented on filehandles");
3422 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3423 if (PL_laststatval < 0)
3425 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3426 if (PL_op->op_type == OP_FTTEXT)
3431 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3432 i = PerlIO_getc(IoIFP(io));
3434 (void)PerlIO_ungetc(IoIFP(io),i);
3436 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3438 len = PerlIO_get_bufsiz(IoIFP(io));
3439 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3440 /* sfio can have large buffers - limit to 512 */
3445 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3447 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3449 SETERRNO(EBADF,RMS_IFI);
3457 PL_laststype = OP_STAT;
3458 sv_setpv(PL_statname, SvPV(sv, n_a));
3459 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3460 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
3461 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3464 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3465 if (PL_laststatval < 0) {
3466 (void)PerlIO_close(fp);
3469 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3470 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3471 (void)PerlIO_close(fp);
3473 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3474 RETPUSHNO; /* special case NFS directories */
3475 RETPUSHYES; /* null file is anything */
3480 /* now scan s to look for textiness */
3481 /* XXX ASCII dependent code */
3483 #if defined(DOSISH) || defined(USEMYBINMODE)
3484 /* ignore trailing ^Z on short files */
3485 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3489 for (i = 0; i < len; i++, s++) {
3490 if (!*s) { /* null never allowed in text */
3495 else if (!(isPRINT(*s) || isSPACE(*s)))
3498 else if (*s & 128) {
3500 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3503 /* utf8 characters don't count as odd */
3504 if (UTF8_IS_START(*s)) {
3505 int ulen = UTF8SKIP(s);
3506 if (ulen < len - i) {
3508 for (j = 1; j < ulen; j++) {
3509 if (!UTF8_IS_CONTINUATION(s[j]))
3512 --ulen; /* loop does extra increment */
3522 *s != '\n' && *s != '\r' && *s != '\b' &&
3523 *s != '\t' && *s != '\f' && *s != 27)
3528 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3553 if( !tmps || !*tmps ) {
3554 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3555 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3557 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3562 deprecate("chdir('') or chdir(undef) as chdir()");
3563 tmps = SvPV(*svp, n_a);
3567 TAINT_PROPER("chdir");
3572 TAINT_PROPER("chdir");
3573 PUSHi( PerlDir_chdir(tmps) >= 0 );
3575 /* Clear the DEFAULT element of ENV so we'll get the new value
3577 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3585 dSP; dMARK; dTARGET;
3586 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3592 DIE(aTHX_ PL_no_func, "chown");
3602 TAINT_PROPER("chroot");
3603 PUSHi( chroot(tmps) >= 0 );
3606 DIE(aTHX_ PL_no_func, "chroot");
3612 dSP; dMARK; dTARGET;
3614 value = (I32)apply(PL_op->op_type, MARK, SP);
3622 dSP; dMARK; dTARGET;
3624 value = (I32)apply(PL_op->op_type, MARK, SP);
3632 dSP; dMARK; dTARGET;
3634 value = (I32)apply(PL_op->op_type, MARK, SP);
3646 char *tmps2 = POPpx;
3647 char *tmps = SvPV(TOPs, n_a);
3648 TAINT_PROPER("rename");
3650 anum = PerlLIO_rename(tmps, tmps2);
3652 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3653 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3656 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3657 (void)UNLINK(tmps2);
3658 if (!(anum = link(tmps, tmps2)))
3659 anum = UNLINK(tmps);
3672 char *tmps2 = POPpx;
3673 char *tmps = SvPV(TOPs, n_a);
3674 TAINT_PROPER("link");
3675 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3678 DIE(aTHX_ PL_no_func, "link");
3687 char *tmps2 = POPpx;
3688 char *tmps = SvPV(TOPs, n_a);
3689 TAINT_PROPER("symlink");
3690 SETi( symlink(tmps, tmps2) >= 0 );
3693 DIE(aTHX_ PL_no_func, "symlink");
3703 char buf[MAXPATHLEN];
3707 #ifndef INCOMPLETE_TAINTS
3711 len = readlink(tmps, buf, sizeof(buf) - 1);
3719 RETSETUNDEF; /* just pretend it's a normal file */
3723 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3725 S_dooneliner(pTHX_ char *cmd, char *filename)
3727 char *save_filename = filename;
3733 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3734 strcpy(cmdline, cmd);
3735 strcat(cmdline, " ");
3736 for (s = cmdline + strlen(cmdline); *filename; ) {
3741 myfp = PerlProc_popen(cmdline, "r");
3745 SV *tmpsv = sv_newmortal();
3746 /* Need to save/restore 'PL_rs' ?? */
3747 s = sv_gets(tmpsv, myfp, 0);
3748 (void)PerlProc_pclose(myfp);
3752 #ifdef HAS_SYS_ERRLIST
3757 /* you don't see this */
3759 #ifdef HAS_SYS_ERRLIST
3767 if (instr(s, errmsg)) {
3774 #define EACCES EPERM
3776 if (instr(s, "cannot make"))
3777 SETERRNO(EEXIST,RMS_FEX);
3778 else if (instr(s, "existing file"))
3779 SETERRNO(EEXIST,RMS_FEX);
3780 else if (instr(s, "ile exists"))
3781 SETERRNO(EEXIST,RMS_FEX);
3782 else if (instr(s, "non-exist"))
3783 SETERRNO(ENOENT,RMS_FNF);
3784 else if (instr(s, "does not exist"))
3785 SETERRNO(ENOENT,RMS_FNF);
3786 else if (instr(s, "not empty"))
3787 SETERRNO(EBUSY,SS_DEVOFFLINE);
3788 else if (instr(s, "cannot access"))
3789 SETERRNO(EACCES,RMS_PRV);
3791 SETERRNO(EPERM,RMS_PRV);
3794 else { /* some mkdirs return no failure indication */
3795 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3796 if (PL_op->op_type == OP_RMDIR)
3801 SETERRNO(EACCES,RMS_PRV); /* a guess */
3810 /* This macro removes trailing slashes from a directory name.
3811 * Different operating and file systems take differently to
3812 * trailing slashes. According to POSIX 1003.1 1996 Edition
3813 * any number of trailing slashes should be allowed.
3814 * Thusly we snip them away so that even non-conforming
3815 * systems are happy.
3816 * We should probably do this "filtering" for all
3817 * the functions that expect (potentially) directory names:
3818 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3819 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3821 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3822 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3825 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3826 (tmps) = savepvn((tmps), (len)); \
3846 TRIMSLASHES(tmps,len,copy);
3848 TAINT_PROPER("mkdir");
3850 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3852 SETi( dooneliner("mkdir", tmps) );
3853 oldumask = PerlLIO_umask(0);
3854 PerlLIO_umask(oldumask);
3855 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3869 TRIMSLASHES(tmps,len,copy);
3870 TAINT_PROPER("rmdir");
3872 SETi( PerlDir_rmdir(tmps) >= 0 );
3874 SETi( dooneliner("rmdir", tmps) );
3881 /* Directory calls. */
3885 #if defined(Direntry_t) && defined(HAS_READDIR)
3888 char *dirname = POPpx;
3890 register IO *io = GvIOn(gv);
3896 PerlDir_close(IoDIRP(io));
3897 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3903 SETERRNO(EBADF,RMS_DIR);
3906 DIE(aTHX_ PL_no_dir_func, "opendir");
3912 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3913 DIE(aTHX_ PL_no_dir_func, "readdir");
3915 #if !defined(I_DIRENT) && !defined(VMS)
3916 Direntry_t *readdir (DIR *);
3922 GV *gv = (GV *)POPs;
3923 register Direntry_t *dp;
3924 register IO *io = GvIOn(gv);
3926 if (!io || !IoDIRP(io))
3930 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3934 sv = newSVpvn(dp->d_name, dp->d_namlen);
3936 sv = newSVpv(dp->d_name, 0);
3938 #ifndef INCOMPLETE_TAINTS
3939 if (!(IoFLAGS(io) & IOf_UNTAINT))
3942 XPUSHs(sv_2mortal(sv));
3944 while (gimme == G_ARRAY);
3946 if (!dp && gimme != G_ARRAY)
3953 SETERRNO(EBADF,RMS_ISI);
3954 if (GIMME == G_ARRAY)
3963 #if defined(HAS_TELLDIR) || defined(telldir)
3965 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3966 /* XXX netbsd still seemed to.
3967 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3968 --JHI 1999-Feb-02 */
3969 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3970 long telldir (DIR *);
3973 register IO *io = GvIOn(gv);
3975 if (!io || !IoDIRP(io))
3978 PUSHi( PerlDir_tell(IoDIRP(io)) );
3982 SETERRNO(EBADF,RMS_ISI);
3985 DIE(aTHX_ PL_no_dir_func, "telldir");
3991 #if defined(HAS_SEEKDIR) || defined(seekdir)
3995 register IO *io = GvIOn(gv);
3997 if (!io || !IoDIRP(io))
4000 (void)PerlDir_seek(IoDIRP(io), along);
4005 SETERRNO(EBADF,RMS_ISI);
4008 DIE(aTHX_ PL_no_dir_func, "seekdir");
4014 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4017 register IO *io = GvIOn(gv);
4019 if (!io || !IoDIRP(io))
4022 (void)PerlDir_rewind(IoDIRP(io));
4026 SETERRNO(EBADF,RMS_ISI);
4029 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4035 #if defined(Direntry_t) && defined(HAS_READDIR)
4038 register IO *io = GvIOn(gv);
4040 if (!io || !IoDIRP(io))
4043 #ifdef VOID_CLOSEDIR
4044 PerlDir_close(IoDIRP(io));
4046 if (PerlDir_close(IoDIRP(io)) < 0) {
4047 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4056 SETERRNO(EBADF,RMS_IFI);
4059 DIE(aTHX_ PL_no_dir_func, "closedir");
4063 /* Process control. */
4073 PERL_FLUSHALL_FOR_CHILD;
4074 childpid = PerlProc_fork();
4079 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
4080 SvREADONLY_off(GvSV(tmpgv));
4081 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4082 SvREADONLY_on(GvSV(tmpgv));
4084 #ifdef THREADS_HAVE_PIDS
4085 PL_ppid = (IV)getppid();
4087 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4092 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4097 PERL_FLUSHALL_FOR_CHILD;
4098 childpid = PerlProc_fork();
4104 DIE(aTHX_ PL_no_func, "fork");
4111 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4116 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4117 childpid = wait4pid(-1, &argflags, 0);
4119 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4124 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4125 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4126 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4128 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4133 DIE(aTHX_ PL_no_func, "wait");
4139 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4148 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4149 result = wait4pid(pid, &argflags, optype);
4151 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4156 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4157 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4158 STATUS_NATIVE_SET((result && result != -1) ? argflags : -1);
4160 STATUS_NATIVE_SET((result > 0) ? argflags : -1);
4165 DIE(aTHX_ PL_no_func, "waitpid");
4171 dSP; dMARK; dORIGMARK; dTARGET;
4179 while (++MARK <= SP) {
4180 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4185 TAINT_PROPER("system");
4187 PERL_FLUSHALL_FOR_CHILD;
4188 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4193 if (PerlProc_pipe(pp) >= 0)
4195 while ((childpid = PerlProc_fork()) == -1) {
4196 if (errno != EAGAIN) {
4201 PerlLIO_close(pp[0]);
4202 PerlLIO_close(pp[1]);
4209 Sigsave_t ihand,qhand; /* place to save signals during system() */
4213 PerlLIO_close(pp[1]);
4215 rsignal_save(SIGINT, SIG_IGN, &ihand);
4216 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4219 result = wait4pid(childpid, &status, 0);
4220 } while (result == -1 && errno == EINTR);
4222 (void)rsignal_restore(SIGINT, &ihand);
4223 (void)rsignal_restore(SIGQUIT, &qhand);
4225 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4226 do_execfree(); /* free any memory child malloced on fork */
4232 while (n < sizeof(int)) {
4233 n1 = PerlLIO_read(pp[0],
4234 (void*)(((char*)&errkid)+n),
4240 PerlLIO_close(pp[0]);
4241 if (n) { /* Error */
4242 if (n != sizeof(int))
4243 DIE(aTHX_ "panic: kid popen errno read");
4244 errno = errkid; /* Propagate errno from kid */
4245 STATUS_CURRENT = -1;
4248 PUSHi(STATUS_CURRENT);
4252 PerlLIO_close(pp[0]);
4253 #if defined(HAS_FCNTL) && defined(F_SETFD)
4254 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4257 if (PL_op->op_flags & OPf_STACKED) {
4258 SV *really = *++MARK;
4259 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4261 else if (SP - MARK != 1)
4262 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4264 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4268 #else /* ! FORK or VMS or OS/2 */
4271 if (PL_op->op_flags & OPf_STACKED) {
4272 SV *really = *++MARK;
4273 # if defined(WIN32) || defined(OS2)
4274 value = (I32)do_aspawn(really, MARK, SP);
4276 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4279 else if (SP - MARK != 1) {
4280 # if defined(WIN32) || defined(OS2)
4281 value = (I32)do_aspawn(Nullsv, MARK, SP);
4283 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4287 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4289 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4291 STATUS_NATIVE_SET(value);
4294 PUSHi(result ? value : STATUS_CURRENT);
4295 #endif /* !FORK or VMS */
4301 dSP; dMARK; dORIGMARK; dTARGET;
4307 while (++MARK <= SP) {
4308 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4313 TAINT_PROPER("exec");
4315 PERL_FLUSHALL_FOR_CHILD;
4316 if (PL_op->op_flags & OPf_STACKED) {
4317 SV *really = *++MARK;
4318 value = (I32)do_aexec(really, MARK, SP);
4320 else if (SP - MARK != 1)
4322 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4326 (void ) do_aspawn(Nullsv, MARK, SP);
4330 value = (I32)do_aexec(Nullsv, MARK, SP);
4335 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4338 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4341 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4354 dSP; dMARK; dTARGET;
4356 value = (I32)apply(PL_op->op_type, MARK, SP);
4361 DIE(aTHX_ PL_no_func, "kill");
4369 # ifdef THREADS_HAVE_PIDS
4372 XPUSHi( getppid() );
4376 DIE(aTHX_ PL_no_func, "getppid");
4392 pgrp = (I32)BSD_GETPGRP(pid);
4394 if (pid != 0 && pid != PerlProc_getpid())
4395 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4401 DIE(aTHX_ PL_no_func, "getpgrp()");
4420 TAINT_PROPER("setpgrp");
4422 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4424 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4425 || (pid != 0 && pid != PerlProc_getpid()))
4427 DIE(aTHX_ "setpgrp can't take arguments");
4429 SETi( setpgrp() >= 0 );
4430 #endif /* USE_BSDPGRP */
4433 DIE(aTHX_ PL_no_func, "setpgrp()");
4439 #ifdef HAS_GETPRIORITY
4443 SETi( getpriority(which, who) );
4446 DIE(aTHX_ PL_no_func, "getpriority()");
4452 #ifdef HAS_SETPRIORITY
4457 TAINT_PROPER("setpriority");
4458 SETi( setpriority(which, who, niceval) >= 0 );
4461 DIE(aTHX_ PL_no_func, "setpriority()");
4471 XPUSHn( time(Null(Time_t*)) );
4473 XPUSHi( time(Null(Time_t*)) );
4484 (void)PerlProc_times(&PL_timesbuf);
4486 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4487 /* struct tms, though same data */
4491 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4492 if (GIMME == G_ARRAY) {
4493 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4494 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4495 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4501 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4503 if (GIMME == G_ARRAY) {
4504 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4505 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4506 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4510 DIE(aTHX_ "times not implemented");
4512 #endif /* HAS_TIMES */
4524 const struct tm *tmbuf;
4525 static const char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4526 static const char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4527 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4533 when = (Time_t)SvNVx(POPs);
4535 when = (Time_t)SvIVx(POPs);
4538 if (PL_op->op_type == OP_LOCALTIME)
4539 tmbuf = localtime(&when);
4541 tmbuf = gmtime(&when);
4543 if (GIMME != G_ARRAY) {
4549 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4550 dayname[tmbuf->tm_wday],
4551 monname[tmbuf->tm_mon],
4556 tmbuf->tm_year + 1900);
4557 PUSHs(sv_2mortal(tsv));
4562 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4563 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4564 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4565 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4566 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4567 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4568 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4569 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4570 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4581 anum = alarm((unsigned int)anum);
4588 DIE(aTHX_ PL_no_func, "alarm");
4599 (void)time(&lasttime);
4604 PerlProc_sleep((unsigned int)duration);
4607 XPUSHi(when - lasttime);
4611 /* Shared memory. */
4625 return pp_shmwrite();
4630 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4631 dSP; dMARK; dTARGET;
4632 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4641 /* Message passing. */
4655 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4656 dSP; dMARK; dTARGET;
4657 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4668 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4669 dSP; dMARK; dTARGET;
4670 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4683 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4684 dSP; dMARK; dTARGET;
4685 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4692 DIE(aTHX_ "System V IPC is not implemented on this machine");
4698 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4699 dSP; dMARK; dTARGET;
4700 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4708 PUSHp(zero_but_true, ZBTLEN);
4718 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4719 dSP; dMARK; dTARGET;
4720 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4729 /* Get system info. */
4733 #ifdef HAS_GETHOSTBYNAME
4734 return pp_ghostent();
4736 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4742 #ifdef HAS_GETHOSTBYADDR
4743 return pp_ghostent();
4745 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4751 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4753 I32 which = PL_op->op_type;
4754 register char **elem;
4756 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4757 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4758 struct hostent *gethostbyname(Netdb_name_t);
4759 struct hostent *gethostent(void);
4761 struct hostent *hent;
4766 if (which == OP_GHBYNAME) {
4767 #ifdef HAS_GETHOSTBYNAME
4768 char* name = POPpbytex;
4769 hent = PerlSock_gethostbyname(name);
4771 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4774 else if (which == OP_GHBYADDR) {
4775 #ifdef HAS_GETHOSTBYADDR
4776 int addrtype = POPi;
4779 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4781 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4783 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4787 #ifdef HAS_GETHOSTENT
4788 hent = PerlSock_gethostent();
4790 DIE(aTHX_ PL_no_sock_func, "gethostent");
4793 #ifdef HOST_NOT_FOUND
4795 #ifdef USE_REENTRANT_API
4796 # ifdef USE_GETHOSTENT_ERRNO
4797 h_errno = PL_reentrant_buffer->_gethostent_errno;
4800 STATUS_NATIVE_SET(h_errno);
4804 if (GIMME != G_ARRAY) {
4805 PUSHs(sv = sv_newmortal());
4807 if (which == OP_GHBYNAME) {
4809 sv_setpvn(sv, hent->h_addr, hent->h_length);
4812 sv_setpv(sv, (char*)hent->h_name);
4818 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4819 sv_setpv(sv, (char*)hent->h_name);
4820 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4821 for (elem = hent->h_aliases; elem && *elem; elem++) {
4822 sv_catpv(sv, *elem);
4824 sv_catpvn(sv, " ", 1);
4826 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4827 sv_setiv(sv, (IV)hent->h_addrtype);
4828 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4829 len = hent->h_length;
4830 sv_setiv(sv, (IV)len);
4832 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4833 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4834 sv_setpvn(sv, *elem, len);
4837 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4839 sv_setpvn(sv, hent->h_addr, len);
4844 DIE(aTHX_ PL_no_sock_func, "gethostent");
4850 #ifdef HAS_GETNETBYNAME
4851 return pp_gnetent();
4853 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4859 #ifdef HAS_GETNETBYADDR
4860 return pp_gnetent();
4862 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4868 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4870 I32 which = PL_op->op_type;
4871 register char **elem;
4873 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4874 struct netent *getnetbyaddr(Netdb_net_t, int);
4875 struct netent *getnetbyname(Netdb_name_t);
4876 struct netent *getnetent(void);
4878 struct netent *nent;
4881 if (which == OP_GNBYNAME){
4882 #ifdef HAS_GETNETBYNAME
4883 char *name = POPpbytex;
4884 nent = PerlSock_getnetbyname(name);
4886 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4889 else if (which == OP_GNBYADDR) {
4890 #ifdef HAS_GETNETBYADDR
4891 int addrtype = POPi;
4892 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4893 nent = PerlSock_getnetbyaddr(addr, addrtype);
4895 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4899 #ifdef HAS_GETNETENT
4900 nent = PerlSock_getnetent();
4902 DIE(aTHX_ PL_no_sock_func, "getnetent");
4905 #ifdef HOST_NOT_FOUND
4907 #ifdef USE_REENTRANT_API
4908 # ifdef USE_GETNETENT_ERRNO
4909 h_errno = PL_reentrant_buffer->_getnetent_errno;
4912 STATUS_NATIVE_SET(h_errno);
4917 if (GIMME != G_ARRAY) {
4918 PUSHs(sv = sv_newmortal());
4920 if (which == OP_GNBYNAME)
4921 sv_setiv(sv, (IV)nent->n_net);
4923 sv_setpv(sv, nent->n_name);
4929 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4930 sv_setpv(sv, nent->n_name);
4931 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4932 for (elem = nent->n_aliases; elem && *elem; elem++) {
4933 sv_catpv(sv, *elem);
4935 sv_catpvn(sv, " ", 1);
4937 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4938 sv_setiv(sv, (IV)nent->n_addrtype);
4939 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4940 sv_setiv(sv, (IV)nent->n_net);
4945 DIE(aTHX_ PL_no_sock_func, "getnetent");
4951 #ifdef HAS_GETPROTOBYNAME
4952 return pp_gprotoent();
4954 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4960 #ifdef HAS_GETPROTOBYNUMBER
4961 return pp_gprotoent();
4963 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4969 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4971 I32 which = PL_op->op_type;
4972 register char **elem;
4974 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4975 struct protoent *getprotobyname(Netdb_name_t);
4976 struct protoent *getprotobynumber(int);
4977 struct protoent *getprotoent(void);
4979 struct protoent *pent;
4982 if (which == OP_GPBYNAME) {
4983 #ifdef HAS_GETPROTOBYNAME
4984 char* name = POPpbytex;
4985 pent = PerlSock_getprotobyname(name);
4987 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4990 else if (which == OP_GPBYNUMBER) {
4991 #ifdef HAS_GETPROTOBYNUMBER
4993 pent = PerlSock_getprotobynumber(number);
4995 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4999 #ifdef HAS_GETPROTOENT
5000 pent = PerlSock_getprotoent();
5002 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5006 if (GIMME != G_ARRAY) {
5007 PUSHs(sv = sv_newmortal());
5009 if (which == OP_GPBYNAME)
5010 sv_setiv(sv, (IV)pent->p_proto);
5012 sv_setpv(sv, pent->p_name);
5018 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5019 sv_setpv(sv, pent->p_name);
5020 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5021 for (elem = pent->p_aliases; elem && *elem; elem++) {
5022 sv_catpv(sv, *elem);
5024 sv_catpvn(sv, " ", 1);
5026 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5027 sv_setiv(sv, (IV)pent->p_proto);
5032 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5038 #ifdef HAS_GETSERVBYNAME
5039 return pp_gservent();
5041 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5047 #ifdef HAS_GETSERVBYPORT
5048 return pp_gservent();
5050 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5056 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5058 I32 which = PL_op->op_type;
5059 register char **elem;
5061 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5062 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5063 struct servent *getservbyport(int, Netdb_name_t);
5064 struct servent *getservent(void);
5066 struct servent *sent;
5069 if (which == OP_GSBYNAME) {
5070 #ifdef HAS_GETSERVBYNAME
5071 char *proto = POPpbytex;
5072 char *name = POPpbytex;
5074 if (proto && !*proto)
5077 sent = PerlSock_getservbyname(name, proto);
5079 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5082 else if (which == OP_GSBYPORT) {
5083 #ifdef HAS_GETSERVBYPORT
5084 char *proto = POPpbytex;
5085 unsigned short port = (unsigned short)POPu;
5087 if (proto && !*proto)
5091 port = PerlSock_htons(port);
5093 sent = PerlSock_getservbyport(port, proto);
5095 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5099 #ifdef HAS_GETSERVENT
5100 sent = PerlSock_getservent();
5102 DIE(aTHX_ PL_no_sock_func, "getservent");
5106 if (GIMME != G_ARRAY) {
5107 PUSHs(sv = sv_newmortal());
5109 if (which == OP_GSBYNAME) {
5111 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5113 sv_setiv(sv, (IV)(sent->s_port));
5117 sv_setpv(sv, sent->s_name);
5123 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5124 sv_setpv(sv, sent->s_name);
5125 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5126 for (elem = sent->s_aliases; elem && *elem; elem++) {
5127 sv_catpv(sv, *elem);
5129 sv_catpvn(sv, " ", 1);
5131 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5133 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5135 sv_setiv(sv, (IV)(sent->s_port));
5137 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5138 sv_setpv(sv, sent->s_proto);
5143 DIE(aTHX_ PL_no_sock_func, "getservent");
5149 #ifdef HAS_SETHOSTENT
5151 PerlSock_sethostent(TOPi);
5154 DIE(aTHX_ PL_no_sock_func, "sethostent");
5160 #ifdef HAS_SETNETENT
5162 PerlSock_setnetent(TOPi);
5165 DIE(aTHX_ PL_no_sock_func, "setnetent");
5171 #ifdef HAS_SETPROTOENT
5173 PerlSock_setprotoent(TOPi);
5176 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5182 #ifdef HAS_SETSERVENT
5184 PerlSock_setservent(TOPi);
5187 DIE(aTHX_ PL_no_sock_func, "setservent");
5193 #ifdef HAS_ENDHOSTENT
5195 PerlSock_endhostent();
5199 DIE(aTHX_ PL_no_sock_func, "endhostent");
5205 #ifdef HAS_ENDNETENT
5207 PerlSock_endnetent();
5211 DIE(aTHX_ PL_no_sock_func, "endnetent");
5217 #ifdef HAS_ENDPROTOENT
5219 PerlSock_endprotoent();
5223 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5229 #ifdef HAS_ENDSERVENT
5231 PerlSock_endservent();
5235 DIE(aTHX_ PL_no_sock_func, "endservent");
5244 DIE(aTHX_ PL_no_func, "getpwnam");
5253 DIE(aTHX_ PL_no_func, "getpwuid");
5261 I32 which = PL_op->op_type;
5264 struct passwd *pwent = NULL;
5266 * We currently support only the SysV getsp* shadow password interface.
5267 * The interface is declared in <shadow.h> and often one needs to link
5268 * with -lsecurity or some such.
5269 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5272 * AIX getpwnam() is clever enough to return the encrypted password
5273 * only if the caller (euid?) is root.
5275 * There are at least three other shadow password APIs. Many platforms
5276 * seem to contain more than one interface for accessing the shadow
5277 * password databases, possibly for compatibility reasons.
5278 * The getsp*() is by far he simplest one, the other two interfaces
5279 * are much more complicated, but also very similar to each other.
5284 * struct pr_passwd *getprpw*();
5285 * The password is in
5286 * char getprpw*(...).ufld.fd_encrypt[]
5287 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5292 * struct es_passwd *getespw*();
5293 * The password is in
5294 * char *(getespw*(...).ufld.fd_encrypt)
5295 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5298 * struct userpw *getuserpw();
5299 * The password is in
5300 * char *(getuserpw(...)).spw_upw_passwd
5301 * (but the de facto standard getpwnam() should work okay)
5303 * Mention I_PROT here so that Configure probes for it.
5305 * In HP-UX for getprpw*() the manual page claims that one should include
5306 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5307 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5308 * and pp_sys.c already includes <shadow.h> if there is such.
5310 * Note that <sys/security.h> is already probed for, but currently
5311 * it is only included in special cases.
5313 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5314 * be preferred interface, even though also the getprpw*() interface
5315 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5316 * One also needs to call set_auth_parameters() in main() before
5317 * doing anything else, whether one is using getespw*() or getprpw*().
5319 * Note that accessing the shadow databases can be magnitudes
5320 * slower than accessing the standard databases.
5325 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5326 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5327 * the pw_comment is left uninitialized. */
5328 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5334 char* name = POPpbytex;
5335 pwent = getpwnam(name);
5341 pwent = getpwuid(uid);
5345 # ifdef HAS_GETPWENT
5347 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5348 if (pwent) pwent = getpwnam(pwent->pw_name);
5351 DIE(aTHX_ PL_no_func, "getpwent");
5357 if (GIMME != G_ARRAY) {
5358 PUSHs(sv = sv_newmortal());
5360 if (which == OP_GPWNAM)
5361 # if Uid_t_sign <= 0
5362 sv_setiv(sv, (IV)pwent->pw_uid);
5364 sv_setuv(sv, (UV)pwent->pw_uid);
5367 sv_setpv(sv, pwent->pw_name);
5373 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5374 sv_setpv(sv, pwent->pw_name);
5376 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5378 /* If we have getspnam(), we try to dig up the shadow
5379 * password. If we are underprivileged, the shadow
5380 * interface will set the errno to EACCES or similar,
5381 * and return a null pointer. If this happens, we will
5382 * use the dummy password (usually "*" or "x") from the
5383 * standard password database.
5385 * In theory we could skip the shadow call completely
5386 * if euid != 0 but in practice we cannot know which
5387 * security measures are guarding the shadow databases
5388 * on a random platform.
5390 * Resist the urge to use additional shadow interfaces.
5391 * Divert the urge to writing an extension instead.
5394 /* Some AIX setups falsely(?) detect some getspnam(), which
5395 * has a different API than the Solaris/IRIX one. */
5396 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5398 struct spwd *spwent;
5399 int saverrno; /* Save and restore errno so that
5400 * underprivileged attempts seem
5401 * to have never made the unsccessful
5402 * attempt to retrieve the shadow password. */
5405 spwent = getspnam(pwent->pw_name);
5407 if (spwent && spwent->sp_pwdp)
5408 sv_setpv(sv, spwent->sp_pwdp);
5412 if (!SvPOK(sv)) /* Use the standard password, then. */
5413 sv_setpv(sv, pwent->pw_passwd);
5416 # ifndef INCOMPLETE_TAINTS
5417 /* passwd is tainted because user himself can diddle with it.
5418 * admittedly not much and in a very limited way, but nevertheless. */
5422 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5423 # if Uid_t_sign <= 0
5424 sv_setiv(sv, (IV)pwent->pw_uid);
5426 sv_setuv(sv, (UV)pwent->pw_uid);
5429 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5430 # if Uid_t_sign <= 0
5431 sv_setiv(sv, (IV)pwent->pw_gid);
5433 sv_setuv(sv, (UV)pwent->pw_gid);
5435 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5436 * because of the poor interface of the Perl getpw*(),
5437 * not because there's some standard/convention saying so.
5438 * A better interface would have been to return a hash,
5439 * but we are accursed by our history, alas. --jhi. */
5440 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5442 sv_setiv(sv, (IV)pwent->pw_change);
5445 sv_setiv(sv, (IV)pwent->pw_quota);
5448 sv_setpv(sv, pwent->pw_age);
5453 /* pw_class and pw_comment are mutually exclusive--.
5454 * see the above note for pw_change, pw_quota, and pw_age. */
5455 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5457 sv_setpv(sv, pwent->pw_class);
5460 sv_setpv(sv, pwent->pw_comment);
5464 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5466 sv_setpv(sv, pwent->pw_gecos);
5468 # ifndef INCOMPLETE_TAINTS
5469 /* pw_gecos is tainted because user himself can diddle with it. */
5473 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5474 sv_setpv(sv, pwent->pw_dir);
5476 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5477 sv_setpv(sv, pwent->pw_shell);
5478 # ifndef INCOMPLETE_TAINTS
5479 /* pw_shell is tainted because user himself can diddle with it. */
5484 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5485 sv_setiv(sv, (IV)pwent->pw_expire);
5490 DIE(aTHX_ PL_no_func, "getpwent");
5496 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5501 DIE(aTHX_ PL_no_func, "setpwent");
5507 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5512 DIE(aTHX_ PL_no_func, "endpwent");
5521 DIE(aTHX_ PL_no_func, "getgrnam");
5530 DIE(aTHX_ PL_no_func, "getgrgid");
5538 I32 which = PL_op->op_type;
5539 register char **elem;
5541 struct group *grent;
5544 if (which == OP_GGRNAM) {
5545 char* name = POPpbytex;
5546 grent = (struct group *)getgrnam(name);
5548 else if (which == OP_GGRGID) {
5550 grent = (struct group *)getgrgid(gid);
5554 grent = (struct group *)getgrent();
5556 DIE(aTHX_ PL_no_func, "getgrent");
5560 if (GIMME != G_ARRAY) {
5561 PUSHs(sv = sv_newmortal());
5563 if (which == OP_GGRNAM)
5564 sv_setiv(sv, (IV)grent->gr_gid);
5566 sv_setpv(sv, grent->gr_name);
5572 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5573 sv_setpv(sv, grent->gr_name);
5575 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5577 sv_setpv(sv, grent->gr_passwd);
5580 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5581 sv_setiv(sv, (IV)grent->gr_gid);
5583 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5584 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5585 /* In UNICOS/mk (_CRAYMPP) the multithreading
5586 * versions (getgrnam_r, getgrgid_r)
5587 * seem to return an illegal pointer
5588 * as the group members list, gr_mem.
5589 * getgrent() doesn't even have a _r version
5590 * but the gr_mem is poisonous anyway.
5591 * So yes, you cannot get the list of group
5592 * members if building multithreaded in UNICOS/mk. */
5593 for (elem = grent->gr_mem; elem && *elem; elem++) {
5594 sv_catpv(sv, *elem);
5596 sv_catpvn(sv, " ", 1);
5603 DIE(aTHX_ PL_no_func, "getgrent");
5609 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5614 DIE(aTHX_ PL_no_func, "setgrent");
5620 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5625 DIE(aTHX_ PL_no_func, "endgrent");
5635 if (!(tmps = PerlProc_getlogin()))
5637 PUSHp(tmps, strlen(tmps));
5640 DIE(aTHX_ PL_no_func, "getlogin");
5644 /* Miscellaneous. */
5649 dSP; dMARK; dORIGMARK; dTARGET;
5650 register I32 items = SP - MARK;
5651 unsigned long a[20];
5657 while (++MARK <= SP) {
5658 if (SvTAINTED(*MARK)) {
5664 TAINT_PROPER("syscall");
5667 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5668 * or where sizeof(long) != sizeof(char*). But such machines will
5669 * not likely have syscall implemented either, so who cares?
5671 while (++MARK <= SP) {
5672 if (SvNIOK(*MARK) || !i)
5673 a[i++] = SvIV(*MARK);
5674 else if (*MARK == &PL_sv_undef)
5677 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5683 DIE(aTHX_ "Too many args to syscall");
5685 DIE(aTHX_ "Too few args to syscall");
5687 retval = syscall(a[0]);
5690 retval = syscall(a[0],a[1]);
5693 retval = syscall(a[0],a[1],a[2]);
5696 retval = syscall(a[0],a[1],a[2],a[3]);
5699 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5702 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5705 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5708 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5712 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5715 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5718 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5722 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5726 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5730 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5731 a[10],a[11],a[12],a[13]);
5733 #endif /* atarist */
5739 DIE(aTHX_ PL_no_func, "syscall");
5743 #ifdef FCNTL_EMULATE_FLOCK
5745 /* XXX Emulate flock() with fcntl().
5746 What's really needed is a good file locking module.
5750 fcntl_emulate_flock(int fd, int operation)
5754 switch (operation & ~LOCK_NB) {
5756 flock.l_type = F_RDLCK;
5759 flock.l_type = F_WRLCK;
5762 flock.l_type = F_UNLCK;
5768 flock.l_whence = SEEK_SET;
5769 flock.l_start = flock.l_len = (Off_t)0;
5771 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5774 #endif /* FCNTL_EMULATE_FLOCK */
5776 #ifdef LOCKF_EMULATE_FLOCK
5778 /* XXX Emulate flock() with lockf(). This is just to increase
5779 portability of scripts. The calls are not completely
5780 interchangeable. What's really needed is a good file
5784 /* The lockf() constants might have been defined in <unistd.h>.
5785 Unfortunately, <unistd.h> causes troubles on some mixed
5786 (BSD/POSIX) systems, such as SunOS 4.1.3.
5788 Further, the lockf() constants aren't POSIX, so they might not be
5789 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5790 just stick in the SVID values and be done with it. Sigh.
5794 # define F_ULOCK 0 /* Unlock a previously locked region */
5797 # define F_LOCK 1 /* Lock a region for exclusive use */
5800 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5803 # define F_TEST 3 /* Test a region for other processes locks */
5807 lockf_emulate_flock(int fd, int operation)
5813 /* flock locks entire file so for lockf we need to do the same */
5815 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5816 if (pos > 0) /* is seekable and needs to be repositioned */
5817 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5818 pos = -1; /* seek failed, so don't seek back afterwards */
5821 switch (operation) {
5823 /* LOCK_SH - get a shared lock */
5825 /* LOCK_EX - get an exclusive lock */
5827 i = lockf (fd, F_LOCK, 0);
5830 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5831 case LOCK_SH|LOCK_NB:
5832 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5833 case LOCK_EX|LOCK_NB:
5834 i = lockf (fd, F_TLOCK, 0);
5836 if ((errno == EAGAIN) || (errno == EACCES))
5837 errno = EWOULDBLOCK;
5840 /* LOCK_UN - unlock (non-blocking is a no-op) */
5842 case LOCK_UN|LOCK_NB:
5843 i = lockf (fd, F_ULOCK, 0);
5846 /* Default - can't decipher operation */
5853 if (pos > 0) /* need to restore position of the handle */
5854 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5859 #endif /* LOCKF_EMULATE_FLOCK */
5863 * c-indentation-style: bsd
5865 * indent-tabs-mode: t
5868 * vim: shiftwidth=4: