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 SvPV_shrink_to_cur(sv);
369 STATUS_NATIVE_SET(PerlProc_pclose(fp));
370 TAINT; /* "I believe that this is not gratuitous!" */
373 STATUS_NATIVE_SET(-1);
374 if (gimme == G_SCALAR)
384 tryAMAGICunTARGET(iter, -1);
386 /* Note that we only ever get here if File::Glob fails to load
387 * without at the same time croaking, for some reason, or if
388 * perl was built with PERL_EXTERNAL_GLOB */
395 * The external globbing program may use things we can't control,
396 * so for security reasons we must assume the worst.
399 taint_proper(PL_no_security, "glob");
403 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
404 PL_last_in_gv = (GV*)*PL_stack_sp--;
406 SAVESPTR(PL_rs); /* This is not permanent, either. */
407 PL_rs = sv_2mortal(newSVpvn("\000", 1));
410 *SvPVX(PL_rs) = '\n';
414 result = do_readline();
421 PL_last_in_gv = cGVOP_gv;
422 return do_readline();
431 if (SP - MARK != 1) {
433 do_join(TARG, &PL_sv_no, MARK, SP);
440 tmps = SvPV(tmpsv, len);
441 if ((!tmps || !len) && PL_errgv) {
443 (void)SvUPGRADE(error, SVt_PV);
444 if (SvPOK(error) && SvCUR(error))
445 sv_catpv(error, "\t...caught");
447 tmps = SvPV(tmpsv, len);
450 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
452 Perl_warn(aTHX_ "%"SVf, tmpsv);
464 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
466 if (SP - MARK != 1) {
468 do_join(TARG, &PL_sv_no, MARK, SP);
470 tmps = SvPV(tmpsv, len);
476 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
480 (void)SvUPGRADE(error, SVt_PV);
481 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
483 SvSetSV(error,tmpsv);
484 else if (sv_isobject(error)) {
485 HV *stash = SvSTASH(SvRV(error));
486 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
488 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
489 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
496 call_sv((SV*)GvCV(gv),
497 G_SCALAR|G_EVAL|G_KEEPERR);
498 sv_setsv(error,*PL_stack_sp--);
504 if (SvPOK(error) && SvCUR(error))
505 sv_catpv(error, "\t...propagated");
507 tmps = SvPV(tmpsv, len);
511 tmpsv = sv_2mortal(newSVpvn("Died", 4));
513 DIE(aTHX_ "%"SVf, tmpsv);
533 DIE(aTHX_ PL_no_usym, "filehandle");
534 if ((io = GvIOp(gv)))
535 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
537 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
538 /* Method's args are same as ours ... */
539 /* ... except handle is replaced by the object */
540 *MARK-- = SvTIED_obj((SV*)io, mg);
544 call_method("OPEN", G_SCALAR);
557 tmps = SvPV(sv, len);
558 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
561 PUSHi( (I32)PL_forkprocess );
562 else if (PL_forkprocess == 0) /* we are a new child */
581 if (gv && (io = GvIO(gv))
582 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
585 XPUSHs(SvTIED_obj((SV*)io, mg));
588 call_method("CLOSE", G_SCALAR);
594 PUSHs(boolSV(do_close(gv, TRUE)));
614 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
615 DIE(aTHX_ PL_no_usym, "filehandle");
620 do_close(rgv, FALSE);
622 do_close(wgv, FALSE);
624 if (PerlProc_pipe(fd) < 0)
627 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
628 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
629 IoOFP(rstio) = IoIFP(rstio);
630 IoIFP(wstio) = IoOFP(wstio);
631 IoTYPE(rstio) = IoTYPE_RDONLY;
632 IoTYPE(wstio) = IoTYPE_WRONLY;
634 if (!IoIFP(rstio) || !IoOFP(wstio)) {
635 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
636 else PerlLIO_close(fd[0]);
637 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
638 else PerlLIO_close(fd[1]);
641 #if defined(HAS_FCNTL) && defined(F_SETFD)
642 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
643 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
650 DIE(aTHX_ PL_no_func, "pipe");
666 if (gv && (io = GvIO(gv))
667 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
670 XPUSHs(SvTIED_obj((SV*)io, mg));
673 call_method("FILENO", G_SCALAR);
679 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
680 /* Can't do this because people seem to do things like
681 defined(fileno($foo)) to check whether $foo is a valid fh.
682 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
683 report_evil_fh(gv, io, PL_op->op_type);
688 PUSHi(PerlIO_fileno(fp));
699 anum = PerlLIO_umask(0);
700 (void)PerlLIO_umask(anum);
703 anum = PerlLIO_umask(POPi);
704 TAINT_PROPER("umask");
707 /* Only DIE if trying to restrict permissions on `user' (self).
708 * Otherwise it's harmless and more useful to just return undef
709 * since 'group' and 'other' concepts probably don't exist here. */
710 if (MAXARG >= 1 && (POPi & 0700))
711 DIE(aTHX_ "umask not implemented");
712 XPUSHs(&PL_sv_undef);
734 if (gv && (io = GvIO(gv))
735 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
738 XPUSHs(SvTIED_obj((SV*)io, mg));
743 call_method("BINMODE", G_SCALAR);
750 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
751 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
752 report_evil_fh(gv, io, PL_op->op_type);
753 SETERRNO(EBADF,RMS_IFI);
758 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
759 (discp) ? SvPV_nolen(discp) : Nullch)) {
760 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
761 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
762 mode_from_discipline(discp),
763 (discp) ? SvPV_nolen(discp) : Nullch)) {
785 I32 markoff = MARK - PL_stack_base;
786 const char *methname;
787 int how = PERL_MAGIC_tied;
791 switch(SvTYPE(varsv)) {
793 methname = "TIEHASH";
794 HvEITER((HV *)varsv) = Null(HE *);
797 methname = "TIEARRAY";
800 #ifdef GV_UNIQUE_CHECK
801 if (GvUNIQUE((GV*)varsv)) {
802 Perl_croak(aTHX_ "Attempt to tie unique GV");
805 methname = "TIEHANDLE";
806 how = PERL_MAGIC_tiedscalar;
807 /* For tied filehandles, we apply tiedscalar magic to the IO
808 slot of the GP rather than the GV itself. AMS 20010812 */
810 GvIOp(varsv) = newIO();
811 varsv = (SV *)GvIOp(varsv);
814 methname = "TIESCALAR";
815 how = PERL_MAGIC_tiedscalar;
819 if (sv_isobject(*MARK)) {
821 PUSHSTACKi(PERLSI_MAGIC);
823 EXTEND(SP,(I32)items);
827 call_method(methname, G_SCALAR);
830 /* Not clear why we don't call call_method here too.
831 * perhaps to get different error message ?
833 stash = gv_stashsv(*MARK, FALSE);
834 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
835 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
839 PUSHSTACKi(PERLSI_MAGIC);
841 EXTEND(SP,(I32)items);
845 call_sv((SV*)GvCV(gv), G_SCALAR);
851 if (sv_isobject(sv)) {
852 sv_unmagic(varsv, how);
853 /* Croak if a self-tie on an aggregate is attempted. */
854 if (varsv == SvRV(sv) &&
855 (SvTYPE(varsv) == SVt_PVAV ||
856 SvTYPE(varsv) == SVt_PVHV))
858 "Self-ties of arrays and hashes are not supported");
859 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
862 SP = PL_stack_base + markoff;
872 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
873 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
875 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
878 if ((mg = SvTIED_mg(sv, how))) {
879 SV *obj = SvRV(SvTIED_obj(sv, mg));
883 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
884 isGV(gv) && (cv = GvCV(gv))) {
886 XPUSHs(SvTIED_obj((SV*)gv, mg));
887 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
890 call_sv((SV *)cv, G_VOID);
894 else if (ckWARN(WARN_UNTIE)) {
895 if (mg && SvREFCNT(obj) > 1)
896 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
897 "untie attempted while %"UVuf" inner references still exist",
898 (UV)SvREFCNT(obj) - 1 ) ;
902 sv_unmagic(sv, how) ;
911 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
912 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
914 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
917 if ((mg = SvTIED_mg(sv, how))) {
918 SV *osv = SvTIED_obj(sv, mg);
919 if (osv == mg->mg_obj)
920 osv = sv_mortalcopy(osv);
938 sv = sv_mortalcopy(&PL_sv_no);
939 sv_setpv(sv, "AnyDBM_File");
940 stash = gv_stashsv(sv, FALSE);
941 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
943 require_pv("AnyDBM_File.pm");
945 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
946 DIE(aTHX_ "No dbm on this machine");
956 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
958 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
961 call_sv((SV*)GvCV(gv), G_SCALAR);
964 if (!sv_isobject(TOPs)) {
969 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
972 call_sv((SV*)GvCV(gv), G_SCALAR);
976 if (sv_isobject(TOPs)) {
977 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
978 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
1000 struct timeval timebuf;
1001 struct timeval *tbuf = &timebuf;
1005 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1010 # if BYTEORDER & 0xf0000
1011 # define ORDERBYTE (0x88888888 - BYTEORDER)
1013 # define ORDERBYTE (0x4444 - BYTEORDER)
1019 for (i = 1; i <= 3; i++) {
1027 /* little endians can use vecs directly */
1028 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1035 masksize = NFDBITS / NBBY;
1037 masksize = sizeof(long); /* documented int, everyone seems to use long */
1039 Zero(&fd_sets[0], 4, char*);
1042 # if SELECT_MIN_BITS == 1
1043 growsize = sizeof(fd_set);
1045 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1046 # undef SELECT_MIN_BITS
1047 # define SELECT_MIN_BITS __FD_SETSIZE
1049 /* If SELECT_MIN_BITS is greater than one we most probably will want
1050 * to align the sizes with SELECT_MIN_BITS/8 because for example
1051 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1052 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1053 * on (sets/tests/clears bits) is 32 bits. */
1054 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1062 timebuf.tv_sec = (long)value;
1063 value -= (NV)timebuf.tv_sec;
1064 timebuf.tv_usec = (long)(value * 1000000.0);
1067 tbuf = Null(struct timeval*);
1069 for (i = 1; i <= 3; i++) {
1075 else if (!SvPOK(sv))
1076 SvPV_force(sv,n_a); /* force string conversion */
1079 Sv_Grow(sv, growsize);
1083 while (++j <= growsize) {
1087 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1089 New(403, fd_sets[i], growsize, char);
1090 for (offset = 0; offset < growsize; offset += masksize) {
1091 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1092 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1095 fd_sets[i] = SvPVX(sv);
1099 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1100 /* Can't make just the (void*) conditional because that would be
1101 * cpp #if within cpp macro, and not all compilers like that. */
1102 nfound = PerlSock_select(
1104 (Select_fd_set_t) fd_sets[1],
1105 (Select_fd_set_t) fd_sets[2],
1106 (Select_fd_set_t) fd_sets[3],
1107 (void*) tbuf); /* Workaround for compiler bug. */
1109 nfound = PerlSock_select(
1111 (Select_fd_set_t) fd_sets[1],
1112 (Select_fd_set_t) fd_sets[2],
1113 (Select_fd_set_t) fd_sets[3],
1116 for (i = 1; i <= 3; i++) {
1119 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1121 for (offset = 0; offset < growsize; offset += masksize) {
1122 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1123 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1125 Safefree(fd_sets[i]);
1132 PUSHs(&PL_sv_undef);
1135 if (GIMME == G_ARRAY && tbuf) {
1136 value = (NV)(timebuf.tv_sec) +
1137 (NV)(timebuf.tv_usec) / 1000000.0;
1138 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1139 sv_setnv(sv, value);
1143 DIE(aTHX_ "select not implemented");
1148 Perl_setdefout(pTHX_ GV *gv)
1151 (void)SvREFCNT_inc(gv);
1153 SvREFCNT_dec(PL_defoutgv);
1160 GV *newdefout, *egv;
1163 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1165 egv = GvEGV(PL_defoutgv);
1170 XPUSHs(&PL_sv_undef);
1172 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1173 if (gvp && *gvp == egv) {
1174 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1178 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1183 if (!GvIO(newdefout))
1184 gv_IOadd(newdefout);
1185 setdefout(newdefout);
1203 if (gv && (io = GvIO(gv))
1204 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1206 I32 gimme = GIMME_V;
1208 XPUSHs(SvTIED_obj((SV*)io, mg));
1211 call_method("GETC", gimme);
1214 if (gimme == G_SCALAR)
1215 SvSetMagicSV_nosteal(TARG, TOPs);
1218 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1219 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1220 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1221 report_evil_fh(gv, io, PL_op->op_type);
1222 SETERRNO(EBADF,RMS_IFI);
1226 sv_setpv(TARG, " ");
1227 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1228 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1229 /* Find out how many bytes the char needs */
1230 Size_t len = UTF8SKIP(SvPVX(TARG));
1233 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1234 SvCUR_set(TARG,1+len);
1244 return pp_sysread();
1248 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1250 register PERL_CONTEXT *cx;
1251 I32 gimme = GIMME_V;
1256 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1258 cx->blk_sub.retop = retop;
1259 PAD_SET_CUR(CvPADLIST(cv), 1);
1261 setdefout(gv); /* locally select filehandle so $% et al work */
1294 SV *tmpsv = sv_newmortal();
1295 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1296 name = SvPV_nolen(tmpsv);
1299 DIE(aTHX_ "Undefined format \"%s\" called", name);
1300 DIE(aTHX_ "Not a format reference");
1303 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1305 IoFLAGS(io) &= ~IOf_DIDTOP;
1306 return doform(cv,gv,PL_op->op_next);
1312 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1313 register IO *io = GvIOp(gv);
1314 PerlIO *ofp = IoOFP(io);
1318 register PERL_CONTEXT *cx;
1320 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1321 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1324 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1325 PL_formtarget != PL_toptarget)
1329 if (!IoTOP_GV(io)) {
1333 if (!IoTOP_NAME(io)) {
1334 if (!IoFMT_NAME(io))
1335 IoFMT_NAME(io) = savepv(GvNAME(gv));
1336 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1337 topgv = gv_fetchsv(topname, FALSE, SVt_PVFM);
1338 if ((topgv && GvFORM(topgv)) ||
1339 !gv_fetchpv("top",FALSE,SVt_PVFM))
1340 IoTOP_NAME(io) = savesvpv(topname);
1342 IoTOP_NAME(io) = savepvn("top", 3);
1344 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1345 if (!topgv || !GvFORM(topgv)) {
1346 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1349 IoTOP_GV(io) = topgv;
1351 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1352 I32 lines = IoLINES_LEFT(io);
1353 char *s = SvPVX(PL_formtarget);
1354 if (lines <= 0) /* Yow, header didn't even fit!!! */
1356 while (lines-- > 0) {
1357 s = strchr(s, '\n');
1363 STRLEN save = SvCUR(PL_formtarget);
1364 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1365 do_print(PL_formtarget, ofp);
1366 SvCUR_set(PL_formtarget, save);
1367 sv_chop(PL_formtarget, s);
1368 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1371 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1372 do_print(PL_formfeed, ofp);
1373 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1375 PL_formtarget = PL_toptarget;
1376 IoFLAGS(io) |= IOf_DIDTOP;
1379 DIE(aTHX_ "bad top format reference");
1384 SV *sv = sv_newmortal();
1385 gv_efullname4(sv, fgv, Nullch, FALSE);
1386 name = SvPV_nolen(sv);
1389 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1392 DIE(aTHX_ "Undefined top format called");
1396 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1397 return doform(cv,gv,PL_op);
1401 POPBLOCK(cx,PL_curpm);
1407 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1409 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1410 else if (ckWARN(WARN_CLOSED))
1411 report_evil_fh(gv, io, PL_op->op_type);
1416 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1417 if (ckWARN(WARN_IO))
1418 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1420 if (!do_print(PL_formtarget, fp))
1423 FmLINES(PL_formtarget) = 0;
1424 SvCUR_set(PL_formtarget, 0);
1425 *SvEND(PL_formtarget) = '\0';
1426 if (IoFLAGS(io) & IOf_FLUSH)
1427 (void)PerlIO_flush(fp);
1432 PL_formtarget = PL_bodytarget;
1434 return cx->blk_sub.retop;
1439 dSP; dMARK; dORIGMARK;
1446 if (PL_op->op_flags & OPf_STACKED)
1451 if (gv && (io = GvIO(gv))
1452 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1454 if (MARK == ORIGMARK) {
1457 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1461 *MARK = SvTIED_obj((SV*)io, mg);
1464 call_method("PRINTF", G_SCALAR);
1467 MARK = ORIGMARK + 1;
1474 if (!(io = GvIO(gv))) {
1475 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1476 report_evil_fh(gv, io, PL_op->op_type);
1477 SETERRNO(EBADF,RMS_IFI);
1480 else if (!(fp = IoOFP(io))) {
1481 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1483 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1484 else if (ckWARN(WARN_CLOSED))
1485 report_evil_fh(gv, io, PL_op->op_type);
1487 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1491 do_sprintf(sv, SP - MARK, MARK + 1);
1492 if (!do_print(sv, fp))
1495 if (IoFLAGS(io) & IOf_FLUSH)
1496 if (PerlIO_flush(fp) == EOF)
1507 PUSHs(&PL_sv_undef);
1528 /* Need TIEHANDLE method ? */
1530 tmps = SvPV(sv, len);
1531 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1532 IoLINES(GvIOp(gv)) = 0;
1536 PUSHs(&PL_sv_undef);
1543 dSP; dMARK; dORIGMARK; dTARGET;
1550 Sock_size_t bufsize;
1559 bool charstart = FALSE;
1560 STRLEN charskip = 0;
1564 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1565 && gv && (io = GvIO(gv))
1566 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1571 *MARK = SvTIED_obj((SV*)io, mg);
1573 call_method("READ", G_SCALAR);
1586 sv_setpvn(bufsv, "", 0);
1587 length = SvIVx(*++MARK);
1590 offset = SvIVx(*++MARK);
1594 if (!io || !IoIFP(io)) {
1595 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1596 report_evil_fh(gv, io, PL_op->op_type);
1597 SETERRNO(EBADF,RMS_IFI);
1600 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1601 buffer = SvPVutf8_force(bufsv, blen);
1602 /* UTF-8 may not have been set if they are all low bytes */
1607 buffer = SvPV_force(bufsv, blen);
1608 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1611 DIE(aTHX_ "Negative length");
1619 if (PL_op->op_type == OP_RECV) {
1620 char namebuf[MAXPATHLEN];
1621 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1622 bufsize = sizeof (struct sockaddr_in);
1624 bufsize = sizeof namebuf;
1626 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1630 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1631 /* 'offset' means 'flags' here */
1632 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1633 (struct sockaddr *)namebuf, &bufsize);
1637 /* Bogus return without padding */
1638 bufsize = sizeof (struct sockaddr_in);
1640 SvCUR_set(bufsv, count);
1641 *SvEND(bufsv) = '\0';
1642 (void)SvPOK_only(bufsv);
1646 /* This should not be marked tainted if the fp is marked clean */
1647 if (!(IoFLAGS(io) & IOf_UNTAINT))
1648 SvTAINTED_on(bufsv);
1650 sv_setpvn(TARG, namebuf, bufsize);
1655 if (PL_op->op_type == OP_RECV)
1656 DIE(aTHX_ PL_no_sock_func, "recv");
1658 if (DO_UTF8(bufsv)) {
1659 /* offset adjust in characters not bytes */
1660 blen = sv_len_utf8(bufsv);
1663 if (-offset > (int)blen)
1664 DIE(aTHX_ "Offset outside string");
1667 if (DO_UTF8(bufsv)) {
1668 /* convert offset-as-chars to offset-as-bytes */
1669 if (offset >= (int)blen)
1670 offset += SvCUR(bufsv) - blen;
1672 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1675 bufsize = SvCUR(bufsv);
1676 /* Allocating length + offset + 1 isn't perfect in the case of reading
1677 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1679 (should be 2 * length + offset + 1, or possibly something longer if
1680 PL_encoding is true) */
1681 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1682 if (offset > bufsize) { /* Zero any newly allocated space */
1683 Zero(buffer+bufsize, offset-bufsize, char);
1685 buffer = buffer + offset;
1687 read_target = bufsv;
1689 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1690 concatenate it to the current buffer. */
1692 /* Truncate the existing buffer to the start of where we will be
1694 SvCUR_set(bufsv, offset);
1696 read_target = sv_newmortal();
1697 SvUPGRADE(read_target, SVt_PV);
1698 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1701 if (PL_op->op_type == OP_SYSREAD) {
1702 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1703 if (IoTYPE(io) == IoTYPE_SOCKET) {
1704 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1710 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1715 #ifdef HAS_SOCKET__bad_code_maybe
1716 if (IoTYPE(io) == IoTYPE_SOCKET) {
1717 char namebuf[MAXPATHLEN];
1718 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1719 bufsize = sizeof (struct sockaddr_in);
1721 bufsize = sizeof namebuf;
1723 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1724 (struct sockaddr *)namebuf, &bufsize);
1729 count = PerlIO_read(IoIFP(io), buffer, length);
1730 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1731 if (count == 0 && PerlIO_error(IoIFP(io)))
1735 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1736 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1739 SvCUR_set(read_target, count+(buffer - SvPVX(read_target)));
1740 *SvEND(read_target) = '\0';
1741 (void)SvPOK_only(read_target);
1742 if (fp_utf8 && !IN_BYTES) {
1743 /* Look at utf8 we got back and count the characters */
1744 char *bend = buffer + count;
1745 while (buffer < bend) {
1747 skip = UTF8SKIP(buffer);
1750 if (buffer - charskip + skip > bend) {
1751 /* partial character - try for rest of it */
1752 length = skip - (bend-buffer);
1753 offset = bend - SvPVX(bufsv);
1765 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1766 provided amount read (count) was what was requested (length)
1768 if (got < wanted && count == length) {
1769 length = wanted - got;
1770 offset = bend - SvPVX(bufsv);
1773 /* return value is character count */
1777 else if (buffer_utf8) {
1778 /* Let svcatsv upgrade the bytes we read in to utf8.
1779 The buffer is a mortal so will be freed soon. */
1780 sv_catsv_nomg(bufsv, read_target);
1783 /* This should not be marked tainted if the fp is marked clean */
1784 if (!(IoFLAGS(io) & IOf_UNTAINT))
1785 SvTAINTED_on(bufsv);
1798 int items = (SP - PL_stack_base) - TOPMARK;
1802 sv = sv_2mortal(newSViv(sv_len(*SP)));
1811 dSP; dMARK; dORIGMARK; dTARGET;
1822 if (PL_op->op_type == OP_SYSWRITE
1823 && gv && (io = GvIO(gv))
1824 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1829 *MARK = SvTIED_obj((SV*)io, mg);
1831 call_method("WRITE", G_SCALAR);
1842 #if Size_t_size > IVSIZE
1843 length = (Size_t)SvNVx(*++MARK);
1845 length = (Size_t)SvIVx(*++MARK);
1847 if ((SSize_t)length < 0)
1848 DIE(aTHX_ "Negative length");
1851 if (!io || !IoIFP(io)) {
1853 if (ckWARN(WARN_CLOSED))
1854 report_evil_fh(gv, io, PL_op->op_type);
1855 SETERRNO(EBADF,RMS_IFI);
1859 if (PerlIO_isutf8(IoIFP(io))) {
1860 if (!SvUTF8(bufsv)) {
1861 bufsv = sv_2mortal(newSVsv(bufsv));
1862 buffer = sv_2pvutf8(bufsv, &blen);
1864 buffer = SvPV(bufsv, blen);
1867 if (DO_UTF8(bufsv)) {
1868 /* Not modifying source SV, so making a temporary copy. */
1869 bufsv = sv_2mortal(newSVsv(bufsv));
1870 sv_utf8_downgrade(bufsv, FALSE);
1872 buffer = SvPV(bufsv, blen);
1875 if (PL_op->op_type == OP_SYSWRITE) {
1877 if (DO_UTF8(bufsv)) {
1878 /* length and offset are in chars */
1879 blen = sv_len_utf8(bufsv);
1882 offset = SvIVx(*++MARK);
1884 if (-offset > (IV)blen)
1885 DIE(aTHX_ "Offset outside string");
1887 } else if (offset >= (IV)blen && blen > 0)
1888 DIE(aTHX_ "Offset outside string");
1891 if (length > blen - offset)
1892 length = blen - offset;
1893 if (DO_UTF8(bufsv)) {
1894 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1895 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1898 buffer = buffer+offset;
1900 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1901 if (IoTYPE(io) == IoTYPE_SOCKET) {
1902 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1908 /* See the note at doio.c:do_print about filesize limits. --jhi */
1909 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1914 else if (SP > MARK) {
1917 sockbuf = SvPVx(*++MARK, mlen);
1918 /* length is really flags */
1919 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1920 length, (struct sockaddr *)sockbuf, mlen);
1923 /* length is really flags */
1924 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1927 DIE(aTHX_ PL_no_sock_func, "send");
1933 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1934 #if Size_t_size > IVSIZE
1948 return pp_sysread();
1959 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1961 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1963 if (io && !IoIFP(io)) {
1964 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1966 IoFLAGS(io) &= ~IOf_START;
1967 do_open(gv, (char *)"-", 1, FALSE, O_RDONLY, 0, Nullfp);
1968 sv_setpvn(GvSV(gv), "-", 1);
1969 SvSETMAGIC(GvSV(gv));
1971 else if (!nextargv(gv))
1976 gv = PL_last_in_gv; /* eof */
1979 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1981 if (gv && (io = GvIO(gv))
1982 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1985 XPUSHs(SvTIED_obj((SV*)io, mg));
1988 call_method("EOF", G_SCALAR);
1994 PUSHs(boolSV(!gv || do_eof(gv)));
2008 gv = PL_last_in_gv = (GV*)POPs;
2010 if (gv && (io = GvIO(gv))
2011 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2014 XPUSHs(SvTIED_obj((SV*)io, mg));
2017 call_method("TELL", G_SCALAR);
2023 #if LSEEKSIZE > IVSIZE
2024 PUSHn( do_tell(gv) );
2026 PUSHi( do_tell(gv) );
2033 return pp_sysseek();
2042 #if LSEEKSIZE > IVSIZE
2043 Off_t offset = (Off_t)SvNVx(POPs);
2045 Off_t offset = (Off_t)SvIVx(POPs);
2049 gv = PL_last_in_gv = (GV*)POPs;
2051 if (gv && (io = GvIO(gv))
2052 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2055 XPUSHs(SvTIED_obj((SV*)io, mg));
2056 #if LSEEKSIZE > IVSIZE
2057 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2059 XPUSHs(sv_2mortal(newSViv(offset)));
2061 XPUSHs(sv_2mortal(newSViv(whence)));
2064 call_method("SEEK", G_SCALAR);
2070 if (PL_op->op_type == OP_SEEK)
2071 PUSHs(boolSV(do_seek(gv, offset, whence)));
2073 Off_t sought = do_sysseek(gv, offset, whence);
2075 PUSHs(&PL_sv_undef);
2078 #if LSEEKSIZE > IVSIZE
2083 : newSVpvn(zero_but_true, ZBTLEN);
2084 PUSHs(sv_2mortal(sv));
2093 /* There seems to be no consensus on the length type of truncate()
2094 * and ftruncate(), both off_t and size_t have supporters. In
2095 * general one would think that when using large files, off_t is
2096 * at least as wide as size_t, so using an off_t should be okay. */
2097 /* XXX Configure probe for the length type of *truncate() needed XXX */
2100 #if Off_t_size > IVSIZE
2105 /* Checking for length < 0 is problematic as the type might or
2106 * might not be signed: if it is not, clever compilers will moan. */
2107 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2114 if (PL_op->op_flags & OPf_SPECIAL) {
2115 tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
2124 TAINT_PROPER("truncate");
2125 if (!(fp = IoIFP(io))) {
2131 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2133 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2144 if (SvTYPE(sv) == SVt_PVGV) {
2145 tmpgv = (GV*)sv; /* *main::FRED for example */
2146 goto do_ftruncate_gv;
2148 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2149 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2150 goto do_ftruncate_gv;
2152 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2153 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2154 goto do_ftruncate_io;
2157 name = SvPV(sv, n_a);
2158 TAINT_PROPER("truncate");
2160 if (truncate(name, len) < 0)
2166 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2169 if (my_chsize(tmpfd, len) < 0)
2171 PerlLIO_close(tmpfd);
2180 SETERRNO(EBADF,RMS_IFI);
2194 unsigned int func = POPu;
2195 const int optype = PL_op->op_type;
2199 IO *io = gv ? GvIOn(gv) : 0;
2201 if (!io || !argsv || !IoIFP(io)) {
2202 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2203 report_evil_fh(gv, io, PL_op->op_type);
2204 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2208 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2211 s = SvPV_force(argsv, len);
2212 need = IOCPARM_LEN(func);
2214 s = Sv_Grow(argsv, need + 1);
2215 SvCUR_set(argsv, need);
2218 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2221 retval = SvIV(argsv);
2222 s = INT2PTR(char*,retval); /* ouch */
2225 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2227 if (optype == OP_IOCTL)
2229 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2231 DIE(aTHX_ "ioctl is not implemented");
2235 DIE(aTHX_ "fcntl is not implemented");
2237 #if defined(OS2) && defined(__EMX__)
2238 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2240 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2244 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2246 if (s[SvCUR(argsv)] != 17)
2247 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2249 s[SvCUR(argsv)] = 0; /* put our null back */
2250 SvSETMAGIC(argsv); /* Assume it has changed */
2259 PUSHp(zero_but_true, ZBTLEN);
2280 if (gv && (io = GvIO(gv)))
2287 (void)PerlIO_flush(fp);
2288 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2291 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2292 report_evil_fh(gv, io, PL_op->op_type);
2294 SETERRNO(EBADF,RMS_IFI);
2299 DIE(aTHX_ PL_no_func, "flock()");
2311 int protocol = POPi;
2317 io = gv ? GvIOn(gv) : NULL;
2320 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2321 report_evil_fh(gv, io, PL_op->op_type);
2323 do_close(gv, FALSE);
2324 SETERRNO(EBADF,LIB_INVARG);
2329 do_close(gv, FALSE);
2331 TAINT_PROPER("socket");
2332 fd = PerlSock_socket(domain, type, protocol);
2335 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2336 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2337 IoTYPE(io) = IoTYPE_SOCKET;
2338 if (!IoIFP(io) || !IoOFP(io)) {
2339 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2340 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2341 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2344 #if defined(HAS_FCNTL) && defined(F_SETFD)
2345 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2349 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2354 DIE(aTHX_ PL_no_sock_func, "socket");
2360 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2366 int protocol = POPi;
2373 io1 = gv1 ? GvIOn(gv1) : NULL;
2374 io2 = gv2 ? GvIOn(gv2) : NULL;
2375 if (!gv1 || !gv2 || !io1 || !io2) {
2376 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2378 report_evil_fh(gv1, io1, PL_op->op_type);
2380 report_evil_fh(gv1, io2, PL_op->op_type);
2383 do_close(gv1, FALSE);
2385 do_close(gv2, FALSE);
2390 do_close(gv1, FALSE);
2392 do_close(gv2, FALSE);
2394 TAINT_PROPER("socketpair");
2395 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2397 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2398 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2399 IoTYPE(io1) = IoTYPE_SOCKET;
2400 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2401 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2402 IoTYPE(io2) = IoTYPE_SOCKET;
2403 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2404 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2405 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2406 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2407 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2408 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2409 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2412 #if defined(HAS_FCNTL) && defined(F_SETFD)
2413 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2414 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2419 DIE(aTHX_ PL_no_sock_func, "socketpair");
2427 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2428 extern void GETPRIVMODE();
2429 extern void GETUSERMODE();
2434 register IO *io = GvIOn(gv);
2438 int mpeprivmode = 0;
2441 if (!io || !IoIFP(io))
2444 addr = SvPV(addrsv, len);
2445 TAINT_PROPER("bind");
2446 #ifdef MPE /* Deal with MPE bind() peculiarities */
2447 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2448 /* The address *MUST* stupidly be zero. */
2449 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2450 /* PRIV mode is required to bind() to ports < 1024. */
2451 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2452 ((struct sockaddr_in *)addr)->sin_port > 0) {
2453 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2458 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2459 (struct sockaddr *)addr, len) >= 0)
2462 #ifdef MPE /* Switch back to USER mode */
2473 if (ckWARN(WARN_CLOSED))
2474 report_evil_fh(gv, io, PL_op->op_type);
2475 SETERRNO(EBADF,SS_IVCHAN);
2478 DIE(aTHX_ PL_no_sock_func, "bind");
2489 register IO *io = GvIOn(gv);
2492 if (!io || !IoIFP(io))
2495 addr = SvPV(addrsv, len);
2496 TAINT_PROPER("connect");
2497 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2503 if (ckWARN(WARN_CLOSED))
2504 report_evil_fh(gv, io, PL_op->op_type);
2505 SETERRNO(EBADF,SS_IVCHAN);
2508 DIE(aTHX_ PL_no_sock_func, "connect");
2518 register IO *io = gv ? GvIOn(gv) : NULL;
2520 if (!gv || !io || !IoIFP(io))
2523 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2529 if (ckWARN(WARN_CLOSED))
2530 report_evil_fh(gv, io, PL_op->op_type);
2531 SETERRNO(EBADF,SS_IVCHAN);
2534 DIE(aTHX_ PL_no_sock_func, "listen");
2546 char namebuf[MAXPATHLEN];
2547 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2548 Sock_size_t len = sizeof (struct sockaddr_in);
2550 Sock_size_t len = sizeof namebuf;
2563 if (!gstio || !IoIFP(gstio))
2567 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2571 do_close(ngv, FALSE);
2572 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2573 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2574 IoTYPE(nstio) = IoTYPE_SOCKET;
2575 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2576 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2577 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2578 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2581 #if defined(HAS_FCNTL) && defined(F_SETFD)
2582 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2586 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2587 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2589 #ifdef __SCO_VERSION__
2590 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2593 PUSHp(namebuf, len);
2597 if (ckWARN(WARN_CLOSED))
2598 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2599 SETERRNO(EBADF,SS_IVCHAN);
2605 DIE(aTHX_ PL_no_sock_func, "accept");
2615 register IO *io = GvIOn(gv);
2617 if (!io || !IoIFP(io))
2620 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2624 if (ckWARN(WARN_CLOSED))
2625 report_evil_fh(gv, io, PL_op->op_type);
2626 SETERRNO(EBADF,SS_IVCHAN);
2629 DIE(aTHX_ PL_no_sock_func, "shutdown");
2636 return pp_ssockopt();
2638 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2646 int optype = PL_op->op_type;
2649 unsigned int optname;
2655 if (optype == OP_GSOCKOPT)
2656 sv = sv_2mortal(NEWSV(22, 257));
2659 optname = (unsigned int) POPi;
2660 lvl = (unsigned int) POPi;
2664 if (!io || !IoIFP(io))
2667 fd = PerlIO_fileno(IoIFP(io));
2671 (void)SvPOK_only(sv);
2675 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2690 aint = (int)SvIV(sv);
2694 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2703 if (ckWARN(WARN_CLOSED))
2704 report_evil_fh(gv, io, optype);
2705 SETERRNO(EBADF,SS_IVCHAN);
2710 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2717 return pp_getpeername();
2719 DIE(aTHX_ PL_no_sock_func, "getsockname");
2727 int optype = PL_op->op_type;
2731 register IO *io = GvIOn(gv);
2734 if (!io || !IoIFP(io))
2737 sv = sv_2mortal(NEWSV(22, 257));
2738 (void)SvPOK_only(sv);
2742 fd = PerlIO_fileno(IoIFP(io));
2744 case OP_GETSOCKNAME:
2745 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2748 case OP_GETPEERNAME:
2749 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2751 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2753 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";
2754 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2755 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2756 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2757 sizeof(u_short) + sizeof(struct in_addr))) {
2764 #ifdef BOGUS_GETNAME_RETURN
2765 /* Interactive Unix, getpeername() and getsockname()
2766 does not return valid namelen */
2767 if (len == BOGUS_GETNAME_RETURN)
2768 len = sizeof(struct sockaddr);
2776 if (ckWARN(WARN_CLOSED))
2777 report_evil_fh(gv, io, optype);
2778 SETERRNO(EBADF,SS_IVCHAN);
2783 DIE(aTHX_ PL_no_sock_func, "getpeername");
2802 if (PL_op->op_flags & OPf_REF) {
2804 if (PL_op->op_type == OP_LSTAT) {
2805 if (gv != PL_defgv) {
2806 if (ckWARN(WARN_IO))
2807 Perl_warner(aTHX_ packWARN(WARN_IO),
2808 "lstat() on filehandle %s", GvENAME(gv));
2809 } else if (PL_laststype != OP_LSTAT)
2810 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2814 if (gv != PL_defgv) {
2815 PL_laststype = OP_STAT;
2817 sv_setpv(PL_statname, "");
2818 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2819 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2821 if (PL_laststatval < 0) {
2822 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2823 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2829 if (SvTYPE(sv) == SVt_PVGV) {
2833 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2835 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2836 Perl_warner(aTHX_ packWARN(WARN_IO),
2837 "lstat() on filehandle %s", GvENAME(gv));
2840 sv_setpv(PL_statname, SvPV(sv,n_a));
2842 PL_laststype = PL_op->op_type;
2843 if (PL_op->op_type == OP_LSTAT)
2844 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2846 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2847 if (PL_laststatval < 0) {
2848 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2849 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2855 if (gimme != G_ARRAY) {
2856 if (gimme != G_VOID)
2857 XPUSHs(boolSV(max));
2863 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2864 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2865 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2866 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2867 #if Uid_t_size > IVSIZE
2868 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2870 # if Uid_t_sign <= 0
2871 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2873 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2876 #if Gid_t_size > IVSIZE
2877 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2879 # if Gid_t_sign <= 0
2880 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2882 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2885 #ifdef USE_STAT_RDEV
2886 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2888 PUSHs(sv_2mortal(newSVpvn("", 0)));
2890 #if Off_t_size > IVSIZE
2891 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2893 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2896 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2897 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2898 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2900 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2901 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2902 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2904 #ifdef USE_STAT_BLOCKS
2905 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2906 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2908 PUSHs(sv_2mortal(newSVpvn("", 0)));
2909 PUSHs(sv_2mortal(newSVpvn("", 0)));
2915 /* This macro is used by the stacked filetest operators :
2916 * if the previous filetest failed, short-circuit and pass its value.
2917 * Else, discard it from the stack and continue. --rgs
2919 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2920 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2921 else { (void)POPs; PUTBACK; } \
2928 STACKED_FTEST_CHECK;
2929 #if defined(HAS_ACCESS) && defined(R_OK)
2930 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2932 result = access(POPpx, R_OK);
2947 if (cando(S_IRUSR, 0, &PL_statcache))
2956 STACKED_FTEST_CHECK;
2957 #if defined(HAS_ACCESS) && defined(W_OK)
2958 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2960 result = access(POPpx, W_OK);
2975 if (cando(S_IWUSR, 0, &PL_statcache))
2984 STACKED_FTEST_CHECK;
2985 #if defined(HAS_ACCESS) && defined(X_OK)
2986 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2988 result = access(POPpx, X_OK);
3003 if (cando(S_IXUSR, 0, &PL_statcache))
3012 STACKED_FTEST_CHECK;
3013 #ifdef PERL_EFF_ACCESS_R_OK
3014 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3016 result = PERL_EFF_ACCESS_R_OK(POPpx);
3031 if (cando(S_IRUSR, 1, &PL_statcache))
3040 STACKED_FTEST_CHECK;
3041 #ifdef PERL_EFF_ACCESS_W_OK
3042 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3044 result = PERL_EFF_ACCESS_W_OK(POPpx);
3059 if (cando(S_IWUSR, 1, &PL_statcache))
3068 STACKED_FTEST_CHECK;
3069 #ifdef PERL_EFF_ACCESS_X_OK
3070 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3072 result = PERL_EFF_ACCESS_X_OK(POPpx);
3087 if (cando(S_IXUSR, 1, &PL_statcache))
3096 STACKED_FTEST_CHECK;
3106 return pp_ftrowned();
3113 STACKED_FTEST_CHECK;
3118 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3128 STACKED_FTEST_CHECK;
3133 if (PL_statcache.st_size == 0)
3142 STACKED_FTEST_CHECK;
3147 #if Off_t_size > IVSIZE
3148 PUSHn(PL_statcache.st_size);
3150 PUSHi(PL_statcache.st_size);
3159 STACKED_FTEST_CHECK;
3164 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3172 STACKED_FTEST_CHECK;
3177 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3185 STACKED_FTEST_CHECK;
3190 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3198 STACKED_FTEST_CHECK;
3203 if (S_ISSOCK(PL_statcache.st_mode))
3212 STACKED_FTEST_CHECK;
3217 if (S_ISCHR(PL_statcache.st_mode))
3226 STACKED_FTEST_CHECK;
3231 if (S_ISBLK(PL_statcache.st_mode))
3240 STACKED_FTEST_CHECK;
3245 if (S_ISREG(PL_statcache.st_mode))
3254 STACKED_FTEST_CHECK;
3259 if (S_ISDIR(PL_statcache.st_mode))
3268 STACKED_FTEST_CHECK;
3273 if (S_ISFIFO(PL_statcache.st_mode))
3280 I32 result = my_lstat();
3284 if (S_ISLNK(PL_statcache.st_mode))
3294 STACKED_FTEST_CHECK;
3299 if (PL_statcache.st_mode & S_ISUID)
3310 STACKED_FTEST_CHECK;
3315 if (PL_statcache.st_mode & S_ISGID)
3326 STACKED_FTEST_CHECK;
3331 if (PL_statcache.st_mode & S_ISVTX)
3344 STACKED_FTEST_CHECK;
3346 if (PL_op->op_flags & OPf_REF)
3348 else if (isGV(TOPs))
3350 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3351 gv = (GV*)SvRV(POPs);
3353 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
3355 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3356 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3357 else if (tmpsv && SvOK(tmpsv)) {
3359 char *tmps = SvPV(tmpsv, n_a);
3367 if (PerlLIO_isatty(fd))
3372 #if defined(atarist) /* this will work with atariST. Configure will
3373 make guesses for other systems. */
3374 # define FILE_base(f) ((f)->_base)
3375 # define FILE_ptr(f) ((f)->_ptr)
3376 # define FILE_cnt(f) ((f)->_cnt)
3377 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3387 register STDCHAR *s;
3394 STACKED_FTEST_CHECK;
3396 if (PL_op->op_flags & OPf_REF)
3398 else if (isGV(TOPs))
3400 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3401 gv = (GV*)SvRV(POPs);
3407 if (gv == PL_defgv) {
3409 io = GvIO(PL_statgv);
3412 goto really_filename;
3417 PL_laststatval = -1;
3418 sv_setpv(PL_statname, "");
3419 io = GvIO(PL_statgv);
3421 if (io && IoIFP(io)) {
3422 if (! PerlIO_has_base(IoIFP(io)))
3423 DIE(aTHX_ "-T and -B not implemented on filehandles");
3424 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3425 if (PL_laststatval < 0)
3427 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3428 if (PL_op->op_type == OP_FTTEXT)
3433 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3434 i = PerlIO_getc(IoIFP(io));
3436 (void)PerlIO_ungetc(IoIFP(io),i);
3438 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3440 len = PerlIO_get_bufsiz(IoIFP(io));
3441 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3442 /* sfio can have large buffers - limit to 512 */
3447 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3449 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3451 SETERRNO(EBADF,RMS_IFI);
3459 PL_laststype = OP_STAT;
3460 sv_setpv(PL_statname, SvPV(sv, n_a));
3461 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3462 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
3463 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3466 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3467 if (PL_laststatval < 0) {
3468 (void)PerlIO_close(fp);
3471 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3472 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3473 (void)PerlIO_close(fp);
3475 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3476 RETPUSHNO; /* special case NFS directories */
3477 RETPUSHYES; /* null file is anything */
3482 /* now scan s to look for textiness */
3483 /* XXX ASCII dependent code */
3485 #if defined(DOSISH) || defined(USEMYBINMODE)
3486 /* ignore trailing ^Z on short files */
3487 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3491 for (i = 0; i < len; i++, s++) {
3492 if (!*s) { /* null never allowed in text */
3497 else if (!(isPRINT(*s) || isSPACE(*s)))
3500 else if (*s & 128) {
3502 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3505 /* utf8 characters don't count as odd */
3506 if (UTF8_IS_START(*s)) {
3507 int ulen = UTF8SKIP(s);
3508 if (ulen < len - i) {
3510 for (j = 1; j < ulen; j++) {
3511 if (!UTF8_IS_CONTINUATION(s[j]))
3514 --ulen; /* loop does extra increment */
3524 *s != '\n' && *s != '\r' && *s != '\b' &&
3525 *s != '\t' && *s != '\f' && *s != 27)
3530 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3555 if( !tmps || !*tmps ) {
3556 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3557 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3559 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3564 deprecate("chdir('') or chdir(undef) as chdir()");
3565 tmps = SvPV(*svp, n_a);
3569 TAINT_PROPER("chdir");
3574 TAINT_PROPER("chdir");
3575 PUSHi( PerlDir_chdir(tmps) >= 0 );
3577 /* Clear the DEFAULT element of ENV so we'll get the new value
3579 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3587 dSP; dMARK; dTARGET;
3588 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3594 DIE(aTHX_ PL_no_func, "chown");
3604 TAINT_PROPER("chroot");
3605 PUSHi( chroot(tmps) >= 0 );
3608 DIE(aTHX_ PL_no_func, "chroot");
3614 dSP; dMARK; dTARGET;
3616 value = (I32)apply(PL_op->op_type, MARK, SP);
3624 dSP; dMARK; dTARGET;
3626 value = (I32)apply(PL_op->op_type, MARK, SP);
3634 dSP; dMARK; dTARGET;
3636 value = (I32)apply(PL_op->op_type, MARK, SP);
3648 char *tmps2 = POPpx;
3649 char *tmps = SvPV(TOPs, n_a);
3650 TAINT_PROPER("rename");
3652 anum = PerlLIO_rename(tmps, tmps2);
3654 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3655 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3658 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3659 (void)UNLINK(tmps2);
3660 if (!(anum = link(tmps, tmps2)))
3661 anum = UNLINK(tmps);
3674 char *tmps2 = POPpx;
3675 char *tmps = SvPV(TOPs, n_a);
3676 TAINT_PROPER("link");
3677 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3680 DIE(aTHX_ PL_no_func, "link");
3689 char *tmps2 = POPpx;
3690 char *tmps = SvPV(TOPs, n_a);
3691 TAINT_PROPER("symlink");
3692 SETi( symlink(tmps, tmps2) >= 0 );
3695 DIE(aTHX_ PL_no_func, "symlink");
3705 char buf[MAXPATHLEN];
3709 #ifndef INCOMPLETE_TAINTS
3713 len = readlink(tmps, buf, sizeof(buf) - 1);
3721 RETSETUNDEF; /* just pretend it's a normal file */
3725 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3727 S_dooneliner(pTHX_ char *cmd, char *filename)
3729 char *save_filename = filename;
3735 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3736 strcpy(cmdline, cmd);
3737 strcat(cmdline, " ");
3738 for (s = cmdline + strlen(cmdline); *filename; ) {
3743 myfp = PerlProc_popen(cmdline, "r");
3747 SV *tmpsv = sv_newmortal();
3748 /* Need to save/restore 'PL_rs' ?? */
3749 s = sv_gets(tmpsv, myfp, 0);
3750 (void)PerlProc_pclose(myfp);
3754 #ifdef HAS_SYS_ERRLIST
3759 /* you don't see this */
3761 #ifdef HAS_SYS_ERRLIST
3769 if (instr(s, errmsg)) {
3776 #define EACCES EPERM
3778 if (instr(s, "cannot make"))
3779 SETERRNO(EEXIST,RMS_FEX);
3780 else if (instr(s, "existing file"))
3781 SETERRNO(EEXIST,RMS_FEX);
3782 else if (instr(s, "ile exists"))
3783 SETERRNO(EEXIST,RMS_FEX);
3784 else if (instr(s, "non-exist"))
3785 SETERRNO(ENOENT,RMS_FNF);
3786 else if (instr(s, "does not exist"))
3787 SETERRNO(ENOENT,RMS_FNF);
3788 else if (instr(s, "not empty"))
3789 SETERRNO(EBUSY,SS_DEVOFFLINE);
3790 else if (instr(s, "cannot access"))
3791 SETERRNO(EACCES,RMS_PRV);
3793 SETERRNO(EPERM,RMS_PRV);
3796 else { /* some mkdirs return no failure indication */
3797 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3798 if (PL_op->op_type == OP_RMDIR)
3803 SETERRNO(EACCES,RMS_PRV); /* a guess */
3812 /* This macro removes trailing slashes from a directory name.
3813 * Different operating and file systems take differently to
3814 * trailing slashes. According to POSIX 1003.1 1996 Edition
3815 * any number of trailing slashes should be allowed.
3816 * Thusly we snip them away so that even non-conforming
3817 * systems are happy.
3818 * We should probably do this "filtering" for all
3819 * the functions that expect (potentially) directory names:
3820 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3821 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3823 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3824 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3827 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3828 (tmps) = savepvn((tmps), (len)); \
3848 TRIMSLASHES(tmps,len,copy);
3850 TAINT_PROPER("mkdir");
3852 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3854 SETi( dooneliner("mkdir", tmps) );
3855 oldumask = PerlLIO_umask(0);
3856 PerlLIO_umask(oldumask);
3857 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3871 TRIMSLASHES(tmps,len,copy);
3872 TAINT_PROPER("rmdir");
3874 SETi( PerlDir_rmdir(tmps) >= 0 );
3876 SETi( dooneliner("rmdir", tmps) );
3883 /* Directory calls. */
3887 #if defined(Direntry_t) && defined(HAS_READDIR)
3890 char *dirname = POPpx;
3892 register IO *io = GvIOn(gv);
3898 PerlDir_close(IoDIRP(io));
3899 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3905 SETERRNO(EBADF,RMS_DIR);
3908 DIE(aTHX_ PL_no_dir_func, "opendir");
3914 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3915 DIE(aTHX_ PL_no_dir_func, "readdir");
3917 #if !defined(I_DIRENT) && !defined(VMS)
3918 Direntry_t *readdir (DIR *);
3924 GV *gv = (GV *)POPs;
3925 register Direntry_t *dp;
3926 register IO *io = GvIOn(gv);
3928 if (!io || !IoDIRP(io))
3932 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3936 sv = newSVpvn(dp->d_name, dp->d_namlen);
3938 sv = newSVpv(dp->d_name, 0);
3940 #ifndef INCOMPLETE_TAINTS
3941 if (!(IoFLAGS(io) & IOf_UNTAINT))
3944 XPUSHs(sv_2mortal(sv));
3946 while (gimme == G_ARRAY);
3948 if (!dp && gimme != G_ARRAY)
3955 SETERRNO(EBADF,RMS_ISI);
3956 if (GIMME == G_ARRAY)
3965 #if defined(HAS_TELLDIR) || defined(telldir)
3967 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3968 /* XXX netbsd still seemed to.
3969 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3970 --JHI 1999-Feb-02 */
3971 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3972 long telldir (DIR *);
3975 register IO *io = GvIOn(gv);
3977 if (!io || !IoDIRP(io))
3980 PUSHi( PerlDir_tell(IoDIRP(io)) );
3984 SETERRNO(EBADF,RMS_ISI);
3987 DIE(aTHX_ PL_no_dir_func, "telldir");
3993 #if defined(HAS_SEEKDIR) || defined(seekdir)
3997 register IO *io = GvIOn(gv);
3999 if (!io || !IoDIRP(io))
4002 (void)PerlDir_seek(IoDIRP(io), along);
4007 SETERRNO(EBADF,RMS_ISI);
4010 DIE(aTHX_ PL_no_dir_func, "seekdir");
4016 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4019 register IO *io = GvIOn(gv);
4021 if (!io || !IoDIRP(io))
4024 (void)PerlDir_rewind(IoDIRP(io));
4028 SETERRNO(EBADF,RMS_ISI);
4031 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4037 #if defined(Direntry_t) && defined(HAS_READDIR)
4040 register IO *io = GvIOn(gv);
4042 if (!io || !IoDIRP(io))
4045 #ifdef VOID_CLOSEDIR
4046 PerlDir_close(IoDIRP(io));
4048 if (PerlDir_close(IoDIRP(io)) < 0) {
4049 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4058 SETERRNO(EBADF,RMS_IFI);
4061 DIE(aTHX_ PL_no_dir_func, "closedir");
4065 /* Process control. */
4075 PERL_FLUSHALL_FOR_CHILD;
4076 childpid = PerlProc_fork();
4081 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
4082 SvREADONLY_off(GvSV(tmpgv));
4083 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4084 SvREADONLY_on(GvSV(tmpgv));
4086 #ifdef THREADS_HAVE_PIDS
4087 PL_ppid = (IV)getppid();
4089 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4094 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4099 PERL_FLUSHALL_FOR_CHILD;
4100 childpid = PerlProc_fork();
4106 DIE(aTHX_ PL_no_func, "fork");
4113 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4118 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4119 childpid = wait4pid(-1, &argflags, 0);
4121 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4126 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4127 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4128 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4130 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4135 DIE(aTHX_ PL_no_func, "wait");
4141 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4150 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4151 result = wait4pid(pid, &argflags, optype);
4153 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4158 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4159 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4160 STATUS_NATIVE_SET((result && result != -1) ? argflags : -1);
4162 STATUS_NATIVE_SET((result > 0) ? argflags : -1);
4167 DIE(aTHX_ PL_no_func, "waitpid");
4173 dSP; dMARK; dORIGMARK; dTARGET;
4181 while (++MARK <= SP) {
4182 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4187 TAINT_PROPER("system");
4189 PERL_FLUSHALL_FOR_CHILD;
4190 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4195 if (PerlProc_pipe(pp) >= 0)
4197 while ((childpid = PerlProc_fork()) == -1) {
4198 if (errno != EAGAIN) {
4203 PerlLIO_close(pp[0]);
4204 PerlLIO_close(pp[1]);
4211 Sigsave_t ihand,qhand; /* place to save signals during system() */
4215 PerlLIO_close(pp[1]);
4217 rsignal_save(SIGINT, SIG_IGN, &ihand);
4218 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4221 result = wait4pid(childpid, &status, 0);
4222 } while (result == -1 && errno == EINTR);
4224 (void)rsignal_restore(SIGINT, &ihand);
4225 (void)rsignal_restore(SIGQUIT, &qhand);
4227 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4228 do_execfree(); /* free any memory child malloced on fork */
4234 while (n < sizeof(int)) {
4235 n1 = PerlLIO_read(pp[0],
4236 (void*)(((char*)&errkid)+n),
4242 PerlLIO_close(pp[0]);
4243 if (n) { /* Error */
4244 if (n != sizeof(int))
4245 DIE(aTHX_ "panic: kid popen errno read");
4246 errno = errkid; /* Propagate errno from kid */
4247 STATUS_CURRENT = -1;
4250 PUSHi(STATUS_CURRENT);
4254 PerlLIO_close(pp[0]);
4255 #if defined(HAS_FCNTL) && defined(F_SETFD)
4256 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4259 if (PL_op->op_flags & OPf_STACKED) {
4260 SV *really = *++MARK;
4261 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4263 else if (SP - MARK != 1)
4264 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4266 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4270 #else /* ! FORK or VMS or OS/2 */
4273 if (PL_op->op_flags & OPf_STACKED) {
4274 SV *really = *++MARK;
4275 # if defined(WIN32) || defined(OS2)
4276 value = (I32)do_aspawn(really, MARK, SP);
4278 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4281 else if (SP - MARK != 1) {
4282 # if defined(WIN32) || defined(OS2)
4283 value = (I32)do_aspawn(Nullsv, MARK, SP);
4285 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4289 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4291 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4293 STATUS_NATIVE_SET(value);
4296 PUSHi(result ? value : STATUS_CURRENT);
4297 #endif /* !FORK or VMS */
4303 dSP; dMARK; dORIGMARK; dTARGET;
4309 while (++MARK <= SP) {
4310 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4315 TAINT_PROPER("exec");
4317 PERL_FLUSHALL_FOR_CHILD;
4318 if (PL_op->op_flags & OPf_STACKED) {
4319 SV *really = *++MARK;
4320 value = (I32)do_aexec(really, MARK, SP);
4322 else if (SP - MARK != 1)
4324 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4328 (void ) do_aspawn(Nullsv, MARK, SP);
4332 value = (I32)do_aexec(Nullsv, MARK, SP);
4337 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4340 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4343 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4356 dSP; dMARK; dTARGET;
4358 value = (I32)apply(PL_op->op_type, MARK, SP);
4363 DIE(aTHX_ PL_no_func, "kill");
4371 # ifdef THREADS_HAVE_PIDS
4374 XPUSHi( getppid() );
4378 DIE(aTHX_ PL_no_func, "getppid");
4394 pgrp = (I32)BSD_GETPGRP(pid);
4396 if (pid != 0 && pid != PerlProc_getpid())
4397 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4403 DIE(aTHX_ PL_no_func, "getpgrp()");
4422 TAINT_PROPER("setpgrp");
4424 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4426 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4427 || (pid != 0 && pid != PerlProc_getpid()))
4429 DIE(aTHX_ "setpgrp can't take arguments");
4431 SETi( setpgrp() >= 0 );
4432 #endif /* USE_BSDPGRP */
4435 DIE(aTHX_ PL_no_func, "setpgrp()");
4441 #ifdef HAS_GETPRIORITY
4445 SETi( getpriority(which, who) );
4448 DIE(aTHX_ PL_no_func, "getpriority()");
4454 #ifdef HAS_SETPRIORITY
4459 TAINT_PROPER("setpriority");
4460 SETi( setpriority(which, who, niceval) >= 0 );
4463 DIE(aTHX_ PL_no_func, "setpriority()");
4473 XPUSHn( time(Null(Time_t*)) );
4475 XPUSHi( time(Null(Time_t*)) );
4486 (void)PerlProc_times(&PL_timesbuf);
4488 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4489 /* struct tms, though same data */
4493 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4494 if (GIMME == G_ARRAY) {
4495 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4496 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4497 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4503 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4505 if (GIMME == G_ARRAY) {
4506 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4507 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4508 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4512 DIE(aTHX_ "times not implemented");
4514 #endif /* HAS_TIMES */
4526 const struct tm *tmbuf;
4527 static const char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4528 static const char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4529 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4535 when = (Time_t)SvNVx(POPs);
4537 when = (Time_t)SvIVx(POPs);
4540 if (PL_op->op_type == OP_LOCALTIME)
4541 tmbuf = localtime(&when);
4543 tmbuf = gmtime(&when);
4545 if (GIMME != G_ARRAY) {
4551 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4552 dayname[tmbuf->tm_wday],
4553 monname[tmbuf->tm_mon],
4558 tmbuf->tm_year + 1900);
4559 PUSHs(sv_2mortal(tsv));
4564 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4565 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4566 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4567 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4568 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4569 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4570 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4571 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4572 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4583 anum = alarm((unsigned int)anum);
4590 DIE(aTHX_ PL_no_func, "alarm");
4601 (void)time(&lasttime);
4606 PerlProc_sleep((unsigned int)duration);
4609 XPUSHi(when - lasttime);
4613 /* Shared memory. */
4627 return pp_shmwrite();
4632 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4633 dSP; dMARK; dTARGET;
4634 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4643 /* Message passing. */
4657 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4658 dSP; dMARK; dTARGET;
4659 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4670 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4671 dSP; dMARK; dTARGET;
4672 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4685 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4686 dSP; dMARK; dTARGET;
4687 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4694 DIE(aTHX_ "System V IPC is not implemented on this machine");
4700 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4701 dSP; dMARK; dTARGET;
4702 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4710 PUSHp(zero_but_true, ZBTLEN);
4720 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4721 dSP; dMARK; dTARGET;
4722 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4731 /* Get system info. */
4735 #ifdef HAS_GETHOSTBYNAME
4736 return pp_ghostent();
4738 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4744 #ifdef HAS_GETHOSTBYADDR
4745 return pp_ghostent();
4747 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4753 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4755 I32 which = PL_op->op_type;
4756 register char **elem;
4758 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4759 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4760 struct hostent *gethostbyname(Netdb_name_t);
4761 struct hostent *gethostent(void);
4763 struct hostent *hent;
4768 if (which == OP_GHBYNAME) {
4769 #ifdef HAS_GETHOSTBYNAME
4770 char* name = POPpbytex;
4771 hent = PerlSock_gethostbyname(name);
4773 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4776 else if (which == OP_GHBYADDR) {
4777 #ifdef HAS_GETHOSTBYADDR
4778 int addrtype = POPi;
4781 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4783 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4785 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4789 #ifdef HAS_GETHOSTENT
4790 hent = PerlSock_gethostent();
4792 DIE(aTHX_ PL_no_sock_func, "gethostent");
4795 #ifdef HOST_NOT_FOUND
4797 #ifdef USE_REENTRANT_API
4798 # ifdef USE_GETHOSTENT_ERRNO
4799 h_errno = PL_reentrant_buffer->_gethostent_errno;
4802 STATUS_NATIVE_SET(h_errno);
4806 if (GIMME != G_ARRAY) {
4807 PUSHs(sv = sv_newmortal());
4809 if (which == OP_GHBYNAME) {
4811 sv_setpvn(sv, hent->h_addr, hent->h_length);
4814 sv_setpv(sv, (char*)hent->h_name);
4820 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4821 sv_setpv(sv, (char*)hent->h_name);
4822 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4823 for (elem = hent->h_aliases; elem && *elem; elem++) {
4824 sv_catpv(sv, *elem);
4826 sv_catpvn(sv, " ", 1);
4828 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4829 sv_setiv(sv, (IV)hent->h_addrtype);
4830 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4831 len = hent->h_length;
4832 sv_setiv(sv, (IV)len);
4834 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4835 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4836 sv_setpvn(sv, *elem, len);
4839 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4841 sv_setpvn(sv, hent->h_addr, len);
4846 DIE(aTHX_ PL_no_sock_func, "gethostent");
4852 #ifdef HAS_GETNETBYNAME
4853 return pp_gnetent();
4855 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4861 #ifdef HAS_GETNETBYADDR
4862 return pp_gnetent();
4864 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4870 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4872 I32 which = PL_op->op_type;
4873 register char **elem;
4875 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4876 struct netent *getnetbyaddr(Netdb_net_t, int);
4877 struct netent *getnetbyname(Netdb_name_t);
4878 struct netent *getnetent(void);
4880 struct netent *nent;
4883 if (which == OP_GNBYNAME){
4884 #ifdef HAS_GETNETBYNAME
4885 char *name = POPpbytex;
4886 nent = PerlSock_getnetbyname(name);
4888 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4891 else if (which == OP_GNBYADDR) {
4892 #ifdef HAS_GETNETBYADDR
4893 int addrtype = POPi;
4894 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4895 nent = PerlSock_getnetbyaddr(addr, addrtype);
4897 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4901 #ifdef HAS_GETNETENT
4902 nent = PerlSock_getnetent();
4904 DIE(aTHX_ PL_no_sock_func, "getnetent");
4907 #ifdef HOST_NOT_FOUND
4909 #ifdef USE_REENTRANT_API
4910 # ifdef USE_GETNETENT_ERRNO
4911 h_errno = PL_reentrant_buffer->_getnetent_errno;
4914 STATUS_NATIVE_SET(h_errno);
4919 if (GIMME != G_ARRAY) {
4920 PUSHs(sv = sv_newmortal());
4922 if (which == OP_GNBYNAME)
4923 sv_setiv(sv, (IV)nent->n_net);
4925 sv_setpv(sv, nent->n_name);
4931 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4932 sv_setpv(sv, nent->n_name);
4933 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4934 for (elem = nent->n_aliases; elem && *elem; elem++) {
4935 sv_catpv(sv, *elem);
4937 sv_catpvn(sv, " ", 1);
4939 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4940 sv_setiv(sv, (IV)nent->n_addrtype);
4941 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4942 sv_setiv(sv, (IV)nent->n_net);
4947 DIE(aTHX_ PL_no_sock_func, "getnetent");
4953 #ifdef HAS_GETPROTOBYNAME
4954 return pp_gprotoent();
4956 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4962 #ifdef HAS_GETPROTOBYNUMBER
4963 return pp_gprotoent();
4965 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4971 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4973 I32 which = PL_op->op_type;
4974 register char **elem;
4976 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4977 struct protoent *getprotobyname(Netdb_name_t);
4978 struct protoent *getprotobynumber(int);
4979 struct protoent *getprotoent(void);
4981 struct protoent *pent;
4984 if (which == OP_GPBYNAME) {
4985 #ifdef HAS_GETPROTOBYNAME
4986 char* name = POPpbytex;
4987 pent = PerlSock_getprotobyname(name);
4989 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4992 else if (which == OP_GPBYNUMBER) {
4993 #ifdef HAS_GETPROTOBYNUMBER
4995 pent = PerlSock_getprotobynumber(number);
4997 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
5001 #ifdef HAS_GETPROTOENT
5002 pent = PerlSock_getprotoent();
5004 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5008 if (GIMME != G_ARRAY) {
5009 PUSHs(sv = sv_newmortal());
5011 if (which == OP_GPBYNAME)
5012 sv_setiv(sv, (IV)pent->p_proto);
5014 sv_setpv(sv, pent->p_name);
5020 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5021 sv_setpv(sv, pent->p_name);
5022 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5023 for (elem = pent->p_aliases; elem && *elem; elem++) {
5024 sv_catpv(sv, *elem);
5026 sv_catpvn(sv, " ", 1);
5028 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5029 sv_setiv(sv, (IV)pent->p_proto);
5034 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5040 #ifdef HAS_GETSERVBYNAME
5041 return pp_gservent();
5043 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5049 #ifdef HAS_GETSERVBYPORT
5050 return pp_gservent();
5052 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5058 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5060 I32 which = PL_op->op_type;
5061 register char **elem;
5063 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5064 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5065 struct servent *getservbyport(int, Netdb_name_t);
5066 struct servent *getservent(void);
5068 struct servent *sent;
5071 if (which == OP_GSBYNAME) {
5072 #ifdef HAS_GETSERVBYNAME
5073 char *proto = POPpbytex;
5074 char *name = POPpbytex;
5076 if (proto && !*proto)
5079 sent = PerlSock_getservbyname(name, proto);
5081 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5084 else if (which == OP_GSBYPORT) {
5085 #ifdef HAS_GETSERVBYPORT
5086 char *proto = POPpbytex;
5087 unsigned short port = (unsigned short)POPu;
5089 if (proto && !*proto)
5093 port = PerlSock_htons(port);
5095 sent = PerlSock_getservbyport(port, proto);
5097 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5101 #ifdef HAS_GETSERVENT
5102 sent = PerlSock_getservent();
5104 DIE(aTHX_ PL_no_sock_func, "getservent");
5108 if (GIMME != G_ARRAY) {
5109 PUSHs(sv = sv_newmortal());
5111 if (which == OP_GSBYNAME) {
5113 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5115 sv_setiv(sv, (IV)(sent->s_port));
5119 sv_setpv(sv, sent->s_name);
5125 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5126 sv_setpv(sv, sent->s_name);
5127 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5128 for (elem = sent->s_aliases; elem && *elem; elem++) {
5129 sv_catpv(sv, *elem);
5131 sv_catpvn(sv, " ", 1);
5133 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5135 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5137 sv_setiv(sv, (IV)(sent->s_port));
5139 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5140 sv_setpv(sv, sent->s_proto);
5145 DIE(aTHX_ PL_no_sock_func, "getservent");
5151 #ifdef HAS_SETHOSTENT
5153 PerlSock_sethostent(TOPi);
5156 DIE(aTHX_ PL_no_sock_func, "sethostent");
5162 #ifdef HAS_SETNETENT
5164 PerlSock_setnetent(TOPi);
5167 DIE(aTHX_ PL_no_sock_func, "setnetent");
5173 #ifdef HAS_SETPROTOENT
5175 PerlSock_setprotoent(TOPi);
5178 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5184 #ifdef HAS_SETSERVENT
5186 PerlSock_setservent(TOPi);
5189 DIE(aTHX_ PL_no_sock_func, "setservent");
5195 #ifdef HAS_ENDHOSTENT
5197 PerlSock_endhostent();
5201 DIE(aTHX_ PL_no_sock_func, "endhostent");
5207 #ifdef HAS_ENDNETENT
5209 PerlSock_endnetent();
5213 DIE(aTHX_ PL_no_sock_func, "endnetent");
5219 #ifdef HAS_ENDPROTOENT
5221 PerlSock_endprotoent();
5225 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5231 #ifdef HAS_ENDSERVENT
5233 PerlSock_endservent();
5237 DIE(aTHX_ PL_no_sock_func, "endservent");
5246 DIE(aTHX_ PL_no_func, "getpwnam");
5255 DIE(aTHX_ PL_no_func, "getpwuid");
5263 I32 which = PL_op->op_type;
5266 struct passwd *pwent = NULL;
5268 * We currently support only the SysV getsp* shadow password interface.
5269 * The interface is declared in <shadow.h> and often one needs to link
5270 * with -lsecurity or some such.
5271 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5274 * AIX getpwnam() is clever enough to return the encrypted password
5275 * only if the caller (euid?) is root.
5277 * There are at least three other shadow password APIs. Many platforms
5278 * seem to contain more than one interface for accessing the shadow
5279 * password databases, possibly for compatibility reasons.
5280 * The getsp*() is by far he simplest one, the other two interfaces
5281 * are much more complicated, but also very similar to each other.
5286 * struct pr_passwd *getprpw*();
5287 * The password is in
5288 * char getprpw*(...).ufld.fd_encrypt[]
5289 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5294 * struct es_passwd *getespw*();
5295 * The password is in
5296 * char *(getespw*(...).ufld.fd_encrypt)
5297 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5300 * struct userpw *getuserpw();
5301 * The password is in
5302 * char *(getuserpw(...)).spw_upw_passwd
5303 * (but the de facto standard getpwnam() should work okay)
5305 * Mention I_PROT here so that Configure probes for it.
5307 * In HP-UX for getprpw*() the manual page claims that one should include
5308 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5309 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5310 * and pp_sys.c already includes <shadow.h> if there is such.
5312 * Note that <sys/security.h> is already probed for, but currently
5313 * it is only included in special cases.
5315 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5316 * be preferred interface, even though also the getprpw*() interface
5317 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5318 * One also needs to call set_auth_parameters() in main() before
5319 * doing anything else, whether one is using getespw*() or getprpw*().
5321 * Note that accessing the shadow databases can be magnitudes
5322 * slower than accessing the standard databases.
5327 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5328 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5329 * the pw_comment is left uninitialized. */
5330 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5336 char* name = POPpbytex;
5337 pwent = getpwnam(name);
5343 pwent = getpwuid(uid);
5347 # ifdef HAS_GETPWENT
5349 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5350 if (pwent) pwent = getpwnam(pwent->pw_name);
5353 DIE(aTHX_ PL_no_func, "getpwent");
5359 if (GIMME != G_ARRAY) {
5360 PUSHs(sv = sv_newmortal());
5362 if (which == OP_GPWNAM)
5363 # if Uid_t_sign <= 0
5364 sv_setiv(sv, (IV)pwent->pw_uid);
5366 sv_setuv(sv, (UV)pwent->pw_uid);
5369 sv_setpv(sv, pwent->pw_name);
5375 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5376 sv_setpv(sv, pwent->pw_name);
5378 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5380 /* If we have getspnam(), we try to dig up the shadow
5381 * password. If we are underprivileged, the shadow
5382 * interface will set the errno to EACCES or similar,
5383 * and return a null pointer. If this happens, we will
5384 * use the dummy password (usually "*" or "x") from the
5385 * standard password database.
5387 * In theory we could skip the shadow call completely
5388 * if euid != 0 but in practice we cannot know which
5389 * security measures are guarding the shadow databases
5390 * on a random platform.
5392 * Resist the urge to use additional shadow interfaces.
5393 * Divert the urge to writing an extension instead.
5396 /* Some AIX setups falsely(?) detect some getspnam(), which
5397 * has a different API than the Solaris/IRIX one. */
5398 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5400 struct spwd *spwent;
5401 int saverrno; /* Save and restore errno so that
5402 * underprivileged attempts seem
5403 * to have never made the unsccessful
5404 * attempt to retrieve the shadow password. */
5407 spwent = getspnam(pwent->pw_name);
5409 if (spwent && spwent->sp_pwdp)
5410 sv_setpv(sv, spwent->sp_pwdp);
5414 if (!SvPOK(sv)) /* Use the standard password, then. */
5415 sv_setpv(sv, pwent->pw_passwd);
5418 # ifndef INCOMPLETE_TAINTS
5419 /* passwd is tainted because user himself can diddle with it.
5420 * admittedly not much and in a very limited way, but nevertheless. */
5424 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5425 # if Uid_t_sign <= 0
5426 sv_setiv(sv, (IV)pwent->pw_uid);
5428 sv_setuv(sv, (UV)pwent->pw_uid);
5431 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5432 # if Uid_t_sign <= 0
5433 sv_setiv(sv, (IV)pwent->pw_gid);
5435 sv_setuv(sv, (UV)pwent->pw_gid);
5437 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5438 * because of the poor interface of the Perl getpw*(),
5439 * not because there's some standard/convention saying so.
5440 * A better interface would have been to return a hash,
5441 * but we are accursed by our history, alas. --jhi. */
5442 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5444 sv_setiv(sv, (IV)pwent->pw_change);
5447 sv_setiv(sv, (IV)pwent->pw_quota);
5450 sv_setpv(sv, pwent->pw_age);
5455 /* pw_class and pw_comment are mutually exclusive--.
5456 * see the above note for pw_change, pw_quota, and pw_age. */
5457 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5459 sv_setpv(sv, pwent->pw_class);
5462 sv_setpv(sv, pwent->pw_comment);
5466 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5468 sv_setpv(sv, pwent->pw_gecos);
5470 # ifndef INCOMPLETE_TAINTS
5471 /* pw_gecos is tainted because user himself can diddle with it. */
5475 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5476 sv_setpv(sv, pwent->pw_dir);
5478 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5479 sv_setpv(sv, pwent->pw_shell);
5480 # ifndef INCOMPLETE_TAINTS
5481 /* pw_shell is tainted because user himself can diddle with it. */
5486 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5487 sv_setiv(sv, (IV)pwent->pw_expire);
5492 DIE(aTHX_ PL_no_func, "getpwent");
5498 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5503 DIE(aTHX_ PL_no_func, "setpwent");
5509 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5514 DIE(aTHX_ PL_no_func, "endpwent");
5523 DIE(aTHX_ PL_no_func, "getgrnam");
5532 DIE(aTHX_ PL_no_func, "getgrgid");
5540 I32 which = PL_op->op_type;
5541 register char **elem;
5543 struct group *grent;
5546 if (which == OP_GGRNAM) {
5547 char* name = POPpbytex;
5548 grent = (struct group *)getgrnam(name);
5550 else if (which == OP_GGRGID) {
5552 grent = (struct group *)getgrgid(gid);
5556 grent = (struct group *)getgrent();
5558 DIE(aTHX_ PL_no_func, "getgrent");
5562 if (GIMME != G_ARRAY) {
5563 PUSHs(sv = sv_newmortal());
5565 if (which == OP_GGRNAM)
5566 sv_setiv(sv, (IV)grent->gr_gid);
5568 sv_setpv(sv, grent->gr_name);
5574 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5575 sv_setpv(sv, grent->gr_name);
5577 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5579 sv_setpv(sv, grent->gr_passwd);
5582 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5583 sv_setiv(sv, (IV)grent->gr_gid);
5585 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5586 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5587 /* In UNICOS/mk (_CRAYMPP) the multithreading
5588 * versions (getgrnam_r, getgrgid_r)
5589 * seem to return an illegal pointer
5590 * as the group members list, gr_mem.
5591 * getgrent() doesn't even have a _r version
5592 * but the gr_mem is poisonous anyway.
5593 * So yes, you cannot get the list of group
5594 * members if building multithreaded in UNICOS/mk. */
5595 for (elem = grent->gr_mem; elem && *elem; elem++) {
5596 sv_catpv(sv, *elem);
5598 sv_catpvn(sv, " ", 1);
5605 DIE(aTHX_ PL_no_func, "getgrent");
5611 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5616 DIE(aTHX_ PL_no_func, "setgrent");
5622 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5627 DIE(aTHX_ PL_no_func, "endgrent");
5637 if (!(tmps = PerlProc_getlogin()))
5639 PUSHp(tmps, strlen(tmps));
5642 DIE(aTHX_ PL_no_func, "getlogin");
5646 /* Miscellaneous. */
5651 dSP; dMARK; dORIGMARK; dTARGET;
5652 register I32 items = SP - MARK;
5653 unsigned long a[20];
5659 while (++MARK <= SP) {
5660 if (SvTAINTED(*MARK)) {
5666 TAINT_PROPER("syscall");
5669 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5670 * or where sizeof(long) != sizeof(char*). But such machines will
5671 * not likely have syscall implemented either, so who cares?
5673 while (++MARK <= SP) {
5674 if (SvNIOK(*MARK) || !i)
5675 a[i++] = SvIV(*MARK);
5676 else if (*MARK == &PL_sv_undef)
5679 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5685 DIE(aTHX_ "Too many args to syscall");
5687 DIE(aTHX_ "Too few args to syscall");
5689 retval = syscall(a[0]);
5692 retval = syscall(a[0],a[1]);
5695 retval = syscall(a[0],a[1],a[2]);
5698 retval = syscall(a[0],a[1],a[2],a[3]);
5701 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5704 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5707 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5710 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5714 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5717 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5720 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5724 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5728 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5732 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5733 a[10],a[11],a[12],a[13]);
5735 #endif /* atarist */
5741 DIE(aTHX_ PL_no_func, "syscall");
5745 #ifdef FCNTL_EMULATE_FLOCK
5747 /* XXX Emulate flock() with fcntl().
5748 What's really needed is a good file locking module.
5752 fcntl_emulate_flock(int fd, int operation)
5756 switch (operation & ~LOCK_NB) {
5758 flock.l_type = F_RDLCK;
5761 flock.l_type = F_WRLCK;
5764 flock.l_type = F_UNLCK;
5770 flock.l_whence = SEEK_SET;
5771 flock.l_start = flock.l_len = (Off_t)0;
5773 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5776 #endif /* FCNTL_EMULATE_FLOCK */
5778 #ifdef LOCKF_EMULATE_FLOCK
5780 /* XXX Emulate flock() with lockf(). This is just to increase
5781 portability of scripts. The calls are not completely
5782 interchangeable. What's really needed is a good file
5786 /* The lockf() constants might have been defined in <unistd.h>.
5787 Unfortunately, <unistd.h> causes troubles on some mixed
5788 (BSD/POSIX) systems, such as SunOS 4.1.3.
5790 Further, the lockf() constants aren't POSIX, so they might not be
5791 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5792 just stick in the SVID values and be done with it. Sigh.
5796 # define F_ULOCK 0 /* Unlock a previously locked region */
5799 # define F_LOCK 1 /* Lock a region for exclusive use */
5802 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5805 # define F_TEST 3 /* Test a region for other processes locks */
5809 lockf_emulate_flock(int fd, int operation)
5815 /* flock locks entire file so for lockf we need to do the same */
5817 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5818 if (pos > 0) /* is seekable and needs to be repositioned */
5819 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5820 pos = -1; /* seek failed, so don't seek back afterwards */
5823 switch (operation) {
5825 /* LOCK_SH - get a shared lock */
5827 /* LOCK_EX - get an exclusive lock */
5829 i = lockf (fd, F_LOCK, 0);
5832 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5833 case LOCK_SH|LOCK_NB:
5834 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5835 case LOCK_EX|LOCK_NB:
5836 i = lockf (fd, F_TLOCK, 0);
5838 if ((errno == EAGAIN) || (errno == EACCES))
5839 errno = EWOULDBLOCK;
5842 /* LOCK_UN - unlock (non-blocking is a no-op) */
5844 case LOCK_UN|LOCK_NB:
5845 i = lockf (fd, F_ULOCK, 0);
5848 /* Default - can't decipher operation */
5855 if (pos > 0) /* need to restore position of the handle */
5856 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5861 #endif /* LOCKF_EMULATE_FLOCK */
5865 * c-indentation-style: bsd
5867 * indent-tabs-mode: t
5870 * vim: shiftwidth=4: