This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pp_sys.c: pp_waitpid and EINTR
[perl5.git] / pp_sys.c
1 /*    pp_sys.c
2  *
3  *    Copyright (C) 1995, 1996, 1997, 1998, 1999,
4  *    2000, 2001, 2002, 2003, by Larry Wall and others
5  *
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.
8  *
9  */
10
11 /*
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.
16  */
17
18 #include "EXTERN.h"
19 #define PERL_IN_PP_SYS_C
20 #include "perl.h"
21
22 #ifdef I_SHADOW
23 /* Shadow password support for solaris - pdo@cs.umd.edu
24  * Not just Solaris: at least HP-UX, IRIX, Linux.
25  * The API is from SysV.
26  *
27  * There are at least two more shadow interfaces,
28  * see the comments in pp_gpwent().
29  *
30  * --jhi */
31 #   ifdef __hpux__
32 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
33  * and another MAXINT from "perl.h" <- <sys/param.h>. */
34 #       undef MAXINT
35 #   endif
36 #   include <shadow.h>
37 #endif
38
39 #ifdef HAS_SYSCALL
40 #ifdef __cplusplus
41 extern "C" int syscall(unsigned long,...);
42 #endif
43 #endif
44
45 #ifdef I_SYS_WAIT
46 # include <sys/wait.h>
47 #endif
48
49 #ifdef I_SYS_RESOURCE
50 # include <sys/resource.h>
51 #endif
52
53 #ifdef NETWARE
54 NETDB_DEFINE_CONTEXT
55 #endif
56
57 #ifdef HAS_SELECT
58 # ifdef I_SYS_SELECT
59 #  include <sys/select.h>
60 # endif
61 #endif
62
63 /* XXX Configure test needed.
64    h_errno might not be a simple 'int', especially for multi-threaded
65    applications, see "extern int errno in perl.h".  Creating such
66    a test requires taking into account the differences between
67    compiling multithreaded and singlethreaded ($ccflags et al).
68    HOST_NOT_FOUND is typically defined in <netdb.h>.
69 */
70 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
71 extern int h_errno;
72 #endif
73
74 #ifdef HAS_PASSWD
75 # ifdef I_PWD
76 #  include <pwd.h>
77 # else
78 #  if !defined(VMS)
79     struct passwd *getpwnam (char *);
80     struct passwd *getpwuid (Uid_t);
81 #  endif
82 # endif
83 # ifdef HAS_GETPWENT
84 #ifndef getpwent
85   struct passwd *getpwent (void);
86 #elif defined (VMS) && defined (my_getpwent)
87   struct passwd *Perl_my_getpwent (void);
88 #endif
89 # endif
90 #endif
91
92 #ifdef HAS_GROUP
93 # ifdef I_GRP
94 #  include <grp.h>
95 # else
96     struct group *getgrnam (char *);
97     struct group *getgrgid (Gid_t);
98 # endif
99 # ifdef HAS_GETGRENT
100 #ifndef getgrent
101     struct group *getgrent (void);
102 #endif
103 # endif
104 #endif
105
106 #ifdef I_UTIME
107 #  if defined(_MSC_VER) || defined(__MINGW32__)
108 #    include <sys/utime.h>
109 #  else
110 #    include <utime.h>
111 #  endif
112 #endif
113
114 #ifdef HAS_CHSIZE
115 # ifdef my_chsize  /* Probably #defined to Perl_my_chsize in embed.h */
116 #   undef my_chsize
117 # endif
118 # define my_chsize PerlLIO_chsize
119 #endif
120
121 #ifdef HAS_FLOCK
122 #  define FLOCK flock
123 #else /* no flock() */
124
125    /* fcntl.h might not have been included, even if it exists, because
126       the current Configure only sets I_FCNTL if it's needed to pick up
127       the *_OK constants.  Make sure it has been included before testing
128       the fcntl() locking constants. */
129 #  if defined(HAS_FCNTL) && !defined(I_FCNTL)
130 #    include <fcntl.h>
131 #  endif
132
133 #  if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
134 #    define FLOCK fcntl_emulate_flock
135 #    define FCNTL_EMULATE_FLOCK
136 #  else /* no flock() or fcntl(F_SETLK,...) */
137 #    ifdef HAS_LOCKF
138 #      define FLOCK lockf_emulate_flock
139 #      define LOCKF_EMULATE_FLOCK
140 #    endif /* lockf */
141 #  endif /* no flock() or fcntl(F_SETLK,...) */
142
143 #  ifdef FLOCK
144      static int FLOCK (int, int);
145
146     /*
147      * These are the flock() constants.  Since this sytems doesn't have
148      * flock(), the values of the constants are probably not available.
149      */
150 #    ifndef LOCK_SH
151 #      define LOCK_SH 1
152 #    endif
153 #    ifndef LOCK_EX
154 #      define LOCK_EX 2
155 #    endif
156 #    ifndef LOCK_NB
157 #      define LOCK_NB 4
158 #    endif
159 #    ifndef LOCK_UN
160 #      define LOCK_UN 8
161 #    endif
162 #  endif /* emulating flock() */
163
164 #endif /* no flock() */
165
166 #define ZBTLEN 10
167 static char zero_but_true[ZBTLEN + 1] = "0 but true";
168
169 #if defined(I_SYS_ACCESS) && !defined(R_OK)
170 #  include <sys/access.h>
171 #endif
172
173 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
174 #  define FD_CLOEXEC 1          /* NeXT needs this */
175 #endif
176
177 #include "reentr.h"
178
179 #ifdef __Lynx__
180 /* Missing protos on LynxOS */
181 void sethostent(int);
182 void endhostent(void);
183 void setnetent(int);
184 void endnetent(void);
185 void setprotoent(int);
186 void endprotoent(void);
187 void setservent(int);
188 void endservent(void);
189 #endif
190
191 #undef PERL_EFF_ACCESS_R_OK     /* EFFective uid/gid ACCESS R_OK */
192 #undef PERL_EFF_ACCESS_W_OK
193 #undef PERL_EFF_ACCESS_X_OK
194
195 /* F_OK unused: if stat() cannot find it... */
196
197 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
198     /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
199 #   define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
200 #   define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
201 #   define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
202 #endif
203
204 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
205 #   ifdef I_SYS_SECURITY
206 #       include <sys/security.h>
207 #   endif
208 #   ifdef ACC_SELF
209         /* HP SecureWare */
210 #       define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
211 #       define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
212 #       define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
213 #   else
214         /* SCO */
215 #       define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
216 #       define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
217 #       define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
218 #   endif
219 #endif
220
221 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
222     /* AIX */
223 #   define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
224 #   define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
225 #   define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
226 #endif
227
228 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS)       \
229     && (defined(HAS_SETREUID) || defined(HAS_SETRESUID)         \
230         || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
231 /* The Hard Way. */
232 STATIC int
233 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
234 {
235     Uid_t ruid = getuid();
236     Uid_t euid = geteuid();
237     Gid_t rgid = getgid();
238     Gid_t egid = getegid();
239     int res;
240
241     LOCK_CRED_MUTEX;
242 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
243     Perl_croak(aTHX_ "switching effective uid is not implemented");
244 #else
245 #ifdef HAS_SETREUID
246     if (setreuid(euid, ruid))
247 #else
248 #ifdef HAS_SETRESUID
249     if (setresuid(euid, ruid, (Uid_t)-1))
250 #endif
251 #endif
252         Perl_croak(aTHX_ "entering effective uid failed");
253 #endif
254
255 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
256     Perl_croak(aTHX_ "switching effective gid is not implemented");
257 #else
258 #ifdef HAS_SETREGID
259     if (setregid(egid, rgid))
260 #else
261 #ifdef HAS_SETRESGID
262     if (setresgid(egid, rgid, (Gid_t)-1))
263 #endif
264 #endif
265         Perl_croak(aTHX_ "entering effective gid failed");
266 #endif
267
268     res = access(path, mode);
269
270 #ifdef HAS_SETREUID
271     if (setreuid(ruid, euid))
272 #else
273 #ifdef HAS_SETRESUID
274     if (setresuid(ruid, euid, (Uid_t)-1))
275 #endif
276 #endif
277         Perl_croak(aTHX_ "leaving effective uid failed");
278
279 #ifdef HAS_SETREGID
280     if (setregid(rgid, egid))
281 #else
282 #ifdef HAS_SETRESGID
283     if (setresgid(rgid, egid, (Gid_t)-1))
284 #endif
285 #endif
286         Perl_croak(aTHX_ "leaving effective gid failed");
287     UNLOCK_CRED_MUTEX;
288
289     return res;
290 }
291 #   define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
292 #   define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
293 #   define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
294 #endif
295
296 #if !defined(PERL_EFF_ACCESS_R_OK)
297 /* With it or without it: anyway you get a warning: either that
298    it is unused, or it is declared static and never defined.
299  */
300 STATIC int
301 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
302 {
303     Perl_croak(aTHX_ "switching effective uid is not implemented");
304     /*NOTREACHED*/
305     return -1;
306 }
307 #endif
308
309 PP(pp_backtick)
310 {
311     dSP; dTARGET;
312     PerlIO *fp;
313     STRLEN n_a;
314     char *tmps = POPpx;
315     I32 gimme = GIMME_V;
316     char *mode = "r";
317
318     TAINT_PROPER("``");
319     if (PL_op->op_private & OPpOPEN_IN_RAW)
320         mode = "rb";
321     else if (PL_op->op_private & OPpOPEN_IN_CRLF)
322         mode = "rt";
323     fp = PerlProc_popen(tmps, mode);
324     if (fp) {
325         char *type = NULL;
326         if (PL_curcop->cop_io) {
327             type = SvPV_nolen(PL_curcop->cop_io);
328         }
329         if (type && *type)
330             PerlIO_apply_layers(aTHX_ fp,mode,type);
331
332         if (gimme == G_VOID) {
333             char tmpbuf[256];
334             while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
335                 /*SUPPRESS 530*/
336                 ;
337         }
338         else if (gimme == G_SCALAR) {
339             SV *oldrs = PL_rs;
340             PL_rs = &PL_sv_undef;
341             sv_setpv(TARG, ""); /* note that this preserves previous buffer */
342             while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
343                 /*SUPPRESS 530*/
344                 ;
345             PL_rs = oldrs;
346             XPUSHs(TARG);
347             SvTAINTED_on(TARG);
348         }
349         else {
350             SV *sv;
351
352             for (;;) {
353                 sv = NEWSV(56, 79);
354                 if (sv_gets(sv, fp, 0) == Nullch) {
355                     SvREFCNT_dec(sv);
356                     break;
357                 }
358                 XPUSHs(sv_2mortal(sv));
359                 if (SvLEN(sv) - SvCUR(sv) > 20) {
360                     SvLEN_set(sv, SvCUR(sv)+1);
361                     Renew(SvPVX(sv), SvLEN(sv), char);
362                 }
363                 SvTAINTED_on(sv);
364             }
365         }
366         STATUS_NATIVE_SET(PerlProc_pclose(fp));
367         TAINT;          /* "I believe that this is not gratuitous!" */
368     }
369     else {
370         STATUS_NATIVE_SET(-1);
371         if (gimme == G_SCALAR)
372             RETPUSHUNDEF;
373     }
374
375     RETURN;
376 }
377
378 PP(pp_glob)
379 {
380     OP *result;
381     tryAMAGICunTARGET(iter, -1);
382
383     /* Note that we only ever get here if File::Glob fails to load
384      * without at the same time croaking, for some reason, or if
385      * perl was built with PERL_EXTERNAL_GLOB */
386
387     ENTER;
388
389 #ifndef VMS
390     if (PL_tainting) {
391         /*
392          * The external globbing program may use things we can't control,
393          * so for security reasons we must assume the worst.
394          */
395         TAINT;
396         taint_proper(PL_no_security, "glob");
397     }
398 #endif /* !VMS */
399
400     SAVESPTR(PL_last_in_gv);    /* We don't want this to be permanent. */
401     PL_last_in_gv = (GV*)*PL_stack_sp--;
402
403     SAVESPTR(PL_rs);            /* This is not permanent, either. */
404     PL_rs = sv_2mortal(newSVpvn("\000", 1));
405 #ifndef DOSISH
406 #ifndef CSH
407     *SvPVX(PL_rs) = '\n';
408 #endif  /* !CSH */
409 #endif  /* !DOSISH */
410
411     result = do_readline();
412     LEAVE;
413     return result;
414 }
415
416 PP(pp_rcatline)
417 {
418     PL_last_in_gv = cGVOP_gv;
419     return do_readline();
420 }
421
422 PP(pp_warn)
423 {
424     dSP; dMARK;
425     SV *tmpsv;
426     char *tmps;
427     STRLEN len;
428     if (SP - MARK != 1) {
429         dTARGET;
430         do_join(TARG, &PL_sv_no, MARK, SP);
431         tmpsv = TARG;
432         SP = MARK + 1;
433     }
434     else {
435         tmpsv = TOPs;
436     }
437     tmps = SvPV(tmpsv, len);
438     if ((!tmps || !len) && PL_errgv) {
439         SV *error = ERRSV;
440         (void)SvUPGRADE(error, SVt_PV);
441         if (SvPOK(error) && SvCUR(error))
442             sv_catpv(error, "\t...caught");
443         tmpsv = error;
444         tmps = SvPV(tmpsv, len);
445     }
446     if (!tmps || !len)
447         tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
448
449     Perl_warn(aTHX_ "%"SVf, tmpsv);
450     RETSETYES;
451 }
452
453 PP(pp_die)
454 {
455     dSP; dMARK;
456     char *tmps;
457     SV *tmpsv;
458     STRLEN len;
459     bool multiarg = 0;
460 #ifdef VMS
461     VMSISH_HUSHED  = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
462 #endif
463     if (SP - MARK != 1) {
464         dTARGET;
465         do_join(TARG, &PL_sv_no, MARK, SP);
466         tmpsv = TARG;
467         tmps = SvPV(tmpsv, len);
468         multiarg = 1;
469         SP = MARK + 1;
470     }
471     else {
472         tmpsv = TOPs;
473         tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
474     }
475     if (!tmps || !len) {
476         SV *error = ERRSV;
477         (void)SvUPGRADE(error, SVt_PV);
478         if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
479             if (!multiarg)
480                 SvSetSV(error,tmpsv);
481             else if (sv_isobject(error)) {
482                 HV *stash = SvSTASH(SvRV(error));
483                 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
484                 if (gv) {
485                     SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
486                     SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
487                     EXTEND(SP, 3);
488                     PUSHMARK(SP);
489                     PUSHs(error);
490                     PUSHs(file);
491                     PUSHs(line);
492                     PUTBACK;
493                     call_sv((SV*)GvCV(gv),
494                             G_SCALAR|G_EVAL|G_KEEPERR);
495                     sv_setsv(error,*PL_stack_sp--);
496                 }
497             }
498             DIE(aTHX_ Nullformat);
499         }
500         else {
501             if (SvPOK(error) && SvCUR(error))
502                 sv_catpv(error, "\t...propagated");
503             tmpsv = error;
504             tmps = SvPV(tmpsv, len);
505         }
506     }
507     if (!tmps || !len)
508         tmpsv = sv_2mortal(newSVpvn("Died", 4));
509
510     DIE(aTHX_ "%"SVf, tmpsv);
511 }
512
513 /* I/O. */
514
515 PP(pp_open)
516 {
517     dSP;
518     dMARK; dORIGMARK;
519     dTARGET;
520     GV *gv;
521     SV *sv;
522     IO *io;
523     char *tmps;
524     STRLEN len;
525     MAGIC *mg;
526     bool  ok;
527
528     gv = (GV *)*++MARK;
529     if (!isGV(gv))
530         DIE(aTHX_ PL_no_usym, "filehandle");
531     if ((io = GvIOp(gv)))
532         IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
533
534     if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
535         /* Method's args are same as ours ... */
536         /* ... except handle is replaced by the object */
537         *MARK-- = SvTIED_obj((SV*)io, mg);
538         PUSHMARK(MARK);
539         PUTBACK;
540         ENTER;
541         call_method("OPEN", G_SCALAR);
542         LEAVE;
543         SPAGAIN;
544         RETURN;
545     }
546
547     if (MARK < SP) {
548         sv = *++MARK;
549     }
550     else {
551         sv = GvSV(gv);
552     }
553
554     tmps = SvPV(sv, len);
555     ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
556     SP = ORIGMARK;
557     if (ok)
558         PUSHi( (I32)PL_forkprocess );
559     else if (PL_forkprocess == 0)               /* we are a new child */
560         PUSHi(0);
561     else
562         RETPUSHUNDEF;
563     RETURN;
564 }
565
566 PP(pp_close)
567 {
568     dSP;
569     GV *gv;
570     IO *io;
571     MAGIC *mg;
572
573     if (MAXARG == 0)
574         gv = PL_defoutgv;
575     else
576         gv = (GV*)POPs;
577
578     if (gv && (io = GvIO(gv))
579         && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
580     {
581         PUSHMARK(SP);
582         XPUSHs(SvTIED_obj((SV*)io, mg));
583         PUTBACK;
584         ENTER;
585         call_method("CLOSE", G_SCALAR);
586         LEAVE;
587         SPAGAIN;
588         RETURN;
589     }
590     EXTEND(SP, 1);
591     PUSHs(boolSV(do_close(gv, TRUE)));
592     RETURN;
593 }
594
595 PP(pp_pipe_op)
596 {
597 #ifdef HAS_PIPE
598     dSP;
599     GV *rgv;
600     GV *wgv;
601     register IO *rstio;
602     register IO *wstio;
603     int fd[2];
604
605     wgv = (GV*)POPs;
606     rgv = (GV*)POPs;
607
608     if (!rgv || !wgv)
609         goto badexit;
610
611     if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
612         DIE(aTHX_ PL_no_usym, "filehandle");
613     rstio = GvIOn(rgv);
614     wstio = GvIOn(wgv);
615
616     if (IoIFP(rstio))
617         do_close(rgv, FALSE);
618     if (IoIFP(wstio))
619         do_close(wgv, FALSE);
620
621     if (PerlProc_pipe(fd) < 0)
622         goto badexit;
623
624     IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
625     IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
626     IoOFP(rstio) = IoIFP(rstio);
627     IoIFP(wstio) = IoOFP(wstio);
628     IoTYPE(rstio) = IoTYPE_RDONLY;
629     IoTYPE(wstio) = IoTYPE_WRONLY;
630
631     if (!IoIFP(rstio) || !IoOFP(wstio)) {
632         if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
633         else PerlLIO_close(fd[0]);
634         if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
635         else PerlLIO_close(fd[1]);
636         goto badexit;
637     }
638 #if defined(HAS_FCNTL) && defined(F_SETFD)
639     fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd);   /* ensure close-on-exec */
640     fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd);   /* ensure close-on-exec */
641 #endif
642     RETPUSHYES;
643
644 badexit:
645     RETPUSHUNDEF;
646 #else
647     DIE(aTHX_ PL_no_func, "pipe");
648 #endif
649 }
650
651 PP(pp_fileno)
652 {
653     dSP; dTARGET;
654     GV *gv;
655     IO *io;
656     PerlIO *fp;
657     MAGIC  *mg;
658
659     if (MAXARG < 1)
660         RETPUSHUNDEF;
661     gv = (GV*)POPs;
662
663     if (gv && (io = GvIO(gv))
664         && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
665     {
666         PUSHMARK(SP);
667         XPUSHs(SvTIED_obj((SV*)io, mg));
668         PUTBACK;
669         ENTER;
670         call_method("FILENO", G_SCALAR);
671         LEAVE;
672         SPAGAIN;
673         RETURN;
674     }
675
676     if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
677         /* Can't do this because people seem to do things like
678            defined(fileno($foo)) to check whether $foo is a valid fh.
679           if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
680               report_evil_fh(gv, io, PL_op->op_type);
681             */
682         RETPUSHUNDEF;
683     }
684
685     PUSHi(PerlIO_fileno(fp));
686     RETURN;
687 }
688
689 PP(pp_umask)
690 {
691     dSP; dTARGET;
692 #ifdef HAS_UMASK
693     Mode_t anum;
694
695     if (MAXARG < 1) {
696         anum = PerlLIO_umask(0);
697         (void)PerlLIO_umask(anum);
698     }
699     else
700         anum = PerlLIO_umask(POPi);
701     TAINT_PROPER("umask");
702     XPUSHi(anum);
703 #else
704     /* Only DIE if trying to restrict permissions on `user' (self).
705      * Otherwise it's harmless and more useful to just return undef
706      * since 'group' and 'other' concepts probably don't exist here. */
707     if (MAXARG >= 1 && (POPi & 0700))
708         DIE(aTHX_ "umask not implemented");
709     XPUSHs(&PL_sv_undef);
710 #endif
711     RETURN;
712 }
713
714 PP(pp_binmode)
715 {
716     dSP;
717     GV *gv;
718     IO *io;
719     PerlIO *fp;
720     MAGIC *mg;
721     SV *discp = Nullsv;
722
723     if (MAXARG < 1)
724         RETPUSHUNDEF;
725     if (MAXARG > 1) {
726         discp = POPs;
727     }
728
729     gv = (GV*)POPs;
730
731     if (gv && (io = GvIO(gv))
732         && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
733     {
734         PUSHMARK(SP);
735         XPUSHs(SvTIED_obj((SV*)io, mg));
736         if (discp)
737             XPUSHs(discp);
738         PUTBACK;
739         ENTER;
740         call_method("BINMODE", G_SCALAR);
741         LEAVE;
742         SPAGAIN;
743         RETURN;
744     }
745
746     EXTEND(SP, 1);
747     if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
748         if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
749             report_evil_fh(gv, io, PL_op->op_type);
750         SETERRNO(EBADF,RMS_IFI);
751         RETPUSHUNDEF;
752     }
753
754     PUTBACK;
755     if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
756                        (discp) ? SvPV_nolen(discp) : Nullch)) {
757         if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
758              if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
759                         mode_from_discipline(discp),
760                        (discp) ? SvPV_nolen(discp) : Nullch)) {
761                 SPAGAIN;
762                 RETPUSHUNDEF;
763              }
764         }
765         SPAGAIN;
766         RETPUSHYES;
767     }
768     else {
769         SPAGAIN;
770         RETPUSHUNDEF;
771     }
772 }
773
774 PP(pp_tie)
775 {
776     dSP;
777     dMARK;
778     SV *varsv;
779     HV* stash;
780     GV *gv;
781     SV *sv;
782     I32 markoff = MARK - PL_stack_base;
783     char *methname;
784     int how = PERL_MAGIC_tied;
785     U32 items;
786
787     varsv = *++MARK;
788     switch(SvTYPE(varsv)) {
789         case SVt_PVHV:
790             methname = "TIEHASH";
791             HvEITER((HV *)varsv) = Null(HE *);
792             break;
793         case SVt_PVAV:
794             methname = "TIEARRAY";
795             break;
796         case SVt_PVGV:
797 #ifdef GV_UNIQUE_CHECK
798             if (GvUNIQUE((GV*)varsv)) {
799                 Perl_croak(aTHX_ "Attempt to tie unique GV");
800             }
801 #endif
802             methname = "TIEHANDLE";
803             how = PERL_MAGIC_tiedscalar;
804             /* For tied filehandles, we apply tiedscalar magic to the IO
805                slot of the GP rather than the GV itself. AMS 20010812 */
806             if (!GvIOp(varsv))
807                 GvIOp(varsv) = newIO();
808             varsv = (SV *)GvIOp(varsv);
809             break;
810         default:
811             methname = "TIESCALAR";
812             how = PERL_MAGIC_tiedscalar;
813             break;
814     }
815     items = SP - MARK++;
816     if (sv_isobject(*MARK)) {
817         ENTER;
818         PUSHSTACKi(PERLSI_MAGIC);
819         PUSHMARK(SP);
820         EXTEND(SP,(I32)items);
821         while (items--)
822             PUSHs(*MARK++);
823         PUTBACK;
824         call_method(methname, G_SCALAR);
825     }
826     else {
827         /* Not clear why we don't call call_method here too.
828          * perhaps to get different error message ?
829          */
830         stash = gv_stashsv(*MARK, FALSE);
831         if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
832             DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
833                  methname, *MARK);
834         }
835         ENTER;
836         PUSHSTACKi(PERLSI_MAGIC);
837         PUSHMARK(SP);
838         EXTEND(SP,(I32)items);
839         while (items--)
840             PUSHs(*MARK++);
841         PUTBACK;
842         call_sv((SV*)GvCV(gv), G_SCALAR);
843     }
844     SPAGAIN;
845
846     sv = TOPs;
847     POPSTACK;
848     if (sv_isobject(sv)) {
849         sv_unmagic(varsv, how);
850         /* Croak if a self-tie on an aggregate is attempted. */
851         if (varsv == SvRV(sv) &&
852             (SvTYPE(varsv) == SVt_PVAV ||
853              SvTYPE(varsv) == SVt_PVHV))
854             Perl_croak(aTHX_
855                        "Self-ties of arrays and hashes are not supported");
856         sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
857     }
858     LEAVE;
859     SP = PL_stack_base + markoff;
860     PUSHs(sv);
861     RETURN;
862 }
863
864 PP(pp_untie)
865 {
866     dSP;
867     MAGIC *mg;
868     SV *sv = POPs;
869     char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
870                 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
871
872     if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
873         RETPUSHYES;
874
875     if ((mg = SvTIED_mg(sv, how))) {
876         SV *obj = SvRV(SvTIED_obj(sv, mg));
877         GV *gv;
878         CV *cv = NULL;
879         if (obj) {
880             if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
881                isGV(gv) && (cv = GvCV(gv))) {
882                PUSHMARK(SP);
883                XPUSHs(SvTIED_obj((SV*)gv, mg));
884                XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
885                PUTBACK;
886                ENTER;
887                call_sv((SV *)cv, G_VOID);
888                LEAVE;
889                SPAGAIN;
890             }
891            else if (ckWARN(WARN_UNTIE)) {
892                if (mg && SvREFCNT(obj) > 1)
893                   Perl_warner(aTHX_ packWARN(WARN_UNTIE),
894                       "untie attempted while %"UVuf" inner references still exist",
895                        (UV)SvREFCNT(obj) - 1 ) ;
896            }
897         }
898     }
899     sv_unmagic(sv, how) ;
900     RETPUSHYES;
901 }
902
903 PP(pp_tied)
904 {
905     dSP;
906     MAGIC *mg;
907     SV *sv = POPs;
908     char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
909                 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
910
911     if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
912         RETPUSHUNDEF;
913
914     if ((mg = SvTIED_mg(sv, how))) {
915         SV *osv = SvTIED_obj(sv, mg);
916         if (osv == mg->mg_obj)
917             osv = sv_mortalcopy(osv);
918         PUSHs(osv);
919         RETURN;
920     }
921     RETPUSHUNDEF;
922 }
923
924 PP(pp_dbmopen)
925 {
926     dSP;
927     HV *hv;
928     dPOPPOPssrl;
929     HV* stash;
930     GV *gv;
931     SV *sv;
932
933     hv = (HV*)POPs;
934
935     sv = sv_mortalcopy(&PL_sv_no);
936     sv_setpv(sv, "AnyDBM_File");
937     stash = gv_stashsv(sv, FALSE);
938     if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
939         PUTBACK;
940         require_pv("AnyDBM_File.pm");
941         SPAGAIN;
942         if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
943             DIE(aTHX_ "No dbm on this machine");
944     }
945
946     ENTER;
947     PUSHMARK(SP);
948
949     EXTEND(SP, 5);
950     PUSHs(sv);
951     PUSHs(left);
952     if (SvIV(right))
953         PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
954     else
955         PUSHs(sv_2mortal(newSVuv(O_RDWR)));
956     PUSHs(right);
957     PUTBACK;
958     call_sv((SV*)GvCV(gv), G_SCALAR);
959     SPAGAIN;
960
961     if (!sv_isobject(TOPs)) {
962         SP--;
963         PUSHMARK(SP);
964         PUSHs(sv);
965         PUSHs(left);
966         PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
967         PUSHs(right);
968         PUTBACK;
969         call_sv((SV*)GvCV(gv), G_SCALAR);
970         SPAGAIN;
971     }
972
973     if (sv_isobject(TOPs)) {
974         sv_unmagic((SV *) hv, PERL_MAGIC_tied);
975         sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
976     }
977     LEAVE;
978     RETURN;
979 }
980
981 PP(pp_dbmclose)
982 {
983     return pp_untie();
984 }
985
986 PP(pp_sselect)
987 {
988 #ifdef HAS_SELECT
989     dSP; dTARGET;
990     register I32 i;
991     register I32 j;
992     register char *s;
993     register SV *sv;
994     NV value;
995     I32 maxlen = 0;
996     I32 nfound;
997     struct timeval timebuf;
998     struct timeval *tbuf = &timebuf;
999     I32 growsize;
1000     char *fd_sets[4];
1001     STRLEN n_a;
1002 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1003         I32 masksize;
1004         I32 offset;
1005         I32 k;
1006
1007 #   if BYTEORDER & 0xf0000
1008 #       define ORDERBYTE (0x88888888 - BYTEORDER)
1009 #   else
1010 #       define ORDERBYTE (0x4444 - BYTEORDER)
1011 #   endif
1012
1013 #endif
1014
1015     SP -= 4;
1016     for (i = 1; i <= 3; i++) {
1017         if (!SvPOK(SP[i]))
1018             continue;
1019         j = SvCUR(SP[i]);
1020         if (maxlen < j)
1021             maxlen = j;
1022     }
1023
1024 /* little endians can use vecs directly */
1025 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1026 #  ifdef NFDBITS
1027
1028 #    ifndef NBBY
1029 #     define NBBY 8
1030 #    endif
1031
1032     masksize = NFDBITS / NBBY;
1033 #  else
1034     masksize = sizeof(long);    /* documented int, everyone seems to use long */
1035 #  endif
1036     Zero(&fd_sets[0], 4, char*);
1037 #endif
1038
1039 #  if SELECT_MIN_BITS == 1
1040     growsize = sizeof(fd_set);
1041 #  else
1042 #   if defined(__GLIBC__) && defined(__FD_SETSIZE)
1043 #      undef SELECT_MIN_BITS
1044 #      define SELECT_MIN_BITS __FD_SETSIZE
1045 #   endif
1046     /* If SELECT_MIN_BITS is greater than one we most probably will want
1047      * to align the sizes with SELECT_MIN_BITS/8 because for example
1048      * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1049      * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1050      * on (sets/tests/clears bits) is 32 bits.  */
1051     growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1052 #  endif
1053
1054     sv = SP[4];
1055     if (SvOK(sv)) {
1056         value = SvNV(sv);
1057         if (value < 0.0)
1058             value = 0.0;
1059         timebuf.tv_sec = (long)value;
1060         value -= (NV)timebuf.tv_sec;
1061         timebuf.tv_usec = (long)(value * 1000000.0);
1062     }
1063     else
1064         tbuf = Null(struct timeval*);
1065
1066     for (i = 1; i <= 3; i++) {
1067         sv = SP[i];
1068         if (!SvOK(sv)) {
1069             fd_sets[i] = 0;
1070             continue;
1071         }
1072         else if (!SvPOK(sv))
1073             SvPV_force(sv,n_a); /* force string conversion */
1074         j = SvLEN(sv);
1075         if (j < growsize) {
1076             Sv_Grow(sv, growsize);
1077         }
1078         j = SvCUR(sv);
1079         s = SvPVX(sv) + j;
1080         while (++j <= growsize) {
1081             *s++ = '\0';
1082         }
1083
1084 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1085         s = SvPVX(sv);
1086         New(403, fd_sets[i], growsize, char);
1087         for (offset = 0; offset < growsize; offset += masksize) {
1088             for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1089                 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1090         }
1091 #else
1092         fd_sets[i] = SvPVX(sv);
1093 #endif
1094     }
1095
1096 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1097     /* Can't make just the (void*) conditional because that would be
1098      * cpp #if within cpp macro, and not all compilers like that. */
1099     nfound = PerlSock_select(
1100         maxlen * 8,
1101         (Select_fd_set_t) fd_sets[1],
1102         (Select_fd_set_t) fd_sets[2],
1103         (Select_fd_set_t) fd_sets[3],
1104         (void*) tbuf); /* Workaround for compiler bug. */
1105 #else
1106     nfound = PerlSock_select(
1107         maxlen * 8,
1108         (Select_fd_set_t) fd_sets[1],
1109         (Select_fd_set_t) fd_sets[2],
1110         (Select_fd_set_t) fd_sets[3],
1111         tbuf);
1112 #endif
1113     for (i = 1; i <= 3; i++) {
1114         if (fd_sets[i]) {
1115             sv = SP[i];
1116 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1117             s = SvPVX(sv);
1118             for (offset = 0; offset < growsize; offset += masksize) {
1119                 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1120                     s[(k % masksize) + offset] = fd_sets[i][j+offset];
1121             }
1122             Safefree(fd_sets[i]);
1123 #endif
1124             SvSETMAGIC(sv);
1125         }
1126     }
1127
1128     PUSHi(nfound);
1129     if (GIMME == G_ARRAY && tbuf) {
1130         value = (NV)(timebuf.tv_sec) +
1131                 (NV)(timebuf.tv_usec) / 1000000.0;
1132         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1133         sv_setnv(sv, value);
1134     }
1135     RETURN;
1136 #else
1137     DIE(aTHX_ "select not implemented");
1138 #endif
1139 }
1140
1141 void
1142 Perl_setdefout(pTHX_ GV *gv)
1143 {
1144     if (gv)
1145         (void)SvREFCNT_inc(gv);
1146     if (PL_defoutgv)
1147         SvREFCNT_dec(PL_defoutgv);
1148     PL_defoutgv = gv;
1149 }
1150
1151 PP(pp_select)
1152 {
1153     dSP; dTARGET;
1154     GV *newdefout, *egv;
1155     HV *hv;
1156
1157     newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1158
1159     egv = GvEGV(PL_defoutgv);
1160     if (!egv)
1161         egv = PL_defoutgv;
1162     hv = GvSTASH(egv);
1163     if (! hv)
1164         XPUSHs(&PL_sv_undef);
1165     else {
1166         GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1167         if (gvp && *gvp == egv) {
1168             gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1169             XPUSHTARG;
1170         }
1171         else {
1172             XPUSHs(sv_2mortal(newRV((SV*)egv)));
1173         }
1174     }
1175
1176     if (newdefout) {
1177         if (!GvIO(newdefout))
1178             gv_IOadd(newdefout);
1179         setdefout(newdefout);
1180     }
1181
1182     RETURN;
1183 }
1184
1185 PP(pp_getc)
1186 {
1187     dSP; dTARGET;
1188     GV *gv;
1189     IO *io = NULL;
1190     MAGIC *mg;
1191
1192     if (MAXARG == 0)
1193         gv = PL_stdingv;
1194     else
1195         gv = (GV*)POPs;
1196
1197     if (gv && (io = GvIO(gv))
1198         && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1199     {
1200         I32 gimme = GIMME_V;
1201         PUSHMARK(SP);
1202         XPUSHs(SvTIED_obj((SV*)io, mg));
1203         PUTBACK;
1204         ENTER;
1205         call_method("GETC", gimme);
1206         LEAVE;
1207         SPAGAIN;
1208         if (gimme == G_SCALAR)
1209             SvSetMagicSV_nosteal(TARG, TOPs);
1210         RETURN;
1211     }
1212     if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1213         if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1214                 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1215             report_evil_fh(gv, io, PL_op->op_type);
1216         SETERRNO(EBADF,RMS_IFI);
1217         RETPUSHUNDEF;
1218     }
1219     TAINT;
1220     sv_setpv(TARG, " ");
1221     *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1222     if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1223         /* Find out how many bytes the char needs */
1224         Size_t len = UTF8SKIP(SvPVX(TARG));
1225         if (len > 1) {
1226             SvGROW(TARG,len+1);
1227             len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1228             SvCUR_set(TARG,1+len);
1229         }
1230         SvUTF8_on(TARG);
1231     }
1232     PUSHTARG;
1233     RETURN;
1234 }
1235
1236 PP(pp_read)
1237 {
1238     return pp_sysread();
1239 }
1240
1241 STATIC OP *
1242 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1243 {
1244     register PERL_CONTEXT *cx;
1245     I32 gimme = GIMME_V;
1246
1247     ENTER;
1248     SAVETMPS;
1249
1250     push_return(retop);
1251     PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1252     PUSHFORMAT(cx);
1253     PAD_SET_CUR(CvPADLIST(cv), 1);
1254
1255     setdefout(gv);          /* locally select filehandle so $% et al work */
1256     return CvSTART(cv);
1257 }
1258
1259 PP(pp_enterwrite)
1260 {
1261     dSP;
1262     register GV *gv;
1263     register IO *io;
1264     GV *fgv;
1265     CV *cv;
1266
1267     if (MAXARG == 0)
1268         gv = PL_defoutgv;
1269     else {
1270         gv = (GV*)POPs;
1271         if (!gv)
1272             gv = PL_defoutgv;
1273     }
1274     EXTEND(SP, 1);
1275     io = GvIO(gv);
1276     if (!io) {
1277         RETPUSHNO;
1278     }
1279     if (IoFMT_GV(io))
1280         fgv = IoFMT_GV(io);
1281     else
1282         fgv = gv;
1283
1284     cv = GvFORM(fgv);
1285     if (!cv) {
1286         char *name = NULL;
1287         if (fgv) {
1288             SV *tmpsv = sv_newmortal();
1289             gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1290             name = SvPV_nolen(tmpsv);
1291         }
1292         if (name && *name)
1293             DIE(aTHX_ "Undefined format \"%s\" called", name);
1294         DIE(aTHX_ "Not a format reference");
1295     }
1296     if (CvCLONE(cv))
1297         cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1298
1299     IoFLAGS(io) &= ~IOf_DIDTOP;
1300     return doform(cv,gv,PL_op->op_next);
1301 }
1302
1303 PP(pp_leavewrite)
1304 {
1305     dSP;
1306     GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1307     register IO *io = GvIOp(gv);
1308     PerlIO *ofp = IoOFP(io);
1309     PerlIO *fp;
1310     SV **newsp;
1311     I32 gimme;
1312     register PERL_CONTEXT *cx;
1313
1314     DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1315           (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1316     if (!io || !ofp)
1317         goto forget_top;
1318     if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1319         PL_formtarget != PL_toptarget)
1320     {
1321         GV *fgv;
1322         CV *cv;
1323         if (!IoTOP_GV(io)) {
1324             GV *topgv;
1325             SV *topname;
1326
1327             if (!IoTOP_NAME(io)) {
1328                 if (!IoFMT_NAME(io))
1329                     IoFMT_NAME(io) = savepv(GvNAME(gv));
1330                 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1331                 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1332                 if ((topgv && GvFORM(topgv)) ||
1333                   !gv_fetchpv("top",FALSE,SVt_PVFM))
1334                     IoTOP_NAME(io) = savepv(SvPVX(topname));
1335                 else
1336                     IoTOP_NAME(io) = savepv("top");
1337             }
1338             topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1339             if (!topgv || !GvFORM(topgv)) {
1340                 IoLINES_LEFT(io) = 100000000;
1341                 goto forget_top;
1342             }
1343             IoTOP_GV(io) = topgv;
1344         }
1345         if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear.  It still doesn't fit. */
1346             I32 lines = IoLINES_LEFT(io);
1347             char *s = SvPVX(PL_formtarget);
1348             if (lines <= 0)             /* Yow, header didn't even fit!!! */
1349                 goto forget_top;
1350             while (lines-- > 0) {
1351                 s = strchr(s, '\n');
1352                 if (!s)
1353                     break;
1354                 s++;
1355             }
1356             if (s) {
1357                 STRLEN save = SvCUR(PL_formtarget);
1358                 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1359                 do_print(PL_formtarget, ofp);
1360                 SvCUR_set(PL_formtarget, save);
1361                 sv_chop(PL_formtarget, s);
1362                 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1363             }
1364         }
1365         if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1366             do_print(PL_formfeed, ofp);
1367         IoLINES_LEFT(io) = IoPAGE_LEN(io);
1368         IoPAGE(io)++;
1369         PL_formtarget = PL_toptarget;
1370         IoFLAGS(io) |= IOf_DIDTOP;
1371         fgv = IoTOP_GV(io);
1372         if (!fgv)
1373             DIE(aTHX_ "bad top format reference");
1374         cv = GvFORM(fgv);
1375         {
1376             char *name = NULL;
1377             if (!cv) {
1378                 SV *sv = sv_newmortal();
1379                 gv_efullname4(sv, fgv, Nullch, FALSE);
1380                 name = SvPV_nolen(sv);
1381             }
1382             if (name && *name)
1383                 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1384             /* why no:
1385             else
1386                 DIE(aTHX_ "Undefined top format called");
1387             ?*/
1388         }
1389         if (CvCLONE(cv))
1390             cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1391         return doform(cv,gv,PL_op);
1392     }
1393
1394   forget_top:
1395     POPBLOCK(cx,PL_curpm);
1396     POPFORMAT(cx);
1397     LEAVE;
1398
1399     fp = IoOFP(io);
1400     if (!fp) {
1401         if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1402             if (IoIFP(io))
1403                 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1404             else if (ckWARN(WARN_CLOSED))
1405                 report_evil_fh(gv, io, PL_op->op_type);
1406         }
1407         PUSHs(&PL_sv_no);
1408     }
1409     else {
1410         if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1411             if (ckWARN(WARN_IO))
1412                 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1413         }
1414         if (!do_print(PL_formtarget, fp))
1415             PUSHs(&PL_sv_no);
1416         else {
1417             FmLINES(PL_formtarget) = 0;
1418             SvCUR_set(PL_formtarget, 0);
1419             *SvEND(PL_formtarget) = '\0';
1420             if (IoFLAGS(io) & IOf_FLUSH)
1421                 (void)PerlIO_flush(fp);
1422             PUSHs(&PL_sv_yes);
1423         }
1424     }
1425     /* bad_ofp: */
1426     PL_formtarget = PL_bodytarget;
1427     PUTBACK;
1428     return pop_return();
1429 }
1430
1431 PP(pp_prtf)
1432 {
1433     dSP; dMARK; dORIGMARK;
1434     GV *gv;
1435     IO *io;
1436     PerlIO *fp;
1437     SV *sv;
1438     MAGIC *mg;
1439
1440     if (PL_op->op_flags & OPf_STACKED)
1441         gv = (GV*)*++MARK;
1442     else
1443         gv = PL_defoutgv;
1444
1445     if (gv && (io = GvIO(gv))
1446         && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1447     {
1448         if (MARK == ORIGMARK) {
1449             MEXTEND(SP, 1);
1450             ++MARK;
1451             Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1452             ++SP;
1453         }
1454         PUSHMARK(MARK - 1);
1455         *MARK = SvTIED_obj((SV*)io, mg);
1456         PUTBACK;
1457         ENTER;
1458         call_method("PRINTF", G_SCALAR);
1459         LEAVE;
1460         SPAGAIN;
1461         MARK = ORIGMARK + 1;
1462         *MARK = *SP;
1463         SP = MARK;
1464         RETURN;
1465     }
1466
1467     sv = NEWSV(0,0);
1468     if (!(io = GvIO(gv))) {
1469         if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1470             report_evil_fh(gv, io, PL_op->op_type);
1471         SETERRNO(EBADF,RMS_IFI);
1472         goto just_say_no;
1473     }
1474     else if (!(fp = IoOFP(io))) {
1475         if (ckWARN2(WARN_CLOSED,WARN_IO))  {
1476             if (IoIFP(io))
1477                 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1478             else if (ckWARN(WARN_CLOSED))
1479                 report_evil_fh(gv, io, PL_op->op_type);
1480         }
1481         SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1482         goto just_say_no;
1483     }
1484     else {
1485         do_sprintf(sv, SP - MARK, MARK + 1);
1486         if (!do_print(sv, fp))
1487             goto just_say_no;
1488
1489         if (IoFLAGS(io) & IOf_FLUSH)
1490             if (PerlIO_flush(fp) == EOF)
1491                 goto just_say_no;
1492     }
1493     SvREFCNT_dec(sv);
1494     SP = ORIGMARK;
1495     PUSHs(&PL_sv_yes);
1496     RETURN;
1497
1498   just_say_no:
1499     SvREFCNT_dec(sv);
1500     SP = ORIGMARK;
1501     PUSHs(&PL_sv_undef);
1502     RETURN;
1503 }
1504
1505 PP(pp_sysopen)
1506 {
1507     dSP;
1508     GV *gv;
1509     SV *sv;
1510     char *tmps;
1511     STRLEN len;
1512     int mode, perm;
1513
1514     if (MAXARG > 3)
1515         perm = POPi;
1516     else
1517         perm = 0666;
1518     mode = POPi;
1519     sv = POPs;
1520     gv = (GV *)POPs;
1521
1522     /* Need TIEHANDLE method ? */
1523
1524     tmps = SvPV(sv, len);
1525     if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1526         IoLINES(GvIOp(gv)) = 0;
1527         PUSHs(&PL_sv_yes);
1528     }
1529     else {
1530         PUSHs(&PL_sv_undef);
1531     }
1532     RETURN;
1533 }
1534
1535 PP(pp_sysread)
1536 {
1537     dSP; dMARK; dORIGMARK; dTARGET;
1538     int offset;
1539     GV *gv;
1540     IO *io;
1541     char *buffer;
1542     SSize_t length;
1543     SSize_t count;
1544     Sock_size_t bufsize;
1545     SV *bufsv;
1546     STRLEN blen;
1547     MAGIC *mg;
1548     int fp_utf8;
1549     Size_t got = 0;
1550     Size_t wanted;
1551     bool charstart = FALSE;
1552     STRLEN charskip = 0;
1553     STRLEN skip = 0;
1554
1555     gv = (GV*)*++MARK;
1556     if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1557         && gv && (io = GvIO(gv))
1558         && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1559     {
1560         SV *sv;
1561         
1562         PUSHMARK(MARK-1);
1563         *MARK = SvTIED_obj((SV*)io, mg);
1564         ENTER;
1565         call_method("READ", G_SCALAR);
1566         LEAVE;
1567         SPAGAIN;
1568         sv = POPs;
1569         SP = ORIGMARK;
1570         PUSHs(sv);
1571         RETURN;
1572     }
1573
1574     if (!gv)
1575         goto say_undef;
1576     bufsv = *++MARK;
1577     if (! SvOK(bufsv))
1578         sv_setpvn(bufsv, "", 0);
1579     length = SvIVx(*++MARK);
1580     SETERRNO(0,0);
1581     if (MARK < SP)
1582         offset = SvIVx(*++MARK);
1583     else
1584         offset = 0;
1585     io = GvIO(gv);
1586     if (!io || !IoIFP(io)) {
1587         if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1588             report_evil_fh(gv, io, PL_op->op_type);
1589         SETERRNO(EBADF,RMS_IFI);
1590         goto say_undef;
1591     }
1592     if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1593         buffer = SvPVutf8_force(bufsv, blen);
1594         /* UTF-8 may not have been set if they are all low bytes */
1595         SvUTF8_on(bufsv);
1596     }
1597     else {
1598         buffer = SvPV_force(bufsv, blen);
1599     }
1600     if (length < 0)
1601         DIE(aTHX_ "Negative length");
1602     wanted = length;
1603
1604     charstart = TRUE;
1605     charskip  = 0;
1606     skip = 0;
1607
1608 #ifdef HAS_SOCKET
1609     if (PL_op->op_type == OP_RECV) {
1610         char namebuf[MAXPATHLEN];
1611 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1612         bufsize = sizeof (struct sockaddr_in);
1613 #else
1614         bufsize = sizeof namebuf;
1615 #endif
1616 #ifdef OS2      /* At least Warp3+IAK: only the first byte of bufsize set */
1617         if (bufsize >= 256)
1618             bufsize = 255;
1619 #endif
1620         buffer = SvGROW(bufsv, (STRLEN)(length+1));
1621         /* 'offset' means 'flags' here */
1622         count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1623                           (struct sockaddr *)namebuf, &bufsize);
1624         if (count < 0)
1625             RETPUSHUNDEF;
1626 #ifdef EPOC
1627         /* Bogus return without padding */
1628         bufsize = sizeof (struct sockaddr_in);
1629 #endif
1630         SvCUR_set(bufsv, count);
1631         *SvEND(bufsv) = '\0';
1632         (void)SvPOK_only(bufsv);
1633         if (fp_utf8)
1634             SvUTF8_on(bufsv);
1635         SvSETMAGIC(bufsv);
1636         /* This should not be marked tainted if the fp is marked clean */
1637         if (!(IoFLAGS(io) & IOf_UNTAINT))
1638             SvTAINTED_on(bufsv);
1639         SP = ORIGMARK;
1640         sv_setpvn(TARG, namebuf, bufsize);
1641         PUSHs(TARG);
1642         RETURN;
1643     }
1644 #else
1645     if (PL_op->op_type == OP_RECV)
1646         DIE(aTHX_ PL_no_sock_func, "recv");
1647 #endif
1648     if (DO_UTF8(bufsv)) {
1649         /* offset adjust in characters not bytes */
1650         blen = sv_len_utf8(bufsv);
1651     }
1652     if (offset < 0) {
1653         if (-offset > (int)blen)
1654             DIE(aTHX_ "Offset outside string");
1655         offset += blen;
1656     }
1657     if (DO_UTF8(bufsv)) {
1658         /* convert offset-as-chars to offset-as-bytes */
1659         offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1660     }
1661  more_bytes:
1662     bufsize = SvCUR(bufsv);
1663     buffer  = SvGROW(bufsv, (STRLEN)(length+offset+1));
1664     if (offset > bufsize) { /* Zero any newly allocated space */
1665         Zero(buffer+bufsize, offset-bufsize, char);
1666     }
1667     buffer = buffer + offset;
1668
1669     if (PL_op->op_type == OP_SYSREAD) {
1670 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1671         if (IoTYPE(io) == IoTYPE_SOCKET) {
1672             count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1673                                    buffer, length, 0);
1674         }
1675         else
1676 #endif
1677         {
1678             count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1679                                   buffer, length);
1680         }
1681     }
1682     else
1683 #ifdef HAS_SOCKET__bad_code_maybe
1684     if (IoTYPE(io) == IoTYPE_SOCKET) {
1685         char namebuf[MAXPATHLEN];
1686 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1687         bufsize = sizeof (struct sockaddr_in);
1688 #else
1689         bufsize = sizeof namebuf;
1690 #endif
1691         count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1692                           (struct sockaddr *)namebuf, &bufsize);
1693     }
1694     else
1695 #endif
1696     {
1697         count = PerlIO_read(IoIFP(io), buffer, length);
1698         /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1699         if (count == 0 && PerlIO_error(IoIFP(io)))
1700             count = -1;
1701     }
1702     if (count < 0) {
1703         if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1704                 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1705         goto say_undef;
1706     }
1707     SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1708     *SvEND(bufsv) = '\0';
1709     (void)SvPOK_only(bufsv);
1710     if (fp_utf8 && !IN_BYTES) {
1711         /* Look at utf8 we got back and count the characters */
1712         char *bend = buffer + count;
1713         while (buffer < bend) {
1714             if (charstart) {
1715                 skip = UTF8SKIP(buffer);
1716                 charskip = 0;
1717             }
1718             if (buffer - charskip + skip > bend) {
1719                 /* partial character - try for rest of it */
1720                 length = skip - (bend-buffer);
1721                 offset = bend - SvPVX(bufsv);
1722                 charstart = FALSE;
1723                 charskip += count;
1724                 goto more_bytes;
1725             }
1726             else {
1727                 got++;
1728                 buffer += skip;
1729                 charstart = TRUE;
1730                 charskip  = 0;
1731             }
1732         }
1733         /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1734            provided amount read (count) was what was requested (length)
1735          */
1736         if (got < wanted && count == length) {
1737             length = wanted - got;
1738             offset = bend - SvPVX(bufsv);
1739             goto more_bytes;
1740         }
1741         /* return value is character count */
1742         count = got;
1743         SvUTF8_on(bufsv);
1744     }
1745     SvSETMAGIC(bufsv);
1746     /* This should not be marked tainted if the fp is marked clean */
1747     if (!(IoFLAGS(io) & IOf_UNTAINT))
1748         SvTAINTED_on(bufsv);
1749     SP = ORIGMARK;
1750     PUSHi(count);
1751     RETURN;
1752
1753   say_undef:
1754     SP = ORIGMARK;
1755     RETPUSHUNDEF;
1756 }
1757
1758 PP(pp_syswrite)
1759 {
1760     dSP;
1761     int items = (SP - PL_stack_base) - TOPMARK;
1762     if (items == 2) {
1763         SV *sv;
1764         EXTEND(SP, 1);
1765         sv = sv_2mortal(newSViv(sv_len(*SP)));
1766         PUSHs(sv);
1767         PUTBACK;
1768     }
1769     return pp_send();
1770 }
1771
1772 PP(pp_send)
1773 {
1774     dSP; dMARK; dORIGMARK; dTARGET;
1775     GV *gv;
1776     IO *io;
1777     SV *bufsv;
1778     char *buffer;
1779     Size_t length;
1780     SSize_t retval;
1781     STRLEN blen;
1782     MAGIC *mg;
1783
1784     gv = (GV*)*++MARK;
1785     if (PL_op->op_type == OP_SYSWRITE
1786         && gv && (io = GvIO(gv))
1787         && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1788     {
1789         SV *sv;
1790         
1791         PUSHMARK(MARK-1);
1792         *MARK = SvTIED_obj((SV*)io, mg);
1793         ENTER;
1794         call_method("WRITE", G_SCALAR);
1795         LEAVE;
1796         SPAGAIN;
1797         sv = POPs;
1798         SP = ORIGMARK;
1799         PUSHs(sv);
1800         RETURN;
1801     }
1802     if (!gv)
1803         goto say_undef;
1804     bufsv = *++MARK;
1805 #if Size_t_size > IVSIZE
1806     length = (Size_t)SvNVx(*++MARK);
1807 #else
1808     length = (Size_t)SvIVx(*++MARK);
1809 #endif
1810     if ((SSize_t)length < 0)
1811         DIE(aTHX_ "Negative length");
1812     SETERRNO(0,0);
1813     io = GvIO(gv);
1814     if (!io || !IoIFP(io)) {
1815         retval = -1;
1816         if (ckWARN(WARN_CLOSED))
1817             report_evil_fh(gv, io, PL_op->op_type);
1818         SETERRNO(EBADF,RMS_IFI);
1819         goto say_undef;
1820     }
1821
1822     if (PerlIO_isutf8(IoIFP(io))) {
1823         buffer = SvPVutf8(bufsv, blen);
1824     }
1825     else {
1826          if (DO_UTF8(bufsv)) {
1827               /* Not modifying source SV, so making a temporary copy. */
1828               bufsv = sv_2mortal(newSVsv(bufsv));
1829               sv_utf8_downgrade(bufsv, FALSE);
1830          }
1831          buffer = SvPV(bufsv, blen);
1832     }
1833
1834     if (PL_op->op_type == OP_SYSWRITE) {
1835         IV offset;
1836         if (DO_UTF8(bufsv)) {
1837             /* length and offset are in chars */
1838             blen   = sv_len_utf8(bufsv);
1839         }
1840         if (MARK < SP) {
1841             offset = SvIVx(*++MARK);
1842             if (offset < 0) {
1843                 if (-offset > (IV)blen)
1844                     DIE(aTHX_ "Offset outside string");
1845                 offset += blen;
1846             } else if (offset >= (IV)blen && blen > 0)
1847                 DIE(aTHX_ "Offset outside string");
1848         } else
1849             offset = 0;
1850         if (length > blen - offset)
1851             length = blen - offset;
1852         if (DO_UTF8(bufsv)) {
1853             buffer = (char*)utf8_hop((U8 *)buffer, offset);
1854             length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1855         }
1856         else {
1857             buffer = buffer+offset;
1858         }
1859 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1860         if (IoTYPE(io) == IoTYPE_SOCKET) {
1861             retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1862                                    buffer, length, 0);
1863         }
1864         else
1865 #endif
1866         {
1867             /* See the note at doio.c:do_print about filesize limits. --jhi */
1868             retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1869                                    buffer, length);
1870         }
1871     }
1872 #ifdef HAS_SOCKET
1873     else if (SP > MARK) {
1874         char *sockbuf;
1875         STRLEN mlen;
1876         sockbuf = SvPVx(*++MARK, mlen);
1877         /* length is really flags */
1878         retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1879                                  length, (struct sockaddr *)sockbuf, mlen);
1880     }
1881     else
1882         /* length is really flags */
1883         retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1884 #else
1885     else
1886         DIE(aTHX_ PL_no_sock_func, "send");
1887 #endif
1888     if (retval < 0)
1889         goto say_undef;
1890     SP = ORIGMARK;
1891     if (DO_UTF8(bufsv))
1892         retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1893 #if Size_t_size > IVSIZE
1894     PUSHn(retval);
1895 #else
1896     PUSHi(retval);
1897 #endif
1898     RETURN;
1899
1900   say_undef:
1901     SP = ORIGMARK;
1902     RETPUSHUNDEF;
1903 }
1904
1905 PP(pp_recv)
1906 {
1907     return pp_sysread();
1908 }
1909
1910 PP(pp_eof)
1911 {
1912     dSP;
1913     GV *gv;
1914     IO *io;
1915     MAGIC *mg;
1916
1917     if (MAXARG == 0) {
1918         if (PL_op->op_flags & OPf_SPECIAL) {    /* eof() */
1919             IO *io;
1920             gv = PL_last_in_gv = GvEGV(PL_argvgv);
1921             io = GvIO(gv);
1922             if (io && !IoIFP(io)) {
1923                 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1924                     IoLINES(io) = 0;
1925                     IoFLAGS(io) &= ~IOf_START;
1926                     do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1927                     sv_setpvn(GvSV(gv), "-", 1);
1928                     SvSETMAGIC(GvSV(gv));
1929                 }
1930                 else if (!nextargv(gv))
1931                     RETPUSHYES;
1932             }
1933         }
1934         else
1935             gv = PL_last_in_gv;                 /* eof */
1936     }
1937     else
1938         gv = PL_last_in_gv = (GV*)POPs;         /* eof(FH) */
1939
1940     if (gv && (io = GvIO(gv))
1941         && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1942     {
1943         PUSHMARK(SP);
1944         XPUSHs(SvTIED_obj((SV*)io, mg));
1945         PUTBACK;
1946         ENTER;
1947         call_method("EOF", G_SCALAR);
1948         LEAVE;
1949         SPAGAIN;
1950         RETURN;
1951     }
1952
1953     PUSHs(boolSV(!gv || do_eof(gv)));
1954     RETURN;
1955 }
1956
1957 PP(pp_tell)
1958 {
1959     dSP; dTARGET;
1960     GV *gv;
1961     IO *io;
1962     MAGIC *mg;
1963
1964     if (MAXARG == 0)
1965         gv = PL_last_in_gv;
1966     else
1967         gv = PL_last_in_gv = (GV*)POPs;
1968
1969     if (gv && (io = GvIO(gv))
1970         && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1971     {
1972         PUSHMARK(SP);
1973         XPUSHs(SvTIED_obj((SV*)io, mg));
1974         PUTBACK;
1975         ENTER;
1976         call_method("TELL", G_SCALAR);
1977         LEAVE;
1978         SPAGAIN;
1979         RETURN;
1980     }
1981
1982 #if LSEEKSIZE > IVSIZE
1983     PUSHn( do_tell(gv) );
1984 #else
1985     PUSHi( do_tell(gv) );
1986 #endif
1987     RETURN;
1988 }
1989
1990 PP(pp_seek)
1991 {
1992     return pp_sysseek();
1993 }
1994
1995 PP(pp_sysseek)
1996 {
1997     dSP;
1998     GV *gv;
1999     IO *io;
2000     int whence = POPi;
2001 #if LSEEKSIZE > IVSIZE
2002     Off_t offset = (Off_t)SvNVx(POPs);
2003 #else
2004     Off_t offset = (Off_t)SvIVx(POPs);
2005 #endif
2006     MAGIC *mg;
2007
2008     gv = PL_last_in_gv = (GV*)POPs;
2009
2010     if (gv && (io = GvIO(gv))
2011         && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2012     {
2013         PUSHMARK(SP);
2014         XPUSHs(SvTIED_obj((SV*)io, mg));
2015 #if LSEEKSIZE > IVSIZE
2016         XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2017 #else
2018         XPUSHs(sv_2mortal(newSViv(offset)));
2019 #endif
2020         XPUSHs(sv_2mortal(newSViv(whence)));
2021         PUTBACK;
2022         ENTER;
2023         call_method("SEEK", G_SCALAR);
2024         LEAVE;
2025         SPAGAIN;
2026         RETURN;
2027     }
2028
2029     if (PL_op->op_type == OP_SEEK)
2030         PUSHs(boolSV(do_seek(gv, offset, whence)));
2031     else {
2032         Off_t sought = do_sysseek(gv, offset, whence);
2033         if (sought < 0)
2034             PUSHs(&PL_sv_undef);
2035         else {
2036             SV* sv = sought ?
2037 #if LSEEKSIZE > IVSIZE
2038                 newSVnv((NV)sought)
2039 #else
2040                 newSViv(sought)
2041 #endif
2042                 : newSVpvn(zero_but_true, ZBTLEN);
2043             PUSHs(sv_2mortal(sv));
2044         }
2045     }
2046     RETURN;
2047 }
2048
2049 PP(pp_truncate)
2050 {
2051     dSP;
2052     /* There seems to be no consensus on the length type of truncate()
2053      * and ftruncate(), both off_t and size_t have supporters. In
2054      * general one would think that when using large files, off_t is
2055      * at least as wide as size_t, so using an off_t should be okay. */
2056     /* XXX Configure probe for the length type of *truncate() needed XXX */
2057     Off_t len;
2058
2059 #if Off_t_size > IVSIZE
2060     len = (Off_t)POPn;
2061 #else
2062     len = (Off_t)POPi;
2063 #endif
2064     /* Checking for length < 0 is problematic as the type might or
2065      * might not be signed: if it is not, clever compilers will moan. */
2066     /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2067     SETERRNO(0,0);
2068 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2069     {
2070         STRLEN n_a;
2071         int result = 1;
2072         GV *tmpgv;
2073         IO *io;
2074
2075         if (PL_op->op_flags & OPf_SPECIAL) {
2076             tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2077
2078         do_ftruncate_gv:
2079             if (!GvIO(tmpgv))
2080                 result = 0;
2081             else {
2082                 PerlIO *fp;
2083                 io = GvIOp(tmpgv);
2084             do_ftruncate_io:
2085                 TAINT_PROPER("truncate");
2086                 if (!(fp = IoIFP(io))) {
2087                     result = 0;
2088                 }
2089                 else {
2090                     PerlIO_flush(fp);
2091 #ifdef HAS_TRUNCATE
2092                     if (ftruncate(PerlIO_fileno(fp), len) < 0)
2093 #else
2094                     if (my_chsize(PerlIO_fileno(fp), len) < 0)
2095 #endif
2096                         result = 0;
2097                 }
2098             }
2099         }
2100         else {
2101             SV *sv = POPs;
2102             char *name;
2103         
2104             if (SvTYPE(sv) == SVt_PVGV) {
2105                 tmpgv = (GV*)sv;                /* *main::FRED for example */
2106                 goto do_ftruncate_gv;
2107             }
2108             else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2109                 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2110                 goto do_ftruncate_gv;
2111             }
2112             else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2113                 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2114                 goto do_ftruncate_io;
2115             }
2116
2117             name = SvPV(sv, n_a);
2118             TAINT_PROPER("truncate");
2119 #ifdef HAS_TRUNCATE
2120             if (truncate(name, len) < 0)
2121                 result = 0;
2122 #else
2123             {
2124                 int tmpfd;
2125
2126                 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2127                     result = 0;
2128                 else {
2129                     if (my_chsize(tmpfd, len) < 0)
2130                         result = 0;
2131                     PerlLIO_close(tmpfd);
2132                 }
2133             }
2134 #endif
2135         }
2136
2137         if (result)
2138             RETPUSHYES;
2139         if (!errno)
2140             SETERRNO(EBADF,RMS_IFI);
2141         RETPUSHUNDEF;
2142     }
2143 #else
2144     DIE(aTHX_ "truncate not implemented");
2145 #endif
2146 }
2147
2148 PP(pp_fcntl)
2149 {
2150     return pp_ioctl();
2151 }
2152
2153 PP(pp_ioctl)
2154 {
2155     dSP; dTARGET;
2156     SV *argsv = POPs;
2157     unsigned int func = POPu;
2158     int optype = PL_op->op_type;
2159     char *s;
2160     IV retval;
2161     GV *gv = (GV*)POPs;
2162     IO *io = gv ? GvIOn(gv) : 0;
2163
2164     if (!io || !argsv || !IoIFP(io)) {
2165         if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2166             report_evil_fh(gv, io, PL_op->op_type);
2167         SETERRNO(EBADF,RMS_IFI);        /* well, sort of... */
2168         RETPUSHUNDEF;
2169     }
2170
2171     if (SvPOK(argsv) || !SvNIOK(argsv)) {
2172         STRLEN len;
2173         STRLEN need;
2174         s = SvPV_force(argsv, len);
2175         need = IOCPARM_LEN(func);
2176         if (len < need) {
2177             s = Sv_Grow(argsv, need + 1);
2178             SvCUR_set(argsv, need);
2179         }
2180
2181         s[SvCUR(argsv)] = 17;   /* a little sanity check here */
2182     }
2183     else {
2184         retval = SvIV(argsv);
2185         s = INT2PTR(char*,retval);              /* ouch */
2186     }
2187
2188     TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2189
2190     if (optype == OP_IOCTL)
2191 #ifdef HAS_IOCTL
2192         retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2193 #else
2194         DIE(aTHX_ "ioctl is not implemented");
2195 #endif
2196     else
2197 #ifndef HAS_FCNTL
2198       DIE(aTHX_ "fcntl is not implemented");
2199 #else
2200 #if defined(OS2) && defined(__EMX__)
2201         retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2202 #else
2203         retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2204 #endif
2205 #endif
2206
2207 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2208     if (SvPOK(argsv)) {
2209         if (s[SvCUR(argsv)] != 17)
2210             DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2211                 OP_NAME(PL_op));
2212         s[SvCUR(argsv)] = 0;            /* put our null back */
2213         SvSETMAGIC(argsv);              /* Assume it has changed */
2214     }
2215
2216     if (retval == -1)
2217         RETPUSHUNDEF;
2218     if (retval != 0) {
2219         PUSHi(retval);
2220     }
2221     else {
2222         PUSHp(zero_but_true, ZBTLEN);
2223     }
2224 #endif
2225     RETURN;
2226 }
2227
2228 PP(pp_flock)
2229 {
2230 #ifdef FLOCK
2231     dSP; dTARGET;
2232     I32 value;
2233     int argtype;
2234     GV *gv;
2235     IO *io = NULL;
2236     PerlIO *fp;
2237
2238     argtype = POPi;
2239     if (MAXARG == 0)
2240         gv = PL_last_in_gv;
2241     else
2242         gv = (GV*)POPs;
2243     if (gv && (io = GvIO(gv)))
2244         fp = IoIFP(io);
2245     else {
2246         fp = Nullfp;
2247         io = NULL;
2248     }
2249     if (fp) {
2250         (void)PerlIO_flush(fp);
2251         value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2252     }
2253     else {
2254         if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2255             report_evil_fh(gv, io, PL_op->op_type);
2256         value = 0;
2257         SETERRNO(EBADF,RMS_IFI);
2258     }
2259     PUSHi(value);
2260     RETURN;
2261 #else
2262     DIE(aTHX_ PL_no_func, "flock()");
2263 #endif
2264 }
2265
2266 /* Sockets. */
2267
2268 PP(pp_socket)
2269 {
2270 #ifdef HAS_SOCKET
2271     dSP;
2272     GV *gv;
2273     register IO *io;
2274     int protocol = POPi;
2275     int type = POPi;
2276     int domain = POPi;
2277     int fd;
2278
2279     gv = (GV*)POPs;
2280     io = gv ? GvIOn(gv) : NULL;
2281
2282     if (!gv || !io) {
2283         if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2284             report_evil_fh(gv, io, PL_op->op_type);
2285         if (IoIFP(io))
2286             do_close(gv, FALSE);
2287         SETERRNO(EBADF,LIB_INVARG);
2288         RETPUSHUNDEF;
2289     }
2290
2291     if (IoIFP(io))
2292         do_close(gv, FALSE);
2293
2294     TAINT_PROPER("socket");
2295     fd = PerlSock_socket(domain, type, protocol);
2296     if (fd < 0)
2297         RETPUSHUNDEF;
2298     IoIFP(io) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE);    /* stdio gets confused about sockets */
2299     IoOFP(io) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2300     IoTYPE(io) = IoTYPE_SOCKET;
2301     if (!IoIFP(io) || !IoOFP(io)) {
2302         if (IoIFP(io)) PerlIO_close(IoIFP(io));
2303         if (IoOFP(io)) PerlIO_close(IoOFP(io));
2304         if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2305         RETPUSHUNDEF;
2306     }
2307 #if defined(HAS_FCNTL) && defined(F_SETFD)
2308     fcntl(fd, F_SETFD, fd > PL_maxsysfd);       /* ensure close-on-exec */
2309 #endif
2310
2311 #ifdef EPOC
2312     setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2313 #endif
2314
2315     RETPUSHYES;
2316 #else
2317     DIE(aTHX_ PL_no_sock_func, "socket");
2318 #endif
2319 }
2320
2321 PP(pp_sockpair)
2322 {
2323 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2324     dSP;
2325     GV *gv1;
2326     GV *gv2;
2327     register IO *io1;
2328     register IO *io2;
2329     int protocol = POPi;
2330     int type = POPi;
2331     int domain = POPi;
2332     int fd[2];
2333
2334     gv2 = (GV*)POPs;
2335     gv1 = (GV*)POPs;
2336     io1 = gv1 ? GvIOn(gv1) : NULL;
2337     io2 = gv2 ? GvIOn(gv2) : NULL;
2338     if (!gv1 || !gv2 || !io1 || !io2) {
2339         if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2340             if (!gv1 || !io1)
2341                 report_evil_fh(gv1, io1, PL_op->op_type);
2342             if (!gv2 || !io2)
2343                 report_evil_fh(gv1, io2, PL_op->op_type);
2344         }
2345         if (IoIFP(io1))
2346             do_close(gv1, FALSE);
2347         if (IoIFP(io2))
2348             do_close(gv2, FALSE);
2349         RETPUSHUNDEF;
2350     }
2351
2352     if (IoIFP(io1))
2353         do_close(gv1, FALSE);
2354     if (IoIFP(io2))
2355         do_close(gv2, FALSE);
2356
2357     TAINT_PROPER("socketpair");
2358     if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2359         RETPUSHUNDEF;
2360     IoIFP(io1) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
2361     IoOFP(io1) = PerlIO_fdopen(fd[0], "w"PIPESOCK_MODE);
2362     IoTYPE(io1) = IoTYPE_SOCKET;
2363     IoIFP(io2) = PerlIO_fdopen(fd[1], "r"PIPESOCK_MODE);
2364     IoOFP(io2) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
2365     IoTYPE(io2) = IoTYPE_SOCKET;
2366     if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2367         if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2368         if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2369         if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2370         if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2371         if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2372         if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2373         RETPUSHUNDEF;
2374     }
2375 #if defined(HAS_FCNTL) && defined(F_SETFD)
2376     fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd);   /* ensure close-on-exec */
2377     fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd);   /* ensure close-on-exec */
2378 #endif
2379
2380     RETPUSHYES;
2381 #else
2382     DIE(aTHX_ PL_no_sock_func, "socketpair");
2383 #endif
2384 }
2385
2386 PP(pp_bind)
2387 {
2388 #ifdef HAS_SOCKET
2389     dSP;
2390 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2391     extern void GETPRIVMODE();
2392     extern void GETUSERMODE();
2393 #endif
2394     SV *addrsv = POPs;
2395     char *addr;
2396     GV *gv = (GV*)POPs;
2397     register IO *io = GvIOn(gv);
2398     STRLEN len;
2399     int bind_ok = 0;
2400 #ifdef MPE
2401     int mpeprivmode = 0;
2402 #endif
2403
2404     if (!io || !IoIFP(io))
2405         goto nuts;
2406
2407     addr = SvPV(addrsv, len);
2408     TAINT_PROPER("bind");
2409 #ifdef MPE /* Deal with MPE bind() peculiarities */
2410     if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2411         /* The address *MUST* stupidly be zero. */
2412         ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2413         /* PRIV mode is required to bind() to ports < 1024. */
2414         if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2415             ((struct sockaddr_in *)addr)->sin_port > 0) {
2416             GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2417             mpeprivmode = 1;
2418         }
2419     }
2420 #endif /* MPE */
2421     if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2422                       (struct sockaddr *)addr, len) >= 0)
2423         bind_ok = 1;
2424
2425 #ifdef MPE /* Switch back to USER mode */
2426     if (mpeprivmode)
2427         GETUSERMODE();
2428 #endif /* MPE */
2429
2430     if (bind_ok)
2431         RETPUSHYES;
2432     else
2433         RETPUSHUNDEF;
2434
2435 nuts:
2436     if (ckWARN(WARN_CLOSED))
2437         report_evil_fh(gv, io, PL_op->op_type);
2438     SETERRNO(EBADF,SS_IVCHAN);
2439     RETPUSHUNDEF;
2440 #else
2441     DIE(aTHX_ PL_no_sock_func, "bind");
2442 #endif
2443 }
2444
2445 PP(pp_connect)
2446 {
2447 #ifdef HAS_SOCKET
2448     dSP;
2449     SV *addrsv = POPs;
2450     char *addr;
2451     GV *gv = (GV*)POPs;
2452     register IO *io = GvIOn(gv);
2453     STRLEN len;
2454
2455     if (!io || !IoIFP(io))
2456         goto nuts;
2457
2458     addr = SvPV(addrsv, len);
2459     TAINT_PROPER("connect");
2460     if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2461         RETPUSHYES;
2462     else
2463         RETPUSHUNDEF;
2464
2465 nuts:
2466     if (ckWARN(WARN_CLOSED))
2467         report_evil_fh(gv, io, PL_op->op_type);
2468     SETERRNO(EBADF,SS_IVCHAN);
2469     RETPUSHUNDEF;
2470 #else
2471     DIE(aTHX_ PL_no_sock_func, "connect");
2472 #endif
2473 }
2474
2475 PP(pp_listen)
2476 {
2477 #ifdef HAS_SOCKET
2478     dSP;
2479     int backlog = POPi;
2480     GV *gv = (GV*)POPs;
2481     register IO *io = gv ? GvIOn(gv) : NULL;
2482
2483     if (!gv || !io || !IoIFP(io))
2484         goto nuts;
2485
2486     if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2487         RETPUSHYES;
2488     else
2489         RETPUSHUNDEF;
2490
2491 nuts:
2492     if (ckWARN(WARN_CLOSED))
2493         report_evil_fh(gv, io, PL_op->op_type);
2494     SETERRNO(EBADF,SS_IVCHAN);
2495     RETPUSHUNDEF;
2496 #else
2497     DIE(aTHX_ PL_no_sock_func, "listen");
2498 #endif
2499 }
2500
2501 PP(pp_accept)
2502 {
2503 #ifdef HAS_SOCKET
2504     dSP; dTARGET;
2505     GV *ngv;
2506     GV *ggv;
2507     register IO *nstio;
2508     register IO *gstio;
2509     char namebuf[MAXPATHLEN];
2510 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2511     Sock_size_t len = sizeof (struct sockaddr_in);
2512 #else
2513     Sock_size_t len = sizeof namebuf;
2514 #endif
2515     int fd;
2516
2517     ggv = (GV*)POPs;
2518     ngv = (GV*)POPs;
2519
2520     if (!ngv)
2521         goto badexit;
2522     if (!ggv)
2523         goto nuts;
2524
2525     gstio = GvIO(ggv);
2526     if (!gstio || !IoIFP(gstio))
2527         goto nuts;
2528
2529     nstio = GvIOn(ngv);
2530     fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2531     if (fd < 0)
2532         goto badexit;
2533     if (IoIFP(nstio))
2534         do_close(ngv, FALSE);
2535     IoIFP(nstio) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE);
2536     IoOFP(nstio) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2537     IoTYPE(nstio) = IoTYPE_SOCKET;
2538     if (!IoIFP(nstio) || !IoOFP(nstio)) {
2539         if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2540         if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2541         if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2542         goto badexit;
2543     }
2544 #if defined(HAS_FCNTL) && defined(F_SETFD)
2545     fcntl(fd, F_SETFD, fd > PL_maxsysfd);       /* ensure close-on-exec */
2546 #endif
2547
2548 #ifdef EPOC
2549     len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2550     setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2551 #endif
2552 #ifdef __SCO_VERSION__
2553     len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2554 #endif
2555
2556     PUSHp(namebuf, len);
2557     RETURN;
2558
2559 nuts:
2560     if (ckWARN(WARN_CLOSED))
2561         report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2562     SETERRNO(EBADF,SS_IVCHAN);
2563
2564 badexit:
2565     RETPUSHUNDEF;
2566
2567 #else
2568     DIE(aTHX_ PL_no_sock_func, "accept");
2569 #endif
2570 }
2571
2572 PP(pp_shutdown)
2573 {
2574 #ifdef HAS_SOCKET
2575     dSP; dTARGET;
2576     int how = POPi;
2577     GV *gv = (GV*)POPs;
2578     register IO *io = GvIOn(gv);
2579
2580     if (!io || !IoIFP(io))
2581         goto nuts;
2582
2583     PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2584     RETURN;
2585
2586 nuts:
2587     if (ckWARN(WARN_CLOSED))
2588         report_evil_fh(gv, io, PL_op->op_type);
2589     SETERRNO(EBADF,SS_IVCHAN);
2590     RETPUSHUNDEF;
2591 #else
2592     DIE(aTHX_ PL_no_sock_func, "shutdown");
2593 #endif
2594 }
2595
2596 PP(pp_gsockopt)
2597 {
2598 #ifdef HAS_SOCKET
2599     return pp_ssockopt();
2600 #else
2601     DIE(aTHX_ PL_no_sock_func, "getsockopt");
2602 #endif
2603 }
2604
2605 PP(pp_ssockopt)
2606 {
2607 #ifdef HAS_SOCKET
2608     dSP;
2609     int optype = PL_op->op_type;
2610     SV *sv;
2611     int fd;
2612     unsigned int optname;
2613     unsigned int lvl;
2614     GV *gv;
2615     register IO *io;
2616     Sock_size_t len;
2617
2618     if (optype == OP_GSOCKOPT)
2619         sv = sv_2mortal(NEWSV(22, 257));
2620     else
2621         sv = POPs;
2622     optname = (unsigned int) POPi;
2623     lvl = (unsigned int) POPi;
2624
2625     gv = (GV*)POPs;
2626     io = GvIOn(gv);
2627     if (!io || !IoIFP(io))
2628         goto nuts;
2629
2630     fd = PerlIO_fileno(IoIFP(io));
2631     switch (optype) {
2632     case OP_GSOCKOPT:
2633         SvGROW(sv, 257);
2634         (void)SvPOK_only(sv);
2635         SvCUR_set(sv,256);
2636         *SvEND(sv) ='\0';
2637         len = SvCUR(sv);
2638         if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2639             goto nuts2;
2640         SvCUR_set(sv, len);
2641         *SvEND(sv) ='\0';
2642         PUSHs(sv);
2643         break;
2644     case OP_SSOCKOPT: {
2645             char *buf;
2646             int aint;
2647             if (SvPOKp(sv)) {
2648                 STRLEN l;
2649                 buf = SvPV(sv, l);
2650                 len = l;
2651             }
2652             else {
2653                 aint = (int)SvIV(sv);
2654                 buf = (char*)&aint;
2655                 len = sizeof(int);
2656             }
2657             if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2658                 goto nuts2;
2659             PUSHs(&PL_sv_yes);
2660         }
2661         break;
2662     }
2663     RETURN;
2664
2665 nuts:
2666     if (ckWARN(WARN_CLOSED))
2667         report_evil_fh(gv, io, optype);
2668     SETERRNO(EBADF,SS_IVCHAN);
2669 nuts2:
2670     RETPUSHUNDEF;
2671
2672 #else
2673     DIE(aTHX_ PL_no_sock_func, "setsockopt");
2674 #endif
2675 }
2676
2677 PP(pp_getsockname)
2678 {
2679 #ifdef HAS_SOCKET
2680     return pp_getpeername();
2681 #else
2682     DIE(aTHX_ PL_no_sock_func, "getsockname");
2683 #endif
2684 }
2685
2686 PP(pp_getpeername)
2687 {
2688 #ifdef HAS_SOCKET
2689     dSP;
2690     int optype = PL_op->op_type;
2691     SV *sv;
2692     int fd;
2693     GV *gv = (GV*)POPs;
2694     register IO *io = GvIOn(gv);
2695     Sock_size_t len;
2696
2697     if (!io || !IoIFP(io))
2698         goto nuts;
2699
2700     sv = sv_2mortal(NEWSV(22, 257));
2701     (void)SvPOK_only(sv);
2702     len = 256;
2703     SvCUR_set(sv, len);
2704     *SvEND(sv) ='\0';
2705     fd = PerlIO_fileno(IoIFP(io));
2706     switch (optype) {
2707     case OP_GETSOCKNAME:
2708         if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2709             goto nuts2;
2710         break;
2711     case OP_GETPEERNAME:
2712         if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2713             goto nuts2;
2714 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2715         {
2716             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";
2717             /* If the call succeeded, make sure we don't have a zeroed port/addr */
2718             if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2719                 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2720                         sizeof(u_short) + sizeof(struct in_addr))) {
2721                 goto nuts2;     
2722             }
2723         }
2724 #endif
2725         break;
2726     }
2727 #ifdef BOGUS_GETNAME_RETURN
2728     /* Interactive Unix, getpeername() and getsockname()
2729       does not return valid namelen */
2730     if (len == BOGUS_GETNAME_RETURN)
2731         len = sizeof(struct sockaddr);
2732 #endif
2733     SvCUR_set(sv, len);
2734     *SvEND(sv) ='\0';
2735     PUSHs(sv);
2736     RETURN;
2737
2738 nuts:
2739     if (ckWARN(WARN_CLOSED))
2740         report_evil_fh(gv, io, optype);
2741     SETERRNO(EBADF,SS_IVCHAN);
2742 nuts2:
2743     RETPUSHUNDEF;
2744
2745 #else
2746     DIE(aTHX_ PL_no_sock_func, "getpeername");
2747 #endif
2748 }
2749
2750 /* Stat calls. */
2751
2752 PP(pp_lstat)
2753 {
2754     return pp_stat();
2755 }
2756
2757 PP(pp_stat)
2758 {
2759     dSP;
2760     GV *gv;
2761     I32 gimme;
2762     I32 max = 13;
2763     STRLEN n_a;
2764
2765     if (PL_op->op_flags & OPf_REF) {
2766         gv = cGVOP_gv;
2767         if (PL_op->op_type == OP_LSTAT) {
2768             if (gv != PL_defgv) {
2769                 if (ckWARN(WARN_IO))
2770                     Perl_warner(aTHX_ packWARN(WARN_IO),
2771                         "lstat() on filehandle %s", GvENAME(gv));
2772             } else if (PL_laststype != OP_LSTAT)
2773                 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2774         }
2775
2776       do_fstat:
2777         if (gv != PL_defgv) {
2778             PL_laststype = OP_STAT;
2779             PL_statgv = gv;
2780             sv_setpv(PL_statname, "");
2781             PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2782                 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2783         }
2784         if (PL_laststatval < 0) {
2785             if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2786                 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2787             max = 0;
2788         }
2789     }
2790     else {
2791         SV* sv = POPs;
2792         if (SvTYPE(sv) == SVt_PVGV) {
2793             gv = (GV*)sv;
2794             goto do_fstat;
2795         }
2796         else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2797             gv = (GV*)SvRV(sv);
2798             if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2799                 Perl_warner(aTHX_ packWARN(WARN_IO),
2800                         "lstat() on filehandle %s", GvENAME(gv));
2801             goto do_fstat;
2802         }
2803         sv_setpv(PL_statname, SvPV(sv,n_a));
2804         PL_statgv = Nullgv;
2805 #ifdef HAS_LSTAT
2806         PL_laststype = PL_op->op_type;
2807         if (PL_op->op_type == OP_LSTAT)
2808             PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2809         else
2810 #endif
2811             PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2812         if (PL_laststatval < 0) {
2813             if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2814                 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2815             max = 0;
2816         }
2817     }
2818
2819     gimme = GIMME_V;
2820     if (gimme != G_ARRAY) {
2821         if (gimme != G_VOID)
2822             XPUSHs(boolSV(max));
2823         RETURN;
2824     }
2825     if (max) {
2826         EXTEND(SP, max);
2827         EXTEND_MORTAL(max);
2828         PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2829         PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2830         PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2831         PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2832 #if Uid_t_size > IVSIZE
2833         PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2834 #else
2835 #   if Uid_t_sign <= 0
2836         PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2837 #   else
2838         PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2839 #   endif
2840 #endif
2841 #if Gid_t_size > IVSIZE
2842         PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2843 #else
2844 #   if Gid_t_sign <= 0
2845         PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2846 #   else
2847         PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2848 #   endif
2849 #endif
2850 #ifdef USE_STAT_RDEV
2851         PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2852 #else
2853         PUSHs(sv_2mortal(newSVpvn("", 0)));
2854 #endif
2855 #if Off_t_size > IVSIZE
2856         PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2857 #else
2858         PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2859 #endif
2860 #ifdef BIG_TIME
2861         PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2862         PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2863         PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2864 #else
2865         PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2866         PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2867         PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2868 #endif
2869 #ifdef USE_STAT_BLOCKS
2870         PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2871         PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2872 #else
2873         PUSHs(sv_2mortal(newSVpvn("", 0)));
2874         PUSHs(sv_2mortal(newSVpvn("", 0)));
2875 #endif
2876     }
2877     RETURN;
2878 }
2879
2880 PP(pp_ftrread)
2881 {
2882     I32 result;
2883     dSP;
2884 #if defined(HAS_ACCESS) && defined(R_OK)
2885     STRLEN n_a;
2886     if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2887         result = access(POPpx, R_OK);
2888         if (result == 0)
2889             RETPUSHYES;
2890         if (result < 0)
2891             RETPUSHUNDEF;
2892         RETPUSHNO;
2893     }
2894     else
2895         result = my_stat();
2896 #else
2897     result = my_stat();
2898 #endif
2899     SPAGAIN;
2900     if (result < 0)
2901         RETPUSHUNDEF;
2902     if (cando(S_IRUSR, 0, &PL_statcache))
2903         RETPUSHYES;
2904     RETPUSHNO;
2905 }
2906
2907 PP(pp_ftrwrite)
2908 {
2909     I32 result;
2910     dSP;
2911 #if defined(HAS_ACCESS) && defined(W_OK)
2912     STRLEN n_a;
2913     if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2914         result = access(POPpx, W_OK);
2915         if (result == 0)
2916             RETPUSHYES;
2917         if (result < 0)
2918             RETPUSHUNDEF;
2919         RETPUSHNO;
2920     }
2921     else
2922         result = my_stat();
2923 #else
2924     result = my_stat();
2925 #endif
2926     SPAGAIN;
2927     if (result < 0)
2928         RETPUSHUNDEF;
2929     if (cando(S_IWUSR, 0, &PL_statcache))
2930         RETPUSHYES;
2931     RETPUSHNO;
2932 }
2933
2934 PP(pp_ftrexec)
2935 {
2936     I32 result;
2937     dSP;
2938 #if defined(HAS_ACCESS) && defined(X_OK)
2939     STRLEN n_a;
2940     if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2941         result = access(POPpx, X_OK);
2942         if (result == 0)
2943             RETPUSHYES;
2944         if (result < 0)
2945             RETPUSHUNDEF;
2946         RETPUSHNO;
2947     }
2948     else
2949         result = my_stat();
2950 #else
2951     result = my_stat();
2952 #endif
2953     SPAGAIN;
2954     if (result < 0)
2955         RETPUSHUNDEF;
2956     if (cando(S_IXUSR, 0, &PL_statcache))
2957         RETPUSHYES;
2958     RETPUSHNO;
2959 }
2960
2961 PP(pp_fteread)
2962 {
2963     I32 result;
2964     dSP;
2965 #ifdef PERL_EFF_ACCESS_R_OK
2966     STRLEN n_a;
2967     if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2968         result = PERL_EFF_ACCESS_R_OK(POPpx);
2969         if (result == 0)
2970             RETPUSHYES;
2971         if (result < 0)
2972             RETPUSHUNDEF;
2973         RETPUSHNO;
2974     }
2975     else
2976         result = my_stat();
2977 #else
2978     result = my_stat();
2979 #endif
2980     SPAGAIN;
2981     if (result < 0)
2982         RETPUSHUNDEF;
2983     if (cando(S_IRUSR, 1, &PL_statcache))
2984         RETPUSHYES;
2985     RETPUSHNO;
2986 }
2987
2988 PP(pp_ftewrite)
2989 {
2990     I32 result;
2991     dSP;
2992 #ifdef PERL_EFF_ACCESS_W_OK
2993     STRLEN n_a;
2994     if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2995         result = PERL_EFF_ACCESS_W_OK(POPpx);
2996         if (result == 0)
2997             RETPUSHYES;
2998         if (result < 0)
2999             RETPUSHUNDEF;
3000         RETPUSHNO;
3001     }
3002     else
3003         result = my_stat();
3004 #else
3005     result = my_stat();
3006 #endif
3007     SPAGAIN;
3008     if (result < 0)
3009         RETPUSHUNDEF;
3010     if (cando(S_IWUSR, 1, &PL_statcache))
3011         RETPUSHYES;
3012     RETPUSHNO;
3013 }
3014
3015 PP(pp_fteexec)
3016 {
3017     I32 result;
3018     dSP;
3019 #ifdef PERL_EFF_ACCESS_X_OK
3020     STRLEN n_a;
3021     if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3022         result = PERL_EFF_ACCESS_X_OK(POPpx);
3023         if (result == 0)
3024             RETPUSHYES;
3025         if (result < 0)
3026             RETPUSHUNDEF;
3027         RETPUSHNO;
3028     }
3029     else
3030         result = my_stat();
3031 #else
3032     result = my_stat();
3033 #endif
3034     SPAGAIN;
3035     if (result < 0)
3036         RETPUSHUNDEF;
3037     if (cando(S_IXUSR, 1, &PL_statcache))
3038         RETPUSHYES;
3039     RETPUSHNO;
3040 }
3041
3042 PP(pp_ftis)
3043 {
3044     I32 result = my_stat();
3045     dSP;
3046     if (result < 0)
3047         RETPUSHUNDEF;
3048     RETPUSHYES;
3049 }
3050
3051 PP(pp_fteowned)
3052 {
3053     return pp_ftrowned();
3054 }
3055
3056 PP(pp_ftrowned)
3057 {
3058     I32 result = my_stat();
3059     dSP;
3060     if (result < 0)
3061         RETPUSHUNDEF;
3062     if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3063                                 PL_euid : PL_uid) )
3064         RETPUSHYES;
3065     RETPUSHNO;
3066 }
3067
3068 PP(pp_ftzero)
3069 {
3070     I32 result = my_stat();
3071     dSP;
3072     if (result < 0)
3073         RETPUSHUNDEF;
3074     if (PL_statcache.st_size == 0)
3075         RETPUSHYES;
3076     RETPUSHNO;
3077 }
3078
3079 PP(pp_ftsize)
3080 {
3081     I32 result = my_stat();
3082     dSP; dTARGET;
3083     if (result < 0)
3084         RETPUSHUNDEF;
3085 #if Off_t_size > IVSIZE
3086     PUSHn(PL_statcache.st_size);
3087 #else
3088     PUSHi(PL_statcache.st_size);
3089 #endif
3090     RETURN;
3091 }
3092
3093 PP(pp_ftmtime)
3094 {
3095     I32 result = my_stat();
3096     dSP; dTARGET;
3097     if (result < 0)
3098         RETPUSHUNDEF;
3099     PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3100     RETURN;
3101 }
3102
3103 PP(pp_ftatime)
3104 {
3105     I32 result = my_stat();
3106     dSP; dTARGET;
3107     if (result < 0)
3108         RETPUSHUNDEF;
3109     PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3110     RETURN;
3111 }
3112
3113 PP(pp_ftctime)
3114 {
3115     I32 result = my_stat();
3116     dSP; dTARGET;
3117     if (result < 0)
3118         RETPUSHUNDEF;
3119     PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3120     RETURN;
3121 }
3122
3123 PP(pp_ftsock)
3124 {
3125     I32 result = my_stat();
3126     dSP;
3127     if (result < 0)
3128         RETPUSHUNDEF;
3129     if (S_ISSOCK(PL_statcache.st_mode))
3130         RETPUSHYES;
3131     RETPUSHNO;
3132 }
3133
3134 PP(pp_ftchr)
3135 {
3136     I32 result = my_stat();
3137     dSP;
3138     if (result < 0)
3139         RETPUSHUNDEF;
3140     if (S_ISCHR(PL_statcache.st_mode))
3141         RETPUSHYES;
3142     RETPUSHNO;
3143 }
3144
3145 PP(pp_ftblk)
3146 {
3147     I32 result = my_stat();
3148     dSP;
3149     if (result < 0)
3150         RETPUSHUNDEF;
3151     if (S_ISBLK(PL_statcache.st_mode))
3152         RETPUSHYES;
3153     RETPUSHNO;
3154 }
3155
3156 PP(pp_ftfile)
3157 {
3158     I32 result = my_stat();
3159     dSP;
3160     if (result < 0)
3161         RETPUSHUNDEF;
3162     if (S_ISREG(PL_statcache.st_mode))
3163         RETPUSHYES;
3164     RETPUSHNO;
3165 }
3166
3167 PP(pp_ftdir)
3168 {
3169     I32 result = my_stat();
3170     dSP;
3171     if (result < 0)
3172         RETPUSHUNDEF;
3173     if (S_ISDIR(PL_statcache.st_mode))
3174         RETPUSHYES;
3175     RETPUSHNO;
3176 }
3177
3178 PP(pp_ftpipe)
3179 {
3180     I32 result = my_stat();
3181     dSP;
3182     if (result < 0)
3183         RETPUSHUNDEF;
3184     if (S_ISFIFO(PL_statcache.st_mode))
3185         RETPUSHYES;
3186     RETPUSHNO;
3187 }
3188
3189 PP(pp_ftlink)
3190 {
3191     I32 result = my_lstat();
3192     dSP;
3193     if (result < 0)
3194         RETPUSHUNDEF;
3195     if (S_ISLNK(PL_statcache.st_mode))
3196         RETPUSHYES;
3197     RETPUSHNO;
3198 }
3199
3200 PP(pp_ftsuid)
3201 {
3202     dSP;
3203 #ifdef S_ISUID
3204     I32 result = my_stat();
3205     SPAGAIN;
3206     if (result < 0)
3207         RETPUSHUNDEF;
3208     if (PL_statcache.st_mode & S_ISUID)
3209         RETPUSHYES;
3210 #endif
3211     RETPUSHNO;
3212 }
3213
3214 PP(pp_ftsgid)
3215 {
3216     dSP;
3217 #ifdef S_ISGID
3218     I32 result = my_stat();
3219     SPAGAIN;
3220     if (result < 0)
3221         RETPUSHUNDEF;
3222     if (PL_statcache.st_mode & S_ISGID)
3223         RETPUSHYES;
3224 #endif
3225     RETPUSHNO;
3226 }
3227
3228 PP(pp_ftsvtx)
3229 {
3230     dSP;
3231 #ifdef S_ISVTX
3232     I32 result = my_stat();
3233     SPAGAIN;
3234     if (result < 0)
3235         RETPUSHUNDEF;
3236     if (PL_statcache.st_mode & S_ISVTX)
3237         RETPUSHYES;
3238 #endif
3239     RETPUSHNO;
3240 }
3241
3242 PP(pp_fttty)
3243 {
3244     dSP;
3245     int fd;
3246     GV *gv;
3247     char *tmps = Nullch;
3248     STRLEN n_a;
3249
3250     if (PL_op->op_flags & OPf_REF)
3251         gv = cGVOP_gv;
3252     else if (isGV(TOPs))
3253         gv = (GV*)POPs;
3254     else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3255         gv = (GV*)SvRV(POPs);
3256     else
3257         gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3258
3259     if (GvIO(gv) && IoIFP(GvIOp(gv)))
3260         fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3261     else if (tmps && isDIGIT(*tmps))
3262         fd = atoi(tmps);
3263     else
3264         RETPUSHUNDEF;
3265     if (PerlLIO_isatty(fd))
3266         RETPUSHYES;
3267     RETPUSHNO;
3268 }
3269
3270 #if defined(atarist) /* this will work with atariST. Configure will
3271                         make guesses for other systems. */
3272 # define FILE_base(f) ((f)->_base)
3273 # define FILE_ptr(f) ((f)->_ptr)
3274 # define FILE_cnt(f) ((f)->_cnt)
3275 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3276 #endif
3277
3278 PP(pp_fttext)
3279 {
3280     dSP;
3281     I32 i;
3282     I32 len;
3283     I32 odd = 0;
3284     STDCHAR tbuf[512];
3285     register STDCHAR *s;
3286     register IO *io;
3287     register SV *sv;
3288     GV *gv;
3289     STRLEN n_a;
3290     PerlIO *fp;
3291
3292     if (PL_op->op_flags & OPf_REF)
3293         gv = cGVOP_gv;
3294     else if (isGV(TOPs))
3295         gv = (GV*)POPs;
3296     else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3297         gv = (GV*)SvRV(POPs);
3298     else
3299         gv = Nullgv;
3300
3301     if (gv) {
3302         EXTEND(SP, 1);
3303         if (gv == PL_defgv) {
3304             if (PL_statgv)
3305                 io = GvIO(PL_statgv);
3306             else {
3307                 sv = PL_statname;
3308                 goto really_filename;
3309             }
3310         }
3311         else {
3312             PL_statgv = gv;
3313             PL_laststatval = -1;
3314             sv_setpv(PL_statname, "");
3315             io = GvIO(PL_statgv);
3316         }
3317         if (io && IoIFP(io)) {
3318             if (! PerlIO_has_base(IoIFP(io)))
3319                 DIE(aTHX_ "-T and -B not implemented on filehandles");
3320             PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3321             if (PL_laststatval < 0)
3322                 RETPUSHUNDEF;
3323             if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3324                 if (PL_op->op_type == OP_FTTEXT)
3325                     RETPUSHNO;
3326                 else
3327                     RETPUSHYES;
3328             }
3329             if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3330                 i = PerlIO_getc(IoIFP(io));
3331                 if (i != EOF)
3332                     (void)PerlIO_ungetc(IoIFP(io),i);
3333             }
3334             if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3335                 RETPUSHYES;
3336             len = PerlIO_get_bufsiz(IoIFP(io));
3337             s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3338             /* sfio can have large buffers - limit to 512 */
3339             if (len > 512)
3340                 len = 512;
3341         }
3342         else {
3343             if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3344                 gv = cGVOP_gv;
3345                 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3346             }
3347             SETERRNO(EBADF,RMS_IFI);
3348             RETPUSHUNDEF;
3349         }
3350     }
3351     else {
3352         sv = POPs;
3353       really_filename:
3354         PL_statgv = Nullgv;
3355         PL_laststatval = -1;
3356         PL_laststype = OP_STAT;
3357         sv_setpv(PL_statname, SvPV(sv, n_a));
3358         if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3359             if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
3360                 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3361             RETPUSHUNDEF;
3362         }
3363         PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3364         if (PL_laststatval < 0) {
3365             (void)PerlIO_close(fp);
3366             RETPUSHUNDEF;
3367         }
3368         PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3369         len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3370         (void)PerlIO_close(fp);
3371         if (len <= 0) {
3372             if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3373                 RETPUSHNO;              /* special case NFS directories */
3374             RETPUSHYES;         /* null file is anything */
3375         }
3376         s = tbuf;
3377     }
3378
3379     /* now scan s to look for textiness */
3380     /*   XXX ASCII dependent code */
3381
3382 #if defined(DOSISH) || defined(USEMYBINMODE)
3383     /* ignore trailing ^Z on short files */
3384     if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3385         --len;
3386 #endif
3387
3388     for (i = 0; i < len; i++, s++) {
3389         if (!*s) {                      /* null never allowed in text */
3390             odd += len;
3391             break;
3392         }
3393 #ifdef EBCDIC
3394         else if (!(isPRINT(*s) || isSPACE(*s)))
3395             odd++;
3396 #else
3397         else if (*s & 128) {
3398 #ifdef USE_LOCALE
3399             if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3400                 continue;
3401 #endif
3402             /* utf8 characters don't count as odd */
3403             if (UTF8_IS_START(*s)) {
3404                 int ulen = UTF8SKIP(s);
3405                 if (ulen < len - i) {
3406                     int j;
3407                     for (j = 1; j < ulen; j++) {
3408                         if (!UTF8_IS_CONTINUATION(s[j]))
3409                             goto not_utf8;
3410                     }
3411                     --ulen;     /* loop does extra increment */
3412                     s += ulen;
3413                     i += ulen;
3414                     continue;
3415                 }
3416             }
3417           not_utf8:
3418             odd++;
3419         }
3420         else if (*s < 32 &&
3421           *s != '\n' && *s != '\r' && *s != '\b' &&
3422           *s != '\t' && *s != '\f' && *s != 27)
3423             odd++;
3424 #endif
3425     }
3426
3427     if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3428         RETPUSHNO;
3429     else
3430         RETPUSHYES;
3431 }
3432
3433 PP(pp_ftbinary)
3434 {
3435     return pp_fttext();
3436 }
3437
3438 /* File calls. */
3439
3440 PP(pp_chdir)
3441 {
3442     dSP; dTARGET;
3443     char *tmps;
3444     SV **svp;
3445     STRLEN n_a;
3446
3447     if( MAXARG == 1 )
3448         tmps = POPpx;
3449     else
3450         tmps = 0;
3451
3452     if( !tmps || !*tmps ) {
3453         if (    (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3454              || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3455 #ifdef VMS
3456              || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3457 #endif
3458            )
3459         {
3460             if( MAXARG == 1 )
3461                 deprecate("chdir('') or chdir(undef) as chdir()");
3462             tmps = SvPV(*svp, n_a);
3463         }
3464         else {
3465             PUSHi(0);
3466             TAINT_PROPER("chdir");
3467             RETURN;
3468         }
3469     }
3470
3471     TAINT_PROPER("chdir");
3472     PUSHi( PerlDir_chdir(tmps) >= 0 );
3473 #ifdef VMS
3474     /* Clear the DEFAULT element of ENV so we'll get the new value
3475      * in the future. */
3476     hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3477 #endif
3478     RETURN;
3479 }
3480
3481 PP(pp_chown)
3482 {
3483 #ifdef HAS_CHOWN
3484     dSP; dMARK; dTARGET;
3485     I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3486
3487     SP = MARK;
3488     PUSHi(value);
3489     RETURN;
3490 #else
3491     DIE(aTHX_ PL_no_func, "chown");
3492 #endif
3493 }
3494
3495 PP(pp_chroot)
3496 {
3497 #ifdef HAS_CHROOT
3498     dSP; dTARGET;
3499     STRLEN n_a;
3500     char *tmps = POPpx;
3501     TAINT_PROPER("chroot");
3502     PUSHi( chroot(tmps) >= 0 );
3503     RETURN;
3504 #else
3505     DIE(aTHX_ PL_no_func, "chroot");
3506 #endif
3507 }
3508
3509 PP(pp_unlink)
3510 {
3511     dSP; dMARK; dTARGET;
3512     I32 value;
3513     value = (I32)apply(PL_op->op_type, MARK, SP);
3514     SP = MARK;
3515     PUSHi(value);
3516     RETURN;
3517 }
3518
3519 PP(pp_chmod)
3520 {
3521     dSP; dMARK; dTARGET;
3522     I32 value;
3523     value = (I32)apply(PL_op->op_type, MARK, SP);
3524     SP = MARK;
3525     PUSHi(value);
3526     RETURN;
3527 }
3528
3529 PP(pp_utime)
3530 {
3531     dSP; dMARK; dTARGET;
3532     I32 value;
3533     value = (I32)apply(PL_op->op_type, MARK, SP);
3534     SP = MARK;
3535     PUSHi(value);
3536     RETURN;
3537 }
3538
3539 PP(pp_rename)
3540 {
3541     dSP; dTARGET;
3542     int anum;
3543     STRLEN n_a;
3544
3545     char *tmps2 = POPpx;
3546     char *tmps = SvPV(TOPs, n_a);
3547     TAINT_PROPER("rename");
3548 #ifdef HAS_RENAME
3549     anum = PerlLIO_rename(tmps, tmps2);
3550 #else
3551     if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3552         if (same_dirent(tmps2, tmps))   /* can always rename to same name */
3553             anum = 1;
3554         else {
3555             if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3556                 (void)UNLINK(tmps2);
3557             if (!(anum = link(tmps, tmps2)))
3558                 anum = UNLINK(tmps);
3559         }
3560     }
3561 #endif
3562     SETi( anum >= 0 );
3563     RETURN;
3564 }
3565
3566 PP(pp_link)
3567 {
3568 #ifdef HAS_LINK
3569     dSP; dTARGET;
3570     STRLEN n_a;
3571     char *tmps2 = POPpx;
3572     char *tmps = SvPV(TOPs, n_a);
3573     TAINT_PROPER("link");
3574     SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3575     RETURN;
3576 #else
3577     DIE(aTHX_ PL_no_func, "link");
3578 #endif
3579 }
3580
3581 PP(pp_symlink)
3582 {
3583 #ifdef HAS_SYMLINK
3584     dSP; dTARGET;
3585     STRLEN n_a;
3586     char *tmps2 = POPpx;
3587     char *tmps = SvPV(TOPs, n_a);
3588     TAINT_PROPER("symlink");
3589     SETi( symlink(tmps, tmps2) >= 0 );
3590     RETURN;
3591 #else
3592     DIE(aTHX_ PL_no_func, "symlink");
3593 #endif
3594 }
3595
3596 PP(pp_readlink)
3597 {
3598     dSP;
3599 #ifdef HAS_SYMLINK
3600     dTARGET;
3601     char *tmps;
3602     char buf[MAXPATHLEN];
3603     int len;
3604     STRLEN n_a;
3605
3606 #ifndef INCOMPLETE_TAINTS
3607     TAINT;
3608 #endif
3609     tmps = POPpx;
3610     len = readlink(tmps, buf, sizeof(buf) - 1);
3611     EXTEND(SP, 1);
3612     if (len < 0)
3613         RETPUSHUNDEF;
3614     PUSHp(buf, len);
3615     RETURN;
3616 #else
3617     EXTEND(SP, 1);
3618     RETSETUNDEF;                /* just pretend it's a normal file */
3619 #endif
3620 }
3621
3622 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3623 STATIC int
3624 S_dooneliner(pTHX_ char *cmd, char *filename)
3625 {
3626     char *save_filename = filename;
3627     char *cmdline;
3628     char *s;
3629     PerlIO *myfp;
3630     int anum = 1;
3631
3632     New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3633     strcpy(cmdline, cmd);
3634     strcat(cmdline, " ");
3635     for (s = cmdline + strlen(cmdline); *filename; ) {
3636         *s++ = '\\';
3637         *s++ = *filename++;
3638     }
3639     strcpy(s, " 2>&1");
3640     myfp = PerlProc_popen(cmdline, "r");
3641     Safefree(cmdline);
3642
3643     if (myfp) {
3644         SV *tmpsv = sv_newmortal();
3645         /* Need to save/restore 'PL_rs' ?? */
3646         s = sv_gets(tmpsv, myfp, 0);
3647         (void)PerlProc_pclose(myfp);
3648         if (s != Nullch) {
3649             int e;
3650             for (e = 1;
3651 #ifdef HAS_SYS_ERRLIST
3652                  e <= sys_nerr
3653 #endif
3654                  ; e++)
3655             {
3656                 /* you don't see this */
3657                 char *errmsg =
3658 #ifdef HAS_SYS_ERRLIST
3659                     sys_errlist[e]
3660 #else
3661                     strerror(e)
3662 #endif
3663                     ;
3664                 if (!errmsg)
3665                     break;
3666                 if (instr(s, errmsg)) {
3667                     SETERRNO(e,0);
3668                     return 0;
3669                 }
3670             }
3671             SETERRNO(0,0);
3672 #ifndef EACCES
3673 #define EACCES EPERM
3674 #endif
3675             if (instr(s, "cannot make"))
3676                 SETERRNO(EEXIST,RMS_FEX);
3677             else if (instr(s, "existing file"))
3678                 SETERRNO(EEXIST,RMS_FEX);
3679             else if (instr(s, "ile exists"))
3680                 SETERRNO(EEXIST,RMS_FEX);
3681             else if (instr(s, "non-exist"))
3682                 SETERRNO(ENOENT,RMS_FNF);
3683             else if (instr(s, "does not exist"))
3684                 SETERRNO(ENOENT,RMS_FNF);
3685             else if (instr(s, "not empty"))
3686                 SETERRNO(EBUSY,SS_DEVOFFLINE);
3687             else if (instr(s, "cannot access"))
3688                 SETERRNO(EACCES,RMS_PRV);
3689             else
3690                 SETERRNO(EPERM,RMS_PRV);
3691             return 0;
3692         }
3693         else {  /* some mkdirs return no failure indication */
3694             anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3695             if (PL_op->op_type == OP_RMDIR)
3696                 anum = !anum;
3697             if (anum)
3698                 SETERRNO(0,0);
3699             else
3700                 SETERRNO(EACCES,RMS_PRV);       /* a guess */
3701         }
3702         return anum;
3703     }
3704     else
3705         return 0;
3706 }
3707 #endif
3708
3709 /* This macro removes trailing slashes from a directory name.
3710  * Different operating and file systems take differently to
3711  * trailing slashes.  According to POSIX 1003.1 1996 Edition
3712  * any number of trailing slashes should be allowed.
3713  * Thusly we snip them away so that even non-conforming
3714  * systems are happy.
3715  * We should probably do this "filtering" for all
3716  * the functions that expect (potentially) directory names:
3717  * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3718  * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3719
3720 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3721     if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3722         do { \
3723             (len)--; \
3724         } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3725         (tmps) = savepvn((tmps), (len)); \
3726         (copy) = TRUE; \
3727     }
3728
3729 PP(pp_mkdir)
3730 {
3731     dSP; dTARGET;
3732     int mode;
3733 #ifndef HAS_MKDIR
3734     int oldumask;
3735 #endif
3736     STRLEN len;
3737     char *tmps;
3738     bool copy = FALSE;
3739
3740     if (MAXARG > 1)
3741         mode = POPi;
3742     else
3743         mode = 0777;
3744
3745     TRIMSLASHES(tmps,len,copy);
3746
3747     TAINT_PROPER("mkdir");
3748 #ifdef HAS_MKDIR
3749     SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3750 #else
3751     SETi( dooneliner("mkdir", tmps) );
3752     oldumask = PerlLIO_umask(0);
3753     PerlLIO_umask(oldumask);
3754     PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3755 #endif
3756     if (copy)
3757         Safefree(tmps);
3758     RETURN;
3759 }
3760
3761 PP(pp_rmdir)
3762 {
3763     dSP; dTARGET;
3764     STRLEN len;
3765     char *tmps;
3766     bool copy = FALSE;
3767
3768     TRIMSLASHES(tmps,len,copy);
3769     TAINT_PROPER("rmdir");
3770 #ifdef HAS_RMDIR
3771     SETi( PerlDir_rmdir(tmps) >= 0 );
3772 #else
3773     SETi( dooneliner("rmdir", tmps) );
3774 #endif
3775     if (copy)
3776         Safefree(tmps);
3777     RETURN;
3778 }
3779
3780 /* Directory calls. */
3781
3782 PP(pp_open_dir)
3783 {
3784 #if defined(Direntry_t) && defined(HAS_READDIR)
3785     dSP;
3786     STRLEN n_a;
3787     char *dirname = POPpx;
3788     GV *gv = (GV*)POPs;
3789     register IO *io = GvIOn(gv);
3790
3791     if (!io)
3792         goto nope;
3793
3794     if (IoDIRP(io))
3795         PerlDir_close(IoDIRP(io));
3796     if (!(IoDIRP(io) = PerlDir_open(dirname)))
3797         goto nope;
3798
3799     RETPUSHYES;
3800 nope:
3801     if (!errno)
3802         SETERRNO(EBADF,RMS_DIR);
3803     RETPUSHUNDEF;
3804 #else
3805     DIE(aTHX_ PL_no_dir_func, "opendir");
3806 #endif
3807 }
3808
3809 PP(pp_readdir)
3810 {
3811 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3812     DIE(aTHX_ PL_no_dir_func, "readdir");
3813 #else
3814 #if !defined(I_DIRENT) && !defined(VMS)
3815     Direntry_t *readdir (DIR *);
3816 #endif
3817     dSP;
3818
3819     SV *sv;
3820     I32 gimme = GIMME;
3821     GV *gv = (GV *)POPs;
3822     register Direntry_t *dp;
3823     register IO *io = GvIOn(gv);
3824
3825     if (!io || !IoDIRP(io))
3826         goto nope;
3827
3828     do {
3829         dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3830         if (!dp)
3831             break;
3832 #ifdef DIRNAMLEN
3833         sv = newSVpvn(dp->d_name, dp->d_namlen);
3834 #else
3835         sv = newSVpv(dp->d_name, 0);
3836 #endif
3837 #ifndef INCOMPLETE_TAINTS
3838         if (!(IoFLAGS(io) & IOf_UNTAINT))
3839             SvTAINTED_on(sv);
3840 #endif
3841         XPUSHs(sv_2mortal(sv));
3842     }
3843     while (gimme == G_ARRAY);
3844
3845     if (!dp && gimme != G_ARRAY)
3846         goto nope;
3847
3848     RETURN;
3849
3850 nope:
3851     if (!errno)
3852         SETERRNO(EBADF,RMS_ISI);
3853     if (GIMME == G_ARRAY)
3854         RETURN;
3855     else
3856         RETPUSHUNDEF;
3857 #endif
3858 }
3859
3860 PP(pp_telldir)
3861 {
3862 #if defined(HAS_TELLDIR) || defined(telldir)
3863     dSP; dTARGET;
3864  /* XXX does _anyone_ need this? --AD 2/20/1998 */
3865  /* XXX netbsd still seemed to.
3866     XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3867     --JHI 1999-Feb-02 */
3868 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3869     long telldir (DIR *);
3870 # endif
3871     GV *gv = (GV*)POPs;
3872     register IO *io = GvIOn(gv);
3873
3874     if (!io || !IoDIRP(io))
3875         goto nope;
3876
3877     PUSHi( PerlDir_tell(IoDIRP(io)) );
3878     RETURN;
3879 nope:
3880     if (!errno)
3881         SETERRNO(EBADF,RMS_ISI);
3882     RETPUSHUNDEF;
3883 #else
3884     DIE(aTHX_ PL_no_dir_func, "telldir");
3885 #endif
3886 }
3887
3888 PP(pp_seekdir)
3889 {
3890 #if defined(HAS_SEEKDIR) || defined(seekdir)
3891     dSP;
3892     long along = POPl;
3893     GV *gv = (GV*)POPs;
3894     register IO *io = GvIOn(gv);
3895
3896     if (!io || !IoDIRP(io))
3897         goto nope;
3898
3899     (void)PerlDir_seek(IoDIRP(io), along);
3900
3901     RETPUSHYES;
3902 nope:
3903     if (!errno)
3904         SETERRNO(EBADF,RMS_ISI);
3905     RETPUSHUNDEF;
3906 #else
3907     DIE(aTHX_ PL_no_dir_func, "seekdir");
3908 #endif
3909 }
3910
3911 PP(pp_rewinddir)
3912 {
3913 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3914     dSP;
3915     GV *gv = (GV*)POPs;
3916     register IO *io = GvIOn(gv);
3917
3918     if (!io || !IoDIRP(io))
3919         goto nope;
3920
3921     (void)PerlDir_rewind(IoDIRP(io));
3922     RETPUSHYES;
3923 nope:
3924     if (!errno)
3925         SETERRNO(EBADF,RMS_ISI);
3926     RETPUSHUNDEF;
3927 #else
3928     DIE(aTHX_ PL_no_dir_func, "rewinddir");
3929 #endif
3930 }
3931
3932 PP(pp_closedir)
3933 {
3934 #if defined(Direntry_t) && defined(HAS_READDIR)
3935     dSP;
3936     GV *gv = (GV*)POPs;
3937     register IO *io = GvIOn(gv);
3938
3939     if (!io || !IoDIRP(io))
3940         goto nope;
3941
3942 #ifdef VOID_CLOSEDIR
3943     PerlDir_close(IoDIRP(io));
3944 #else
3945     if (PerlDir_close(IoDIRP(io)) < 0) {
3946         IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3947         goto nope;
3948     }
3949 #endif
3950     IoDIRP(io) = 0;
3951
3952     RETPUSHYES;
3953 nope:
3954     if (!errno)
3955         SETERRNO(EBADF,RMS_IFI);
3956     RETPUSHUNDEF;
3957 #else
3958     DIE(aTHX_ PL_no_dir_func, "closedir");
3959 #endif
3960 }
3961
3962 /* Process control. */
3963
3964 PP(pp_fork)
3965 {
3966 #ifdef HAS_FORK
3967     dSP; dTARGET;
3968     Pid_t childpid;
3969     GV *tmpgv;
3970
3971     EXTEND(SP, 1);
3972     PERL_FLUSHALL_FOR_CHILD;
3973     childpid = PerlProc_fork();
3974     if (childpid < 0)
3975         RETSETUNDEF;
3976     if (!childpid) {
3977         /*SUPPRESS 560*/
3978         if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3979             SvREADONLY_off(GvSV(tmpgv));
3980             sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3981             SvREADONLY_on(GvSV(tmpgv));
3982         }
3983 #ifdef THREADS_HAVE_PIDS
3984         PL_ppid = (IV)getppid();
3985 #endif
3986         hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3987     }
3988     PUSHi(childpid);
3989     RETURN;
3990 #else
3991 #  if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3992     dSP; dTARGET;
3993     Pid_t childpid;
3994
3995     EXTEND(SP, 1);
3996     PERL_FLUSHALL_FOR_CHILD;
3997     childpid = PerlProc_fork();
3998     if (childpid == -1)
3999         RETSETUNDEF;
4000     PUSHi(childpid);
4001     RETURN;
4002 #  else
4003     DIE(aTHX_ PL_no_func, "fork");
4004 #  endif
4005 #endif
4006 }
4007
4008 PP(pp_wait)
4009 {
4010 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4011     dSP; dTARGET;
4012     Pid_t childpid;
4013     int argflags;
4014
4015     if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4016         childpid = wait4pid(-1, &argflags, 0);
4017     else {
4018         while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4019                errno == EINTR) {
4020           PERL_ASYNC_CHECK();
4021         }
4022     }
4023 #  if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4024     /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4025     STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4026 #  else
4027     STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4028 #  endif
4029     XPUSHi(childpid);
4030     RETURN;
4031 #else
4032     DIE(aTHX_ PL_no_func, "wait");
4033 #endif
4034 }
4035
4036 PP(pp_waitpid)
4037 {
4038 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4039     dSP; dTARGET;
4040     Pid_t pid;
4041     Pid_t result;
4042     int optype;
4043     int argflags;
4044
4045     optype = POPi;
4046     pid = TOPi;
4047     if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4048         result = wait4pid(pid, &argflags, optype);
4049     else {
4050         while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4051                errno == EINTR) {
4052           PERL_ASYNC_CHECK();
4053         }
4054     }
4055 #  if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4056     /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4057     STATUS_NATIVE_SET((result && result != -1) ? argflags : -1);
4058 #  else
4059     STATUS_NATIVE_SET((result > 0) ? argflags : -1);
4060 #  endif
4061     SETi(result);
4062     RETURN;
4063 #else
4064     DIE(aTHX_ PL_no_func, "waitpid");
4065 #endif
4066 }
4067
4068 PP(pp_system)
4069 {
4070     dSP; dMARK; dORIGMARK; dTARGET;
4071     I32 value;
4072     STRLEN n_a;
4073     int result;
4074     I32 did_pipes = 0;
4075
4076     if (PL_tainting) {
4077         TAINT_ENV();
4078         while (++MARK <= SP) {
4079             (void)SvPV_nolen(*MARK);      /* stringify for taint check */
4080             if (PL_tainted)
4081                 break;
4082         }
4083         MARK = ORIGMARK;
4084         TAINT_PROPER("system");
4085     }
4086     PERL_FLUSHALL_FOR_CHILD;
4087 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4088     {
4089         Pid_t childpid;
4090         int pp[2];
4091
4092         if (PerlProc_pipe(pp) >= 0)
4093             did_pipes = 1;
4094         while ((childpid = PerlProc_fork()) == -1) {
4095             if (errno != EAGAIN) {
4096                 value = -1;
4097                 SP = ORIGMARK;
4098                 PUSHi(value);
4099                 if (did_pipes) {
4100                     PerlLIO_close(pp[0]);
4101                     PerlLIO_close(pp[1]);
4102                 }
4103                 RETURN;
4104             }
4105             sleep(5);
4106         }
4107         if (childpid > 0) {
4108             Sigsave_t ihand,qhand; /* place to save signals during system() */
4109             int status;
4110
4111             if (did_pipes)
4112                 PerlLIO_close(pp[1]);
4113 #ifndef PERL_MICRO
4114             rsignal_save(SIGINT, SIG_IGN, &ihand);
4115             rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4116 #endif
4117             do {
4118                 result = wait4pid(childpid, &status, 0);
4119             } while (result == -1 && errno == EINTR);
4120 #ifndef PERL_MICRO
4121             (void)rsignal_restore(SIGINT, &ihand);
4122             (void)rsignal_restore(SIGQUIT, &qhand);
4123 #endif
4124             STATUS_NATIVE_SET(result == -1 ? -1 : status);
4125             do_execfree();      /* free any memory child malloced on fork */
4126             SP = ORIGMARK;
4127             if (did_pipes) {
4128                 int errkid;
4129                 int n = 0, n1;
4130
4131                 while (n < sizeof(int)) {
4132                     n1 = PerlLIO_read(pp[0],
4133                                       (void*)(((char*)&errkid)+n),
4134                                       (sizeof(int)) - n);
4135                     if (n1 <= 0)
4136                         break;
4137                     n += n1;
4138                 }
4139                 PerlLIO_close(pp[0]);
4140                 if (n) {                        /* Error */
4141                     if (n != sizeof(int))
4142                         DIE(aTHX_ "panic: kid popen errno read");
4143                     errno = errkid;             /* Propagate errno from kid */
4144                     STATUS_CURRENT = -1;
4145                 }
4146             }
4147             PUSHi(STATUS_CURRENT);
4148             RETURN;
4149         }
4150         if (did_pipes) {
4151             PerlLIO_close(pp[0]);
4152 #if defined(HAS_FCNTL) && defined(F_SETFD)
4153             fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4154 #endif
4155         }
4156         if (PL_op->op_flags & OPf_STACKED) {
4157             SV *really = *++MARK;
4158             value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4159         }
4160         else if (SP - MARK != 1)
4161             value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4162         else {
4163             value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4164         }
4165         PerlProc__exit(-1);
4166     }
4167 #else /* ! FORK or VMS or OS/2 */
4168     PL_statusvalue = 0;
4169     result = 0;
4170     if (PL_op->op_flags & OPf_STACKED) {
4171         SV *really = *++MARK;
4172 #  if defined(WIN32) || defined(OS2)
4173         value = (I32)do_aspawn(really, MARK, SP);
4174 #  else
4175         value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4176 #  endif
4177     }
4178     else if (SP - MARK != 1) {
4179 #  if defined(WIN32) || defined(OS2)
4180         value = (I32)do_aspawn(Nullsv, MARK, SP);
4181 #  else
4182         value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4183 #  endif
4184     }
4185     else {
4186         value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4187     }
4188     if (PL_statusvalue == -1)   /* hint that value must be returned as is */
4189         result = 1;
4190     STATUS_NATIVE_SET(value);
4191     do_execfree();
4192     SP = ORIGMARK;
4193     PUSHi(result ? value : STATUS_CURRENT);
4194 #endif /* !FORK or VMS */
4195     RETURN;
4196 }
4197
4198 PP(pp_exec)
4199 {
4200     dSP; dMARK; dORIGMARK; dTARGET;
4201     I32 value;
4202     STRLEN n_a;
4203
4204     if (PL_tainting) {
4205         TAINT_ENV();
4206         while (++MARK <= SP) {
4207             (void)SvPV_nolen(*MARK);      /* stringify for taint check */
4208             if (PL_tainted)
4209                 break;
4210         }
4211         MARK = ORIGMARK;
4212         TAINT_PROPER("exec");
4213     }
4214     PERL_FLUSHALL_FOR_CHILD;
4215     if (PL_op->op_flags & OPf_STACKED) {
4216         SV *really = *++MARK;
4217         value = (I32)do_aexec(really, MARK, SP);
4218     }
4219     else if (SP - MARK != 1)
4220 #ifdef VMS
4221         value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4222 #else
4223 #  ifdef __OPEN_VM
4224         {
4225            (void ) do_aspawn(Nullsv, MARK, SP);
4226            value = 0;
4227         }
4228 #  else
4229         value = (I32)do_aexec(Nullsv, MARK, SP);
4230 #  endif
4231 #endif
4232     else {
4233 #ifdef VMS
4234         value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4235 #else
4236 #  ifdef __OPEN_VM
4237         (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4238         value = 0;
4239 #  else
4240         value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4241 #  endif
4242 #endif
4243     }
4244
4245     SP = ORIGMARK;
4246     PUSHi(value);
4247     RETURN;
4248 }
4249
4250 PP(pp_kill)
4251 {
4252 #ifdef HAS_KILL
4253     dSP; dMARK; dTARGET;
4254     I32 value;
4255     value = (I32)apply(PL_op->op_type, MARK, SP);
4256     SP = MARK;
4257     PUSHi(value);
4258     RETURN;
4259 #else
4260     DIE(aTHX_ PL_no_func, "kill");
4261 #endif
4262 }
4263
4264 PP(pp_getppid)
4265 {
4266 #ifdef HAS_GETPPID
4267     dSP; dTARGET;
4268 #   ifdef THREADS_HAVE_PIDS
4269     XPUSHi( PL_ppid );
4270 #   else
4271     XPUSHi( getppid() );
4272 #   endif
4273     RETURN;
4274 #else
4275     DIE(aTHX_ PL_no_func, "getppid");
4276 #endif
4277 }
4278
4279 PP(pp_getpgrp)
4280 {
4281 #ifdef HAS_GETPGRP
4282     dSP; dTARGET;
4283     Pid_t pid;
4284     Pid_t pgrp;
4285
4286     if (MAXARG < 1)
4287         pid = 0;
4288     else
4289         pid = SvIVx(POPs);
4290 #ifdef BSD_GETPGRP
4291     pgrp = (I32)BSD_GETPGRP(pid);
4292 #else
4293     if (pid != 0 && pid != PerlProc_getpid())
4294         DIE(aTHX_ "POSIX getpgrp can't take an argument");
4295     pgrp = getpgrp();
4296 #endif
4297     XPUSHi(pgrp);
4298     RETURN;
4299 #else
4300     DIE(aTHX_ PL_no_func, "getpgrp()");
4301 #endif
4302 }
4303
4304 PP(pp_setpgrp)
4305 {
4306 #ifdef HAS_SETPGRP
4307     dSP; dTARGET;
4308     Pid_t pgrp;
4309     Pid_t pid;
4310     if (MAXARG < 2) {
4311         pgrp = 0;
4312         pid = 0;
4313     }
4314     else {
4315         pgrp = POPi;
4316         pid = TOPi;
4317     }
4318
4319     TAINT_PROPER("setpgrp");
4320 #ifdef BSD_SETPGRP
4321     SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4322 #else
4323     if ((pgrp != 0 && pgrp != PerlProc_getpid())
4324         || (pid != 0 && pid != PerlProc_getpid()))
4325     {
4326         DIE(aTHX_ "setpgrp can't take arguments");
4327     }
4328     SETi( setpgrp() >= 0 );
4329 #endif /* USE_BSDPGRP */
4330     RETURN;
4331 #else
4332     DIE(aTHX_ PL_no_func, "setpgrp()");
4333 #endif
4334 }
4335
4336 PP(pp_getpriority)
4337 {
4338 #ifdef HAS_GETPRIORITY
4339     dSP; dTARGET;
4340     int who = POPi;
4341     int which = TOPi;
4342     SETi( getpriority(which, who) );
4343     RETURN;
4344 #else
4345     DIE(aTHX_ PL_no_func, "getpriority()");
4346 #endif
4347 }
4348
4349 PP(pp_setpriority)
4350 {
4351 #ifdef HAS_SETPRIORITY
4352     dSP; dTARGET;
4353     int niceval = POPi;
4354     int who = POPi;
4355     int which = TOPi;
4356     TAINT_PROPER("setpriority");
4357     SETi( setpriority(which, who, niceval) >= 0 );
4358     RETURN;
4359 #else
4360     DIE(aTHX_ PL_no_func, "setpriority()");
4361 #endif
4362 }
4363
4364 /* Time calls. */
4365
4366 PP(pp_time)
4367 {
4368     dSP; dTARGET;
4369 #ifdef BIG_TIME
4370     XPUSHn( time(Null(Time_t*)) );
4371 #else
4372     XPUSHi( time(Null(Time_t*)) );
4373 #endif
4374     RETURN;
4375 }
4376
4377 PP(pp_tms)
4378 {
4379 #ifdef HAS_TIMES
4380     dSP;
4381     EXTEND(SP, 4);
4382 #ifndef VMS
4383     (void)PerlProc_times(&PL_timesbuf);
4384 #else
4385     (void)PerlProc_times((tbuffer_t *)&PL_timesbuf);  /* time.h uses different name for */
4386                                                    /* struct tms, though same data   */
4387                                                    /* is returned.                   */
4388 #endif
4389
4390     PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4391     if (GIMME == G_ARRAY) {
4392         PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4393         PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4394         PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4395     }
4396     RETURN;
4397 #else
4398 #   ifdef PERL_MICRO
4399     dSP;
4400     PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4401     EXTEND(SP, 4);
4402     if (GIMME == G_ARRAY) {
4403          PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4404          PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4405          PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4406     }
4407     RETURN;
4408 #   else
4409     DIE(aTHX_ "times not implemented");
4410 #   endif
4411 #endif /* HAS_TIMES */
4412 }
4413
4414 PP(pp_localtime)
4415 {
4416     return pp_gmtime();
4417 }
4418
4419 PP(pp_gmtime)
4420 {
4421     dSP;
4422     Time_t when;
4423     struct tm *tmbuf;
4424     static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4425     static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4426                               "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4427
4428     if (MAXARG < 1)
4429         (void)time(&when);
4430     else
4431 #ifdef BIG_TIME
4432         when = (Time_t)SvNVx(POPs);
4433 #else
4434         when = (Time_t)SvIVx(POPs);
4435 #endif
4436
4437     if (PL_op->op_type == OP_LOCALTIME)
4438         tmbuf = localtime(&when);
4439     else
4440         tmbuf = gmtime(&when);
4441
4442     if (GIMME != G_ARRAY) {
4443         SV *tsv;
4444         EXTEND(SP, 1);
4445         EXTEND_MORTAL(1);
4446         if (!tmbuf)
4447             RETPUSHUNDEF;
4448         tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4449                             dayname[tmbuf->tm_wday],
4450                             monname[tmbuf->tm_mon],
4451                             tmbuf->tm_mday,
4452                             tmbuf->tm_hour,
4453                             tmbuf->tm_min,
4454                             tmbuf->tm_sec,
4455                             tmbuf->tm_year + 1900);
4456         PUSHs(sv_2mortal(tsv));
4457     }
4458     else if (tmbuf) {
4459         EXTEND(SP, 9);
4460         EXTEND_MORTAL(9);
4461         PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4462         PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4463         PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4464         PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4465         PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4466         PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4467         PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4468         PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4469         PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4470     }
4471     RETURN;
4472 }
4473
4474 PP(pp_alarm)
4475 {
4476 #ifdef HAS_ALARM
4477     dSP; dTARGET;
4478     int anum;
4479     anum = POPi;
4480     anum = alarm((unsigned int)anum);
4481     EXTEND(SP, 1);
4482     if (anum < 0)
4483         RETPUSHUNDEF;
4484     PUSHi(anum);
4485     RETURN;
4486 #else
4487     DIE(aTHX_ PL_no_func, "alarm");
4488 #endif
4489 }
4490
4491 PP(pp_sleep)
4492 {
4493     dSP; dTARGET;
4494     I32 duration;
4495     Time_t lasttime;
4496     Time_t when;
4497
4498     (void)time(&lasttime);
4499     if (MAXARG < 1)
4500         PerlProc_pause();
4501     else {
4502         duration = POPi;
4503         PerlProc_sleep((unsigned int)duration);
4504     }
4505     (void)time(&when);
4506     XPUSHi(when - lasttime);
4507     RETURN;
4508 }
4509
4510 /* Shared memory. */
4511
4512 PP(pp_shmget)
4513 {
4514     return pp_semget();
4515 }
4516
4517 PP(pp_shmctl)
4518 {
4519     return pp_semctl();
4520 }
4521
4522 PP(pp_shmread)
4523 {
4524     return pp_shmwrite();
4525 }
4526
4527 PP(pp_shmwrite)
4528 {
4529 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4530     dSP; dMARK; dTARGET;
4531     I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4532     SP = MARK;
4533     PUSHi(value);
4534     RETURN;
4535 #else
4536     return pp_semget();
4537 #endif
4538 }
4539
4540 /* Message passing. */
4541
4542 PP(pp_msgget)
4543 {
4544     return pp_semget();
4545 }
4546
4547 PP(pp_msgctl)
4548 {
4549     return pp_semctl();
4550 }
4551
4552 PP(pp_msgsnd)
4553 {
4554 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4555     dSP; dMARK; dTARGET;
4556     I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4557     SP = MARK;
4558     PUSHi(value);
4559     RETURN;
4560 #else
4561     return pp_semget();
4562 #endif
4563 }
4564
4565 PP(pp_msgrcv)
4566 {
4567 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4568     dSP; dMARK; dTARGET;
4569     I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4570     SP = MARK;
4571     PUSHi(value);
4572     RETURN;
4573 #else
4574     return pp_semget();
4575 #endif
4576 }
4577
4578 /* Semaphores. */
4579
4580 PP(pp_semget)
4581 {
4582 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4583     dSP; dMARK; dTARGET;
4584     int anum = do_ipcget(PL_op->op_type, MARK, SP);
4585     SP = MARK;
4586     if (anum == -1)
4587         RETPUSHUNDEF;
4588     PUSHi(anum);
4589     RETURN;
4590 #else
4591     DIE(aTHX_ "System V IPC is not implemented on this machine");
4592 #endif
4593 }
4594
4595 PP(pp_semctl)
4596 {
4597 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4598     dSP; dMARK; dTARGET;
4599     int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4600     SP = MARK;
4601     if (anum == -1)
4602         RETSETUNDEF;
4603     if (anum != 0) {
4604         PUSHi(anum);
4605     }
4606     else {
4607         PUSHp(zero_but_true, ZBTLEN);
4608     }
4609     RETURN;
4610 #else
4611     return pp_semget();
4612 #endif
4613 }
4614
4615 PP(pp_semop)
4616 {
4617 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4618     dSP; dMARK; dTARGET;
4619     I32 value = (I32)(do_semop(MARK, SP) >= 0);
4620     SP = MARK;
4621     PUSHi(value);
4622     RETURN;
4623 #else
4624     return pp_semget();
4625 #endif
4626 }
4627
4628 /* Get system info. */
4629
4630 PP(pp_ghbyname)
4631 {
4632 #ifdef HAS_GETHOSTBYNAME
4633     return pp_ghostent();
4634 #else
4635     DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4636 #endif
4637 }
4638
4639 PP(pp_ghbyaddr)
4640 {
4641 #ifdef HAS_GETHOSTBYADDR
4642     return pp_ghostent();
4643 #else
4644     DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4645 #endif
4646 }
4647
4648 PP(pp_ghostent)
4649 {
4650 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4651     dSP;
4652     I32 which = PL_op->op_type;
4653     register char **elem;
4654     register SV *sv;
4655 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4656     struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4657     struct hostent *gethostbyname(Netdb_name_t);
4658     struct hostent *gethostent(void);
4659 #endif
4660     struct hostent *hent;
4661     unsigned long len;
4662     STRLEN n_a;
4663
4664     EXTEND(SP, 10);
4665     if (which == OP_GHBYNAME) {
4666 #ifdef HAS_GETHOSTBYNAME
4667         char* name = POPpbytex;
4668         hent = PerlSock_gethostbyname(name);
4669 #else
4670         DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4671 #endif
4672     }
4673     else if (which == OP_GHBYADDR) {
4674 #ifdef HAS_GETHOSTBYADDR
4675         int addrtype = POPi;
4676         SV *addrsv = POPs;
4677         STRLEN addrlen;
4678         Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4679
4680         hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4681 #else
4682         DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4683 #endif
4684     }
4685     else
4686 #ifdef HAS_GETHOSTENT
4687         hent = PerlSock_gethostent();
4688 #else
4689         DIE(aTHX_ PL_no_sock_func, "gethostent");
4690 #endif
4691
4692 #ifdef HOST_NOT_FOUND
4693         if (!hent) {
4694 #ifdef USE_REENTRANT_API
4695 #   ifdef USE_GETHOSTENT_ERRNO
4696             h_errno = PL_reentrant_buffer->_gethostent_errno;
4697 #   endif
4698 #endif
4699             STATUS_NATIVE_SET(h_errno);
4700         }
4701 #endif
4702
4703     if (GIMME != G_ARRAY) {
4704         PUSHs(sv = sv_newmortal());
4705         if (hent) {
4706             if (which == OP_GHBYNAME) {
4707                 if (hent->h_addr)
4708                     sv_setpvn(sv, hent->h_addr, hent->h_length);
4709             }
4710             else
4711                 sv_setpv(sv, (char*)hent->h_name);
4712         }
4713         RETURN;
4714     }
4715
4716     if (hent) {
4717         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4718         sv_setpv(sv, (char*)hent->h_name);
4719         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4720         for (elem = hent->h_aliases; elem && *elem; elem++) {
4721             sv_catpv(sv, *elem);
4722             if (elem[1])
4723                 sv_catpvn(sv, " ", 1);
4724         }
4725         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4726         sv_setiv(sv, (IV)hent->h_addrtype);
4727         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4728         len = hent->h_length;
4729         sv_setiv(sv, (IV)len);
4730 #ifdef h_addr
4731         for (elem = hent->h_addr_list; elem && *elem; elem++) {
4732             XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4733             sv_setpvn(sv, *elem, len);
4734         }
4735 #else
4736         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4737         if (hent->h_addr)
4738             sv_setpvn(sv, hent->h_addr, len);
4739 #endif /* h_addr */
4740     }
4741     RETURN;
4742 #else
4743     DIE(aTHX_ PL_no_sock_func, "gethostent");
4744 #endif
4745 }
4746
4747 PP(pp_gnbyname)
4748 {
4749 #ifdef HAS_GETNETBYNAME
4750     return pp_gnetent();
4751 #else
4752     DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4753 #endif
4754 }
4755
4756 PP(pp_gnbyaddr)
4757 {
4758 #ifdef HAS_GETNETBYADDR
4759     return pp_gnetent();
4760 #else
4761     DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4762 #endif
4763 }
4764
4765 PP(pp_gnetent)
4766 {
4767 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4768     dSP;
4769     I32 which = PL_op->op_type;
4770     register char **elem;
4771     register SV *sv;
4772 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4773     struct netent *getnetbyaddr(Netdb_net_t, int);
4774     struct netent *getnetbyname(Netdb_name_t);
4775     struct netent *getnetent(void);
4776 #endif
4777     struct netent *nent;
4778     STRLEN n_a;
4779
4780     if (which == OP_GNBYNAME){
4781 #ifdef HAS_GETNETBYNAME
4782         char *name = POPpbytex;
4783         nent = PerlSock_getnetbyname(name);
4784 #else
4785         DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4786 #endif
4787     }
4788     else if (which == OP_GNBYADDR) {
4789 #ifdef HAS_GETNETBYADDR
4790         int addrtype = POPi;
4791         Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4792         nent = PerlSock_getnetbyaddr(addr, addrtype);
4793 #else
4794         DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4795 #endif
4796     }
4797     else
4798 #ifdef HAS_GETNETENT
4799         nent = PerlSock_getnetent();
4800 #else
4801         DIE(aTHX_ PL_no_sock_func, "getnetent");
4802 #endif
4803
4804 #ifdef HOST_NOT_FOUND
4805         if (!nent) {
4806 #ifdef USE_REENTRANT_API
4807 #   ifdef USE_GETNETENT_ERRNO
4808              h_errno = PL_reentrant_buffer->_getnetent_errno;
4809 #   endif
4810 #endif
4811             STATUS_NATIVE_SET(h_errno);
4812         }
4813 #endif
4814
4815     EXTEND(SP, 4);
4816     if (GIMME != G_ARRAY) {
4817         PUSHs(sv = sv_newmortal());
4818         if (nent) {
4819             if (which == OP_GNBYNAME)
4820                 sv_setiv(sv, (IV)nent->n_net);
4821             else
4822                 sv_setpv(sv, nent->n_name);
4823         }
4824         RETURN;
4825     }
4826
4827     if (nent) {
4828         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4829         sv_setpv(sv, nent->n_name);
4830         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4831         for (elem = nent->n_aliases; elem && *elem; elem++) {
4832             sv_catpv(sv, *elem);
4833             if (elem[1])
4834                 sv_catpvn(sv, " ", 1);
4835         }
4836         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4837         sv_setiv(sv, (IV)nent->n_addrtype);
4838         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4839         sv_setiv(sv, (IV)nent->n_net);
4840     }
4841
4842     RETURN;
4843 #else
4844     DIE(aTHX_ PL_no_sock_func, "getnetent");
4845 #endif
4846 }
4847
4848 PP(pp_gpbyname)
4849 {
4850 #ifdef HAS_GETPROTOBYNAME
4851     return pp_gprotoent();
4852 #else
4853     DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4854 #endif
4855 }
4856
4857 PP(pp_gpbynumber)
4858 {
4859 #ifdef HAS_GETPROTOBYNUMBER
4860     return pp_gprotoent();
4861 #else
4862     DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4863 #endif
4864 }
4865
4866 PP(pp_gprotoent)
4867 {
4868 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4869     dSP;
4870     I32 which = PL_op->op_type;
4871     register char **elem;
4872     register SV *sv;
4873 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4874     struct protoent *getprotobyname(Netdb_name_t);
4875     struct protoent *getprotobynumber(int);
4876     struct protoent *getprotoent(void);
4877 #endif
4878     struct protoent *pent;
4879     STRLEN n_a;
4880
4881     if (which == OP_GPBYNAME) {
4882 #ifdef HAS_GETPROTOBYNAME
4883         char* name = POPpbytex;
4884         pent = PerlSock_getprotobyname(name);
4885 #else
4886         DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4887 #endif
4888     }
4889     else if (which == OP_GPBYNUMBER) {
4890 #ifdef HAS_GETPROTOBYNUMBER
4891         int number = POPi;
4892         pent = PerlSock_getprotobynumber(number);
4893 #else
4894         DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4895 #endif
4896     }
4897     else
4898 #ifdef HAS_GETPROTOENT
4899         pent = PerlSock_getprotoent();
4900 #else
4901         DIE(aTHX_ PL_no_sock_func, "getprotoent");
4902 #endif
4903
4904     EXTEND(SP, 3);
4905     if (GIMME != G_ARRAY) {
4906         PUSHs(sv = sv_newmortal());
4907         if (pent) {
4908             if (which == OP_GPBYNAME)
4909                 sv_setiv(sv, (IV)pent->p_proto);
4910             else
4911                 sv_setpv(sv, pent->p_name);
4912         }
4913         RETURN;
4914     }
4915
4916     if (pent) {
4917         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4918         sv_setpv(sv, pent->p_name);
4919         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4920         for (elem = pent->p_aliases; elem && *elem; elem++) {
4921             sv_catpv(sv, *elem);
4922             if (elem[1])
4923                 sv_catpvn(sv, " ", 1);
4924         }
4925         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4926         sv_setiv(sv, (IV)pent->p_proto);
4927     }
4928
4929     RETURN;
4930 #else
4931     DIE(aTHX_ PL_no_sock_func, "getprotoent");
4932 #endif
4933 }
4934
4935 PP(pp_gsbyname)
4936 {
4937 #ifdef HAS_GETSERVBYNAME
4938     return pp_gservent();
4939 #else
4940     DIE(aTHX_ PL_no_sock_func, "getservbyname");
4941 #endif
4942 }
4943
4944 PP(pp_gsbyport)
4945 {
4946 #ifdef HAS_GETSERVBYPORT
4947     return pp_gservent();
4948 #else
4949     DIE(aTHX_ PL_no_sock_func, "getservbyport");
4950 #endif
4951 }
4952
4953 PP(pp_gservent)
4954 {
4955 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4956     dSP;
4957     I32 which = PL_op->op_type;
4958     register char **elem;
4959     register SV *sv;
4960 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4961     struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4962     struct servent *getservbyport(int, Netdb_name_t);
4963     struct servent *getservent(void);
4964 #endif
4965     struct servent *sent;
4966     STRLEN n_a;
4967
4968     if (which == OP_GSBYNAME) {
4969 #ifdef HAS_GETSERVBYNAME
4970         char *proto = POPpbytex;
4971         char *name = POPpbytex;
4972
4973         if (proto && !*proto)
4974             proto = Nullch;
4975
4976         sent = PerlSock_getservbyname(name, proto);
4977 #else
4978         DIE(aTHX_ PL_no_sock_func, "getservbyname");
4979 #endif
4980     }
4981     else if (which == OP_GSBYPORT) {
4982 #ifdef HAS_GETSERVBYPORT
4983         char *proto = POPpbytex;
4984         unsigned short port = (unsigned short)POPu;
4985
4986         if (proto && !*proto)
4987             proto = Nullch;
4988
4989 #ifdef HAS_HTONS
4990         port = PerlSock_htons(port);
4991 #endif
4992         sent = PerlSock_getservbyport(port, proto);
4993 #else
4994         DIE(aTHX_ PL_no_sock_func, "getservbyport");
4995 #endif
4996     }
4997     else
4998 #ifdef HAS_GETSERVENT
4999         sent = PerlSock_getservent();
5000 #else
5001         DIE(aTHX_ PL_no_sock_func, "getservent");
5002 #endif
5003
5004     EXTEND(SP, 4);
5005     if (GIMME != G_ARRAY) {
5006         PUSHs(sv = sv_newmortal());
5007         if (sent) {
5008             if (which == OP_GSBYNAME) {
5009 #ifdef HAS_NTOHS
5010                 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5011 #else
5012                 sv_setiv(sv, (IV)(sent->s_port));
5013 #endif
5014             }
5015             else
5016                 sv_setpv(sv, sent->s_name);
5017         }
5018         RETURN;
5019     }
5020
5021     if (sent) {
5022         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5023         sv_setpv(sv, sent->s_name);
5024         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5025         for (elem = sent->s_aliases; elem && *elem; elem++) {
5026             sv_catpv(sv, *elem);
5027             if (elem[1])
5028                 sv_catpvn(sv, " ", 1);
5029         }
5030         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5031 #ifdef HAS_NTOHS
5032         sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5033 #else
5034         sv_setiv(sv, (IV)(sent->s_port));
5035 #endif
5036         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5037         sv_setpv(sv, sent->s_proto);
5038     }
5039
5040     RETURN;
5041 #else
5042     DIE(aTHX_ PL_no_sock_func, "getservent");
5043 #endif
5044 }
5045
5046 PP(pp_shostent)
5047 {
5048 #ifdef HAS_SETHOSTENT
5049     dSP;
5050     PerlSock_sethostent(TOPi);
5051     RETSETYES;
5052 #else
5053     DIE(aTHX_ PL_no_sock_func, "sethostent");
5054 #endif
5055 }
5056
5057 PP(pp_snetent)
5058 {
5059 #ifdef HAS_SETNETENT
5060     dSP;
5061     PerlSock_setnetent(TOPi);
5062     RETSETYES;
5063 #else
5064     DIE(aTHX_ PL_no_sock_func, "setnetent");
5065 #endif
5066 }
5067
5068 PP(pp_sprotoent)
5069 {
5070 #ifdef HAS_SETPROTOENT
5071     dSP;
5072     PerlSock_setprotoent(TOPi);
5073     RETSETYES;
5074 #else
5075     DIE(aTHX_ PL_no_sock_func, "setprotoent");
5076 #endif
5077 }
5078
5079 PP(pp_sservent)
5080 {
5081 #ifdef HAS_SETSERVENT
5082     dSP;
5083     PerlSock_setservent(TOPi);
5084     RETSETYES;
5085 #else
5086     DIE(aTHX_ PL_no_sock_func, "setservent");
5087 #endif
5088 }
5089
5090 PP(pp_ehostent)
5091 {
5092 #ifdef HAS_ENDHOSTENT
5093     dSP;
5094     PerlSock_endhostent();
5095     EXTEND(SP,1);
5096     RETPUSHYES;
5097 #else
5098     DIE(aTHX_ PL_no_sock_func, "endhostent");
5099 #endif
5100 }
5101
5102 PP(pp_enetent)
5103 {
5104 #ifdef HAS_ENDNETENT
5105     dSP;
5106     PerlSock_endnetent();
5107     EXTEND(SP,1);
5108     RETPUSHYES;
5109 #else
5110     DIE(aTHX_ PL_no_sock_func, "endnetent");
5111 #endif
5112 }
5113
5114 PP(pp_eprotoent)
5115 {
5116 #ifdef HAS_ENDPROTOENT
5117     dSP;
5118     PerlSock_endprotoent();
5119     EXTEND(SP,1);
5120     RETPUSHYES;
5121 #else
5122     DIE(aTHX_ PL_no_sock_func, "endprotoent");
5123 #endif
5124 }
5125
5126 PP(pp_eservent)
5127 {
5128 #ifdef HAS_ENDSERVENT
5129     dSP;
5130     PerlSock_endservent();
5131     EXTEND(SP,1);
5132     RETPUSHYES;
5133 #else
5134     DIE(aTHX_ PL_no_sock_func, "endservent");
5135 #endif
5136 }
5137
5138 PP(pp_gpwnam)
5139 {
5140 #ifdef HAS_PASSWD
5141     return pp_gpwent();
5142 #else
5143     DIE(aTHX_ PL_no_func, "getpwnam");
5144 #endif
5145 }
5146
5147 PP(pp_gpwuid)
5148 {
5149 #ifdef HAS_PASSWD
5150     return pp_gpwent();
5151 #else
5152     DIE(aTHX_ PL_no_func, "getpwuid");
5153 #endif
5154 }
5155
5156 PP(pp_gpwent)
5157 {
5158 #ifdef HAS_PASSWD
5159     dSP;
5160     I32 which = PL_op->op_type;
5161     register SV *sv;
5162     STRLEN n_a;
5163     struct passwd *pwent  = NULL;
5164     /*
5165      * We currently support only the SysV getsp* shadow password interface.
5166      * The interface is declared in <shadow.h> and often one needs to link
5167      * with -lsecurity or some such.
5168      * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5169      * (and SCO?)
5170      *
5171      * AIX getpwnam() is clever enough to return the encrypted password
5172      * only if the caller (euid?) is root.
5173      *
5174      * There are at least three other shadow password APIs.  Many platforms
5175      * seem to contain more than one interface for accessing the shadow
5176      * password databases, possibly for compatibility reasons.
5177      * The getsp*() is by far he simplest one, the other two interfaces
5178      * are much more complicated, but also very similar to each other.
5179      *
5180      * <sys/types.h>
5181      * <sys/security.h>
5182      * <prot.h>
5183      * struct pr_passwd *getprpw*();
5184      * The password is in
5185      * char getprpw*(...).ufld.fd_encrypt[]
5186      * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5187      *
5188      * <sys/types.h>
5189      * <sys/security.h>
5190      * <prot.h>
5191      * struct es_passwd *getespw*();
5192      * The password is in
5193      * char *(getespw*(...).ufld.fd_encrypt)
5194      * Mention HAS_GETESPWNAM here so that Configure probes for it.
5195      *
5196      * <userpw.h> (AIX)
5197      * struct userpw *getuserpw();
5198      * The password is in
5199      * char *(getuserpw(...)).spw_upw_passwd
5200      * (but the de facto standard getpwnam() should work okay)
5201      *
5202      * Mention I_PROT here so that Configure probes for it.
5203      *
5204      * In HP-UX for getprpw*() the manual page claims that one should include
5205      * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5206      * if one includes <shadow.h> as that includes <hpsecurity.h>,
5207      * and pp_sys.c already includes <shadow.h> if there is such.
5208      *
5209      * Note that <sys/security.h> is already probed for, but currently
5210      * it is only included in special cases.
5211      *
5212      * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5213      * be preferred interface, even though also the getprpw*() interface
5214      * is available) one needs to link with -lsecurity -ldb -laud -lm.
5215      * One also needs to call set_auth_parameters() in main() before
5216      * doing anything else, whether one is using getespw*() or getprpw*().
5217      *
5218      * Note that accessing the shadow databases can be magnitudes
5219      * slower than accessing the standard databases.
5220      *
5221      * --jhi
5222      */
5223
5224 #   if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5225     /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5226      * the pw_comment is left uninitialized. */
5227     PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5228 #   endif
5229
5230     switch (which) {
5231     case OP_GPWNAM:
5232       {
5233         char* name = POPpbytex;
5234         pwent  = getpwnam(name);
5235       }
5236       break;
5237     case OP_GPWUID:
5238       {
5239         Uid_t uid = POPi;
5240         pwent = getpwuid(uid);
5241       }
5242         break;
5243     case OP_GPWENT:
5244 #   ifdef HAS_GETPWENT
5245         pwent  = getpwent();
5246 #ifdef POSIX_BC   /* In some cases pw_passwd has invalid addresses */
5247         if (pwent) pwent = getpwnam(pwent->pw_name);
5248 #endif
5249 #   else
5250         DIE(aTHX_ PL_no_func, "getpwent");
5251 #   endif
5252         break;
5253     }
5254
5255     EXTEND(SP, 10);
5256     if (GIMME != G_ARRAY) {
5257         PUSHs(sv = sv_newmortal());
5258         if (pwent) {
5259             if (which == OP_GPWNAM)
5260 #   if Uid_t_sign <= 0
5261                 sv_setiv(sv, (IV)pwent->pw_uid);
5262 #   else
5263                 sv_setuv(sv, (UV)pwent->pw_uid);
5264 #   endif
5265             else
5266                 sv_setpv(sv, pwent->pw_name);
5267         }
5268         RETURN;
5269     }
5270
5271     if (pwent) {
5272         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5273         sv_setpv(sv, pwent->pw_name);
5274
5275         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5276         SvPOK_off(sv);
5277         /* If we have getspnam(), we try to dig up the shadow
5278          * password.  If we are underprivileged, the shadow
5279          * interface will set the errno to EACCES or similar,
5280          * and return a null pointer.  If this happens, we will
5281          * use the dummy password (usually "*" or "x") from the
5282          * standard password database.
5283          *
5284          * In theory we could skip the shadow call completely
5285          * if euid != 0 but in practice we cannot know which
5286          * security measures are guarding the shadow databases
5287          * on a random platform.
5288          *
5289          * Resist the urge to use additional shadow interfaces.
5290          * Divert the urge to writing an extension instead.
5291          *
5292          * --jhi */
5293         /* Some AIX setups falsely(?) detect some getspnam(), which
5294          * has a different API than the Solaris/IRIX one. */
5295 #   if defined(HAS_GETSPNAM) && !defined(_AIX)
5296         {
5297             struct spwd *spwent;
5298             int saverrno; /* Save and restore errno so that
5299                            * underprivileged attempts seem
5300                            * to have never made the unsccessful
5301                            * attempt to retrieve the shadow password. */
5302
5303             saverrno = errno;
5304             spwent = getspnam(pwent->pw_name);
5305             errno = saverrno;
5306             if (spwent && spwent->sp_pwdp)
5307                 sv_setpv(sv, spwent->sp_pwdp);
5308         }
5309 #   endif
5310 #   ifdef PWPASSWD
5311         if (!SvPOK(sv)) /* Use the standard password, then. */
5312             sv_setpv(sv, pwent->pw_passwd);
5313 #   endif
5314
5315 #   ifndef INCOMPLETE_TAINTS
5316         /* passwd is tainted because user himself can diddle with it.
5317          * admittedly not much and in a very limited way, but nevertheless. */
5318         SvTAINTED_on(sv);
5319 #   endif
5320
5321         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5322 #   if Uid_t_sign <= 0
5323         sv_setiv(sv, (IV)pwent->pw_uid);
5324 #   else
5325         sv_setuv(sv, (UV)pwent->pw_uid);
5326 #   endif
5327
5328         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5329 #   if Uid_t_sign <= 0
5330         sv_setiv(sv, (IV)pwent->pw_gid);
5331 #   else
5332         sv_setuv(sv, (UV)pwent->pw_gid);
5333 #   endif
5334         /* pw_change, pw_quota, and pw_age are mutually exclusive--
5335          * because of the poor interface of the Perl getpw*(),
5336          * not because there's some standard/convention saying so.
5337          * A better interface would have been to return a hash,
5338          * but we are accursed by our history, alas. --jhi.  */
5339         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5340 #   ifdef PWCHANGE
5341         sv_setiv(sv, (IV)pwent->pw_change);
5342 #   else
5343 #       ifdef PWQUOTA
5344         sv_setiv(sv, (IV)pwent->pw_quota);
5345 #       else
5346 #           ifdef PWAGE
5347         sv_setpv(sv, pwent->pw_age);
5348 #           endif
5349 #       endif
5350 #   endif
5351
5352         /* pw_class and pw_comment are mutually exclusive--.
5353          * see the above note for pw_change, pw_quota, and pw_age. */
5354         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5355 #   ifdef PWCLASS
5356         sv_setpv(sv, pwent->pw_class);
5357 #   else
5358 #       ifdef PWCOMMENT
5359         sv_setpv(sv, pwent->pw_comment);
5360 #       endif
5361 #   endif
5362
5363         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5364 #   ifdef PWGECOS
5365         sv_setpv(sv, pwent->pw_gecos);
5366 #   endif
5367 #   ifndef INCOMPLETE_TAINTS
5368         /* pw_gecos is tainted because user himself can diddle with it. */
5369         SvTAINTED_on(sv);
5370 #   endif
5371
5372         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5373         sv_setpv(sv, pwent->pw_dir);
5374
5375         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5376         sv_setpv(sv, pwent->pw_shell);
5377 #   ifndef INCOMPLETE_TAINTS
5378         /* pw_shell is tainted because user himself can diddle with it. */
5379         SvTAINTED_on(sv);
5380 #   endif
5381
5382 #   ifdef PWEXPIRE
5383         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5384         sv_setiv(sv, (IV)pwent->pw_expire);
5385 #   endif
5386     }
5387     RETURN;
5388 #else
5389     DIE(aTHX_ PL_no_func, "getpwent");
5390 #endif
5391 }
5392
5393 PP(pp_spwent)
5394 {
5395 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5396     dSP;
5397     setpwent();
5398     RETPUSHYES;
5399 #else
5400     DIE(aTHX_ PL_no_func, "setpwent");
5401 #endif
5402 }
5403
5404 PP(pp_epwent)
5405 {
5406 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5407     dSP;
5408     endpwent();
5409     RETPUSHYES;
5410 #else
5411     DIE(aTHX_ PL_no_func, "endpwent");
5412 #endif
5413 }
5414
5415 PP(pp_ggrnam)
5416 {
5417 #ifdef HAS_GROUP
5418     return pp_ggrent();
5419 #else
5420     DIE(aTHX_ PL_no_func, "getgrnam");
5421 #endif
5422 }
5423
5424 PP(pp_ggrgid)
5425 {
5426 #ifdef HAS_GROUP
5427     return pp_ggrent();
5428 #else
5429     DIE(aTHX_ PL_no_func, "getgrgid");
5430 #endif
5431 }
5432
5433 PP(pp_ggrent)
5434 {
5435 #ifdef HAS_GROUP
5436     dSP;
5437     I32 which = PL_op->op_type;
5438     register char **elem;
5439     register SV *sv;
5440     struct group *grent;
5441     STRLEN n_a;
5442
5443     if (which == OP_GGRNAM) {
5444         char* name = POPpbytex;
5445         grent = (struct group *)getgrnam(name);
5446     }
5447     else if (which == OP_GGRGID) {
5448         Gid_t gid = POPi;
5449         grent = (struct group *)getgrgid(gid);
5450     }
5451     else
5452 #ifdef HAS_GETGRENT
5453         grent = (struct group *)getgrent();
5454 #else
5455         DIE(aTHX_ PL_no_func, "getgrent");
5456 #endif
5457
5458     EXTEND(SP, 4);
5459     if (GIMME != G_ARRAY) {
5460         PUSHs(sv = sv_newmortal());
5461         if (grent) {
5462             if (which == OP_GGRNAM)
5463                 sv_setiv(sv, (IV)grent->gr_gid);
5464             else
5465                 sv_setpv(sv, grent->gr_name);
5466         }
5467         RETURN;
5468     }
5469
5470     if (grent) {
5471         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5472         sv_setpv(sv, grent->gr_name);
5473
5474         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5475 #ifdef GRPASSWD
5476         sv_setpv(sv, grent->gr_passwd);
5477 #endif
5478
5479         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5480         sv_setiv(sv, (IV)grent->gr_gid);
5481
5482 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5483         PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5484         /* In UNICOS/mk (_CRAYMPP) the multithreading
5485          * versions (getgrnam_r, getgrgid_r)
5486          * seem to return an illegal pointer
5487          * as the group members list, gr_mem.
5488          * getgrent() doesn't even have a _r version
5489          * but the gr_mem is poisonous anyway.
5490          * So yes, you cannot get the list of group
5491          * members if building multithreaded in UNICOS/mk. */
5492         for (elem = grent->gr_mem; elem && *elem; elem++) {
5493             sv_catpv(sv, *elem);
5494             if (elem[1])
5495                 sv_catpvn(sv, " ", 1);
5496         }
5497 #endif
5498     }
5499
5500     RETURN;
5501 #else
5502     DIE(aTHX_ PL_no_func, "getgrent");
5503 #endif
5504 }
5505
5506 PP(pp_sgrent)
5507 {
5508 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5509     dSP;
5510     setgrent();
5511     RETPUSHYES;
5512 #else
5513     DIE(aTHX_ PL_no_func, "setgrent");
5514 #endif
5515 }
5516
5517 PP(pp_egrent)
5518 {
5519 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5520     dSP;
5521     endgrent();
5522     RETPUSHYES;
5523 #else
5524     DIE(aTHX_ PL_no_func, "endgrent");
5525 #endif
5526 }
5527
5528 PP(pp_getlogin)
5529 {
5530 #ifdef HAS_GETLOGIN
5531     dSP; dTARGET;
5532     char *tmps;
5533     EXTEND(SP, 1);
5534     if (!(tmps = PerlProc_getlogin()))
5535         RETPUSHUNDEF;
5536     PUSHp(tmps, strlen(tmps));
5537     RETURN;
5538 #else
5539     DIE(aTHX_ PL_no_func, "getlogin");
5540 #endif
5541 }
5542
5543 /* Miscellaneous. */
5544
5545 PP(pp_syscall)
5546 {
5547 #ifdef HAS_SYSCALL
5548     dSP; dMARK; dORIGMARK; dTARGET;
5549     register I32 items = SP - MARK;
5550     unsigned long a[20];
5551     register I32 i = 0;
5552     I32 retval = -1;
5553     STRLEN n_a;
5554
5555     if (PL_tainting) {
5556         while (++MARK <= SP) {
5557             if (SvTAINTED(*MARK)) {
5558                 TAINT;
5559                 break;
5560             }
5561         }
5562         MARK = ORIGMARK;
5563         TAINT_PROPER("syscall");
5564     }
5565
5566     /* This probably won't work on machines where sizeof(long) != sizeof(int)
5567      * or where sizeof(long) != sizeof(char*).  But such machines will
5568      * not likely have syscall implemented either, so who cares?
5569      */
5570     while (++MARK <= SP) {
5571         if (SvNIOK(*MARK) || !i)
5572             a[i++] = SvIV(*MARK);
5573         else if (*MARK == &PL_sv_undef)
5574             a[i++] = 0;
5575         else
5576             a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5577         if (i > 15)
5578             break;
5579     }
5580     switch (items) {
5581     default:
5582         DIE(aTHX_ "Too many args to syscall");
5583     case 0:
5584         DIE(aTHX_ "Too few args to syscall");
5585     case 1:
5586         retval = syscall(a[0]);
5587         break;
5588     case 2:
5589         retval = syscall(a[0],a[1]);
5590         break;
5591     case 3:
5592         retval = syscall(a[0],a[1],a[2]);
5593         break;
5594     case 4:
5595         retval = syscall(a[0],a[1],a[2],a[3]);
5596         break;
5597     case 5:
5598         retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5599         break;
5600     case 6:
5601         retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5602         break;
5603     case 7:
5604         retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5605         break;
5606     case 8:
5607         retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5608         break;
5609 #ifdef atarist
5610     case 9:
5611         retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5612         break;
5613     case 10:
5614         retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5615         break;
5616     case 11:
5617         retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5618           a[10]);
5619         break;
5620     case 12:
5621         retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5622           a[10],a[11]);
5623         break;
5624     case 13:
5625         retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5626           a[10],a[11],a[12]);
5627         break;
5628     case 14:
5629         retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5630           a[10],a[11],a[12],a[13]);
5631         break;
5632 #endif /* atarist */
5633     }
5634     SP = ORIGMARK;
5635     PUSHi(retval);
5636     RETURN;
5637 #else
5638     DIE(aTHX_ PL_no_func, "syscall");
5639 #endif
5640 }
5641
5642 #ifdef FCNTL_EMULATE_FLOCK
5643
5644 /*  XXX Emulate flock() with fcntl().
5645     What's really needed is a good file locking module.
5646 */
5647
5648 static int
5649 fcntl_emulate_flock(int fd, int operation)
5650 {
5651     struct flock flock;
5652
5653     switch (operation & ~LOCK_NB) {
5654     case LOCK_SH:
5655         flock.l_type = F_RDLCK;
5656         break;
5657     case LOCK_EX:
5658         flock.l_type = F_WRLCK;
5659         break;
5660     case LOCK_UN:
5661         flock.l_type = F_UNLCK;
5662         break;
5663     default:
5664         errno = EINVAL;
5665         return -1;
5666     }
5667     flock.l_whence = SEEK_SET;
5668     flock.l_start = flock.l_len = (Off_t)0;
5669
5670     return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5671 }
5672
5673 #endif /* FCNTL_EMULATE_FLOCK */
5674
5675 #ifdef LOCKF_EMULATE_FLOCK
5676
5677 /*  XXX Emulate flock() with lockf().  This is just to increase
5678     portability of scripts.  The calls are not completely
5679     interchangeable.  What's really needed is a good file
5680     locking module.
5681 */
5682
5683 /*  The lockf() constants might have been defined in <unistd.h>.
5684     Unfortunately, <unistd.h> causes troubles on some mixed
5685     (BSD/POSIX) systems, such as SunOS 4.1.3.
5686
5687    Further, the lockf() constants aren't POSIX, so they might not be
5688    visible if we're compiling with _POSIX_SOURCE defined.  Thus, we'll
5689    just stick in the SVID values and be done with it.  Sigh.
5690 */
5691
5692 # ifndef F_ULOCK
5693 #  define F_ULOCK       0       /* Unlock a previously locked region */
5694 # endif
5695 # ifndef F_LOCK
5696 #  define F_LOCK        1       /* Lock a region for exclusive use */
5697 # endif
5698 # ifndef F_TLOCK
5699 #  define F_TLOCK       2       /* Test and lock a region for exclusive use */
5700 # endif
5701 # ifndef F_TEST
5702 #  define F_TEST        3       /* Test a region for other processes locks */
5703 # endif
5704
5705 static int
5706 lockf_emulate_flock(int fd, int operation)
5707 {
5708     int i;
5709     int save_errno;
5710     Off_t pos;
5711
5712     /* flock locks entire file so for lockf we need to do the same      */
5713     save_errno = errno;
5714     pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR);    /* get pos to restore later */
5715     if (pos > 0)        /* is seekable and needs to be repositioned     */
5716         if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5717             pos = -1;   /* seek failed, so don't seek back afterwards   */
5718     errno = save_errno;
5719
5720     switch (operation) {
5721
5722         /* LOCK_SH - get a shared lock */
5723         case LOCK_SH:
5724         /* LOCK_EX - get an exclusive lock */
5725         case LOCK_EX:
5726             i = lockf (fd, F_LOCK, 0);
5727             break;
5728
5729         /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5730         case LOCK_SH|LOCK_NB:
5731         /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5732         case LOCK_EX|LOCK_NB:
5733             i = lockf (fd, F_TLOCK, 0);
5734             if (i == -1)
5735                 if ((errno == EAGAIN) || (errno == EACCES))
5736                     errno = EWOULDBLOCK;
5737             break;
5738
5739         /* LOCK_UN - unlock (non-blocking is a no-op) */
5740         case LOCK_UN:
5741         case LOCK_UN|LOCK_NB:
5742             i = lockf (fd, F_ULOCK, 0);
5743             break;
5744
5745         /* Default - can't decipher operation */
5746         default:
5747             i = -1;
5748             errno = EINVAL;
5749             break;
5750     }
5751
5752     if (pos > 0)      /* need to restore position of the handle */
5753         PerlLIO_lseek(fd, pos, SEEK_SET);       /* ignore error here    */
5754
5755     return (i);
5756 }
5757
5758 #endif /* LOCKF_EMULATE_FLOCK */