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