3 * Copyright (c) 1991-2001, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * But only a short way ahead its floor and the walls on either side were
12 * cloven by a great fissure, out of which the red glare came, now leaping
13 * up, now dying down into darkness; and all the while far below there was
14 * a rumour and a trouble as of great engines throbbing and labouring.
18 #define PERL_IN_PP_SYS_C
22 /* Shadow password support for solaris - pdo@cs.umd.edu
23 * Not just Solaris: at least HP-UX, IRIX, Linux.
24 * The API is from SysV.
26 * There are at least two more shadow interfaces,
27 * see the comments in pp_gpwent().
31 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
32 * and another MAXINT from "perl.h" <- <sys/param.h>. */
40 extern "C" int syscall(unsigned long,...);
45 # include <sys/wait.h>
49 # include <sys/resource.h>
54 # include <sys/select.h>
58 /* XXX Configure test needed.
59 h_errno might not be a simple 'int', especially for multi-threaded
60 applications, see "extern int errno in perl.h". Creating such
61 a test requires taking into account the differences between
62 compiling multithreaded and singlethreaded ($ccflags et al).
63 HOST_NOT_FOUND is typically defined in <netdb.h>.
65 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
73 struct passwd *getpwnam (char *);
74 struct passwd *getpwuid (Uid_t);
77 struct passwd *getpwent (void);
85 struct group *getgrnam (char *);
86 struct group *getgrgid (Gid_t);
89 struct group *getgrent (void);
94 # if defined(_MSC_VER) || defined(__MINGW32__)
95 # include <sys/utime.h>
107 /* Put this after #includes because fork and vfork prototypes may conflict. */
113 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
116 # define my_chsize PerlLIO_chsize
121 #else /* no flock() */
123 /* fcntl.h might not have been included, even if it exists, because
124 the current Configure only sets I_FCNTL if it's needed to pick up
125 the *_OK constants. Make sure it has been included before testing
126 the fcntl() locking constants. */
127 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
131 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
132 # define FLOCK fcntl_emulate_flock
133 # define FCNTL_EMULATE_FLOCK
134 # else /* no flock() or fcntl(F_SETLK,...) */
136 # define FLOCK lockf_emulate_flock
137 # define LOCKF_EMULATE_FLOCK
139 # endif /* no flock() or fcntl(F_SETLK,...) */
142 static int FLOCK (int, int);
145 * These are the flock() constants. Since this sytems doesn't have
146 * flock(), the values of the constants are probably not available.
160 # endif /* emulating flock() */
162 #endif /* no flock() */
165 static char zero_but_true[ZBTLEN + 1] = "0 but true";
167 #if defined(I_SYS_ACCESS) && !defined(R_OK)
168 # include <sys/access.h>
171 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
172 # define FD_CLOEXEC 1 /* NeXT needs this */
175 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
176 #undef PERL_EFF_ACCESS_W_OK
177 #undef PERL_EFF_ACCESS_X_OK
179 /* F_OK unused: if stat() cannot find it... */
181 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
182 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
183 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
184 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
185 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
188 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
189 # ifdef I_SYS_SECURITY
190 # include <sys/security.h>
194 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
195 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
196 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
199 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
200 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
201 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
205 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
207 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
208 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
209 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
212 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
213 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
214 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
217 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
219 Uid_t ruid = getuid();
220 Uid_t euid = geteuid();
221 Gid_t rgid = getgid();
222 Gid_t egid = getegid();
226 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
227 Perl_croak(aTHX_ "switching effective uid is not implemented");
230 if (setreuid(euid, ruid))
233 if (setresuid(euid, ruid, (Uid_t)-1))
236 Perl_croak(aTHX_ "entering effective uid failed");
239 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
240 Perl_croak(aTHX_ "switching effective gid is not implemented");
243 if (setregid(egid, rgid))
246 if (setresgid(egid, rgid, (Gid_t)-1))
249 Perl_croak(aTHX_ "entering effective gid failed");
252 res = access(path, mode);
255 if (setreuid(ruid, euid))
258 if (setresuid(ruid, euid, (Uid_t)-1))
261 Perl_croak(aTHX_ "leaving effective uid failed");
264 if (setregid(rgid, egid))
267 if (setresgid(rgid, egid, (Gid_t)-1))
270 Perl_croak(aTHX_ "leaving effective gid failed");
275 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
276 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
277 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
280 #if !defined(PERL_EFF_ACCESS_R_OK)
282 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
284 Perl_croak(aTHX_ "switching effective uid is not implemented");
300 if (PL_op->op_private & OPpOPEN_IN_RAW)
302 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
304 fp = PerlProc_popen(tmps, mode);
307 if (PL_curcop->cop_io) {
308 type = SvPV_nolen(PL_curcop->cop_io);
311 PerlIO_apply_layers(aTHX_ fp,mode,type);
313 if (gimme == G_VOID) {
315 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
319 else if (gimme == G_SCALAR) {
320 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
321 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
332 if (sv_gets(sv, fp, 0) == Nullch) {
336 XPUSHs(sv_2mortal(sv));
337 if (SvLEN(sv) - SvCUR(sv) > 20) {
338 SvLEN_set(sv, SvCUR(sv)+1);
339 Renew(SvPVX(sv), SvLEN(sv), char);
344 STATUS_NATIVE_SET(PerlProc_pclose(fp));
345 TAINT; /* "I believe that this is not gratuitous!" */
348 STATUS_NATIVE_SET(-1);
349 if (gimme == G_SCALAR)
359 tryAMAGICunTARGET(iter, -1);
361 /* Note that we only ever get here if File::Glob fails to load
362 * without at the same time croaking, for some reason, or if
363 * perl was built with PERL_EXTERNAL_GLOB */
370 * The external globbing program may use things we can't control,
371 * so for security reasons we must assume the worst.
374 taint_proper(PL_no_security, "glob");
378 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
379 PL_last_in_gv = (GV*)*PL_stack_sp--;
381 SAVESPTR(PL_rs); /* This is not permanent, either. */
382 PL_rs = sv_2mortal(newSVpvn("\000", 1));
385 *SvPVX(PL_rs) = '\n';
389 result = do_readline();
394 #if 0 /* XXX never used! */
398 PL_last_in_gv = gv_fetchpv(SvPVx(GvSV((GV*)(*PL_stack_sp--)), n_a), TRUE,SVt_PVIO);
399 return do_readline();
405 PL_last_in_gv = cGVOP_gv;
406 return do_readline();
415 if (SP - MARK != 1) {
417 do_join(TARG, &PL_sv_no, MARK, SP);
424 tmps = SvPV(tmpsv, len);
427 (void)SvUPGRADE(error, SVt_PV);
428 if (SvPOK(error) && SvCUR(error))
429 sv_catpv(error, "\t...caught");
431 tmps = SvPV(tmpsv, len);
434 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
436 Perl_warn(aTHX_ "%"SVf, tmpsv);
447 if (SP - MARK != 1) {
449 do_join(TARG, &PL_sv_no, MARK, SP);
451 tmps = SvPV(tmpsv, len);
457 tmps = (SvROK(tmpsv) && PL_in_eval) ? Nullch : SvPV(tmpsv, len);
461 (void)SvUPGRADE(error, SVt_PV);
462 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
464 SvSetSV(error,tmpsv);
465 else if (sv_isobject(error)) {
466 HV *stash = SvSTASH(SvRV(error));
467 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
469 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
470 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
477 call_sv((SV*)GvCV(gv),
478 G_SCALAR|G_EVAL|G_KEEPERR);
479 sv_setsv(error,*PL_stack_sp--);
485 if (SvPOK(error) && SvCUR(error))
486 sv_catpv(error, "\t...propagated");
488 tmps = SvPV(tmpsv, len);
492 tmpsv = sv_2mortal(newSVpvn("Died", 4));
494 DIE(aTHX_ "%"SVf, tmpsv);
513 DIE(aTHX_ PL_no_usym, "filehandle");
515 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
517 if ((mg = SvTIED_mg((SV*)gv, 'q'))) {
518 /* Method's args are same as ours ... */
519 /* ... except handle is replaced by the object */
520 *MARK-- = SvTIED_obj((SV*)gv, mg);
524 call_method("OPEN", G_SCALAR);
537 tmps = SvPV(sv, len);
538 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
541 PUSHi( (I32)PL_forkprocess );
542 else if (PL_forkprocess == 0) /* we are a new child */
560 if ((mg = SvTIED_mg((SV*)gv, 'q'))) {
562 XPUSHs(SvTIED_obj((SV*)gv, mg));
565 call_method("CLOSE", G_SCALAR);
571 PUSHs(boolSV(do_close(gv, TRUE)));
591 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
592 DIE(aTHX_ PL_no_usym, "filehandle");
597 do_close(rgv, FALSE);
599 do_close(wgv, FALSE);
601 if (PerlProc_pipe(fd) < 0)
604 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
605 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
606 IoIFP(wstio) = IoOFP(wstio);
607 IoTYPE(rstio) = IoTYPE_RDONLY;
608 IoTYPE(wstio) = IoTYPE_WRONLY;
610 if (!IoIFP(rstio) || !IoOFP(wstio)) {
611 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
612 else PerlLIO_close(fd[0]);
613 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
614 else PerlLIO_close(fd[1]);
617 #if defined(HAS_FCNTL) && defined(F_SETFD)
618 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
619 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
626 DIE(aTHX_ PL_no_func, "pipe");
642 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
644 XPUSHs(SvTIED_obj((SV*)gv, mg));
647 call_method("FILENO", G_SCALAR);
653 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
654 /* Can't do this because people seem to do things like
655 defined(fileno($foo)) to check whether $foo is a valid fh.
656 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
657 report_evil_fh(gv, io, PL_op->op_type);
662 PUSHi(PerlIO_fileno(fp));
673 anum = PerlLIO_umask(0);
674 (void)PerlLIO_umask(anum);
677 anum = PerlLIO_umask(POPi);
678 TAINT_PROPER("umask");
681 /* Only DIE if trying to restrict permissions on `user' (self).
682 * Otherwise it's harmless and more useful to just return undef
683 * since 'group' and 'other' concepts probably don't exist here. */
684 if (MAXARG >= 1 && (POPi & 0700))
685 DIE(aTHX_ "umask not implemented");
686 XPUSHs(&PL_sv_undef);
710 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
712 XPUSHs(SvTIED_obj((SV*)gv, mg));
717 call_method("BINMODE", G_SCALAR);
724 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
725 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
726 report_evil_fh(gv, io, PL_op->op_type);
731 names = SvPV(discp,len);
734 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
735 (discp) ? SvPV_nolen(discp) : Nullch))
749 I32 markoff = MARK - PL_stack_base;
756 switch(SvTYPE(varsv)) {
758 methname = "TIEHASH";
761 methname = "TIEARRAY";
764 #ifdef GV_SHARED_CHECK
765 if (GvSHARED((GV*)varsv)) {
766 Perl_croak(aTHX_ "Attempt to tie shared GV");
769 methname = "TIEHANDLE";
773 methname = "TIESCALAR";
778 if (sv_isobject(*MARK)) {
780 PUSHSTACKi(PERLSI_MAGIC);
786 call_method(methname, G_SCALAR);
789 /* Not clear why we don't call call_method here too.
790 * perhaps to get different error message ?
792 stash = gv_stashsv(*MARK, FALSE);
793 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
794 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%s\"",
795 methname, SvPV(*MARK,n_a));
798 PUSHSTACKi(PERLSI_MAGIC);
804 call_sv((SV*)GvCV(gv), G_SCALAR);
810 if (sv_isobject(sv)) {
811 sv_unmagic(varsv, how);
812 /* Croak if a self-tie on an aggregate is attempted. */
813 if (varsv == SvRV(sv) &&
814 (SvTYPE(sv) == SVt_PVAV ||
815 SvTYPE(sv) == SVt_PVHV))
817 "Self-ties of arrays and hashes are not supported");
818 sv_magic(varsv, sv, how, Nullch, 0);
821 SP = PL_stack_base + markoff;
830 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) ? 'P' : 'q';
833 if ((mg = SvTIED_mg(sv, how))) {
834 SV *obj = SvRV(mg->mg_obj);
837 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
838 isGV(gv) && (cv = GvCV(gv))) {
840 XPUSHs(SvTIED_obj((SV*)gv, mg));
841 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
844 call_sv((SV *)cv, G_VOID);
848 else if (ckWARN(WARN_UNTIE)) {
849 if (mg && SvREFCNT(obj) > 1)
850 Perl_warner(aTHX_ WARN_UNTIE,
851 "untie attempted while %"UVuf" inner references still exist",
852 (UV)SvREFCNT(obj) - 1 ) ;
863 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) ? 'P' : 'q';
866 if ((mg = SvTIED_mg(sv, how))) {
867 SV *osv = SvTIED_obj(sv, mg);
868 if (osv == mg->mg_obj)
869 osv = sv_mortalcopy(osv);
887 sv = sv_mortalcopy(&PL_sv_no);
888 sv_setpv(sv, "AnyDBM_File");
889 stash = gv_stashsv(sv, FALSE);
890 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
892 require_pv("AnyDBM_File.pm");
894 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
895 DIE(aTHX_ "No dbm on this machine");
905 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
907 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
910 call_sv((SV*)GvCV(gv), G_SCALAR);
913 if (!sv_isobject(TOPs)) {
918 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
921 call_sv((SV*)GvCV(gv), G_SCALAR);
925 if (sv_isobject(TOPs)) {
926 sv_unmagic((SV *) hv, 'P');
927 sv_magic((SV*)hv, TOPs, 'P', Nullch, 0);
949 struct timeval timebuf;
950 struct timeval *tbuf = &timebuf;
954 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
959 # if BYTEORDER & 0xf0000
960 # define ORDERBYTE (0x88888888 - BYTEORDER)
962 # define ORDERBYTE (0x4444 - BYTEORDER)
968 for (i = 1; i <= 3; i++) {
976 /* little endians can use vecs directly */
977 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
978 # if SELECT_MIN_BITS > 1
979 /* If SELECT_MIN_BITS is greater than one we most probably will want
980 * to align the sizes with SELECT_MIN_BITS/8 because for example
981 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
982 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
983 * on (sets/tests/clears bits) is 32 bits. */
984 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
986 growsize = sizeof(fd_set);
995 masksize = NFDBITS / NBBY;
997 masksize = sizeof(long); /* documented int, everyone seems to use long */
999 growsize = maxlen + (masksize - (maxlen % masksize));
1000 Zero(&fd_sets[0], 4, char*);
1008 timebuf.tv_sec = (long)value;
1009 value -= (NV)timebuf.tv_sec;
1010 timebuf.tv_usec = (long)(value * 1000000.0);
1013 tbuf = Null(struct timeval*);
1015 for (i = 1; i <= 3; i++) {
1021 else if (!SvPOK(sv))
1022 SvPV_force(sv,n_a); /* force string conversion */
1025 Sv_Grow(sv, growsize);
1029 while (++j <= growsize) {
1033 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1035 New(403, fd_sets[i], growsize, char);
1036 for (offset = 0; offset < growsize; offset += masksize) {
1037 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1038 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1041 fd_sets[i] = SvPVX(sv);
1045 nfound = PerlSock_select(
1047 (Select_fd_set_t) fd_sets[1],
1048 (Select_fd_set_t) fd_sets[2],
1049 (Select_fd_set_t) fd_sets[3],
1051 for (i = 1; i <= 3; i++) {
1054 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1056 for (offset = 0; offset < growsize; offset += masksize) {
1057 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1058 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1060 Safefree(fd_sets[i]);
1067 if (GIMME == G_ARRAY && tbuf) {
1068 value = (NV)(timebuf.tv_sec) +
1069 (NV)(timebuf.tv_usec) / 1000000.0;
1070 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1071 sv_setnv(sv, value);
1075 DIE(aTHX_ "select not implemented");
1080 Perl_setdefout(pTHX_ GV *gv)
1083 (void)SvREFCNT_inc(gv);
1085 SvREFCNT_dec(PL_defoutgv);
1092 GV *newdefout, *egv;
1095 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1097 egv = GvEGV(PL_defoutgv);
1102 XPUSHs(&PL_sv_undef);
1104 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1105 if (gvp && *gvp == egv) {
1106 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1110 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1115 if (!GvIO(newdefout))
1116 gv_IOadd(newdefout);
1117 setdefout(newdefout);
1134 if ((mg = SvTIED_mg((SV*)gv, 'q'))) {
1135 I32 gimme = GIMME_V;
1137 XPUSHs(SvTIED_obj((SV*)gv, mg));
1140 call_method("GETC", gimme);
1143 if (gimme == G_SCALAR)
1144 SvSetMagicSV_nosteal(TARG, TOPs);
1147 if (!gv || do_eof(gv)) /* make sure we have fp with something */
1150 sv_setpv(TARG, " ");
1151 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1152 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1153 /* Find out how many bytes the char needs */
1154 Size_t len = UTF8SKIP(SvPVX(TARG));
1157 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1158 SvCUR_set(TARG,1+len);
1168 return pp_sysread();
1172 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1174 register PERL_CONTEXT *cx;
1175 I32 gimme = GIMME_V;
1176 AV* padlist = CvPADLIST(cv);
1177 SV** svp = AvARRAY(padlist);
1183 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1185 SAVEVPTR(PL_curpad);
1186 PL_curpad = AvARRAY((AV*)svp[1]);
1188 setdefout(gv); /* locally select filehandle so $% et al work */
1221 SV *tmpsv = sv_newmortal();
1222 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1223 name = SvPV_nolen(tmpsv);
1226 DIE(aTHX_ "Undefined format \"%s\" called", name);
1227 DIE(aTHX_ "Not a format reference");
1230 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1232 IoFLAGS(io) &= ~IOf_DIDTOP;
1233 return doform(cv,gv,PL_op->op_next);
1239 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1240 register IO *io = GvIOp(gv);
1241 PerlIO *ofp = IoOFP(io);
1245 register PERL_CONTEXT *cx;
1247 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1248 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1251 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1252 PL_formtarget != PL_toptarget)
1256 if (!IoTOP_GV(io)) {
1260 if (!IoTOP_NAME(io)) {
1261 if (!IoFMT_NAME(io))
1262 IoFMT_NAME(io) = savepv(GvNAME(gv));
1263 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1264 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1265 if ((topgv && GvFORM(topgv)) ||
1266 !gv_fetchpv("top",FALSE,SVt_PVFM))
1267 IoTOP_NAME(io) = savepv(SvPVX(topname));
1269 IoTOP_NAME(io) = savepv("top");
1271 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1272 if (!topgv || !GvFORM(topgv)) {
1273 IoLINES_LEFT(io) = 100000000;
1276 IoTOP_GV(io) = topgv;
1278 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1279 I32 lines = IoLINES_LEFT(io);
1280 char *s = SvPVX(PL_formtarget);
1281 if (lines <= 0) /* Yow, header didn't even fit!!! */
1283 while (lines-- > 0) {
1284 s = strchr(s, '\n');
1290 STRLEN save = SvCUR(PL_formtarget);
1291 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1292 do_print(PL_formtarget, ofp);
1293 SvCUR_set(PL_formtarget, save);
1294 sv_chop(PL_formtarget, s);
1295 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1298 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1299 do_print(PL_formfeed, ofp);
1300 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1302 PL_formtarget = PL_toptarget;
1303 IoFLAGS(io) |= IOf_DIDTOP;
1306 DIE(aTHX_ "bad top format reference");
1311 SV *sv = sv_newmortal();
1312 gv_efullname4(sv, fgv, Nullch, FALSE);
1313 name = SvPV_nolen(sv);
1316 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1319 DIE(aTHX_ "Undefined top format called");
1323 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1324 return doform(cv,gv,PL_op);
1328 POPBLOCK(cx,PL_curpm);
1334 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1336 /* integrate with report_evil_fh()? */
1339 SV* sv = sv_newmortal();
1340 gv_efullname4(sv, gv, Nullch, FALSE);
1341 name = SvPV_nolen(sv);
1344 Perl_warner(aTHX_ WARN_IO,
1345 "Filehandle %s opened only for input", name);
1347 Perl_warner(aTHX_ WARN_IO,
1348 "Filehandle opened only for input");
1350 else if (ckWARN(WARN_CLOSED))
1351 report_evil_fh(gv, io, PL_op->op_type);
1356 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1357 if (ckWARN(WARN_IO))
1358 Perl_warner(aTHX_ WARN_IO, "page overflow");
1360 if (!do_print(PL_formtarget, fp))
1363 FmLINES(PL_formtarget) = 0;
1364 SvCUR_set(PL_formtarget, 0);
1365 *SvEND(PL_formtarget) = '\0';
1366 if (IoFLAGS(io) & IOf_FLUSH)
1367 (void)PerlIO_flush(fp);
1372 PL_formtarget = PL_bodytarget;
1374 return pop_return();
1379 dSP; dMARK; dORIGMARK;
1386 if (PL_op->op_flags & OPf_STACKED)
1391 if ((mg = SvTIED_mg((SV*)gv, 'q'))) {
1392 if (MARK == ORIGMARK) {
1395 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1399 *MARK = SvTIED_obj((SV*)gv, mg);
1402 call_method("PRINTF", G_SCALAR);
1405 MARK = ORIGMARK + 1;
1412 if (!(io = GvIO(gv))) {
1413 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1414 report_evil_fh(gv, io, PL_op->op_type);
1415 SETERRNO(EBADF,RMS$_IFI);
1418 else if (!(fp = IoOFP(io))) {
1419 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1420 /* integrate with report_evil_fh()? */
1424 gv_efullname4(sv, gv, Nullch, FALSE);
1425 name = SvPV_nolen(sv);
1428 Perl_warner(aTHX_ WARN_IO,
1429 "Filehandle %s opened only for input", name);
1431 Perl_warner(aTHX_ WARN_IO,
1432 "Filehandle opened only for input");
1434 else if (ckWARN(WARN_CLOSED))
1435 report_evil_fh(gv, io, PL_op->op_type);
1437 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
1441 do_sprintf(sv, SP - MARK, MARK + 1);
1442 if (!do_print(sv, fp))
1445 if (IoFLAGS(io) & IOf_FLUSH)
1446 if (PerlIO_flush(fp) == EOF)
1457 PUSHs(&PL_sv_undef);
1478 /* Need TIEHANDLE method ? */
1480 tmps = SvPV(sv, len);
1481 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1482 IoLINES(GvIOp(gv)) = 0;
1486 PUSHs(&PL_sv_undef);
1493 dSP; dMARK; dORIGMARK; dTARGET;
1500 Sock_size_t bufsize;
1509 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD) &&
1510 (mg = SvTIED_mg((SV*)gv, 'q')))
1515 *MARK = SvTIED_obj((SV*)gv, mg);
1517 call_method("READ", G_SCALAR);
1530 sv_setpvn(bufsv, "", 0);
1531 length = SvIVx(*++MARK);
1534 offset = SvIVx(*++MARK);
1538 if (!io || !IoIFP(io))
1540 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTE) {
1541 buffer = SvPVutf8_force(bufsv, blen);
1542 /* UTF8 may not have been set if they are all low bytes */
1546 buffer = SvPV_force(bufsv, blen);
1549 DIE(aTHX_ "Negative length");
1553 if (PL_op->op_type == OP_RECV) {
1554 char namebuf[MAXPATHLEN];
1555 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE)
1556 bufsize = sizeof (struct sockaddr_in);
1558 bufsize = sizeof namebuf;
1560 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1564 buffer = SvGROW(bufsv, length+1);
1565 /* 'offset' means 'flags' here */
1566 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1567 (struct sockaddr *)namebuf, &bufsize);
1570 SvCUR_set(bufsv, count);
1571 *SvEND(bufsv) = '\0';
1572 (void)SvPOK_only(bufsv);
1576 /* This should not be marked tainted if the fp is marked clean */
1577 if (!(IoFLAGS(io) & IOf_UNTAINT))
1578 SvTAINTED_on(bufsv);
1580 #if defined(I_SYS_UN) && defined(__linux__)
1581 /* Linux returns the sum of actual pathname string length and the
1582 size of the other members of sockaddr_un members. It should
1583 return sizeof(struct sockaddr_un). */
1584 if (((struct sockaddr *)namebuf)->sa_family == AF_UNIX)
1585 bufsize = sizeof(struct sockaddr_un);
1587 sv_setpvn(TARG, namebuf, bufsize);
1592 if (PL_op->op_type == OP_RECV)
1593 DIE(aTHX_ PL_no_sock_func, "recv");
1595 if (DO_UTF8(bufsv)) {
1596 /* offset adjust in characters not bytes */
1597 blen = sv_len_utf8(bufsv);
1601 DIE(aTHX_ "Offset outside string");
1604 if (DO_UTF8(bufsv)) {
1605 /* convert offset-as-chars to offset-as-bytes */
1606 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1609 bufsize = SvCUR(bufsv);
1610 buffer = SvGROW(bufsv, length+offset+1);
1611 if (offset > bufsize) { /* Zero any newly allocated space */
1612 Zero(buffer+bufsize, offset-bufsize, char);
1614 buffer = buffer + offset;
1616 if (PL_op->op_type == OP_SYSREAD) {
1617 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1618 if (IoTYPE(io) == IoTYPE_SOCKET) {
1619 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1625 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1630 #ifdef HAS_SOCKET__bad_code_maybe
1631 if (IoTYPE(io) == IoTYPE_SOCKET) {
1632 char namebuf[MAXPATHLEN];
1633 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1634 bufsize = sizeof (struct sockaddr_in);
1636 bufsize = sizeof namebuf;
1638 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1639 (struct sockaddr *)namebuf, &bufsize);
1644 count = PerlIO_read(IoIFP(io), buffer, length);
1645 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1646 if (count == 0 && PerlIO_error(IoIFP(io)))
1650 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1652 /* integrate with report_evil_fh()? */
1655 SV* sv = sv_newmortal();
1656 gv_efullname4(sv, gv, Nullch, FALSE);
1657 name = SvPV_nolen(sv);
1660 Perl_warner(aTHX_ WARN_IO,
1661 "Filehandle %s opened only for output", name);
1663 Perl_warner(aTHX_ WARN_IO,
1664 "Filehandle opened only for output");
1668 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1669 *SvEND(bufsv) = '\0';
1670 (void)SvPOK_only(bufsv);
1671 if (fp_utf8 && !IN_BYTE) {
1672 /* Look at utf8 we got back and count the characters */
1673 char *bend = buffer + count;
1674 while (buffer < bend) {
1675 STRLEN skip = UTF8SKIP(buffer);
1676 if (buffer+skip > bend) {
1677 /* partial character - try for rest of it */
1678 length = skip - (bend-buffer);
1679 offset = bend - SvPVX(bufsv);
1687 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1688 provided amount read (count) was what was requested (length)
1690 if (got < wanted && count == length) {
1691 length = (wanted-got);
1692 offset = bend - SvPVX(bufsv);
1695 /* return value is character count */
1700 /* This should not be marked tainted if the fp is marked clean */
1701 if (!(IoFLAGS(io) & IOf_UNTAINT))
1702 SvTAINTED_on(bufsv);
1715 int items = (SP - PL_stack_base) - TOPMARK;
1719 sv = sv_2mortal(newSViv(sv_len(*SP)));
1728 dSP; dMARK; dORIGMARK; dTARGET;
1739 if (PL_op->op_type == OP_SYSWRITE && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1743 *MARK = SvTIED_obj((SV*)gv, mg);
1745 call_method("WRITE", G_SCALAR);
1756 #if Size_t_size > IVSIZE
1757 length = (Size_t)SvNVx(*++MARK);
1759 length = (Size_t)SvIVx(*++MARK);
1761 if ((SSize_t)length < 0)
1762 DIE(aTHX_ "Negative length");
1765 if (!io || !IoIFP(io)) {
1767 if (ckWARN(WARN_CLOSED))
1768 report_evil_fh(gv, io, PL_op->op_type);
1772 if (PerlIO_isutf8(IoIFP(io))) {
1773 buffer = SvPVutf8(bufsv, blen);
1777 sv_utf8_downgrade(bufsv, FALSE);
1778 buffer = SvPV(bufsv, blen);
1781 if (PL_op->op_type == OP_SYSWRITE) {
1783 if (DO_UTF8(bufsv)) {
1784 /* length and offset are in chars */
1785 blen = sv_len_utf8(bufsv);
1788 offset = SvIVx(*++MARK);
1791 DIE(aTHX_ "Offset outside string");
1793 } else if (offset >= blen && blen > 0)
1794 DIE(aTHX_ "Offset outside string");
1797 if (length > blen - offset)
1798 length = blen - offset;
1799 if (DO_UTF8(bufsv)) {
1800 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1801 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1804 buffer = buffer+offset;
1806 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1807 if (IoTYPE(io) == IoTYPE_SOCKET) {
1808 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1814 /* See the note at doio.c:do_print about filesize limits. --jhi */
1815 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1820 else if (SP > MARK) {
1823 sockbuf = SvPVx(*++MARK, mlen);
1824 /* length is really flags */
1825 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1826 length, (struct sockaddr *)sockbuf, mlen);
1829 /* length is really flags */
1830 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1833 DIE(aTHX_ PL_no_sock_func, "send");
1838 #if Size_t_size > IVSIZE
1852 return pp_sysread();
1862 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1864 gv = PL_last_in_gv = PL_argvgv;
1866 if (io && !IoIFP(io)) {
1867 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1869 IoFLAGS(io) &= ~IOf_START;
1870 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1871 sv_setpvn(GvSV(gv), "-", 1);
1872 SvSETMAGIC(GvSV(gv));
1874 else if (!nextargv(gv))
1879 gv = PL_last_in_gv; /* eof */
1882 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1884 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1886 XPUSHs(SvTIED_obj((SV*)gv, mg));
1889 call_method("EOF", G_SCALAR);
1895 PUSHs(boolSV(!gv || do_eof(gv)));
1908 gv = PL_last_in_gv = (GV*)POPs;
1910 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1912 XPUSHs(SvTIED_obj((SV*)gv, mg));
1915 call_method("TELL", G_SCALAR);
1921 #if LSEEKSIZE > IVSIZE
1922 PUSHn( do_tell(gv) );
1924 PUSHi( do_tell(gv) );
1931 return pp_sysseek();
1939 #if LSEEKSIZE > IVSIZE
1940 Off_t offset = (Off_t)SvNVx(POPs);
1942 Off_t offset = (Off_t)SvIVx(POPs);
1946 gv = PL_last_in_gv = (GV*)POPs;
1948 if (gv && (mg = SvTIED_mg((SV*)gv, 'q'))) {
1950 XPUSHs(SvTIED_obj((SV*)gv, mg));
1951 #if LSEEKSIZE > IVSIZE
1952 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
1954 XPUSHs(sv_2mortal(newSViv(offset)));
1956 XPUSHs(sv_2mortal(newSViv(whence)));
1959 call_method("SEEK", G_SCALAR);
1965 if (PL_op->op_type == OP_SEEK)
1966 PUSHs(boolSV(do_seek(gv, offset, whence)));
1968 Off_t sought = do_sysseek(gv, offset, whence);
1970 PUSHs(&PL_sv_undef);
1973 #if LSEEKSIZE > IVSIZE
1978 : newSVpvn(zero_but_true, ZBTLEN);
1979 PUSHs(sv_2mortal(sv));
1988 /* There seems to be no consensus on the length type of truncate()
1989 * and ftruncate(), both off_t and size_t have supporters. In
1990 * general one would think that when using large files, off_t is
1991 * at least as wide as size_t, so using an off_t should be okay. */
1992 /* XXX Configure probe for the length type of *truncate() needed XXX */
1998 #if Size_t_size > IVSIZE
2003 /* Checking for length < 0 is problematic as the type might or
2004 * might not be signed: if it is not, clever compilers will moan. */
2005 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2007 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2008 if (PL_op->op_flags & OPf_SPECIAL) {
2009 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2011 TAINT_PROPER("truncate");
2012 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)))
2015 PerlIO_flush(IoIFP(GvIOp(tmpgv)));
2017 if (ftruncate(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
2019 if (my_chsize(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
2029 if (SvTYPE(sv) == SVt_PVGV) {
2030 tmpgv = (GV*)sv; /* *main::FRED for example */
2033 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2034 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2038 name = SvPV(sv, n_a);
2039 TAINT_PROPER("truncate");
2041 if (truncate(name, len) < 0)
2046 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2049 if (my_chsize(tmpfd, len) < 0)
2051 PerlLIO_close(tmpfd);
2060 SETERRNO(EBADF,RMS$_IFI);
2063 DIE(aTHX_ "truncate not implemented");
2076 unsigned int func = U_I(POPn);
2077 int optype = PL_op->op_type;
2081 IO *io = gv ? GvIOn(gv) : 0;
2083 if (!io || !argsv || !IoIFP(io)) {
2084 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2085 report_evil_fh(gv, io, PL_op->op_type);
2086 SETERRNO(EBADF,RMS$_IFI); /* well, sort of... */
2090 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2093 s = SvPV_force(argsv, len);
2094 need = IOCPARM_LEN(func);
2096 s = Sv_Grow(argsv, need + 1);
2097 SvCUR_set(argsv, need);
2100 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2103 retval = SvIV(argsv);
2104 s = INT2PTR(char*,retval); /* ouch */
2107 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2109 if (optype == OP_IOCTL)
2111 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2113 DIE(aTHX_ "ioctl is not implemented");
2117 #if defined(OS2) && defined(__EMX__)
2118 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2120 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2123 DIE(aTHX_ "fcntl is not implemented");
2127 if (s[SvCUR(argsv)] != 17)
2128 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2129 PL_op_name[optype]);
2130 s[SvCUR(argsv)] = 0; /* put our null back */
2131 SvSETMAGIC(argsv); /* Assume it has changed */
2140 PUSHp(zero_but_true, ZBTLEN);
2160 if (gv && (io = GvIO(gv)))
2167 (void)PerlIO_flush(fp);
2168 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2171 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2172 report_evil_fh(gv, io, PL_op->op_type);
2174 SETERRNO(EBADF,RMS$_IFI);
2179 DIE(aTHX_ PL_no_func, "flock()");
2191 int protocol = POPi;
2197 io = gv ? GvIOn(gv) : NULL;
2200 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2201 report_evil_fh(gv, io, PL_op->op_type);
2203 do_close(gv, FALSE);
2204 SETERRNO(EBADF,LIB$_INVARG);
2209 do_close(gv, FALSE);
2211 TAINT_PROPER("socket");
2212 fd = PerlSock_socket(domain, type, protocol);
2215 IoIFP(io) = PerlIO_fdopen(fd, "r"); /* stdio gets confused about sockets */
2216 IoOFP(io) = PerlIO_fdopen(fd, "w");
2217 IoTYPE(io) = IoTYPE_SOCKET;
2218 if (!IoIFP(io) || !IoOFP(io)) {
2219 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2220 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2221 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2224 #if defined(HAS_FCNTL) && defined(F_SETFD)
2225 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2229 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2234 DIE(aTHX_ PL_no_sock_func, "socket");
2241 #ifdef HAS_SOCKETPAIR
2246 int protocol = POPi;
2253 io1 = gv1 ? GvIOn(gv1) : NULL;
2254 io2 = gv2 ? GvIOn(gv2) : NULL;
2255 if (!gv1 || !gv2 || !io1 || !io2) {
2256 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2258 report_evil_fh(gv1, io1, PL_op->op_type);
2260 report_evil_fh(gv1, io2, PL_op->op_type);
2263 do_close(gv1, FALSE);
2265 do_close(gv2, FALSE);
2270 do_close(gv1, FALSE);
2272 do_close(gv2, FALSE);
2274 TAINT_PROPER("socketpair");
2275 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2277 IoIFP(io1) = PerlIO_fdopen(fd[0], "r");
2278 IoOFP(io1) = PerlIO_fdopen(fd[0], "w");
2279 IoTYPE(io1) = IoTYPE_SOCKET;
2280 IoIFP(io2) = PerlIO_fdopen(fd[1], "r");
2281 IoOFP(io2) = PerlIO_fdopen(fd[1], "w");
2282 IoTYPE(io2) = IoTYPE_SOCKET;
2283 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2284 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2285 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2286 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2287 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2288 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2289 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2292 #if defined(HAS_FCNTL) && defined(F_SETFD)
2293 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2294 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2299 DIE(aTHX_ PL_no_sock_func, "socketpair");
2307 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2308 extern GETPRIVMODE();
2309 extern GETUSERMODE();
2314 register IO *io = GvIOn(gv);
2318 int mpeprivmode = 0;
2321 if (!io || !IoIFP(io))
2324 addr = SvPV(addrsv, len);
2325 TAINT_PROPER("bind");
2326 #ifdef MPE /* Deal with MPE bind() peculiarities */
2327 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2328 /* The address *MUST* stupidly be zero. */
2329 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2330 /* PRIV mode is required to bind() to ports < 1024. */
2331 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2332 ((struct sockaddr_in *)addr)->sin_port > 0) {
2333 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2338 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2339 (struct sockaddr *)addr, len) >= 0)
2342 #ifdef MPE /* Switch back to USER mode */
2353 if (ckWARN(WARN_CLOSED))
2354 report_evil_fh(gv, io, PL_op->op_type);
2355 SETERRNO(EBADF,SS$_IVCHAN);
2358 DIE(aTHX_ PL_no_sock_func, "bind");
2369 register IO *io = GvIOn(gv);
2372 if (!io || !IoIFP(io))
2375 addr = SvPV(addrsv, len);
2376 TAINT_PROPER("connect");
2377 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2383 if (ckWARN(WARN_CLOSED))
2384 report_evil_fh(gv, io, PL_op->op_type);
2385 SETERRNO(EBADF,SS$_IVCHAN);
2388 DIE(aTHX_ PL_no_sock_func, "connect");
2398 register IO *io = gv ? GvIOn(gv) : NULL;
2400 if (!gv || !io || !IoIFP(io))
2403 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2409 if (ckWARN(WARN_CLOSED))
2410 report_evil_fh(gv, io, PL_op->op_type);
2411 SETERRNO(EBADF,SS$_IVCHAN);
2414 DIE(aTHX_ PL_no_sock_func, "listen");
2426 struct sockaddr saddr; /* use a struct to avoid alignment problems */
2427 Sock_size_t len = sizeof saddr;
2439 if (!gstio || !IoIFP(gstio))
2444 do_close(ngv, FALSE);
2446 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
2449 IoIFP(nstio) = PerlIO_fdopen(fd, "r");
2450 IoOFP(nstio) = PerlIO_fdopen(fd, "w");
2451 IoTYPE(nstio) = IoTYPE_SOCKET;
2452 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2453 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2454 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2455 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2458 #if defined(HAS_FCNTL) && defined(F_SETFD)
2459 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2463 len = sizeof saddr; /* EPOC somehow truncates info */
2464 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2467 #if defined(I_SYS_UN) && defined(__linux__)
2468 /* see the comment in pp_sysread */
2469 if (saddr.sa_family == AF_UNIX)
2470 len = sizeof(struct sockaddr_un);
2473 PUSHp((char *)&saddr, len);
2477 if (ckWARN(WARN_CLOSED))
2478 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2479 SETERRNO(EBADF,SS$_IVCHAN);
2485 DIE(aTHX_ PL_no_sock_func, "accept");
2495 register IO *io = GvIOn(gv);
2497 if (!io || !IoIFP(io))
2500 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2504 if (ckWARN(WARN_CLOSED))
2505 report_evil_fh(gv, io, PL_op->op_type);
2506 SETERRNO(EBADF,SS$_IVCHAN);
2509 DIE(aTHX_ PL_no_sock_func, "shutdown");
2516 return pp_ssockopt();
2518 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2526 int optype = PL_op->op_type;
2529 unsigned int optname;
2535 if (optype == OP_GSOCKOPT)
2536 sv = sv_2mortal(NEWSV(22, 257));
2539 optname = (unsigned int) POPi;
2540 lvl = (unsigned int) POPi;
2544 if (!io || !IoIFP(io))
2547 fd = PerlIO_fileno(IoIFP(io));
2551 (void)SvPOK_only(sv);
2555 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2570 aint = (int)SvIV(sv);
2574 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2583 if (ckWARN(WARN_CLOSED))
2584 report_evil_fh(gv, io, optype);
2585 SETERRNO(EBADF,SS$_IVCHAN);
2590 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2597 return pp_getpeername();
2599 DIE(aTHX_ PL_no_sock_func, "getsockname");
2607 int optype = PL_op->op_type;
2611 register IO *io = GvIOn(gv);
2614 if (!io || !IoIFP(io))
2617 sv = sv_2mortal(NEWSV(22, 257));
2618 (void)SvPOK_only(sv);
2622 fd = PerlIO_fileno(IoIFP(io));
2624 case OP_GETSOCKNAME:
2625 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2628 case OP_GETPEERNAME:
2629 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2631 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2633 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";
2634 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2635 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2636 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2637 sizeof(u_short) + sizeof(struct in_addr))) {
2644 #ifdef BOGUS_GETNAME_RETURN
2645 /* Interactive Unix, getpeername() and getsockname()
2646 does not return valid namelen */
2647 if (len == BOGUS_GETNAME_RETURN)
2648 len = sizeof(struct sockaddr);
2650 #if defined(I_SYS_UN) && defined(__linux__)
2651 /* see the comment in pp_sysread */
2652 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_UNIX)
2653 len = sizeof(struct sockaddr_un);
2661 if (ckWARN(WARN_CLOSED))
2662 report_evil_fh(gv, io, optype);
2663 SETERRNO(EBADF,SS$_IVCHAN);
2668 DIE(aTHX_ PL_no_sock_func, "getpeername");
2687 if (PL_op->op_flags & OPf_REF) {
2689 if (PL_op->op_type == OP_LSTAT) {
2690 if (PL_laststype != OP_LSTAT)
2691 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2692 if (ckWARN(WARN_IO) && gv != PL_defgv)
2693 Perl_warner(aTHX_ WARN_IO,
2694 "lstat() on filehandle %s", GvENAME(gv));
2695 /* Perl_my_lstat (-l) croak's on filehandle, why warn here? */
2699 if (gv != PL_defgv) {
2700 PL_laststype = OP_STAT;
2702 sv_setpv(PL_statname, "");
2703 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2704 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2706 if (PL_laststatval < 0) {
2707 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2708 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2714 if (SvTYPE(sv) == SVt_PVGV) {
2718 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2722 sv_setpv(PL_statname, SvPV(sv,n_a));
2725 PL_laststype = PL_op->op_type;
2726 if (PL_op->op_type == OP_LSTAT)
2727 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2730 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2731 if (PL_laststatval < 0) {
2732 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2733 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
2739 if (gimme != G_ARRAY) {
2740 if (gimme != G_VOID)
2741 XPUSHs(boolSV(max));
2747 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2748 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2749 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2750 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2751 #if Uid_t_size > IVSIZE
2752 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2754 # if Uid_t_sign <= 0
2755 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2757 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2760 #if Gid_t_size > IVSIZE
2761 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2763 # if Gid_t_sign <= 0
2764 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2766 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2769 #ifdef USE_STAT_RDEV
2770 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2772 PUSHs(sv_2mortal(newSVpvn("", 0)));
2774 #if Off_t_size > IVSIZE
2775 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_size)));
2777 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2780 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2781 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2782 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2784 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2785 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2786 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2788 #ifdef USE_STAT_BLOCKS
2789 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2790 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2792 PUSHs(sv_2mortal(newSVpvn("", 0)));
2793 PUSHs(sv_2mortal(newSVpvn("", 0)));
2803 #if defined(HAS_ACCESS) && defined(R_OK)
2805 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2806 result = access(TOPpx, R_OK);
2821 if (cando(S_IRUSR, 0, &PL_statcache))
2830 #if defined(HAS_ACCESS) && defined(W_OK)
2832 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2833 result = access(TOPpx, W_OK);
2848 if (cando(S_IWUSR, 0, &PL_statcache))
2857 #if defined(HAS_ACCESS) && defined(X_OK)
2859 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2860 result = access(TOPpx, X_OK);
2875 if (cando(S_IXUSR, 0, &PL_statcache))
2884 #ifdef PERL_EFF_ACCESS_R_OK
2886 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2887 result = PERL_EFF_ACCESS_R_OK(TOPpx);
2902 if (cando(S_IRUSR, 1, &PL_statcache))
2911 #ifdef PERL_EFF_ACCESS_W_OK
2913 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2914 result = PERL_EFF_ACCESS_W_OK(TOPpx);
2929 if (cando(S_IWUSR, 1, &PL_statcache))
2938 #ifdef PERL_EFF_ACCESS_X_OK
2940 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2941 result = PERL_EFF_ACCESS_X_OK(TOPpx);
2956 if (cando(S_IXUSR, 1, &PL_statcache))
2963 I32 result = my_stat();
2972 return pp_ftrowned();
2977 I32 result = my_stat();
2981 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
2989 I32 result = my_stat();
2993 if (PL_statcache.st_size == 0)
3000 I32 result = my_stat();
3004 #if Off_t_size > IVSIZE
3005 PUSHn(PL_statcache.st_size);
3007 PUSHi(PL_statcache.st_size);
3014 I32 result = my_stat();
3018 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
3024 I32 result = my_stat();
3028 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
3034 I32 result = my_stat();
3038 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
3044 I32 result = my_stat();
3048 if (S_ISSOCK(PL_statcache.st_mode))
3055 I32 result = my_stat();
3059 if (S_ISCHR(PL_statcache.st_mode))
3066 I32 result = my_stat();
3070 if (S_ISBLK(PL_statcache.st_mode))
3077 I32 result = my_stat();
3081 if (S_ISREG(PL_statcache.st_mode))
3088 I32 result = my_stat();
3092 if (S_ISDIR(PL_statcache.st_mode))
3099 I32 result = my_stat();
3103 if (S_ISFIFO(PL_statcache.st_mode))
3110 I32 result = my_lstat();
3114 if (S_ISLNK(PL_statcache.st_mode))
3123 I32 result = my_stat();
3127 if (PL_statcache.st_mode & S_ISUID)
3137 I32 result = my_stat();
3141 if (PL_statcache.st_mode & S_ISGID)
3151 I32 result = my_stat();
3155 if (PL_statcache.st_mode & S_ISVTX)
3166 char *tmps = Nullch;
3169 if (PL_op->op_flags & OPf_REF)
3171 else if (isGV(TOPs))
3173 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3174 gv = (GV*)SvRV(POPs);
3176 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3178 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3179 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3180 else if (tmps && isDIGIT(*tmps))
3184 if (PerlLIO_isatty(fd))
3189 #if defined(atarist) /* this will work with atariST. Configure will
3190 make guesses for other systems. */
3191 # define FILE_base(f) ((f)->_base)
3192 # define FILE_ptr(f) ((f)->_ptr)
3193 # define FILE_cnt(f) ((f)->_cnt)
3194 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3204 register STDCHAR *s;
3211 if (PL_op->op_flags & OPf_REF)
3213 else if (isGV(TOPs))
3215 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3216 gv = (GV*)SvRV(POPs);
3222 if (gv == PL_defgv) {
3224 io = GvIO(PL_statgv);
3227 goto really_filename;
3232 PL_laststatval = -1;
3233 sv_setpv(PL_statname, "");
3234 io = GvIO(PL_statgv);
3236 if (io && IoIFP(io)) {
3237 if (! PerlIO_has_base(IoIFP(io)))
3238 DIE(aTHX_ "-T and -B not implemented on filehandles");
3239 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3240 if (PL_laststatval < 0)
3242 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3243 if (PL_op->op_type == OP_FTTEXT)
3248 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3249 i = PerlIO_getc(IoIFP(io));
3251 (void)PerlIO_ungetc(IoIFP(io),i);
3253 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3255 len = PerlIO_get_bufsiz(IoIFP(io));
3256 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3257 /* sfio can have large buffers - limit to 512 */
3262 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3264 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3266 SETERRNO(EBADF,RMS$_IFI);
3274 PL_laststatval = -1;
3275 sv_setpv(PL_statname, SvPV(sv, n_a));
3276 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3277 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
3278 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
3281 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3282 if (PL_laststatval < 0) {
3283 (void)PerlIO_close(fp);
3286 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3287 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3288 (void)PerlIO_close(fp);
3290 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3291 RETPUSHNO; /* special case NFS directories */
3292 RETPUSHYES; /* null file is anything */
3297 /* now scan s to look for textiness */
3298 /* XXX ASCII dependent code */
3300 #if defined(DOSISH) || defined(USEMYBINMODE)
3301 /* ignore trailing ^Z on short files */
3302 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3306 for (i = 0; i < len; i++, s++) {
3307 if (!*s) { /* null never allowed in text */
3312 else if (!(isPRINT(*s) || isSPACE(*s)))
3315 else if (*s & 128) {
3317 if ((PL_op->op_private & OPpLOCALE) && isALPHA_LC(*s))
3320 /* utf8 characters don't count as odd */
3321 if (UTF8_IS_START(*s)) {
3322 int ulen = UTF8SKIP(s);
3323 if (ulen < len - i) {
3325 for (j = 1; j < ulen; j++) {
3326 if (!UTF8_IS_CONTINUATION(s[j]))
3329 --ulen; /* loop does extra increment */
3339 *s != '\n' && *s != '\r' && *s != '\b' &&
3340 *s != '\t' && *s != '\f' && *s != 27)
3345 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3369 if (!tmps || !*tmps) {
3370 svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE);
3372 tmps = SvPV(*svp, n_a);
3374 if (!tmps || !*tmps) {
3375 svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE);
3377 tmps = SvPV(*svp, n_a);
3380 if (!tmps || !*tmps) {
3381 svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE);
3383 tmps = SvPV(*svp, n_a);
3386 TAINT_PROPER("chdir");
3387 PUSHi( PerlDir_chdir(tmps) >= 0 );
3389 /* Clear the DEFAULT element of ENV so we'll get the new value
3391 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3398 dSP; dMARK; dTARGET;
3401 value = (I32)apply(PL_op->op_type, MARK, SP);
3406 DIE(aTHX_ PL_no_func, "Unsupported function chown");
3417 TAINT_PROPER("chroot");
3418 PUSHi( chroot(tmps) >= 0 );
3421 DIE(aTHX_ PL_no_func, "chroot");
3427 dSP; dMARK; dTARGET;
3429 value = (I32)apply(PL_op->op_type, MARK, SP);
3437 dSP; dMARK; dTARGET;
3439 value = (I32)apply(PL_op->op_type, MARK, SP);
3447 dSP; dMARK; dTARGET;
3449 value = (I32)apply(PL_op->op_type, MARK, SP);
3461 char *tmps2 = POPpx;
3462 char *tmps = SvPV(TOPs, n_a);
3463 TAINT_PROPER("rename");
3465 anum = PerlLIO_rename(tmps, tmps2);
3467 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3468 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3471 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3472 (void)UNLINK(tmps2);
3473 if (!(anum = link(tmps, tmps2)))
3474 anum = UNLINK(tmps);
3487 char *tmps2 = POPpx;
3488 char *tmps = SvPV(TOPs, n_a);
3489 TAINT_PROPER("link");
3490 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3492 DIE(aTHX_ PL_no_func, "Unsupported function link");
3502 char *tmps2 = POPpx;
3503 char *tmps = SvPV(TOPs, n_a);
3504 TAINT_PROPER("symlink");
3505 SETi( symlink(tmps, tmps2) >= 0 );
3508 DIE(aTHX_ PL_no_func, "symlink");
3517 char buf[MAXPATHLEN];
3521 #ifndef INCOMPLETE_TAINTS
3525 len = readlink(tmps, buf, sizeof buf);
3533 RETSETUNDEF; /* just pretend it's a normal file */
3537 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3539 S_dooneliner(pTHX_ char *cmd, char *filename)
3541 char *save_filename = filename;
3547 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3548 strcpy(cmdline, cmd);
3549 strcat(cmdline, " ");
3550 for (s = cmdline + strlen(cmdline); *filename; ) {
3555 myfp = PerlProc_popen(cmdline, "r");
3559 SV *tmpsv = sv_newmortal();
3560 /* Need to save/restore 'PL_rs' ?? */
3561 s = sv_gets(tmpsv, myfp, 0);
3562 (void)PerlProc_pclose(myfp);
3566 #ifdef HAS_SYS_ERRLIST
3571 /* you don't see this */
3573 #ifdef HAS_SYS_ERRLIST
3581 if (instr(s, errmsg)) {
3588 #define EACCES EPERM
3590 if (instr(s, "cannot make"))
3591 SETERRNO(EEXIST,RMS$_FEX);
3592 else if (instr(s, "existing file"))
3593 SETERRNO(EEXIST,RMS$_FEX);
3594 else if (instr(s, "ile exists"))
3595 SETERRNO(EEXIST,RMS$_FEX);
3596 else if (instr(s, "non-exist"))
3597 SETERRNO(ENOENT,RMS$_FNF);
3598 else if (instr(s, "does not exist"))
3599 SETERRNO(ENOENT,RMS$_FNF);
3600 else if (instr(s, "not empty"))
3601 SETERRNO(EBUSY,SS$_DEVOFFLINE);
3602 else if (instr(s, "cannot access"))
3603 SETERRNO(EACCES,RMS$_PRV);
3605 SETERRNO(EPERM,RMS$_PRV);
3608 else { /* some mkdirs return no failure indication */
3609 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3610 if (PL_op->op_type == OP_RMDIR)
3615 SETERRNO(EACCES,RMS$_PRV); /* a guess */
3640 tmps = SvPV(TOPs, len);
3641 /* Different operating and file systems take differently to
3642 * trailing slashes. According to POSIX 1003.1 1996 Edition
3643 * any number of trailing slashes should be allowed.
3644 * Thusly we snip them away so that even non-conforming
3645 * systems are happy. */
3646 /* We should probably do this "filtering" for all
3647 * the functions that expect (potentially) directory names:
3648 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3649 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3650 if (len > 1 && tmps[len-1] == '/') {
3651 while (tmps[len] == '/' && len > 1)
3653 tmps = savepvn(tmps, len);
3657 TAINT_PROPER("mkdir");
3659 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3661 SETi( dooneliner("mkdir", tmps) );
3662 oldumask = PerlLIO_umask(0);
3663 PerlLIO_umask(oldumask);
3664 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3678 TAINT_PROPER("rmdir");
3680 XPUSHi( PerlDir_rmdir(tmps) >= 0 );
3682 XPUSHi( dooneliner("rmdir", tmps) );
3687 /* Directory calls. */
3692 #if defined(Direntry_t) && defined(HAS_READDIR)
3694 char *dirname = POPpx;
3696 register IO *io = GvIOn(gv);
3702 PerlDir_close(IoDIRP(io));
3703 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3709 SETERRNO(EBADF,RMS$_DIR);
3712 DIE(aTHX_ PL_no_dir_func, "opendir");
3719 #if defined(Direntry_t) && defined(HAS_READDIR)
3721 Direntry_t *readdir (DIR *);
3723 register Direntry_t *dp;
3725 register IO *io = GvIOn(gv);
3728 if (!io || !IoDIRP(io))
3731 if (GIMME == G_ARRAY) {
3733 while ((dp = (Direntry_t *)PerlDir_read(IoDIRP(io)))) {
3735 sv = newSVpvn(dp->d_name, dp->d_namlen);
3737 sv = newSVpv(dp->d_name, 0);
3739 #ifndef INCOMPLETE_TAINTS
3740 if (!(IoFLAGS(io) & IOf_UNTAINT))
3743 XPUSHs(sv_2mortal(sv));
3747 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3750 sv = newSVpvn(dp->d_name, dp->d_namlen);
3752 sv = newSVpv(dp->d_name, 0);
3754 #ifndef INCOMPLETE_TAINTS
3755 if (!(IoFLAGS(io) & IOf_UNTAINT))
3758 XPUSHs(sv_2mortal(sv));
3764 SETERRNO(EBADF,RMS$_ISI);
3765 if (GIMME == G_ARRAY)
3770 DIE(aTHX_ PL_no_dir_func, "readdir");
3777 #if defined(HAS_TELLDIR) || defined(telldir)
3778 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3779 /* XXX netbsd still seemed to.
3780 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3781 --JHI 1999-Feb-02 */
3782 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3783 long telldir (DIR *);
3786 register IO *io = GvIOn(gv);
3788 if (!io || !IoDIRP(io))
3791 PUSHi( PerlDir_tell(IoDIRP(io)) );
3795 SETERRNO(EBADF,RMS$_ISI);
3798 DIE(aTHX_ PL_no_dir_func, "telldir");
3805 #if defined(HAS_SEEKDIR) || defined(seekdir)
3808 register IO *io = GvIOn(gv);
3810 if (!io || !IoDIRP(io))
3813 (void)PerlDir_seek(IoDIRP(io), along);
3818 SETERRNO(EBADF,RMS$_ISI);
3821 DIE(aTHX_ PL_no_dir_func, "seekdir");
3828 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3830 register IO *io = GvIOn(gv);
3832 if (!io || !IoDIRP(io))
3835 (void)PerlDir_rewind(IoDIRP(io));
3839 SETERRNO(EBADF,RMS$_ISI);
3842 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3849 #if defined(Direntry_t) && defined(HAS_READDIR)
3851 register IO *io = GvIOn(gv);
3853 if (!io || !IoDIRP(io))
3856 #ifdef VOID_CLOSEDIR
3857 PerlDir_close(IoDIRP(io));
3859 if (PerlDir_close(IoDIRP(io)) < 0) {
3860 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3869 SETERRNO(EBADF,RMS$_IFI);
3872 DIE(aTHX_ PL_no_dir_func, "closedir");
3876 /* Process control. */
3886 PERL_FLUSHALL_FOR_CHILD;
3892 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV)))
3893 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3894 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3899 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3904 PERL_FLUSHALL_FOR_CHILD;
3905 childpid = PerlProc_fork();
3911 DIE(aTHX_ PL_no_func, "Unsupported function fork");
3918 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3923 childpid = wait4pid(-1, &argflags, 0);
3924 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3925 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3926 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
3928 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3933 DIE(aTHX_ PL_no_func, "Unsupported function wait");
3939 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3947 childpid = wait4pid(childpid, &argflags, optype);
3948 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3949 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3950 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
3952 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3957 DIE(aTHX_ PL_no_func, "Unsupported function waitpid");
3963 dSP; dMARK; dORIGMARK; dTARGET;
3968 Sigsave_t ihand,qhand; /* place to save signals during system() */
3973 if (SP - MARK == 1) {
3975 char *junk = SvPV(TOPs, n_a);
3977 TAINT_PROPER("system");
3980 PERL_FLUSHALL_FOR_CHILD;
3981 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) && !defined(__CYGWIN__) || defined(PERL_MICRO)
3982 if (PerlProc_pipe(pp) >= 0)
3984 while ((childpid = vfork()) == -1) {
3985 if (errno != EAGAIN) {
3990 PerlLIO_close(pp[0]);
3991 PerlLIO_close(pp[1]);
3999 PerlLIO_close(pp[1]);
4001 rsignal_save(SIGINT, SIG_IGN, &ihand);
4002 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4005 result = wait4pid(childpid, &status, 0);
4006 } while (result == -1 && errno == EINTR);
4008 (void)rsignal_restore(SIGINT, &ihand);
4009 (void)rsignal_restore(SIGQUIT, &qhand);
4011 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4012 do_execfree(); /* free any memory child malloced on vfork */
4018 while (n < sizeof(int)) {
4019 n1 = PerlLIO_read(pp[0],
4020 (void*)(((char*)&errkid)+n),
4026 PerlLIO_close(pp[0]);
4027 if (n) { /* Error */
4028 if (n != sizeof(int))
4029 DIE(aTHX_ "panic: kid popen errno read");
4030 errno = errkid; /* Propagate errno from kid */
4031 STATUS_CURRENT = -1;
4034 PUSHi(STATUS_CURRENT);
4038 PerlLIO_close(pp[0]);
4039 #if defined(HAS_FCNTL) && defined(F_SETFD)
4040 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4043 if (PL_op->op_flags & OPf_STACKED) {
4044 SV *really = *++MARK;
4045 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4047 else if (SP - MARK != 1)
4048 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4050 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4053 #else /* ! FORK or VMS or OS/2 */
4056 if (PL_op->op_flags & OPf_STACKED) {
4057 SV *really = *++MARK;
4058 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4060 else if (SP - MARK != 1)
4061 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4063 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4065 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4067 STATUS_NATIVE_SET(value);
4070 PUSHi(result ? value : STATUS_CURRENT);
4071 #endif /* !FORK or VMS */
4077 dSP; dMARK; dORIGMARK; dTARGET;
4081 PERL_FLUSHALL_FOR_CHILD;
4082 if (PL_op->op_flags & OPf_STACKED) {
4083 SV *really = *++MARK;
4084 value = (I32)do_aexec(really, MARK, SP);
4086 else if (SP - MARK != 1)
4088 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4092 (void ) do_aspawn(Nullsv, MARK, SP);
4096 value = (I32)do_aexec(Nullsv, MARK, SP);
4101 char *junk = SvPV(*SP, n_a);
4103 TAINT_PROPER("exec");
4106 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4109 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4112 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4117 #if !defined(HAS_FORK) && defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4129 dSP; dMARK; dTARGET;
4132 value = (I32)apply(PL_op->op_type, MARK, SP);
4137 DIE(aTHX_ PL_no_func, "Unsupported function kill");
4145 XPUSHi( getppid() );
4148 DIE(aTHX_ PL_no_func, "getppid");
4164 pgrp = (I32)BSD_GETPGRP(pid);
4166 if (pid != 0 && pid != PerlProc_getpid())
4167 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4173 DIE(aTHX_ PL_no_func, "getpgrp()");
4192 TAINT_PROPER("setpgrp");
4194 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4196 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4197 || (pid != 0 && pid != PerlProc_getpid()))
4199 DIE(aTHX_ "setpgrp can't take arguments");
4201 SETi( setpgrp() >= 0 );
4202 #endif /* USE_BSDPGRP */
4205 DIE(aTHX_ PL_no_func, "setpgrp()");
4214 #ifdef HAS_GETPRIORITY
4217 SETi( getpriority(which, who) );
4220 DIE(aTHX_ PL_no_func, "getpriority()");
4230 #ifdef HAS_SETPRIORITY
4234 TAINT_PROPER("setpriority");
4235 SETi( setpriority(which, who, niceval) >= 0 );
4238 DIE(aTHX_ PL_no_func, "setpriority()");
4248 XPUSHn( time(Null(Time_t*)) );
4250 XPUSHi( time(Null(Time_t*)) );
4255 /* XXX The POSIX name is CLK_TCK; it is to be preferred
4256 to HZ. Probably. For now, assume that if the system
4257 defines HZ, it does so correctly. (Will this break
4259 Probably we ought to use _sysconf(_SC_CLK_TCK), if
4260 it's supported. --AD 9/96.
4276 DIE(aTHX_ "times not implemented");
4281 (void)PerlProc_times(&PL_timesbuf);
4283 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4284 /* struct tms, though same data */
4288 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/HZ)));
4289 if (GIMME == G_ARRAY) {
4290 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/HZ)));
4291 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/HZ)));
4292 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/HZ)));
4295 #endif /* HAS_TIMES */
4308 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4309 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4310 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4316 when = (Time_t)SvNVx(POPs);
4318 when = (Time_t)SvIVx(POPs);
4321 if (PL_op->op_type == OP_LOCALTIME)
4322 tmbuf = localtime(&when);
4324 tmbuf = gmtime(&when);
4328 if (GIMME != G_ARRAY) {
4332 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4333 dayname[tmbuf->tm_wday],
4334 monname[tmbuf->tm_mon],
4339 tmbuf->tm_year + 1900);
4340 PUSHs(sv_2mortal(tsv));
4343 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4344 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4345 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4346 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4347 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4348 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4349 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4350 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4351 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4362 anum = alarm((unsigned int)anum);
4369 DIE(aTHX_ PL_no_func, "Unsupported function alarm");
4380 (void)time(&lasttime);
4385 PerlProc_sleep((unsigned int)duration);
4388 XPUSHi(when - lasttime);
4392 /* Shared memory. */
4406 return pp_shmwrite();
4411 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4412 dSP; dMARK; dTARGET;
4413 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4422 /* Message passing. */
4436 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4437 dSP; dMARK; dTARGET;
4438 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4449 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4450 dSP; dMARK; dTARGET;
4451 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4464 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4465 dSP; dMARK; dTARGET;
4466 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4473 DIE(aTHX_ "System V IPC is not implemented on this machine");
4479 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4480 dSP; dMARK; dTARGET;
4481 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4489 PUSHp(zero_but_true, ZBTLEN);
4499 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4500 dSP; dMARK; dTARGET;
4501 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4510 /* Get system info. */
4514 #ifdef HAS_GETHOSTBYNAME
4515 return pp_ghostent();
4517 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4523 #ifdef HAS_GETHOSTBYADDR
4524 return pp_ghostent();
4526 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4533 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4534 I32 which = PL_op->op_type;
4535 register char **elem;
4537 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4538 struct hostent *PerlSock_gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4539 struct hostent *PerlSock_gethostbyname(Netdb_name_t);
4540 struct hostent *PerlSock_gethostent(void);
4542 struct hostent *hent;
4547 if (which == OP_GHBYNAME)
4548 #ifdef HAS_GETHOSTBYNAME
4549 hent = PerlSock_gethostbyname(POPpbytex);
4551 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4553 else if (which == OP_GHBYADDR) {
4554 #ifdef HAS_GETHOSTBYADDR
4555 int addrtype = POPi;
4558 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4560 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4562 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4566 #ifdef HAS_GETHOSTENT
4567 hent = PerlSock_gethostent();
4569 DIE(aTHX_ PL_no_sock_func, "gethostent");
4572 #ifdef HOST_NOT_FOUND
4574 STATUS_NATIVE_SET(h_errno);
4577 if (GIMME != G_ARRAY) {
4578 PUSHs(sv = sv_newmortal());
4580 if (which == OP_GHBYNAME) {
4582 sv_setpvn(sv, hent->h_addr, hent->h_length);
4585 sv_setpv(sv, (char*)hent->h_name);
4591 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4592 sv_setpv(sv, (char*)hent->h_name);
4593 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4594 for (elem = hent->h_aliases; elem && *elem; elem++) {
4595 sv_catpv(sv, *elem);
4597 sv_catpvn(sv, " ", 1);
4599 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4600 sv_setiv(sv, (IV)hent->h_addrtype);
4601 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4602 len = hent->h_length;
4603 sv_setiv(sv, (IV)len);
4605 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4606 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4607 sv_setpvn(sv, *elem, len);
4610 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4612 sv_setpvn(sv, hent->h_addr, len);
4617 DIE(aTHX_ PL_no_sock_func, "gethostent");
4623 #ifdef HAS_GETNETBYNAME
4624 return pp_gnetent();
4626 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4632 #ifdef HAS_GETNETBYADDR
4633 return pp_gnetent();
4635 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4642 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4643 I32 which = PL_op->op_type;
4644 register char **elem;
4646 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4647 struct netent *PerlSock_getnetbyaddr(Netdb_net_t, int);
4648 struct netent *PerlSock_getnetbyname(Netdb_name_t);
4649 struct netent *PerlSock_getnetent(void);
4651 struct netent *nent;
4654 if (which == OP_GNBYNAME)
4655 #ifdef HAS_GETNETBYNAME
4656 nent = PerlSock_getnetbyname(POPpbytex);
4658 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4660 else if (which == OP_GNBYADDR) {
4661 #ifdef HAS_GETNETBYADDR
4662 int addrtype = POPi;
4663 Netdb_net_t addr = (Netdb_net_t) U_L(POPn);
4664 nent = PerlSock_getnetbyaddr(addr, addrtype);
4666 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4670 #ifdef HAS_GETNETENT
4671 nent = PerlSock_getnetent();
4673 DIE(aTHX_ PL_no_sock_func, "getnetent");
4677 if (GIMME != G_ARRAY) {
4678 PUSHs(sv = sv_newmortal());
4680 if (which == OP_GNBYNAME)
4681 sv_setiv(sv, (IV)nent->n_net);
4683 sv_setpv(sv, nent->n_name);
4689 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4690 sv_setpv(sv, nent->n_name);
4691 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4692 for (elem = nent->n_aliases; elem && *elem; elem++) {
4693 sv_catpv(sv, *elem);
4695 sv_catpvn(sv, " ", 1);
4697 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4698 sv_setiv(sv, (IV)nent->n_addrtype);
4699 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4700 sv_setiv(sv, (IV)nent->n_net);
4705 DIE(aTHX_ PL_no_sock_func, "getnetent");
4711 #ifdef HAS_GETPROTOBYNAME
4712 return pp_gprotoent();
4714 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4720 #ifdef HAS_GETPROTOBYNUMBER
4721 return pp_gprotoent();
4723 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4730 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4731 I32 which = PL_op->op_type;
4732 register char **elem;
4734 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4735 struct protoent *PerlSock_getprotobyname(Netdb_name_t);
4736 struct protoent *PerlSock_getprotobynumber(int);
4737 struct protoent *PerlSock_getprotoent(void);
4739 struct protoent *pent;
4742 if (which == OP_GPBYNAME)
4743 #ifdef HAS_GETPROTOBYNAME
4744 pent = PerlSock_getprotobyname(POPpbytex);
4746 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4748 else if (which == OP_GPBYNUMBER)
4749 #ifdef HAS_GETPROTOBYNUMBER
4750 pent = PerlSock_getprotobynumber(POPi);
4752 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4755 #ifdef HAS_GETPROTOENT
4756 pent = PerlSock_getprotoent();
4758 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4762 if (GIMME != G_ARRAY) {
4763 PUSHs(sv = sv_newmortal());
4765 if (which == OP_GPBYNAME)
4766 sv_setiv(sv, (IV)pent->p_proto);
4768 sv_setpv(sv, pent->p_name);
4774 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4775 sv_setpv(sv, pent->p_name);
4776 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4777 for (elem = pent->p_aliases; elem && *elem; elem++) {
4778 sv_catpv(sv, *elem);
4780 sv_catpvn(sv, " ", 1);
4782 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4783 sv_setiv(sv, (IV)pent->p_proto);
4788 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4794 #ifdef HAS_GETSERVBYNAME
4795 return pp_gservent();
4797 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4803 #ifdef HAS_GETSERVBYPORT
4804 return pp_gservent();
4806 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4813 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4814 I32 which = PL_op->op_type;
4815 register char **elem;
4817 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4818 struct servent *PerlSock_getservbyname(Netdb_name_t, Netdb_name_t);
4819 struct servent *PerlSock_getservbyport(int, Netdb_name_t);
4820 struct servent *PerlSock_getservent(void);
4822 struct servent *sent;
4825 if (which == OP_GSBYNAME) {
4826 #ifdef HAS_GETSERVBYNAME
4827 char *proto = POPpbytex;
4828 char *name = POPpbytex;
4830 if (proto && !*proto)
4833 sent = PerlSock_getservbyname(name, proto);
4835 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4838 else if (which == OP_GSBYPORT) {
4839 #ifdef HAS_GETSERVBYPORT
4840 char *proto = POPpbytex;
4841 unsigned short port = POPu;
4844 port = PerlSock_htons(port);
4846 sent = PerlSock_getservbyport(port, proto);
4848 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4852 #ifdef HAS_GETSERVENT
4853 sent = PerlSock_getservent();
4855 DIE(aTHX_ PL_no_sock_func, "getservent");
4859 if (GIMME != G_ARRAY) {
4860 PUSHs(sv = sv_newmortal());
4862 if (which == OP_GSBYNAME) {
4864 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4866 sv_setiv(sv, (IV)(sent->s_port));
4870 sv_setpv(sv, sent->s_name);
4876 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4877 sv_setpv(sv, sent->s_name);
4878 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4879 for (elem = sent->s_aliases; elem && *elem; elem++) {
4880 sv_catpv(sv, *elem);
4882 sv_catpvn(sv, " ", 1);
4884 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4886 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4888 sv_setiv(sv, (IV)(sent->s_port));
4890 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4891 sv_setpv(sv, sent->s_proto);
4896 DIE(aTHX_ PL_no_sock_func, "getservent");
4903 #ifdef HAS_SETHOSTENT
4904 PerlSock_sethostent(TOPi);
4907 DIE(aTHX_ PL_no_sock_func, "sethostent");
4914 #ifdef HAS_SETNETENT
4915 PerlSock_setnetent(TOPi);
4918 DIE(aTHX_ PL_no_sock_func, "setnetent");
4925 #ifdef HAS_SETPROTOENT
4926 PerlSock_setprotoent(TOPi);
4929 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4936 #ifdef HAS_SETSERVENT
4937 PerlSock_setservent(TOPi);
4940 DIE(aTHX_ PL_no_sock_func, "setservent");
4947 #ifdef HAS_ENDHOSTENT
4948 PerlSock_endhostent();
4952 DIE(aTHX_ PL_no_sock_func, "endhostent");
4959 #ifdef HAS_ENDNETENT
4960 PerlSock_endnetent();
4964 DIE(aTHX_ PL_no_sock_func, "endnetent");
4971 #ifdef HAS_ENDPROTOENT
4972 PerlSock_endprotoent();
4976 DIE(aTHX_ PL_no_sock_func, "endprotoent");
4983 #ifdef HAS_ENDSERVENT
4984 PerlSock_endservent();
4988 DIE(aTHX_ PL_no_sock_func, "endservent");
4997 DIE(aTHX_ PL_no_func, "getpwnam");
5006 DIE(aTHX_ PL_no_func, "getpwuid");
5014 I32 which = PL_op->op_type;
5017 struct passwd *pwent = NULL;
5019 * We currently support only the SysV getsp* shadow password interface.
5020 * The interface is declared in <shadow.h> and often one needs to link
5021 * with -lsecurity or some such.
5022 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5025 * AIX getpwnam() is clever enough to return the encrypted password
5026 * only if the caller (euid?) is root.
5028 * There are at least two other shadow password APIs. Many platforms
5029 * seem to contain more than one interface for accessing the shadow
5030 * password databases, possibly for compatibility reasons.
5031 * The getsp*() is by far he simplest one, the other two interfaces
5032 * are much more complicated, but also very similar to each other.
5037 * struct pr_passwd *getprpw*();
5038 * The password is in
5039 * char getprpw*(...).ufld.fd_encrypt[]
5040 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5045 * struct es_passwd *getespw*();
5046 * The password is in
5047 * char *(getespw*(...).ufld.fd_encrypt)
5048 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5050 * Mention I_PROT here so that Configure probes for it.
5052 * In HP-UX for getprpw*() the manual page claims that one should include
5053 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5054 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5055 * and pp_sys.c already includes <shadow.h> if there is such.
5057 * Note that <sys/security.h> is already probed for, but currently
5058 * it is only included in special cases.
5060 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5061 * be preferred interface, even though also the getprpw*() interface
5062 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5063 * One also needs to call set_auth_parameters() in main() before
5064 * doing anything else, whether one is using getespw*() or getprpw*().
5066 * Note that accessing the shadow databases can be magnitudes
5067 * slower than accessing the standard databases.
5074 pwent = getpwnam(POPpbytex);
5077 pwent = getpwuid((Uid_t)POPi);
5080 # ifdef HAS_GETPWENT
5083 DIE(aTHX_ PL_no_func, "getpwent");
5089 if (GIMME != G_ARRAY) {
5090 PUSHs(sv = sv_newmortal());
5092 if (which == OP_GPWNAM)
5093 # if Uid_t_sign <= 0
5094 sv_setiv(sv, (IV)pwent->pw_uid);
5096 sv_setuv(sv, (UV)pwent->pw_uid);
5099 sv_setpv(sv, pwent->pw_name);
5105 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5106 sv_setpv(sv, pwent->pw_name);
5108 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5110 /* If we have getspnam(), we try to dig up the shadow
5111 * password. If we are underprivileged, the shadow
5112 * interface will set the errno to EACCES or similar,
5113 * and return a null pointer. If this happens, we will
5114 * use the dummy password (usually "*" or "x") from the
5115 * standard password database.
5117 * In theory we could skip the shadow call completely
5118 * if euid != 0 but in practice we cannot know which
5119 * security measures are guarding the shadow databases
5120 * on a random platform.
5122 * Resist the urge to use additional shadow interfaces.
5123 * Divert the urge to writing an extension instead.
5126 # ifdef HAS_GETSPNAM
5128 struct spwd *spwent;
5129 int saverrno; /* Save and restore errno so that
5130 * underprivileged attempts seem
5131 * to have never made the unsccessful
5132 * attempt to retrieve the shadow password. */
5135 spwent = getspnam(pwent->pw_name);
5137 if (spwent && spwent->sp_pwdp)
5138 sv_setpv(sv, spwent->sp_pwdp);
5142 if (!SvPOK(sv)) /* Use the standard password, then. */
5143 sv_setpv(sv, pwent->pw_passwd);
5146 # ifndef INCOMPLETE_TAINTS
5147 /* passwd is tainted because user himself can diddle with it.
5148 * admittedly not much and in a very limited way, but nevertheless. */
5152 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5153 # if Uid_t_sign <= 0
5154 sv_setiv(sv, (IV)pwent->pw_uid);
5156 sv_setuv(sv, (UV)pwent->pw_uid);
5159 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5160 # if Uid_t_sign <= 0
5161 sv_setiv(sv, (IV)pwent->pw_gid);
5163 sv_setuv(sv, (UV)pwent->pw_gid);
5165 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5166 * because of the poor interface of the Perl getpw*(),
5167 * not because there's some standard/convention saying so.
5168 * A better interface would have been to return a hash,
5169 * but we are accursed by our history, alas. --jhi. */
5170 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5172 sv_setiv(sv, (IV)pwent->pw_change);
5175 sv_setiv(sv, (IV)pwent->pw_quota);
5178 sv_setpv(sv, pwent->pw_age);
5183 /* pw_class and pw_comment are mutually exclusive--.
5184 * see the above note for pw_change, pw_quota, and pw_age. */
5185 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5187 sv_setpv(sv, pwent->pw_class);
5190 sv_setpv(sv, pwent->pw_comment);
5194 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5196 sv_setpv(sv, pwent->pw_gecos);
5198 # ifndef INCOMPLETE_TAINTS
5199 /* pw_gecos is tainted because user himself can diddle with it. */
5203 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5204 sv_setpv(sv, pwent->pw_dir);
5206 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5207 sv_setpv(sv, pwent->pw_shell);
5208 # ifndef INCOMPLETE_TAINTS
5209 /* pw_shell is tainted because user himself can diddle with it. */
5214 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5215 sv_setiv(sv, (IV)pwent->pw_expire);
5220 DIE(aTHX_ PL_no_func, "getpwent");
5227 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5231 DIE(aTHX_ PL_no_func, "setpwent");
5238 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5242 DIE(aTHX_ PL_no_func, "endpwent");
5251 DIE(aTHX_ PL_no_func, "getgrnam");
5260 DIE(aTHX_ PL_no_func, "getgrgid");
5268 I32 which = PL_op->op_type;
5269 register char **elem;
5271 struct group *grent;
5274 if (which == OP_GGRNAM)
5275 grent = (struct group *)getgrnam(POPpbytex);
5276 else if (which == OP_GGRGID)
5277 grent = (struct group *)getgrgid(POPi);
5280 grent = (struct group *)getgrent();
5282 DIE(aTHX_ PL_no_func, "getgrent");
5286 if (GIMME != G_ARRAY) {
5287 PUSHs(sv = sv_newmortal());
5289 if (which == OP_GGRNAM)
5290 sv_setiv(sv, (IV)grent->gr_gid);
5292 sv_setpv(sv, grent->gr_name);
5298 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5299 sv_setpv(sv, grent->gr_name);
5301 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5303 sv_setpv(sv, grent->gr_passwd);
5306 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5307 sv_setiv(sv, (IV)grent->gr_gid);
5309 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5310 for (elem = grent->gr_mem; elem && *elem; elem++) {
5311 sv_catpv(sv, *elem);
5313 sv_catpvn(sv, " ", 1);
5319 DIE(aTHX_ PL_no_func, "getgrent");
5326 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5330 DIE(aTHX_ PL_no_func, "setgrent");
5337 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5341 DIE(aTHX_ PL_no_func, "endgrent");
5351 if (!(tmps = PerlProc_getlogin()))
5353 PUSHp(tmps, strlen(tmps));
5356 DIE(aTHX_ PL_no_func, "getlogin");
5360 /* Miscellaneous. */
5365 dSP; dMARK; dORIGMARK; dTARGET;
5366 register I32 items = SP - MARK;
5367 unsigned long a[20];
5373 while (++MARK <= SP) {
5374 if (SvTAINTED(*MARK)) {
5380 TAINT_PROPER("syscall");
5383 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5384 * or where sizeof(long) != sizeof(char*). But such machines will
5385 * not likely have syscall implemented either, so who cares?
5387 while (++MARK <= SP) {
5388 if (SvNIOK(*MARK) || !i)
5389 a[i++] = SvIV(*MARK);
5390 else if (*MARK == &PL_sv_undef)
5393 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5399 DIE(aTHX_ "Too many args to syscall");
5401 DIE(aTHX_ "Too few args to syscall");
5403 retval = syscall(a[0]);
5406 retval = syscall(a[0],a[1]);
5409 retval = syscall(a[0],a[1],a[2]);
5412 retval = syscall(a[0],a[1],a[2],a[3]);
5415 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5418 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5421 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5424 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5428 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5431 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5434 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5438 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5442 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5446 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5447 a[10],a[11],a[12],a[13]);
5449 #endif /* atarist */
5455 DIE(aTHX_ PL_no_func, "syscall");
5459 #ifdef FCNTL_EMULATE_FLOCK
5461 /* XXX Emulate flock() with fcntl().
5462 What's really needed is a good file locking module.
5466 fcntl_emulate_flock(int fd, int operation)
5470 switch (operation & ~LOCK_NB) {
5472 flock.l_type = F_RDLCK;
5475 flock.l_type = F_WRLCK;
5478 flock.l_type = F_UNLCK;
5484 flock.l_whence = SEEK_SET;
5485 flock.l_start = flock.l_len = (Off_t)0;
5487 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5490 #endif /* FCNTL_EMULATE_FLOCK */
5492 #ifdef LOCKF_EMULATE_FLOCK
5494 /* XXX Emulate flock() with lockf(). This is just to increase
5495 portability of scripts. The calls are not completely
5496 interchangeable. What's really needed is a good file
5500 /* The lockf() constants might have been defined in <unistd.h>.
5501 Unfortunately, <unistd.h> causes troubles on some mixed
5502 (BSD/POSIX) systems, such as SunOS 4.1.3.
5504 Further, the lockf() constants aren't POSIX, so they might not be
5505 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5506 just stick in the SVID values and be done with it. Sigh.
5510 # define F_ULOCK 0 /* Unlock a previously locked region */
5513 # define F_LOCK 1 /* Lock a region for exclusive use */
5516 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5519 # define F_TEST 3 /* Test a region for other processes locks */
5523 lockf_emulate_flock(int fd, int operation)
5529 /* flock locks entire file so for lockf we need to do the same */
5531 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5532 if (pos > 0) /* is seekable and needs to be repositioned */
5533 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5534 pos = -1; /* seek failed, so don't seek back afterwards */
5537 switch (operation) {
5539 /* LOCK_SH - get a shared lock */
5541 /* LOCK_EX - get an exclusive lock */
5543 i = lockf (fd, F_LOCK, 0);
5546 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5547 case LOCK_SH|LOCK_NB:
5548 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5549 case LOCK_EX|LOCK_NB:
5550 i = lockf (fd, F_TLOCK, 0);
5552 if ((errno == EAGAIN) || (errno == EACCES))
5553 errno = EWOULDBLOCK;
5556 /* LOCK_UN - unlock (non-blocking is a no-op) */
5558 case LOCK_UN|LOCK_NB:
5559 i = lockf (fd, F_ULOCK, 0);
5562 /* Default - can't decipher operation */
5569 if (pos > 0) /* need to restore position of the handle */
5570 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5575 #endif /* LOCKF_EMULATE_FLOCK */