| 1 | /* doio.c |
| 2 | * |
| 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
| 4 | * 2001, 2002, 2003, 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 | * Far below them they saw the white waters pour into a foaming bowl, and |
| 13 | * then swirl darkly about a deep oval basin in the rocks, until they found |
| 14 | * their way out again through a narrow gate, and flowed away, fuming and |
| 15 | * chattering, into calmer and more level reaches. |
| 16 | * |
| 17 | * [p.684 of _The Lord of the Rings_, IV/vi: "The Forbidden Pool"] |
| 18 | */ |
| 19 | |
| 20 | /* This file contains functions that do the actual I/O on behalf of ops. |
| 21 | * For example, pp_print() calls the do_print() function in this file for |
| 22 | * each argument needing printing. |
| 23 | */ |
| 24 | |
| 25 | #include "EXTERN.h" |
| 26 | #define PERL_IN_DOIO_C |
| 27 | #include "perl.h" |
| 28 | |
| 29 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
| 30 | #ifndef HAS_SEM |
| 31 | #include <sys/ipc.h> |
| 32 | #endif |
| 33 | #ifdef HAS_MSG |
| 34 | #include <sys/msg.h> |
| 35 | #endif |
| 36 | #ifdef HAS_SHM |
| 37 | #include <sys/shm.h> |
| 38 | # ifndef HAS_SHMAT_PROTOTYPE |
| 39 | extern Shmat_t shmat (int, char *, int); |
| 40 | # endif |
| 41 | #endif |
| 42 | #endif |
| 43 | |
| 44 | #ifdef I_UTIME |
| 45 | # if defined(_MSC_VER) || defined(__MINGW32__) |
| 46 | # include <sys/utime.h> |
| 47 | # else |
| 48 | # include <utime.h> |
| 49 | # endif |
| 50 | #endif |
| 51 | |
| 52 | #ifdef O_EXCL |
| 53 | # define OPEN_EXCL O_EXCL |
| 54 | #else |
| 55 | # define OPEN_EXCL 0 |
| 56 | #endif |
| 57 | |
| 58 | #define PERL_MODE_MAX 8 |
| 59 | #define PERL_FLAGS_MAX 10 |
| 60 | |
| 61 | #include <signal.h> |
| 62 | |
| 63 | bool |
| 64 | Perl_do_openn(pTHX_ GV *gv, register const char *oname, I32 len, int as_raw, |
| 65 | int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp, |
| 66 | I32 num_svs) |
| 67 | { |
| 68 | dVAR; |
| 69 | register IO * const io = GvIOn(gv); |
| 70 | PerlIO *saveifp = NULL; |
| 71 | PerlIO *saveofp = NULL; |
| 72 | int savefd = -1; |
| 73 | char savetype = IoTYPE_CLOSED; |
| 74 | int writing = 0; |
| 75 | PerlIO *fp; |
| 76 | int fd; |
| 77 | int result; |
| 78 | bool was_fdopen = FALSE; |
| 79 | bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0; |
| 80 | char *type = NULL; |
| 81 | char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */ |
| 82 | SV *namesv; |
| 83 | |
| 84 | PERL_ARGS_ASSERT_DO_OPENN; |
| 85 | |
| 86 | Zero(mode,sizeof(mode),char); |
| 87 | PL_forkprocess = 1; /* assume true if no fork */ |
| 88 | |
| 89 | /* Collect default raw/crlf info from the op */ |
| 90 | if (PL_op && PL_op->op_type == OP_OPEN) { |
| 91 | /* set up IO layers */ |
| 92 | const U8 flags = PL_op->op_private; |
| 93 | in_raw = (flags & OPpOPEN_IN_RAW); |
| 94 | in_crlf = (flags & OPpOPEN_IN_CRLF); |
| 95 | out_raw = (flags & OPpOPEN_OUT_RAW); |
| 96 | out_crlf = (flags & OPpOPEN_OUT_CRLF); |
| 97 | } |
| 98 | |
| 99 | /* If currently open - close before we re-open */ |
| 100 | if (IoIFP(io)) { |
| 101 | fd = PerlIO_fileno(IoIFP(io)); |
| 102 | if (IoTYPE(io) == IoTYPE_STD) { |
| 103 | /* This is a clone of one of STD* handles */ |
| 104 | result = 0; |
| 105 | } |
| 106 | else if (fd >= 0 && fd <= PL_maxsysfd) { |
| 107 | /* This is one of the original STD* handles */ |
| 108 | saveifp = IoIFP(io); |
| 109 | saveofp = IoOFP(io); |
| 110 | savetype = IoTYPE(io); |
| 111 | savefd = fd; |
| 112 | result = 0; |
| 113 | } |
| 114 | else if (IoTYPE(io) == IoTYPE_PIPE) |
| 115 | result = PerlProc_pclose(IoIFP(io)); |
| 116 | else if (IoIFP(io) != IoOFP(io)) { |
| 117 | if (IoOFP(io)) { |
| 118 | result = PerlIO_close(IoOFP(io)); |
| 119 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
| 120 | } |
| 121 | else |
| 122 | result = PerlIO_close(IoIFP(io)); |
| 123 | } |
| 124 | else |
| 125 | result = PerlIO_close(IoIFP(io)); |
| 126 | if (result == EOF && fd > PL_maxsysfd) { |
| 127 | /* Why is this not Perl_warn*() call ? */ |
| 128 | PerlIO_printf(Perl_error_log, |
| 129 | "Warning: unable to close filehandle %"HEKf" properly.\n", |
| 130 | HEKfARG(GvENAME_HEK(gv)) |
| 131 | ); |
| 132 | } |
| 133 | IoOFP(io) = IoIFP(io) = NULL; |
| 134 | } |
| 135 | |
| 136 | if (as_raw) { |
| 137 | /* sysopen style args, i.e. integer mode and permissions */ |
| 138 | STRLEN ix = 0; |
| 139 | const int appendtrunc = |
| 140 | 0 |
| 141 | #ifdef O_APPEND /* Not fully portable. */ |
| 142 | |O_APPEND |
| 143 | #endif |
| 144 | #ifdef O_TRUNC /* Not fully portable. */ |
| 145 | |O_TRUNC |
| 146 | #endif |
| 147 | ; |
| 148 | const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc; |
| 149 | int ismodifying; |
| 150 | |
| 151 | if (num_svs != 0) { |
| 152 | Perl_croak(aTHX_ "panic: sysopen with multiple args"); |
| 153 | } |
| 154 | /* It's not always |
| 155 | |
| 156 | O_RDONLY 0 |
| 157 | O_WRONLY 1 |
| 158 | O_RDWR 2 |
| 159 | |
| 160 | It might be (in OS/390 and Mac OS Classic it is) |
| 161 | |
| 162 | O_WRONLY 1 |
| 163 | O_RDONLY 2 |
| 164 | O_RDWR 3 |
| 165 | |
| 166 | This means that simple & with O_RDWR would look |
| 167 | like O_RDONLY is present. Therefore we have to |
| 168 | be more careful. |
| 169 | */ |
| 170 | if ((ismodifying = (rawmode & modifyingmode))) { |
| 171 | if ((ismodifying & O_WRONLY) == O_WRONLY || |
| 172 | (ismodifying & O_RDWR) == O_RDWR || |
| 173 | (ismodifying & (O_CREAT|appendtrunc))) |
| 174 | TAINT_PROPER("sysopen"); |
| 175 | } |
| 176 | mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */ |
| 177 | |
| 178 | #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE) |
| 179 | rawmode |= O_LARGEFILE; /* Transparently largefiley. */ |
| 180 | #endif |
| 181 | |
| 182 | IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing); |
| 183 | |
| 184 | namesv = newSVpvn_flags(oname, len, SVs_TEMP); |
| 185 | num_svs = 1; |
| 186 | svp = &namesv; |
| 187 | type = NULL; |
| 188 | fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp); |
| 189 | } |
| 190 | else { |
| 191 | /* Regular (non-sys) open */ |
| 192 | char *name; |
| 193 | STRLEN olen = len; |
| 194 | char *tend; |
| 195 | int dodup = 0; |
| 196 | |
| 197 | type = savepvn(oname, len); |
| 198 | tend = type+len; |
| 199 | SAVEFREEPV(type); |
| 200 | |
| 201 | /* Lose leading and trailing white space */ |
| 202 | while (isSPACE(*type)) |
| 203 | type++; |
| 204 | while (tend > type && isSPACE(tend[-1])) |
| 205 | *--tend = '\0'; |
| 206 | |
| 207 | if (num_svs) { |
| 208 | /* New style explicit name, type is just mode and layer info */ |
| 209 | #ifdef USE_STDIO |
| 210 | if (SvROK(*svp) && !strchr(oname,'&')) { |
| 211 | if (ckWARN(WARN_IO)) |
| 212 | Perl_warner(aTHX_ packWARN(WARN_IO), |
| 213 | "Can't open a reference"); |
| 214 | SETERRNO(EINVAL, LIB_INVARG); |
| 215 | goto say_false; |
| 216 | } |
| 217 | #endif /* USE_STDIO */ |
| 218 | name = (SvOK(*svp) || SvGMAGICAL(*svp)) ? |
| 219 | savesvpv (*svp) : savepvs (""); |
| 220 | SAVEFREEPV(name); |
| 221 | } |
| 222 | else { |
| 223 | name = type; |
| 224 | len = tend-type; |
| 225 | } |
| 226 | IoTYPE(io) = *type; |
| 227 | if ((*type == IoTYPE_RDWR) && /* scary */ |
| 228 | (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) && |
| 229 | ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) { |
| 230 | TAINT_PROPER("open"); |
| 231 | mode[1] = *type++; |
| 232 | writing = 1; |
| 233 | } |
| 234 | |
| 235 | if (*type == IoTYPE_PIPE) { |
| 236 | if (num_svs) { |
| 237 | if (type[1] != IoTYPE_STD) { |
| 238 | unknown_open_mode: |
| 239 | Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname); |
| 240 | } |
| 241 | type++; |
| 242 | } |
| 243 | do { |
| 244 | type++; |
| 245 | } while (isSPACE(*type)); |
| 246 | if (!num_svs) { |
| 247 | name = type; |
| 248 | len = tend-type; |
| 249 | } |
| 250 | if (*name == '\0') { |
| 251 | /* command is missing 19990114 */ |
| 252 | if (ckWARN(WARN_PIPE)) |
| 253 | Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open"); |
| 254 | errno = EPIPE; |
| 255 | goto say_false; |
| 256 | } |
| 257 | if (!(*name == '-' && name[1] == '\0') || num_svs) |
| 258 | TAINT_ENV(); |
| 259 | TAINT_PROPER("piped open"); |
| 260 | if (!num_svs && name[len-1] == '|') { |
| 261 | name[--len] = '\0' ; |
| 262 | if (ckWARN(WARN_PIPE)) |
| 263 | Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe"); |
| 264 | } |
| 265 | mode[0] = 'w'; |
| 266 | writing = 1; |
| 267 | if (out_raw) |
| 268 | mode[1] = 'b'; |
| 269 | else if (out_crlf) |
| 270 | mode[1] = 't'; |
| 271 | if (num_svs > 1) { |
| 272 | fp = PerlProc_popen_list(mode, num_svs, svp); |
| 273 | } |
| 274 | else { |
| 275 | fp = PerlProc_popen(name,mode); |
| 276 | } |
| 277 | if (num_svs) { |
| 278 | if (*type) { |
| 279 | if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) { |
| 280 | goto say_false; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } /* IoTYPE_PIPE */ |
| 285 | else if (*type == IoTYPE_WRONLY) { |
| 286 | TAINT_PROPER("open"); |
| 287 | type++; |
| 288 | if (*type == IoTYPE_WRONLY) { |
| 289 | /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */ |
| 290 | mode[0] = IoTYPE(io) = IoTYPE_APPEND; |
| 291 | type++; |
| 292 | } |
| 293 | else { |
| 294 | mode[0] = 'w'; |
| 295 | } |
| 296 | writing = 1; |
| 297 | |
| 298 | if (out_raw) |
| 299 | mode[1] = 'b'; |
| 300 | else if (out_crlf) |
| 301 | mode[1] = 't'; |
| 302 | if (*type == '&') { |
| 303 | duplicity: |
| 304 | dodup = PERLIO_DUP_FD; |
| 305 | type++; |
| 306 | if (*type == '=') { |
| 307 | dodup = 0; |
| 308 | type++; |
| 309 | } |
| 310 | if (!num_svs && !*type && supplied_fp) { |
| 311 | /* "<+&" etc. is used by typemaps */ |
| 312 | fp = supplied_fp; |
| 313 | } |
| 314 | else { |
| 315 | PerlIO *that_fp = NULL; |
| 316 | if (num_svs > 1) { |
| 317 | /* diag_listed_as: More than one argument to '%s' open */ |
| 318 | Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io)); |
| 319 | } |
| 320 | while (isSPACE(*type)) |
| 321 | type++; |
| 322 | if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) { |
| 323 | fd = SvUV(*svp); |
| 324 | num_svs = 0; |
| 325 | } |
| 326 | else if (isDIGIT(*type)) { |
| 327 | fd = atoi(type); |
| 328 | } |
| 329 | else { |
| 330 | const IO* thatio; |
| 331 | if (num_svs) { |
| 332 | thatio = sv_2io(*svp); |
| 333 | } |
| 334 | else { |
| 335 | GV * const thatgv = gv_fetchpvn_flags(type, tend - type, |
| 336 | 0, SVt_PVIO); |
| 337 | thatio = GvIO(thatgv); |
| 338 | } |
| 339 | if (!thatio) { |
| 340 | #ifdef EINVAL |
| 341 | SETERRNO(EINVAL,SS_IVCHAN); |
| 342 | #endif |
| 343 | goto say_false; |
| 344 | } |
| 345 | if ((that_fp = IoIFP(thatio))) { |
| 346 | /* Flush stdio buffer before dup. --mjd |
| 347 | * Unfortunately SEEK_CURing 0 seems to |
| 348 | * be optimized away on most platforms; |
| 349 | * only Solaris and Linux seem to flush |
| 350 | * on that. --jhi */ |
| 351 | #ifdef USE_SFIO |
| 352 | /* sfio fails to clear error on next |
| 353 | sfwrite, contrary to documentation. |
| 354 | -- Nicholas Clark */ |
| 355 | if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1) |
| 356 | PerlIO_clearerr(that_fp); |
| 357 | #endif |
| 358 | /* On the other hand, do all platforms |
| 359 | * take gracefully to flushing a read-only |
| 360 | * filehandle? Perhaps we should do |
| 361 | * fsetpos(src)+fgetpos(dst)? --nik */ |
| 362 | PerlIO_flush(that_fp); |
| 363 | fd = PerlIO_fileno(that_fp); |
| 364 | /* When dup()ing STDIN, STDOUT or STDERR |
| 365 | * explicitly set appropriate access mode */ |
| 366 | if (that_fp == PerlIO_stdout() |
| 367 | || that_fp == PerlIO_stderr()) |
| 368 | IoTYPE(io) = IoTYPE_WRONLY; |
| 369 | else if (that_fp == PerlIO_stdin()) |
| 370 | IoTYPE(io) = IoTYPE_RDONLY; |
| 371 | /* When dup()ing a socket, say result is |
| 372 | * one as well */ |
| 373 | else if (IoTYPE(thatio) == IoTYPE_SOCKET) |
| 374 | IoTYPE(io) = IoTYPE_SOCKET; |
| 375 | } |
| 376 | else |
| 377 | fd = -1; |
| 378 | } |
| 379 | if (!num_svs) |
| 380 | type = NULL; |
| 381 | if (that_fp) { |
| 382 | fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup); |
| 383 | } |
| 384 | else { |
| 385 | if (dodup) |
| 386 | fd = PerlLIO_dup(fd); |
| 387 | else |
| 388 | was_fdopen = TRUE; |
| 389 | if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) { |
| 390 | if (dodup && fd >= 0) |
| 391 | PerlLIO_close(fd); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | } /* & */ |
| 396 | else { |
| 397 | while (isSPACE(*type)) |
| 398 | type++; |
| 399 | if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) { |
| 400 | type++; |
| 401 | fp = PerlIO_stdout(); |
| 402 | IoTYPE(io) = IoTYPE_STD; |
| 403 | if (num_svs > 1) { |
| 404 | /* diag_listed_as: More than one argument to '%s' open */ |
| 405 | Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD); |
| 406 | } |
| 407 | } |
| 408 | else { |
| 409 | if (!num_svs) { |
| 410 | namesv = newSVpvn_flags(type, tend - type, SVs_TEMP); |
| 411 | num_svs = 1; |
| 412 | svp = &namesv; |
| 413 | type = NULL; |
| 414 | } |
| 415 | fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp); |
| 416 | } |
| 417 | } /* !& */ |
| 418 | if (!fp && type && *type && *type != ':' && !isIDFIRST(*type)) |
| 419 | goto unknown_open_mode; |
| 420 | } /* IoTYPE_WRONLY */ |
| 421 | else if (*type == IoTYPE_RDONLY) { |
| 422 | do { |
| 423 | type++; |
| 424 | } while (isSPACE(*type)); |
| 425 | mode[0] = 'r'; |
| 426 | if (in_raw) |
| 427 | mode[1] = 'b'; |
| 428 | else if (in_crlf) |
| 429 | mode[1] = 't'; |
| 430 | if (*type == '&') { |
| 431 | goto duplicity; |
| 432 | } |
| 433 | if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) { |
| 434 | type++; |
| 435 | fp = PerlIO_stdin(); |
| 436 | IoTYPE(io) = IoTYPE_STD; |
| 437 | if (num_svs > 1) { |
| 438 | /* diag_listed_as: More than one argument to '%s' open */ |
| 439 | Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD); |
| 440 | } |
| 441 | } |
| 442 | else { |
| 443 | if (!num_svs) { |
| 444 | namesv = newSVpvn_flags(type, tend - type, SVs_TEMP); |
| 445 | num_svs = 1; |
| 446 | svp = &namesv; |
| 447 | type = NULL; |
| 448 | } |
| 449 | fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp); |
| 450 | } |
| 451 | if (!fp && type && *type && *type != ':' && !isIDFIRST(*type)) |
| 452 | goto unknown_open_mode; |
| 453 | } /* IoTYPE_RDONLY */ |
| 454 | else if ((num_svs && /* '-|...' or '...|' */ |
| 455 | type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) || |
| 456 | (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) { |
| 457 | if (num_svs) { |
| 458 | type += 2; /* skip over '-|' */ |
| 459 | } |
| 460 | else { |
| 461 | *--tend = '\0'; |
| 462 | while (tend > type && isSPACE(tend[-1])) |
| 463 | *--tend = '\0'; |
| 464 | for (; isSPACE(*type); type++) |
| 465 | ; |
| 466 | name = type; |
| 467 | len = tend-type; |
| 468 | } |
| 469 | if (*name == '\0') { |
| 470 | /* command is missing 19990114 */ |
| 471 | if (ckWARN(WARN_PIPE)) |
| 472 | Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open"); |
| 473 | errno = EPIPE; |
| 474 | goto say_false; |
| 475 | } |
| 476 | if (!(*name == '-' && name[1] == '\0') || num_svs) |
| 477 | TAINT_ENV(); |
| 478 | TAINT_PROPER("piped open"); |
| 479 | mode[0] = 'r'; |
| 480 | |
| 481 | if (in_raw) |
| 482 | mode[1] = 'b'; |
| 483 | else if (in_crlf) |
| 484 | mode[1] = 't'; |
| 485 | |
| 486 | if (num_svs > 1) { |
| 487 | fp = PerlProc_popen_list(mode,num_svs,svp); |
| 488 | } |
| 489 | else { |
| 490 | fp = PerlProc_popen(name,mode); |
| 491 | } |
| 492 | IoTYPE(io) = IoTYPE_PIPE; |
| 493 | if (num_svs) { |
| 494 | while (isSPACE(*type)) |
| 495 | type++; |
| 496 | if (*type) { |
| 497 | if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) { |
| 498 | goto say_false; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | else { /* layer(Args) */ |
| 504 | if (num_svs) |
| 505 | goto unknown_open_mode; |
| 506 | name = type; |
| 507 | IoTYPE(io) = IoTYPE_RDONLY; |
| 508 | for (; isSPACE(*name); name++) |
| 509 | ; |
| 510 | mode[0] = 'r'; |
| 511 | |
| 512 | if (in_raw) |
| 513 | mode[1] = 'b'; |
| 514 | else if (in_crlf) |
| 515 | mode[1] = 't'; |
| 516 | |
| 517 | if (*name == '-' && name[1] == '\0') { |
| 518 | fp = PerlIO_stdin(); |
| 519 | IoTYPE(io) = IoTYPE_STD; |
| 520 | } |
| 521 | else { |
| 522 | if (!num_svs) { |
| 523 | namesv = newSVpvn_flags(type, tend - type, SVs_TEMP); |
| 524 | num_svs = 1; |
| 525 | svp = &namesv; |
| 526 | type = NULL; |
| 527 | } |
| 528 | fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | if (!fp) { |
| 533 | if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE) |
| 534 | && strchr(oname, '\n') |
| 535 | |
| 536 | ) |
| 537 | Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open"); |
| 538 | goto say_false; |
| 539 | } |
| 540 | |
| 541 | if (ckWARN(WARN_IO)) { |
| 542 | if ((IoTYPE(io) == IoTYPE_RDONLY) && |
| 543 | (fp == PerlIO_stdout() || fp == PerlIO_stderr())) { |
| 544 | Perl_warner(aTHX_ packWARN(WARN_IO), |
| 545 | "Filehandle STD%s reopened as %"HEKf |
| 546 | " only for input", |
| 547 | ((fp == PerlIO_stdout()) ? "OUT" : "ERR"), |
| 548 | HEKfARG(GvENAME_HEK(gv))); |
| 549 | } |
| 550 | else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) { |
| 551 | Perl_warner(aTHX_ packWARN(WARN_IO), |
| 552 | "Filehandle STDIN reopened as %"HEKf" only for output", |
| 553 | HEKfARG(GvENAME_HEK(gv)) |
| 554 | ); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | fd = PerlIO_fileno(fp); |
| 559 | /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a |
| 560 | * socket - this covers PerlIO::scalar - otherwise unless we "know" the |
| 561 | * type probe for socket-ness. |
| 562 | */ |
| 563 | if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) { |
| 564 | if (PerlLIO_fstat(fd,&PL_statbuf) < 0) { |
| 565 | /* If PerlIO claims to have fd we had better be able to fstat() it. */ |
| 566 | (void) PerlIO_close(fp); |
| 567 | goto say_false; |
| 568 | } |
| 569 | #ifndef PERL_MICRO |
| 570 | if (S_ISSOCK(PL_statbuf.st_mode)) |
| 571 | IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */ |
| 572 | #ifdef HAS_SOCKET |
| 573 | else if ( |
| 574 | #ifdef S_IFMT |
| 575 | !(PL_statbuf.st_mode & S_IFMT) |
| 576 | #else |
| 577 | !PL_statbuf.st_mode |
| 578 | #endif |
| 579 | && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */ |
| 580 | && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */ |
| 581 | ) { /* on OS's that return 0 on fstat()ed pipe */ |
| 582 | char tmpbuf[256]; |
| 583 | Sock_size_t buflen = sizeof tmpbuf; |
| 584 | if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0 |
| 585 | || errno != ENOTSOCK) |
| 586 | IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */ |
| 587 | /* but some return 0 for streams too, sigh */ |
| 588 | } |
| 589 | #endif /* HAS_SOCKET */ |
| 590 | #endif /* !PERL_MICRO */ |
| 591 | } |
| 592 | |
| 593 | /* Eeek - FIXME !!! |
| 594 | * If this is a standard handle we discard all the layer stuff |
| 595 | * and just dup the fd into whatever was on the handle before ! |
| 596 | */ |
| 597 | |
| 598 | if (saveifp) { /* must use old fp? */ |
| 599 | /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR |
| 600 | then dup the new fileno down |
| 601 | */ |
| 602 | if (saveofp) { |
| 603 | PerlIO_flush(saveofp); /* emulate PerlIO_close() */ |
| 604 | if (saveofp != saveifp) { /* was a socket? */ |
| 605 | PerlIO_close(saveofp); |
| 606 | } |
| 607 | } |
| 608 | if (savefd != fd) { |
| 609 | /* Still a small can-of-worms here if (say) PerlIO::scalar |
| 610 | is assigned to (say) STDOUT - for now let dup2() fail |
| 611 | and provide the error |
| 612 | */ |
| 613 | if (PerlLIO_dup2(fd, savefd) < 0) { |
| 614 | (void)PerlIO_close(fp); |
| 615 | goto say_false; |
| 616 | } |
| 617 | #ifdef VMS |
| 618 | if (savefd != PerlIO_fileno(PerlIO_stdin())) { |
| 619 | char newname[FILENAME_MAX+1]; |
| 620 | if (PerlIO_getname(fp, newname)) { |
| 621 | if (fd == PerlIO_fileno(PerlIO_stdout())) |
| 622 | Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname); |
| 623 | if (fd == PerlIO_fileno(PerlIO_stderr())) |
| 624 | Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname); |
| 625 | } |
| 626 | } |
| 627 | #endif |
| 628 | |
| 629 | #if !defined(WIN32) |
| 630 | /* PL_fdpid isn't used on Windows, so avoid this useless work. |
| 631 | * XXX Probably the same for a lot of other places. */ |
| 632 | { |
| 633 | Pid_t pid; |
| 634 | SV *sv; |
| 635 | |
| 636 | sv = *av_fetch(PL_fdpid,fd,TRUE); |
| 637 | SvUPGRADE(sv, SVt_IV); |
| 638 | pid = SvIVX(sv); |
| 639 | SvIV_set(sv, 0); |
| 640 | sv = *av_fetch(PL_fdpid,savefd,TRUE); |
| 641 | SvUPGRADE(sv, SVt_IV); |
| 642 | SvIV_set(sv, pid); |
| 643 | } |
| 644 | #endif |
| 645 | |
| 646 | if (was_fdopen) { |
| 647 | /* need to close fp without closing underlying fd */ |
| 648 | int ofd = PerlIO_fileno(fp); |
| 649 | int dupfd = PerlLIO_dup(ofd); |
| 650 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
| 651 | /* Assume if we have F_SETFD we have F_GETFD */ |
| 652 | int coe = fcntl(ofd,F_GETFD); |
| 653 | #endif |
| 654 | PerlIO_close(fp); |
| 655 | PerlLIO_dup2(dupfd,ofd); |
| 656 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
| 657 | /* The dup trick has lost close-on-exec on ofd */ |
| 658 | fcntl(ofd,F_SETFD, coe); |
| 659 | #endif |
| 660 | PerlLIO_close(dupfd); |
| 661 | } |
| 662 | else |
| 663 | PerlIO_close(fp); |
| 664 | } |
| 665 | fp = saveifp; |
| 666 | PerlIO_clearerr(fp); |
| 667 | fd = PerlIO_fileno(fp); |
| 668 | } |
| 669 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
| 670 | if (fd >= 0) { |
| 671 | dSAVE_ERRNO; |
| 672 | fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */ |
| 673 | RESTORE_ERRNO; |
| 674 | } |
| 675 | #endif |
| 676 | IoIFP(io) = fp; |
| 677 | |
| 678 | IoFLAGS(io) &= ~IOf_NOLINE; |
| 679 | if (writing) { |
| 680 | if (IoTYPE(io) == IoTYPE_SOCKET |
| 681 | || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) { |
| 682 | char *s = mode; |
| 683 | if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC) |
| 684 | s++; |
| 685 | *s = 'w'; |
| 686 | if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) { |
| 687 | PerlIO_close(fp); |
| 688 | IoIFP(io) = NULL; |
| 689 | goto say_false; |
| 690 | } |
| 691 | } |
| 692 | else |
| 693 | IoOFP(io) = fp; |
| 694 | } |
| 695 | return TRUE; |
| 696 | |
| 697 | say_false: |
| 698 | IoIFP(io) = saveifp; |
| 699 | IoOFP(io) = saveofp; |
| 700 | IoTYPE(io) = savetype; |
| 701 | return FALSE; |
| 702 | } |
| 703 | |
| 704 | PerlIO * |
| 705 | Perl_nextargv(pTHX_ register GV *gv) |
| 706 | { |
| 707 | dVAR; |
| 708 | register SV *sv; |
| 709 | #ifndef FLEXFILENAMES |
| 710 | int filedev; |
| 711 | int fileino; |
| 712 | #endif |
| 713 | Uid_t fileuid; |
| 714 | Gid_t filegid; |
| 715 | IO * const io = GvIOp(gv); |
| 716 | |
| 717 | PERL_ARGS_ASSERT_NEXTARGV; |
| 718 | |
| 719 | if (!PL_argvoutgv) |
| 720 | PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
| 721 | if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) { |
| 722 | IoFLAGS(io) &= ~IOf_START; |
| 723 | if (PL_inplace) { |
| 724 | assert(PL_defoutgv); |
| 725 | Perl_av_create_and_push(aTHX_ &PL_argvout_stack, |
| 726 | SvREFCNT_inc_simple_NN(PL_defoutgv)); |
| 727 | } |
| 728 | } |
| 729 | if (PL_filemode & (S_ISUID|S_ISGID)) { |
| 730 | PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */ |
| 731 | #ifdef HAS_FCHMOD |
| 732 | if (PL_lastfd != -1) |
| 733 | (void)fchmod(PL_lastfd,PL_filemode); |
| 734 | #else |
| 735 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
| 736 | #endif |
| 737 | } |
| 738 | PL_lastfd = -1; |
| 739 | PL_filemode = 0; |
| 740 | if (!GvAV(gv)) |
| 741 | return NULL; |
| 742 | while (av_len(GvAV(gv)) >= 0) { |
| 743 | STRLEN oldlen; |
| 744 | sv = av_shift(GvAV(gv)); |
| 745 | SAVEFREESV(sv); |
| 746 | sv_setsv(GvSVn(gv),sv); |
| 747 | SvSETMAGIC(GvSV(gv)); |
| 748 | PL_oldname = SvPVx(GvSV(gv), oldlen); |
| 749 | if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) { |
| 750 | if (PL_inplace) { |
| 751 | TAINT_PROPER("inplace open"); |
| 752 | if (oldlen == 1 && *PL_oldname == '-') { |
| 753 | setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, |
| 754 | SVt_PVIO)); |
| 755 | return IoIFP(GvIOp(gv)); |
| 756 | } |
| 757 | #ifndef FLEXFILENAMES |
| 758 | filedev = PL_statbuf.st_dev; |
| 759 | fileino = PL_statbuf.st_ino; |
| 760 | #endif |
| 761 | PL_filemode = PL_statbuf.st_mode; |
| 762 | fileuid = PL_statbuf.st_uid; |
| 763 | filegid = PL_statbuf.st_gid; |
| 764 | if (!S_ISREG(PL_filemode)) { |
| 765 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
| 766 | "Can't do inplace edit: %s is not a regular file", |
| 767 | PL_oldname ); |
| 768 | do_close(gv,FALSE); |
| 769 | continue; |
| 770 | } |
| 771 | if (*PL_inplace && strNE(PL_inplace, "*")) { |
| 772 | const char *star = strchr(PL_inplace, '*'); |
| 773 | if (star) { |
| 774 | const char *begin = PL_inplace; |
| 775 | sv_setpvs(sv, ""); |
| 776 | do { |
| 777 | sv_catpvn(sv, begin, star - begin); |
| 778 | sv_catpvn(sv, PL_oldname, oldlen); |
| 779 | begin = ++star; |
| 780 | } while ((star = strchr(begin, '*'))); |
| 781 | if (*begin) |
| 782 | sv_catpv(sv,begin); |
| 783 | } |
| 784 | else { |
| 785 | sv_catpv(sv,PL_inplace); |
| 786 | } |
| 787 | #ifndef FLEXFILENAMES |
| 788 | if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0 |
| 789 | && PL_statbuf.st_dev == filedev |
| 790 | && PL_statbuf.st_ino == fileino) |
| 791 | #ifdef DJGPP |
| 792 | || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0) |
| 793 | #endif |
| 794 | ) |
| 795 | { |
| 796 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
| 797 | "Can't do inplace edit: %"SVf" would not be unique", |
| 798 | SVfARG(sv)); |
| 799 | do_close(gv,FALSE); |
| 800 | continue; |
| 801 | } |
| 802 | #endif |
| 803 | #ifdef HAS_RENAME |
| 804 | #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC) |
| 805 | if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) { |
| 806 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
| 807 | "Can't rename %s to %"SVf": %s, skipping file", |
| 808 | PL_oldname, SVfARG(sv), Strerror(errno)); |
| 809 | do_close(gv,FALSE); |
| 810 | continue; |
| 811 | } |
| 812 | #else |
| 813 | do_close(gv,FALSE); |
| 814 | (void)PerlLIO_unlink(SvPVX_const(sv)); |
| 815 | (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv)); |
| 816 | do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),TRUE,O_RDONLY,0,NULL); |
| 817 | #endif /* DOSISH */ |
| 818 | #else |
| 819 | (void)UNLINK(SvPVX_const(sv)); |
| 820 | if (link(PL_oldname,SvPVX_const(sv)) < 0) { |
| 821 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
| 822 | "Can't rename %s to %"SVf": %s, skipping file", |
| 823 | PL_oldname, SVfARG(sv), Strerror(errno) ); |
| 824 | do_close(gv,FALSE); |
| 825 | continue; |
| 826 | } |
| 827 | (void)UNLINK(PL_oldname); |
| 828 | #endif |
| 829 | } |
| 830 | else { |
| 831 | #if !defined(DOSISH) && !defined(AMIGAOS) |
| 832 | # ifndef VMS /* Don't delete; use automatic file versioning */ |
| 833 | if (UNLINK(PL_oldname) < 0) { |
| 834 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
| 835 | "Can't remove %s: %s, skipping file", |
| 836 | PL_oldname, Strerror(errno) ); |
| 837 | do_close(gv,FALSE); |
| 838 | continue; |
| 839 | } |
| 840 | # endif |
| 841 | #else |
| 842 | Perl_croak(aTHX_ "Can't do inplace edit without backup"); |
| 843 | #endif |
| 844 | } |
| 845 | |
| 846 | sv_setpvn(sv,PL_oldname,oldlen); |
| 847 | SETERRNO(0,0); /* in case sprintf set errno */ |
| 848 | if (!Perl_do_openn(aTHX_ PL_argvoutgv, (char*)SvPVX_const(sv), |
| 849 | SvCUR(sv), TRUE, |
| 850 | #ifdef VMS |
| 851 | O_WRONLY|O_CREAT|O_TRUNC,0, |
| 852 | #else |
| 853 | O_WRONLY|O_CREAT|OPEN_EXCL,0600, |
| 854 | #endif |
| 855 | NULL, NULL, 0)) { |
| 856 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s", |
| 857 | PL_oldname, Strerror(errno) ); |
| 858 | do_close(gv,FALSE); |
| 859 | continue; |
| 860 | } |
| 861 | setdefout(PL_argvoutgv); |
| 862 | PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv))); |
| 863 | (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf); |
| 864 | #ifdef HAS_FCHMOD |
| 865 | (void)fchmod(PL_lastfd,PL_filemode); |
| 866 | #else |
| 867 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
| 868 | #endif |
| 869 | if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) { |
| 870 | #ifdef HAS_FCHOWN |
| 871 | (void)fchown(PL_lastfd,fileuid,filegid); |
| 872 | #else |
| 873 | #ifdef HAS_CHOWN |
| 874 | (void)PerlLIO_chown(PL_oldname,fileuid,filegid); |
| 875 | #endif |
| 876 | #endif |
| 877 | } |
| 878 | } |
| 879 | return IoIFP(GvIOp(gv)); |
| 880 | } |
| 881 | else { |
| 882 | if (ckWARN_d(WARN_INPLACE)) { |
| 883 | const int eno = errno; |
| 884 | if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0 |
| 885 | && !S_ISREG(PL_statbuf.st_mode)) |
| 886 | { |
| 887 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), |
| 888 | "Can't do inplace edit: %s is not a regular file", |
| 889 | PL_oldname); |
| 890 | } |
| 891 | else |
| 892 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s", |
| 893 | PL_oldname, Strerror(eno)); |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | if (io && (IoFLAGS(io) & IOf_ARGV)) |
| 898 | IoFLAGS(io) |= IOf_START; |
| 899 | if (PL_inplace) { |
| 900 | (void)do_close(PL_argvoutgv,FALSE); |
| 901 | if (io && (IoFLAGS(io) & IOf_ARGV) |
| 902 | && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0) |
| 903 | { |
| 904 | GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack)); |
| 905 | setdefout(oldout); |
| 906 | SvREFCNT_dec(oldout); |
| 907 | return NULL; |
| 908 | } |
| 909 | setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO)); |
| 910 | } |
| 911 | return NULL; |
| 912 | } |
| 913 | |
| 914 | /* explicit renamed to avoid C++ conflict -- kja */ |
| 915 | bool |
| 916 | Perl_do_close(pTHX_ GV *gv, bool not_implicit) |
| 917 | { |
| 918 | dVAR; |
| 919 | bool retval; |
| 920 | IO *io; |
| 921 | |
| 922 | if (!gv) |
| 923 | gv = PL_argvgv; |
| 924 | if (!gv || !isGV_with_GP(gv)) { |
| 925 | if (not_implicit) |
| 926 | SETERRNO(EBADF,SS_IVCHAN); |
| 927 | return FALSE; |
| 928 | } |
| 929 | io = GvIO(gv); |
| 930 | if (!io) { /* never opened */ |
| 931 | if (not_implicit) { |
| 932 | report_evil_fh(gv); |
| 933 | SETERRNO(EBADF,SS_IVCHAN); |
| 934 | } |
| 935 | return FALSE; |
| 936 | } |
| 937 | retval = io_close(io, not_implicit); |
| 938 | if (not_implicit) { |
| 939 | IoLINES(io) = 0; |
| 940 | IoPAGE(io) = 0; |
| 941 | IoLINES_LEFT(io) = IoPAGE_LEN(io); |
| 942 | } |
| 943 | IoTYPE(io) = IoTYPE_CLOSED; |
| 944 | return retval; |
| 945 | } |
| 946 | |
| 947 | bool |
| 948 | Perl_io_close(pTHX_ IO *io, bool not_implicit) |
| 949 | { |
| 950 | dVAR; |
| 951 | bool retval = FALSE; |
| 952 | |
| 953 | PERL_ARGS_ASSERT_IO_CLOSE; |
| 954 | |
| 955 | if (IoIFP(io)) { |
| 956 | if (IoTYPE(io) == IoTYPE_PIPE) { |
| 957 | const int status = PerlProc_pclose(IoIFP(io)); |
| 958 | if (not_implicit) { |
| 959 | STATUS_NATIVE_CHILD_SET(status); |
| 960 | retval = (STATUS_UNIX == 0); |
| 961 | } |
| 962 | else { |
| 963 | retval = (status != -1); |
| 964 | } |
| 965 | } |
| 966 | else if (IoTYPE(io) == IoTYPE_STD) |
| 967 | retval = TRUE; |
| 968 | else { |
| 969 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */ |
| 970 | const bool prev_err = PerlIO_error(IoOFP(io)); |
| 971 | retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err); |
| 972 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
| 973 | } |
| 974 | else { |
| 975 | const bool prev_err = PerlIO_error(IoIFP(io)); |
| 976 | retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err); |
| 977 | } |
| 978 | } |
| 979 | IoOFP(io) = IoIFP(io) = NULL; |
| 980 | } |
| 981 | else if (not_implicit) { |
| 982 | SETERRNO(EBADF,SS_IVCHAN); |
| 983 | } |
| 984 | |
| 985 | return retval; |
| 986 | } |
| 987 | |
| 988 | bool |
| 989 | Perl_do_eof(pTHX_ GV *gv) |
| 990 | { |
| 991 | dVAR; |
| 992 | register IO * const io = GvIO(gv); |
| 993 | |
| 994 | PERL_ARGS_ASSERT_DO_EOF; |
| 995 | |
| 996 | if (!io) |
| 997 | return TRUE; |
| 998 | else if (IoTYPE(io) == IoTYPE_WRONLY) |
| 999 | report_wrongway_fh(gv, '>'); |
| 1000 | |
| 1001 | while (IoIFP(io)) { |
| 1002 | if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */ |
| 1003 | if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */ |
| 1004 | return FALSE; /* this is the most usual case */ |
| 1005 | } |
| 1006 | |
| 1007 | { |
| 1008 | /* getc and ungetc can stomp on errno */ |
| 1009 | dSAVE_ERRNO; |
| 1010 | const int ch = PerlIO_getc(IoIFP(io)); |
| 1011 | if (ch != EOF) { |
| 1012 | (void)PerlIO_ungetc(IoIFP(io),ch); |
| 1013 | RESTORE_ERRNO; |
| 1014 | return FALSE; |
| 1015 | } |
| 1016 | RESTORE_ERRNO; |
| 1017 | } |
| 1018 | |
| 1019 | if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) { |
| 1020 | if (PerlIO_get_cnt(IoIFP(io)) < -1) |
| 1021 | PerlIO_set_cnt(IoIFP(io),-1); |
| 1022 | } |
| 1023 | if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */ |
| 1024 | if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */ |
| 1025 | return TRUE; |
| 1026 | } |
| 1027 | else |
| 1028 | return TRUE; /* normal fp, definitely end of file */ |
| 1029 | } |
| 1030 | return TRUE; |
| 1031 | } |
| 1032 | |
| 1033 | Off_t |
| 1034 | Perl_do_tell(pTHX_ GV *gv) |
| 1035 | { |
| 1036 | dVAR; |
| 1037 | IO *const io = GvIO(gv); |
| 1038 | register PerlIO *fp; |
| 1039 | |
| 1040 | PERL_ARGS_ASSERT_DO_TELL; |
| 1041 | |
| 1042 | if (io && (fp = IoIFP(io))) { |
| 1043 | #ifdef ULTRIX_STDIO_BOTCH |
| 1044 | if (PerlIO_eof(fp)) |
| 1045 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ |
| 1046 | #endif |
| 1047 | return PerlIO_tell(fp); |
| 1048 | } |
| 1049 | report_evil_fh(gv); |
| 1050 | SETERRNO(EBADF,RMS_IFI); |
| 1051 | return (Off_t)-1; |
| 1052 | } |
| 1053 | |
| 1054 | bool |
| 1055 | Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence) |
| 1056 | { |
| 1057 | dVAR; |
| 1058 | IO *const io = GvIO(gv); |
| 1059 | register PerlIO *fp; |
| 1060 | |
| 1061 | if (io && (fp = IoIFP(io))) { |
| 1062 | #ifdef ULTRIX_STDIO_BOTCH |
| 1063 | if (PerlIO_eof(fp)) |
| 1064 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ |
| 1065 | #endif |
| 1066 | return PerlIO_seek(fp, pos, whence) >= 0; |
| 1067 | } |
| 1068 | report_evil_fh(gv); |
| 1069 | SETERRNO(EBADF,RMS_IFI); |
| 1070 | return FALSE; |
| 1071 | } |
| 1072 | |
| 1073 | Off_t |
| 1074 | Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence) |
| 1075 | { |
| 1076 | dVAR; |
| 1077 | IO *const io = GvIO(gv); |
| 1078 | register PerlIO *fp; |
| 1079 | |
| 1080 | PERL_ARGS_ASSERT_DO_SYSSEEK; |
| 1081 | |
| 1082 | if (io && (fp = IoIFP(io))) |
| 1083 | return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence); |
| 1084 | report_evil_fh(gv); |
| 1085 | SETERRNO(EBADF,RMS_IFI); |
| 1086 | return (Off_t)-1; |
| 1087 | } |
| 1088 | |
| 1089 | int |
| 1090 | Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len) |
| 1091 | { |
| 1092 | int mode = O_BINARY; |
| 1093 | if (s) { |
| 1094 | while (*s) { |
| 1095 | if (*s == ':') { |
| 1096 | switch (s[1]) { |
| 1097 | case 'r': |
| 1098 | if (s[2] == 'a' && s[3] == 'w' |
| 1099 | && (!s[4] || s[4] == ':' || isSPACE(s[4]))) |
| 1100 | { |
| 1101 | mode = O_BINARY; |
| 1102 | s += 4; |
| 1103 | len -= 4; |
| 1104 | break; |
| 1105 | } |
| 1106 | /* FALL THROUGH */ |
| 1107 | case 'c': |
| 1108 | if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f' |
| 1109 | && (!s[5] || s[5] == ':' || isSPACE(s[5]))) |
| 1110 | { |
| 1111 | mode = O_TEXT; |
| 1112 | s += 5; |
| 1113 | len -= 5; |
| 1114 | break; |
| 1115 | } |
| 1116 | /* FALL THROUGH */ |
| 1117 | default: |
| 1118 | goto fail_discipline; |
| 1119 | } |
| 1120 | } |
| 1121 | else if (isSPACE(*s)) { |
| 1122 | ++s; |
| 1123 | --len; |
| 1124 | } |
| 1125 | else { |
| 1126 | const char *end; |
| 1127 | fail_discipline: |
| 1128 | end = strchr(s+1, ':'); |
| 1129 | if (!end) |
| 1130 | end = s+len; |
| 1131 | #ifndef PERLIO_LAYERS |
| 1132 | Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s); |
| 1133 | #else |
| 1134 | len -= end-s; |
| 1135 | s = end; |
| 1136 | #endif |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | return mode; |
| 1141 | } |
| 1142 | |
| 1143 | #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) |
| 1144 | I32 |
| 1145 | my_chsize(int fd, Off_t length) |
| 1146 | { |
| 1147 | #ifdef F_FREESP |
| 1148 | /* code courtesy of William Kucharski */ |
| 1149 | #define HAS_CHSIZE |
| 1150 | |
| 1151 | Stat_t filebuf; |
| 1152 | |
| 1153 | if (PerlLIO_fstat(fd, &filebuf) < 0) |
| 1154 | return -1; |
| 1155 | |
| 1156 | if (filebuf.st_size < length) { |
| 1157 | |
| 1158 | /* extend file length */ |
| 1159 | |
| 1160 | if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0) |
| 1161 | return -1; |
| 1162 | |
| 1163 | /* write a "0" byte */ |
| 1164 | |
| 1165 | if ((PerlLIO_write(fd, "", 1)) != 1) |
| 1166 | return -1; |
| 1167 | } |
| 1168 | else { |
| 1169 | /* truncate length */ |
| 1170 | struct flock fl; |
| 1171 | fl.l_whence = 0; |
| 1172 | fl.l_len = 0; |
| 1173 | fl.l_start = length; |
| 1174 | fl.l_type = F_WRLCK; /* write lock on file space */ |
| 1175 | |
| 1176 | /* |
| 1177 | * This relies on the UNDOCUMENTED F_FREESP argument to |
| 1178 | * fcntl(2), which truncates the file so that it ends at the |
| 1179 | * position indicated by fl.l_start. |
| 1180 | * |
| 1181 | * Will minor miracles never cease? |
| 1182 | */ |
| 1183 | |
| 1184 | if (fcntl(fd, F_FREESP, &fl) < 0) |
| 1185 | return -1; |
| 1186 | |
| 1187 | } |
| 1188 | return 0; |
| 1189 | #else |
| 1190 | Perl_croak_nocontext("truncate not implemented"); |
| 1191 | #endif /* F_FREESP */ |
| 1192 | return -1; |
| 1193 | } |
| 1194 | #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */ |
| 1195 | |
| 1196 | bool |
| 1197 | Perl_do_print(pTHX_ register SV *sv, PerlIO *fp) |
| 1198 | { |
| 1199 | dVAR; |
| 1200 | |
| 1201 | PERL_ARGS_ASSERT_DO_PRINT; |
| 1202 | |
| 1203 | /* assuming fp is checked earlier */ |
| 1204 | if (!sv) |
| 1205 | return TRUE; |
| 1206 | if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) { |
| 1207 | assert(!SvGMAGICAL(sv)); |
| 1208 | if (SvIsUV(sv)) |
| 1209 | PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv)); |
| 1210 | else |
| 1211 | PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv)); |
| 1212 | return !PerlIO_error(fp); |
| 1213 | } |
| 1214 | else { |
| 1215 | STRLEN len; |
| 1216 | /* Do this first to trigger any overloading. */ |
| 1217 | const char *tmps = SvPV_const(sv, len); |
| 1218 | U8 *tmpbuf = NULL; |
| 1219 | bool happy = TRUE; |
| 1220 | |
| 1221 | if (PerlIO_isutf8(fp)) { |
| 1222 | if (!SvUTF8(sv)) { |
| 1223 | /* We don't modify the original scalar. */ |
| 1224 | tmpbuf = bytes_to_utf8((const U8*) tmps, &len); |
| 1225 | tmps = (char *) tmpbuf; |
| 1226 | } |
| 1227 | else if (ckWARN_d(WARN_UTF8)) { |
| 1228 | (void) check_utf8_print((const U8*) tmps, len); |
| 1229 | } |
| 1230 | } |
| 1231 | else if (DO_UTF8(sv)) { |
| 1232 | STRLEN tmplen = len; |
| 1233 | bool utf8 = TRUE; |
| 1234 | U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8); |
| 1235 | if (!utf8) { |
| 1236 | tmpbuf = result; |
| 1237 | tmps = (char *) tmpbuf; |
| 1238 | len = tmplen; |
| 1239 | } |
| 1240 | else { |
| 1241 | assert((char *)result == tmps); |
| 1242 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), |
| 1243 | "Wide character in %s", |
| 1244 | PL_op ? OP_DESC(PL_op) : "print" |
| 1245 | ); |
| 1246 | /* Could also check that isn't one of the things to avoid |
| 1247 | * in utf8 by using check_utf8_print(), but not doing so, |
| 1248 | * since the stream isn't a UTF8 stream */ |
| 1249 | } |
| 1250 | } |
| 1251 | /* To detect whether the process is about to overstep its |
| 1252 | * filesize limit we would need getrlimit(). We could then |
| 1253 | * also transparently raise the limit with setrlimit() -- |
| 1254 | * but only until the system hard limit/the filesystem limit, |
| 1255 | * at which we would get EPERM. Note that when using buffered |
| 1256 | * io the write failure can be delayed until the flush/close. --jhi */ |
| 1257 | if (len && (PerlIO_write(fp,tmps,len) == 0)) |
| 1258 | happy = FALSE; |
| 1259 | Safefree(tmpbuf); |
| 1260 | return happy ? !PerlIO_error(fp) : FALSE; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | I32 |
| 1265 | Perl_my_stat_flags(pTHX_ const U32 flags) |
| 1266 | { |
| 1267 | dVAR; |
| 1268 | dSP; |
| 1269 | IO *io; |
| 1270 | GV* gv; |
| 1271 | |
| 1272 | if (PL_op->op_flags & OPf_REF) { |
| 1273 | EXTEND(SP,1); |
| 1274 | gv = cGVOP_gv; |
| 1275 | do_fstat: |
| 1276 | if (gv == PL_defgv) |
| 1277 | return PL_laststatval; |
| 1278 | io = GvIO(gv); |
| 1279 | do_fstat_have_io: |
| 1280 | PL_laststype = OP_STAT; |
| 1281 | PL_statgv = gv; |
| 1282 | sv_setpvs(PL_statname, ""); |
| 1283 | if(io) { |
| 1284 | if (IoIFP(io)) { |
| 1285 | return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache)); |
| 1286 | } else if (IoDIRP(io)) { |
| 1287 | return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache)); |
| 1288 | } else { |
| 1289 | report_evil_fh(gv); |
| 1290 | return (PL_laststatval = -1); |
| 1291 | } |
| 1292 | } else { |
| 1293 | report_evil_fh(gv); |
| 1294 | return (PL_laststatval = -1); |
| 1295 | } |
| 1296 | } |
| 1297 | else if (PL_op->op_private & OPpFT_STACKED) { |
| 1298 | return PL_laststatval; |
| 1299 | } |
| 1300 | else { |
| 1301 | SV* const sv = POPs; |
| 1302 | const char *s; |
| 1303 | STRLEN len; |
| 1304 | PUTBACK; |
| 1305 | if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) { |
| 1306 | goto do_fstat; |
| 1307 | } |
| 1308 | else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) { |
| 1309 | io = MUTABLE_IO(SvRV(sv)); |
| 1310 | gv = NULL; |
| 1311 | goto do_fstat_have_io; |
| 1312 | } |
| 1313 | |
| 1314 | s = SvPV_flags_const(sv, len, flags); |
| 1315 | PL_statgv = NULL; |
| 1316 | sv_setpvn(PL_statname, s, len); |
| 1317 | s = SvPVX_const(PL_statname); /* s now NUL-terminated */ |
| 1318 | PL_laststype = OP_STAT; |
| 1319 | PL_laststatval = PerlLIO_stat(s, &PL_statcache); |
| 1320 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n')) |
| 1321 | Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat"); |
| 1322 | return PL_laststatval; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | |
| 1327 | I32 |
| 1328 | Perl_my_lstat_flags(pTHX_ const U32 flags) |
| 1329 | { |
| 1330 | dVAR; |
| 1331 | static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat"; |
| 1332 | dSP; |
| 1333 | SV *sv; |
| 1334 | const char *file; |
| 1335 | if (PL_op->op_flags & OPf_REF) { |
| 1336 | EXTEND(SP,1); |
| 1337 | if (cGVOP_gv == PL_defgv) { |
| 1338 | if (PL_laststype != OP_LSTAT) |
| 1339 | Perl_croak(aTHX_ no_prev_lstat); |
| 1340 | return PL_laststatval; |
| 1341 | } |
| 1342 | if (ckWARN(WARN_IO)) { |
| 1343 | Perl_warner(aTHX_ packWARN(WARN_IO), |
| 1344 | "Use of -l on filehandle %"HEKf, |
| 1345 | HEKfARG(GvENAME_HEK(cGVOP_gv))); |
| 1346 | } |
| 1347 | return (PL_laststatval = -1); |
| 1348 | } |
| 1349 | else if (PL_op->op_private & OPpFT_STACKED) { |
| 1350 | if (PL_laststype != OP_LSTAT) |
| 1351 | Perl_croak(aTHX_ no_prev_lstat); |
| 1352 | return PL_laststatval; |
| 1353 | } |
| 1354 | |
| 1355 | PL_laststype = OP_LSTAT; |
| 1356 | PL_statgv = NULL; |
| 1357 | sv = POPs; |
| 1358 | PUTBACK; |
| 1359 | file = SvPV_flags_const_nolen(sv, flags); |
| 1360 | sv_setpv(PL_statname,file); |
| 1361 | PL_laststatval = PerlLIO_lstat(file,&PL_statcache); |
| 1362 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(file, '\n')) |
| 1363 | Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat"); |
| 1364 | return PL_laststatval; |
| 1365 | } |
| 1366 | |
| 1367 | static void |
| 1368 | S_exec_failed(pTHX_ const char *cmd, int fd, int do_report) |
| 1369 | { |
| 1370 | const int e = errno; |
| 1371 | PERL_ARGS_ASSERT_EXEC_FAILED; |
| 1372 | if (ckWARN(WARN_EXEC)) |
| 1373 | Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s", |
| 1374 | cmd, Strerror(e)); |
| 1375 | if (do_report) { |
| 1376 | PerlLIO_write(fd, (void*)&e, sizeof(int)); |
| 1377 | PerlLIO_close(fd); |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | bool |
| 1382 | Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp, |
| 1383 | int fd, int do_report) |
| 1384 | { |
| 1385 | dVAR; |
| 1386 | PERL_ARGS_ASSERT_DO_AEXEC5; |
| 1387 | #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__) |
| 1388 | Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system"); |
| 1389 | #else |
| 1390 | if (sp > mark) { |
| 1391 | const char **a; |
| 1392 | const char *tmps = NULL; |
| 1393 | Newx(PL_Argv, sp - mark + 1, const char*); |
| 1394 | a = PL_Argv; |
| 1395 | |
| 1396 | while (++mark <= sp) { |
| 1397 | if (*mark) |
| 1398 | *a++ = SvPV_nolen_const(*mark); |
| 1399 | else |
| 1400 | *a++ = ""; |
| 1401 | } |
| 1402 | *a = NULL; |
| 1403 | if (really) |
| 1404 | tmps = SvPV_nolen_const(really); |
| 1405 | if ((!really && *PL_Argv[0] != '/') || |
| 1406 | (really && *tmps != '/')) /* will execvp use PATH? */ |
| 1407 | TAINT_ENV(); /* testing IFS here is overkill, probably */ |
| 1408 | PERL_FPU_PRE_EXEC |
| 1409 | if (really && *tmps) |
| 1410 | PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv)); |
| 1411 | else |
| 1412 | PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv)); |
| 1413 | PERL_FPU_POST_EXEC |
| 1414 | S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report); |
| 1415 | } |
| 1416 | do_execfree(); |
| 1417 | #endif |
| 1418 | return FALSE; |
| 1419 | } |
| 1420 | |
| 1421 | void |
| 1422 | Perl_do_execfree(pTHX) |
| 1423 | { |
| 1424 | dVAR; |
| 1425 | Safefree(PL_Argv); |
| 1426 | PL_Argv = NULL; |
| 1427 | Safefree(PL_Cmd); |
| 1428 | PL_Cmd = NULL; |
| 1429 | } |
| 1430 | |
| 1431 | #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION |
| 1432 | |
| 1433 | bool |
| 1434 | Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report) |
| 1435 | { |
| 1436 | dVAR; |
| 1437 | register const char **a; |
| 1438 | register char *s; |
| 1439 | char *buf; |
| 1440 | char *cmd; |
| 1441 | /* Make a copy so we can change it */ |
| 1442 | const Size_t cmdlen = strlen(incmd) + 1; |
| 1443 | |
| 1444 | PERL_ARGS_ASSERT_DO_EXEC3; |
| 1445 | |
| 1446 | Newx(buf, cmdlen, char); |
| 1447 | cmd = buf; |
| 1448 | memcpy(cmd, incmd, cmdlen); |
| 1449 | |
| 1450 | while (*cmd && isSPACE(*cmd)) |
| 1451 | cmd++; |
| 1452 | |
| 1453 | /* save an extra exec if possible */ |
| 1454 | |
| 1455 | #ifdef CSH |
| 1456 | { |
| 1457 | char flags[PERL_FLAGS_MAX]; |
| 1458 | if (strnEQ(cmd,PL_cshname,PL_cshlen) && |
| 1459 | strnEQ(cmd+PL_cshlen," -c",3)) { |
| 1460 | my_strlcpy(flags, "-c", PERL_FLAGS_MAX); |
| 1461 | s = cmd+PL_cshlen+3; |
| 1462 | if (*s == 'f') { |
| 1463 | s++; |
| 1464 | my_strlcat(flags, "f", PERL_FLAGS_MAX - 2); |
| 1465 | } |
| 1466 | if (*s == ' ') |
| 1467 | s++; |
| 1468 | if (*s++ == '\'') { |
| 1469 | char * const ncmd = s; |
| 1470 | |
| 1471 | while (*s) |
| 1472 | s++; |
| 1473 | if (s[-1] == '\n') |
| 1474 | *--s = '\0'; |
| 1475 | if (s[-1] == '\'') { |
| 1476 | *--s = '\0'; |
| 1477 | PERL_FPU_PRE_EXEC |
| 1478 | PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL); |
| 1479 | PERL_FPU_POST_EXEC |
| 1480 | *s = '\''; |
| 1481 | S_exec_failed(aTHX_ PL_cshname, fd, do_report); |
| 1482 | Safefree(buf); |
| 1483 | return FALSE; |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | } |
| 1488 | #endif /* CSH */ |
| 1489 | |
| 1490 | /* see if there are shell metacharacters in it */ |
| 1491 | |
| 1492 | if (*cmd == '.' && isSPACE(cmd[1])) |
| 1493 | goto doshell; |
| 1494 | |
| 1495 | if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4])) |
| 1496 | goto doshell; |
| 1497 | |
| 1498 | s = cmd; |
| 1499 | while (isALNUM(*s)) |
| 1500 | s++; /* catch VAR=val gizmo */ |
| 1501 | if (*s == '=') |
| 1502 | goto doshell; |
| 1503 | |
| 1504 | for (s = cmd; *s; s++) { |
| 1505 | if (*s != ' ' && !isALPHA(*s) && |
| 1506 | strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) { |
| 1507 | if (*s == '\n' && !s[1]) { |
| 1508 | *s = '\0'; |
| 1509 | break; |
| 1510 | } |
| 1511 | /* handle the 2>&1 construct at the end */ |
| 1512 | if (*s == '>' && s[1] == '&' && s[2] == '1' |
| 1513 | && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2]) |
| 1514 | && (!s[3] || isSPACE(s[3]))) |
| 1515 | { |
| 1516 | const char *t = s + 3; |
| 1517 | |
| 1518 | while (*t && isSPACE(*t)) |
| 1519 | ++t; |
| 1520 | if (!*t && (PerlLIO_dup2(1,2) != -1)) { |
| 1521 | s[-2] = '\0'; |
| 1522 | break; |
| 1523 | } |
| 1524 | } |
| 1525 | doshell: |
| 1526 | PERL_FPU_PRE_EXEC |
| 1527 | PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL); |
| 1528 | PERL_FPU_POST_EXEC |
| 1529 | S_exec_failed(aTHX_ PL_sh_path, fd, do_report); |
| 1530 | Safefree(buf); |
| 1531 | return FALSE; |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | Newx(PL_Argv, (s - cmd) / 2 + 2, const char*); |
| 1536 | PL_Cmd = savepvn(cmd, s-cmd); |
| 1537 | a = PL_Argv; |
| 1538 | for (s = PL_Cmd; *s;) { |
| 1539 | while (isSPACE(*s)) |
| 1540 | s++; |
| 1541 | if (*s) |
| 1542 | *(a++) = s; |
| 1543 | while (*s && !isSPACE(*s)) |
| 1544 | s++; |
| 1545 | if (*s) |
| 1546 | *s++ = '\0'; |
| 1547 | } |
| 1548 | *a = NULL; |
| 1549 | if (PL_Argv[0]) { |
| 1550 | PERL_FPU_PRE_EXEC |
| 1551 | PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv)); |
| 1552 | PERL_FPU_POST_EXEC |
| 1553 | if (errno == ENOEXEC) { /* for system V NIH syndrome */ |
| 1554 | do_execfree(); |
| 1555 | goto doshell; |
| 1556 | } |
| 1557 | S_exec_failed(aTHX_ PL_Argv[0], fd, do_report); |
| 1558 | } |
| 1559 | do_execfree(); |
| 1560 | Safefree(buf); |
| 1561 | return FALSE; |
| 1562 | } |
| 1563 | |
| 1564 | #endif /* OS2 || WIN32 */ |
| 1565 | |
| 1566 | I32 |
| 1567 | Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp) |
| 1568 | { |
| 1569 | dVAR; |
| 1570 | register I32 val; |
| 1571 | register I32 tot = 0; |
| 1572 | const char *const what = PL_op_name[type]; |
| 1573 | const char *s; |
| 1574 | STRLEN len; |
| 1575 | SV ** const oldmark = mark; |
| 1576 | |
| 1577 | PERL_ARGS_ASSERT_APPLY; |
| 1578 | |
| 1579 | /* Doing this ahead of the switch statement preserves the old behaviour, |
| 1580 | where attempting to use kill as a taint test test would fail on |
| 1581 | platforms where kill was not defined. */ |
| 1582 | #ifndef HAS_KILL |
| 1583 | if (type == OP_KILL) |
| 1584 | Perl_die(aTHX_ PL_no_func, what); |
| 1585 | #endif |
| 1586 | #ifndef HAS_CHOWN |
| 1587 | if (type == OP_CHOWN) |
| 1588 | Perl_die(aTHX_ PL_no_func, what); |
| 1589 | #endif |
| 1590 | |
| 1591 | |
| 1592 | #define APPLY_TAINT_PROPER() \ |
| 1593 | STMT_START { \ |
| 1594 | if (PL_tainted) { TAINT_PROPER(what); } \ |
| 1595 | } STMT_END |
| 1596 | |
| 1597 | /* This is a first heuristic; it doesn't catch tainting magic. */ |
| 1598 | if (PL_tainting) { |
| 1599 | while (++mark <= sp) { |
| 1600 | if (SvTAINTED(*mark)) { |
| 1601 | TAINT; |
| 1602 | break; |
| 1603 | } |
| 1604 | } |
| 1605 | mark = oldmark; |
| 1606 | } |
| 1607 | switch (type) { |
| 1608 | case OP_CHMOD: |
| 1609 | APPLY_TAINT_PROPER(); |
| 1610 | if (++mark <= sp) { |
| 1611 | val = SvIV(*mark); |
| 1612 | APPLY_TAINT_PROPER(); |
| 1613 | tot = sp - mark; |
| 1614 | while (++mark <= sp) { |
| 1615 | GV* gv; |
| 1616 | if ((gv = MAYBE_DEREF_GV(*mark))) { |
| 1617 | if (GvIO(gv) && IoIFP(GvIOp(gv))) { |
| 1618 | #ifdef HAS_FCHMOD |
| 1619 | APPLY_TAINT_PROPER(); |
| 1620 | if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val)) |
| 1621 | tot--; |
| 1622 | #else |
| 1623 | Perl_die(aTHX_ PL_no_func, "fchmod"); |
| 1624 | #endif |
| 1625 | } |
| 1626 | else { |
| 1627 | tot--; |
| 1628 | } |
| 1629 | } |
| 1630 | else { |
| 1631 | const char *name = SvPV_nomg_const_nolen(*mark); |
| 1632 | APPLY_TAINT_PROPER(); |
| 1633 | if (PerlLIO_chmod(name, val)) |
| 1634 | tot--; |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | break; |
| 1639 | #ifdef HAS_CHOWN |
| 1640 | case OP_CHOWN: |
| 1641 | APPLY_TAINT_PROPER(); |
| 1642 | if (sp - mark > 2) { |
| 1643 | register I32 val2; |
| 1644 | val = SvIVx(*++mark); |
| 1645 | val2 = SvIVx(*++mark); |
| 1646 | APPLY_TAINT_PROPER(); |
| 1647 | tot = sp - mark; |
| 1648 | while (++mark <= sp) { |
| 1649 | GV* gv; |
| 1650 | if ((gv = MAYBE_DEREF_GV(*mark))) { |
| 1651 | if (GvIO(gv) && IoIFP(GvIOp(gv))) { |
| 1652 | #ifdef HAS_FCHOWN |
| 1653 | APPLY_TAINT_PROPER(); |
| 1654 | if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2)) |
| 1655 | tot--; |
| 1656 | #else |
| 1657 | Perl_die(aTHX_ PL_no_func, "fchown"); |
| 1658 | #endif |
| 1659 | } |
| 1660 | else { |
| 1661 | tot--; |
| 1662 | } |
| 1663 | } |
| 1664 | else { |
| 1665 | const char *name = SvPV_nomg_const_nolen(*mark); |
| 1666 | APPLY_TAINT_PROPER(); |
| 1667 | if (PerlLIO_chown(name, val, val2)) |
| 1668 | tot--; |
| 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | break; |
| 1673 | #endif |
| 1674 | /* |
| 1675 | XXX Should we make lchown() directly available from perl? |
| 1676 | For now, we'll let Configure test for HAS_LCHOWN, but do |
| 1677 | nothing in the core. |
| 1678 | --AD 5/1998 |
| 1679 | */ |
| 1680 | #ifdef HAS_KILL |
| 1681 | case OP_KILL: |
| 1682 | APPLY_TAINT_PROPER(); |
| 1683 | if (mark == sp) |
| 1684 | break; |
| 1685 | s = SvPVx_const(*++mark, len); |
| 1686 | if (isALPHA(*s)) { |
| 1687 | if (*s == 'S' && s[1] == 'I' && s[2] == 'G') { |
| 1688 | s += 3; |
| 1689 | len -= 3; |
| 1690 | } |
| 1691 | if ((val = whichsig_pvn(s, len)) < 0) |
| 1692 | Perl_croak(aTHX_ "Unrecognized signal name \"%"SVf"\"", SVfARG(*mark)); |
| 1693 | } |
| 1694 | else |
| 1695 | val = SvIV(*mark); |
| 1696 | APPLY_TAINT_PROPER(); |
| 1697 | tot = sp - mark; |
| 1698 | #ifdef VMS |
| 1699 | /* kill() doesn't do process groups (job trees?) under VMS */ |
| 1700 | if (val < 0) val = -val; |
| 1701 | if (val == SIGKILL) { |
| 1702 | # include <starlet.h> |
| 1703 | /* Use native sys$delprc() to insure that target process is |
| 1704 | * deleted; supervisor-mode images don't pay attention to |
| 1705 | * CRTL's emulation of Unix-style signals and kill() |
| 1706 | */ |
| 1707 | while (++mark <= sp) { |
| 1708 | I32 proc; |
| 1709 | register unsigned long int __vmssts; |
| 1710 | SvGETMAGIC(*mark); |
| 1711 | if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark))) |
| 1712 | Perl_croak(aTHX_ "Can't kill a non-numeric process ID"); |
| 1713 | proc = SvIV_nomg(*mark); |
| 1714 | APPLY_TAINT_PROPER(); |
| 1715 | if (!((__vmssts = sys$delprc(&proc,0)) & 1)) { |
| 1716 | tot--; |
| 1717 | switch (__vmssts) { |
| 1718 | case SS$_NONEXPR: |
| 1719 | case SS$_NOSUCHNODE: |
| 1720 | SETERRNO(ESRCH,__vmssts); |
| 1721 | break; |
| 1722 | case SS$_NOPRIV: |
| 1723 | SETERRNO(EPERM,__vmssts); |
| 1724 | break; |
| 1725 | default: |
| 1726 | SETERRNO(EVMSERR,__vmssts); |
| 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | PERL_ASYNC_CHECK(); |
| 1731 | break; |
| 1732 | } |
| 1733 | #endif |
| 1734 | if (val < 0) { |
| 1735 | val = -val; |
| 1736 | while (++mark <= sp) { |
| 1737 | I32 proc; |
| 1738 | SvGETMAGIC(*mark); |
| 1739 | if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark))) |
| 1740 | Perl_croak(aTHX_ "Can't kill a non-numeric process ID"); |
| 1741 | proc = SvIV_nomg(*mark); |
| 1742 | APPLY_TAINT_PROPER(); |
| 1743 | #ifdef HAS_KILLPG |
| 1744 | if (PerlProc_killpg(proc,val)) /* BSD */ |
| 1745 | #else |
| 1746 | if (PerlProc_kill(-proc,val)) /* SYSV */ |
| 1747 | #endif |
| 1748 | tot--; |
| 1749 | } |
| 1750 | } |
| 1751 | else { |
| 1752 | while (++mark <= sp) { |
| 1753 | I32 proc; |
| 1754 | SvGETMAGIC(*mark); |
| 1755 | if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark))) |
| 1756 | Perl_croak(aTHX_ "Can't kill a non-numeric process ID"); |
| 1757 | proc = SvIV_nomg(*mark); |
| 1758 | APPLY_TAINT_PROPER(); |
| 1759 | if (PerlProc_kill(proc, val)) |
| 1760 | tot--; |
| 1761 | } |
| 1762 | } |
| 1763 | PERL_ASYNC_CHECK(); |
| 1764 | break; |
| 1765 | #endif |
| 1766 | case OP_UNLINK: |
| 1767 | APPLY_TAINT_PROPER(); |
| 1768 | tot = sp - mark; |
| 1769 | while (++mark <= sp) { |
| 1770 | s = SvPV_nolen_const(*mark); |
| 1771 | APPLY_TAINT_PROPER(); |
| 1772 | if (PL_euid || PL_unsafe) { |
| 1773 | if (UNLINK(s)) |
| 1774 | tot--; |
| 1775 | } |
| 1776 | else { /* don't let root wipe out directories without -U */ |
| 1777 | if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode)) |
| 1778 | tot--; |
| 1779 | else { |
| 1780 | if (UNLINK(s)) |
| 1781 | tot--; |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | break; |
| 1786 | #if defined(HAS_UTIME) || defined(HAS_FUTIMES) |
| 1787 | case OP_UTIME: |
| 1788 | APPLY_TAINT_PROPER(); |
| 1789 | if (sp - mark > 2) { |
| 1790 | #if defined(HAS_FUTIMES) |
| 1791 | struct timeval utbuf[2]; |
| 1792 | void *utbufp = utbuf; |
| 1793 | #elif defined(I_UTIME) || defined(VMS) |
| 1794 | struct utimbuf utbuf; |
| 1795 | struct utimbuf *utbufp = &utbuf; |
| 1796 | #else |
| 1797 | struct { |
| 1798 | Time_t actime; |
| 1799 | Time_t modtime; |
| 1800 | } utbuf; |
| 1801 | void *utbufp = &utbuf; |
| 1802 | #endif |
| 1803 | |
| 1804 | SV* const accessed = *++mark; |
| 1805 | SV* const modified = *++mark; |
| 1806 | |
| 1807 | /* Be like C, and if both times are undefined, let the C |
| 1808 | * library figure out what to do. This usually means |
| 1809 | * "current time". */ |
| 1810 | |
| 1811 | if ( accessed == &PL_sv_undef && modified == &PL_sv_undef ) |
| 1812 | utbufp = NULL; |
| 1813 | else { |
| 1814 | Zero(&utbuf, sizeof utbuf, char); |
| 1815 | #ifdef HAS_FUTIMES |
| 1816 | utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */ |
| 1817 | utbuf[0].tv_usec = 0; |
| 1818 | utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */ |
| 1819 | utbuf[1].tv_usec = 0; |
| 1820 | #elif defined(BIG_TIME) |
| 1821 | utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */ |
| 1822 | utbuf.modtime = (Time_t)SvNV(modified); /* time modified */ |
| 1823 | #else |
| 1824 | utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */ |
| 1825 | utbuf.modtime = (Time_t)SvIV(modified); /* time modified */ |
| 1826 | #endif |
| 1827 | } |
| 1828 | APPLY_TAINT_PROPER(); |
| 1829 | tot = sp - mark; |
| 1830 | while (++mark <= sp) { |
| 1831 | GV* gv; |
| 1832 | if ((gv = MAYBE_DEREF_GV(*mark))) { |
| 1833 | if (GvIO(gv) && IoIFP(GvIOp(gv))) { |
| 1834 | #ifdef HAS_FUTIMES |
| 1835 | APPLY_TAINT_PROPER(); |
| 1836 | if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))), |
| 1837 | (struct timeval *) utbufp)) |
| 1838 | tot--; |
| 1839 | #else |
| 1840 | Perl_die(aTHX_ PL_no_func, "futimes"); |
| 1841 | #endif |
| 1842 | } |
| 1843 | else { |
| 1844 | tot--; |
| 1845 | } |
| 1846 | } |
| 1847 | else { |
| 1848 | const char * const name = SvPV_nomg_const_nolen(*mark); |
| 1849 | APPLY_TAINT_PROPER(); |
| 1850 | #ifdef HAS_FUTIMES |
| 1851 | if (utimes(name, (struct timeval *)utbufp)) |
| 1852 | #else |
| 1853 | if (PerlLIO_utime(name, utbufp)) |
| 1854 | #endif |
| 1855 | tot--; |
| 1856 | } |
| 1857 | |
| 1858 | } |
| 1859 | } |
| 1860 | else |
| 1861 | tot = 0; |
| 1862 | break; |
| 1863 | #endif |
| 1864 | } |
| 1865 | return tot; |
| 1866 | |
| 1867 | #undef APPLY_TAINT_PROPER |
| 1868 | } |
| 1869 | |
| 1870 | /* Do the permissions allow some operation? Assumes statcache already set. */ |
| 1871 | #ifndef VMS /* VMS' cando is in vms.c */ |
| 1872 | bool |
| 1873 | Perl_cando(pTHX_ Mode_t mode, bool effective, register const Stat_t *statbufp) |
| 1874 | /* effective is a flag, true for EUID, or for checking if the effective gid |
| 1875 | * is in the list of groups returned from getgroups(). |
| 1876 | */ |
| 1877 | { |
| 1878 | dVAR; |
| 1879 | |
| 1880 | PERL_ARGS_ASSERT_CANDO; |
| 1881 | |
| 1882 | #ifdef DOSISH |
| 1883 | /* [Comments and code from Len Reed] |
| 1884 | * MS-DOS "user" is similar to UNIX's "superuser," but can't write |
| 1885 | * to write-protected files. The execute permission bit is set |
| 1886 | * by the Microsoft C library stat() function for the following: |
| 1887 | * .exe files |
| 1888 | * .com files |
| 1889 | * .bat files |
| 1890 | * directories |
| 1891 | * All files and directories are readable. |
| 1892 | * Directories and special files, e.g. "CON", cannot be |
| 1893 | * write-protected. |
| 1894 | * [Comment by Tom Dinger -- a directory can have the write-protect |
| 1895 | * bit set in the file system, but DOS permits changes to |
| 1896 | * the directory anyway. In addition, all bets are off |
| 1897 | * here for networked software, such as Novell and |
| 1898 | * Sun's PC-NFS.] |
| 1899 | */ |
| 1900 | |
| 1901 | /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat |
| 1902 | * too so it will actually look into the files for magic numbers |
| 1903 | */ |
| 1904 | return (mode & statbufp->st_mode) ? TRUE : FALSE; |
| 1905 | |
| 1906 | #else /* ! DOSISH */ |
| 1907 | # ifdef __CYGWIN__ |
| 1908 | if (ingroup(544,effective)) { /* member of Administrators */ |
| 1909 | # else |
| 1910 | if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */ |
| 1911 | # endif |
| 1912 | if (mode == S_IXUSR) { |
| 1913 | if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode)) |
| 1914 | return TRUE; |
| 1915 | } |
| 1916 | else |
| 1917 | return TRUE; /* root reads and writes anything */ |
| 1918 | return FALSE; |
| 1919 | } |
| 1920 | if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) { |
| 1921 | if (statbufp->st_mode & mode) |
| 1922 | return TRUE; /* ok as "user" */ |
| 1923 | } |
| 1924 | else if (ingroup(statbufp->st_gid,effective)) { |
| 1925 | if (statbufp->st_mode & mode >> 3) |
| 1926 | return TRUE; /* ok as "group" */ |
| 1927 | } |
| 1928 | else if (statbufp->st_mode & mode >> 6) |
| 1929 | return TRUE; /* ok as "other" */ |
| 1930 | return FALSE; |
| 1931 | #endif /* ! DOSISH */ |
| 1932 | } |
| 1933 | #endif /* ! VMS */ |
| 1934 | |
| 1935 | static bool |
| 1936 | S_ingroup(pTHX_ Gid_t testgid, bool effective) |
| 1937 | { |
| 1938 | dVAR; |
| 1939 | if (testgid == (effective ? PL_egid : PL_gid)) |
| 1940 | return TRUE; |
| 1941 | #ifdef HAS_GETGROUPS |
| 1942 | { |
| 1943 | Groups_t *gary = NULL; |
| 1944 | I32 anum; |
| 1945 | bool rc = FALSE; |
| 1946 | |
| 1947 | anum = getgroups(0, gary); |
| 1948 | Newx(gary, anum, Groups_t); |
| 1949 | anum = getgroups(anum, gary); |
| 1950 | while (--anum >= 0) |
| 1951 | if (gary[anum] == testgid) { |
| 1952 | rc = TRUE; |
| 1953 | break; |
| 1954 | } |
| 1955 | |
| 1956 | Safefree(gary); |
| 1957 | return rc; |
| 1958 | } |
| 1959 | #else |
| 1960 | return FALSE; |
| 1961 | #endif |
| 1962 | } |
| 1963 | |
| 1964 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
| 1965 | |
| 1966 | I32 |
| 1967 | Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp) |
| 1968 | { |
| 1969 | dVAR; |
| 1970 | const key_t key = (key_t)SvNVx(*++mark); |
| 1971 | SV *nsv = optype == OP_MSGGET ? NULL : *++mark; |
| 1972 | const I32 flags = SvIVx(*++mark); |
| 1973 | |
| 1974 | PERL_ARGS_ASSERT_DO_IPCGET; |
| 1975 | PERL_UNUSED_ARG(sp); |
| 1976 | |
| 1977 | SETERRNO(0,0); |
| 1978 | switch (optype) |
| 1979 | { |
| 1980 | #ifdef HAS_MSG |
| 1981 | case OP_MSGGET: |
| 1982 | return msgget(key, flags); |
| 1983 | #endif |
| 1984 | #ifdef HAS_SEM |
| 1985 | case OP_SEMGET: |
| 1986 | return semget(key, (int) SvIV(nsv), flags); |
| 1987 | #endif |
| 1988 | #ifdef HAS_SHM |
| 1989 | case OP_SHMGET: |
| 1990 | return shmget(key, (size_t) SvUV(nsv), flags); |
| 1991 | #endif |
| 1992 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
| 1993 | default: |
| 1994 | /* diag_listed_as: msg%s not implemented */ |
| 1995 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
| 1996 | #endif |
| 1997 | } |
| 1998 | return -1; /* should never happen */ |
| 1999 | } |
| 2000 | |
| 2001 | I32 |
| 2002 | Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp) |
| 2003 | { |
| 2004 | dVAR; |
| 2005 | char *a; |
| 2006 | I32 ret = -1; |
| 2007 | const I32 id = SvIVx(*++mark); |
| 2008 | #ifdef Semctl |
| 2009 | const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0; |
| 2010 | #endif |
| 2011 | const I32 cmd = SvIVx(*++mark); |
| 2012 | SV * const astr = *++mark; |
| 2013 | STRLEN infosize = 0; |
| 2014 | I32 getinfo = (cmd == IPC_STAT); |
| 2015 | |
| 2016 | PERL_ARGS_ASSERT_DO_IPCCTL; |
| 2017 | PERL_UNUSED_ARG(sp); |
| 2018 | |
| 2019 | switch (optype) |
| 2020 | { |
| 2021 | #ifdef HAS_MSG |
| 2022 | case OP_MSGCTL: |
| 2023 | if (cmd == IPC_STAT || cmd == IPC_SET) |
| 2024 | infosize = sizeof(struct msqid_ds); |
| 2025 | break; |
| 2026 | #endif |
| 2027 | #ifdef HAS_SHM |
| 2028 | case OP_SHMCTL: |
| 2029 | if (cmd == IPC_STAT || cmd == IPC_SET) |
| 2030 | infosize = sizeof(struct shmid_ds); |
| 2031 | break; |
| 2032 | #endif |
| 2033 | #ifdef HAS_SEM |
| 2034 | case OP_SEMCTL: |
| 2035 | #ifdef Semctl |
| 2036 | if (cmd == IPC_STAT || cmd == IPC_SET) |
| 2037 | infosize = sizeof(struct semid_ds); |
| 2038 | else if (cmd == GETALL || cmd == SETALL) |
| 2039 | { |
| 2040 | struct semid_ds semds; |
| 2041 | union semun semun; |
| 2042 | #ifdef EXTRA_F_IN_SEMUN_BUF |
| 2043 | semun.buff = &semds; |
| 2044 | #else |
| 2045 | semun.buf = &semds; |
| 2046 | #endif |
| 2047 | getinfo = (cmd == GETALL); |
| 2048 | if (Semctl(id, 0, IPC_STAT, semun) == -1) |
| 2049 | return -1; |
| 2050 | infosize = semds.sem_nsems * sizeof(short); |
| 2051 | /* "short" is technically wrong but much more portable |
| 2052 | than guessing about u_?short(_t)? */ |
| 2053 | } |
| 2054 | #else |
| 2055 | /* diag_listed_as: sem%s not implemented */ |
| 2056 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
| 2057 | #endif |
| 2058 | break; |
| 2059 | #endif |
| 2060 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
| 2061 | default: |
| 2062 | /* diag_listed_as: shm%s not implemented */ |
| 2063 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
| 2064 | #endif |
| 2065 | } |
| 2066 | |
| 2067 | if (infosize) |
| 2068 | { |
| 2069 | if (getinfo) |
| 2070 | { |
| 2071 | SvPV_force_nolen(astr); |
| 2072 | a = SvGROW(astr, infosize+1); |
| 2073 | } |
| 2074 | else |
| 2075 | { |
| 2076 | STRLEN len; |
| 2077 | a = SvPV(astr, len); |
| 2078 | if (len != infosize) |
| 2079 | Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld", |
| 2080 | PL_op_desc[optype], |
| 2081 | (unsigned long)len, |
| 2082 | (long)infosize); |
| 2083 | } |
| 2084 | } |
| 2085 | else |
| 2086 | { |
| 2087 | const IV i = SvIV(astr); |
| 2088 | a = INT2PTR(char *,i); /* ouch */ |
| 2089 | } |
| 2090 | SETERRNO(0,0); |
| 2091 | switch (optype) |
| 2092 | { |
| 2093 | #ifdef HAS_MSG |
| 2094 | case OP_MSGCTL: |
| 2095 | ret = msgctl(id, cmd, (struct msqid_ds *)a); |
| 2096 | break; |
| 2097 | #endif |
| 2098 | #ifdef HAS_SEM |
| 2099 | case OP_SEMCTL: { |
| 2100 | #ifdef Semctl |
| 2101 | union semun unsemds; |
| 2102 | |
| 2103 | #ifdef EXTRA_F_IN_SEMUN_BUF |
| 2104 | unsemds.buff = (struct semid_ds *)a; |
| 2105 | #else |
| 2106 | unsemds.buf = (struct semid_ds *)a; |
| 2107 | #endif |
| 2108 | ret = Semctl(id, n, cmd, unsemds); |
| 2109 | #else |
| 2110 | /* diag_listed_as: sem%s not implemented */ |
| 2111 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
| 2112 | #endif |
| 2113 | } |
| 2114 | break; |
| 2115 | #endif |
| 2116 | #ifdef HAS_SHM |
| 2117 | case OP_SHMCTL: |
| 2118 | ret = shmctl(id, cmd, (struct shmid_ds *)a); |
| 2119 | break; |
| 2120 | #endif |
| 2121 | } |
| 2122 | if (getinfo && ret >= 0) { |
| 2123 | SvCUR_set(astr, infosize); |
| 2124 | *SvEND(astr) = '\0'; |
| 2125 | SvSETMAGIC(astr); |
| 2126 | } |
| 2127 | return ret; |
| 2128 | } |
| 2129 | |
| 2130 | I32 |
| 2131 | Perl_do_msgsnd(pTHX_ SV **mark, SV **sp) |
| 2132 | { |
| 2133 | dVAR; |
| 2134 | #ifdef HAS_MSG |
| 2135 | STRLEN len; |
| 2136 | const I32 id = SvIVx(*++mark); |
| 2137 | SV * const mstr = *++mark; |
| 2138 | const I32 flags = SvIVx(*++mark); |
| 2139 | const char * const mbuf = SvPV_const(mstr, len); |
| 2140 | const I32 msize = len - sizeof(long); |
| 2141 | |
| 2142 | PERL_ARGS_ASSERT_DO_MSGSND; |
| 2143 | PERL_UNUSED_ARG(sp); |
| 2144 | |
| 2145 | if (msize < 0) |
| 2146 | Perl_croak(aTHX_ "Arg too short for msgsnd"); |
| 2147 | SETERRNO(0,0); |
| 2148 | return msgsnd(id, (struct msgbuf *)mbuf, msize, flags); |
| 2149 | #else |
| 2150 | PERL_UNUSED_ARG(sp); |
| 2151 | PERL_UNUSED_ARG(mark); |
| 2152 | /* diag_listed_as: msg%s not implemented */ |
| 2153 | Perl_croak(aTHX_ "msgsnd not implemented"); |
| 2154 | #endif |
| 2155 | } |
| 2156 | |
| 2157 | I32 |
| 2158 | Perl_do_msgrcv(pTHX_ SV **mark, SV **sp) |
| 2159 | { |
| 2160 | #ifdef HAS_MSG |
| 2161 | dVAR; |
| 2162 | char *mbuf; |
| 2163 | long mtype; |
| 2164 | I32 msize, flags, ret; |
| 2165 | const I32 id = SvIVx(*++mark); |
| 2166 | SV * const mstr = *++mark; |
| 2167 | |
| 2168 | PERL_ARGS_ASSERT_DO_MSGRCV; |
| 2169 | PERL_UNUSED_ARG(sp); |
| 2170 | |
| 2171 | /* suppress warning when reading into undef var --jhi */ |
| 2172 | if (! SvOK(mstr)) |
| 2173 | sv_setpvs(mstr, ""); |
| 2174 | msize = SvIVx(*++mark); |
| 2175 | mtype = (long)SvIVx(*++mark); |
| 2176 | flags = SvIVx(*++mark); |
| 2177 | SvPV_force_nolen(mstr); |
| 2178 | mbuf = SvGROW(mstr, sizeof(long)+msize+1); |
| 2179 | |
| 2180 | SETERRNO(0,0); |
| 2181 | ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags); |
| 2182 | if (ret >= 0) { |
| 2183 | SvCUR_set(mstr, sizeof(long)+ret); |
| 2184 | *SvEND(mstr) = '\0'; |
| 2185 | #ifndef INCOMPLETE_TAINTS |
| 2186 | /* who knows who has been playing with this message? */ |
| 2187 | SvTAINTED_on(mstr); |
| 2188 | #endif |
| 2189 | } |
| 2190 | return ret; |
| 2191 | #else |
| 2192 | PERL_UNUSED_ARG(sp); |
| 2193 | PERL_UNUSED_ARG(mark); |
| 2194 | /* diag_listed_as: msg%s not implemented */ |
| 2195 | Perl_croak(aTHX_ "msgrcv not implemented"); |
| 2196 | #endif |
| 2197 | } |
| 2198 | |
| 2199 | I32 |
| 2200 | Perl_do_semop(pTHX_ SV **mark, SV **sp) |
| 2201 | { |
| 2202 | #ifdef HAS_SEM |
| 2203 | dVAR; |
| 2204 | STRLEN opsize; |
| 2205 | const I32 id = SvIVx(*++mark); |
| 2206 | SV * const opstr = *++mark; |
| 2207 | const char * const opbuf = SvPV_const(opstr, opsize); |
| 2208 | |
| 2209 | PERL_ARGS_ASSERT_DO_SEMOP; |
| 2210 | PERL_UNUSED_ARG(sp); |
| 2211 | |
| 2212 | if (opsize < 3 * SHORTSIZE |
| 2213 | || (opsize % (3 * SHORTSIZE))) { |
| 2214 | SETERRNO(EINVAL,LIB_INVARG); |
| 2215 | return -1; |
| 2216 | } |
| 2217 | SETERRNO(0,0); |
| 2218 | /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */ |
| 2219 | { |
| 2220 | const int nsops = opsize / (3 * sizeof (short)); |
| 2221 | int i = nsops; |
| 2222 | short * const ops = (short *) opbuf; |
| 2223 | short *o = ops; |
| 2224 | struct sembuf *temps, *t; |
| 2225 | I32 result; |
| 2226 | |
| 2227 | Newx (temps, nsops, struct sembuf); |
| 2228 | t = temps; |
| 2229 | while (i--) { |
| 2230 | t->sem_num = *o++; |
| 2231 | t->sem_op = *o++; |
| 2232 | t->sem_flg = *o++; |
| 2233 | t++; |
| 2234 | } |
| 2235 | result = semop(id, temps, nsops); |
| 2236 | t = temps; |
| 2237 | o = ops; |
| 2238 | i = nsops; |
| 2239 | while (i--) { |
| 2240 | *o++ = t->sem_num; |
| 2241 | *o++ = t->sem_op; |
| 2242 | *o++ = t->sem_flg; |
| 2243 | t++; |
| 2244 | } |
| 2245 | Safefree(temps); |
| 2246 | return result; |
| 2247 | } |
| 2248 | #else |
| 2249 | /* diag_listed_as: sem%s not implemented */ |
| 2250 | Perl_croak(aTHX_ "semop not implemented"); |
| 2251 | #endif |
| 2252 | } |
| 2253 | |
| 2254 | I32 |
| 2255 | Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp) |
| 2256 | { |
| 2257 | #ifdef HAS_SHM |
| 2258 | dVAR; |
| 2259 | char *shm; |
| 2260 | struct shmid_ds shmds; |
| 2261 | const I32 id = SvIVx(*++mark); |
| 2262 | SV * const mstr = *++mark; |
| 2263 | const I32 mpos = SvIVx(*++mark); |
| 2264 | const I32 msize = SvIVx(*++mark); |
| 2265 | |
| 2266 | PERL_ARGS_ASSERT_DO_SHMIO; |
| 2267 | PERL_UNUSED_ARG(sp); |
| 2268 | |
| 2269 | SETERRNO(0,0); |
| 2270 | if (shmctl(id, IPC_STAT, &shmds) == -1) |
| 2271 | return -1; |
| 2272 | if (mpos < 0 || msize < 0 |
| 2273 | || (size_t)mpos + msize > (size_t)shmds.shm_segsz) { |
| 2274 | SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */ |
| 2275 | return -1; |
| 2276 | } |
| 2277 | shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0); |
| 2278 | if (shm == (char *)-1) /* I hate System V IPC, I really do */ |
| 2279 | return -1; |
| 2280 | if (optype == OP_SHMREAD) { |
| 2281 | char *mbuf; |
| 2282 | /* suppress warning when reading into undef var (tchrist 3/Mar/00) */ |
| 2283 | if (! SvOK(mstr)) |
| 2284 | sv_setpvs(mstr, ""); |
| 2285 | SvUPGRADE(mstr, SVt_PV); |
| 2286 | SvPOK_only(mstr); |
| 2287 | mbuf = SvGROW(mstr, (STRLEN)msize+1); |
| 2288 | |
| 2289 | Copy(shm + mpos, mbuf, msize, char); |
| 2290 | SvCUR_set(mstr, msize); |
| 2291 | *SvEND(mstr) = '\0'; |
| 2292 | SvSETMAGIC(mstr); |
| 2293 | #ifndef INCOMPLETE_TAINTS |
| 2294 | /* who knows who has been playing with this shared memory? */ |
| 2295 | SvTAINTED_on(mstr); |
| 2296 | #endif |
| 2297 | } |
| 2298 | else { |
| 2299 | STRLEN len; |
| 2300 | |
| 2301 | const char *mbuf = SvPV_const(mstr, len); |
| 2302 | const I32 n = ((I32)len > msize) ? msize : (I32)len; |
| 2303 | Copy(mbuf, shm + mpos, n, char); |
| 2304 | if (n < msize) |
| 2305 | memzero(shm + mpos + n, msize - n); |
| 2306 | } |
| 2307 | return shmdt(shm); |
| 2308 | #else |
| 2309 | /* diag_listed_as: shm%s not implemented */ |
| 2310 | Perl_croak(aTHX_ "shm I/O not implemented"); |
| 2311 | #endif |
| 2312 | } |
| 2313 | |
| 2314 | #endif /* SYSV IPC */ |
| 2315 | |
| 2316 | /* |
| 2317 | =head1 IO Functions |
| 2318 | |
| 2319 | =for apidoc start_glob |
| 2320 | |
| 2321 | Function called by C<do_readline> to spawn a glob (or do the glob inside |
| 2322 | perl on VMS). This code used to be inline, but now perl uses C<File::Glob> |
| 2323 | this glob starter is only used by miniperl during the build process. |
| 2324 | Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up. |
| 2325 | |
| 2326 | =cut |
| 2327 | */ |
| 2328 | |
| 2329 | PerlIO * |
| 2330 | Perl_start_glob (pTHX_ SV *tmpglob, IO *io) |
| 2331 | { |
| 2332 | dVAR; |
| 2333 | SV * const tmpcmd = newSV(0); |
| 2334 | PerlIO *fp; |
| 2335 | |
| 2336 | PERL_ARGS_ASSERT_START_GLOB; |
| 2337 | |
| 2338 | ENTER; |
| 2339 | SAVEFREESV(tmpcmd); |
| 2340 | #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */ |
| 2341 | /* since spawning off a process is a real performance hit */ |
| 2342 | |
| 2343 | PerlIO * |
| 2344 | Perl_vms_start_glob |
| 2345 | (pTHX_ SV *tmpglob, |
| 2346 | IO *io); |
| 2347 | |
| 2348 | fp = Perl_vms_start_glob(aTHX_ tmpglob, io); |
| 2349 | |
| 2350 | #else /* !VMS */ |
| 2351 | #ifdef DOSISH |
| 2352 | #ifdef OS2 |
| 2353 | sv_setpv(tmpcmd, "for a in "); |
| 2354 | sv_catsv(tmpcmd, tmpglob); |
| 2355 | sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |"); |
| 2356 | #else |
| 2357 | #ifdef DJGPP |
| 2358 | sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */ |
| 2359 | sv_catsv(tmpcmd, tmpglob); |
| 2360 | #else |
| 2361 | sv_setpv(tmpcmd, "perlglob "); |
| 2362 | sv_catsv(tmpcmd, tmpglob); |
| 2363 | sv_catpv(tmpcmd, " |"); |
| 2364 | #endif /* !DJGPP */ |
| 2365 | #endif /* !OS2 */ |
| 2366 | #else /* !DOSISH */ |
| 2367 | #if defined(CSH) |
| 2368 | sv_setpvn(tmpcmd, PL_cshname, PL_cshlen); |
| 2369 | sv_catpv(tmpcmd, " -cf 'set nonomatch; glob "); |
| 2370 | sv_catsv(tmpcmd, tmpglob); |
| 2371 | sv_catpv(tmpcmd, "' 2>/dev/null |"); |
| 2372 | #else |
| 2373 | sv_setpv(tmpcmd, "echo "); |
| 2374 | sv_catsv(tmpcmd, tmpglob); |
| 2375 | #if 'z' - 'a' == 25 |
| 2376 | sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|"); |
| 2377 | #else |
| 2378 | sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|"); |
| 2379 | #endif |
| 2380 | #endif /* !CSH */ |
| 2381 | #endif /* !DOSISH */ |
| 2382 | save_hash(gv_fetchpvs("ENV", 0, SVt_PVHV)); |
| 2383 | (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd), |
| 2384 | FALSE, O_RDONLY, 0, NULL); |
| 2385 | fp = IoIFP(io); |
| 2386 | #endif /* !VMS */ |
| 2387 | LEAVE; |
| 2388 | return fp; |
| 2389 | } |
| 2390 | |
| 2391 | /* |
| 2392 | * Local variables: |
| 2393 | * c-indentation-style: bsd |
| 2394 | * c-basic-offset: 4 |
| 2395 | * indent-tabs-mode: t |
| 2396 | * End: |
| 2397 | * |
| 2398 | * ex: set ts=8 sts=4 sw=4 noet: |
| 2399 | */ |