Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* doio.c |
a687059c | 2 | * |
1129b882 NC |
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 | |
a687059c | 5 | * |
6e21c824 LW |
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. | |
a687059c | 8 | * |
a0d0e21e LW |
9 | */ |
10 | ||
11 | /* | |
4ac71550 TC |
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"] | |
a687059c LW |
18 | */ |
19 | ||
166f8a29 DM |
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 | ||
a687059c | 25 | #include "EXTERN.h" |
864dbfa3 | 26 | #define PERL_IN_DOIO_C |
a687059c LW |
27 | #include "perl.h" |
28 | ||
fe14fcc3 | 29 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
aec308ec | 30 | #ifndef HAS_SEM |
c2ab57d4 | 31 | #include <sys/ipc.h> |
aec308ec | 32 | #endif |
fe14fcc3 | 33 | #ifdef HAS_MSG |
c2ab57d4 | 34 | #include <sys/msg.h> |
e5d73d77 | 35 | #endif |
fe14fcc3 | 36 | #ifdef HAS_SHM |
c2ab57d4 | 37 | #include <sys/shm.h> |
a0d0e21e | 38 | # ifndef HAS_SHMAT_PROTOTYPE |
20ce7b12 | 39 | extern Shmat_t shmat (int, char *, int); |
a0d0e21e | 40 | # endif |
c2ab57d4 | 41 | #endif |
e5d73d77 | 42 | #endif |
c2ab57d4 | 43 | |
663a0e37 | 44 | #ifdef I_UTIME |
3730b96e | 45 | # if defined(_MSC_VER) || defined(__MINGW32__) |
3fe9a6f1 | 46 | # include <sys/utime.h> |
47 | # else | |
48 | # include <utime.h> | |
49 | # endif | |
663a0e37 | 50 | #endif |
85aff577 | 51 | |
85aff577 CS |
52 | #ifdef O_EXCL |
53 | # define OPEN_EXCL O_EXCL | |
54 | #else | |
55 | # define OPEN_EXCL 0 | |
56 | #endif | |
a687059c | 57 | |
0c19750d SP |
58 | #define PERL_MODE_MAX 8 |
59 | #define PERL_FLAGS_MAX 10 | |
60 | ||
76121258 | 61 | #include <signal.h> |
76121258 | 62 | |
a687059c | 63 | bool |
2fbb330f | 64 | Perl_do_openn(pTHX_ GV *gv, register const char *oname, I32 len, int as_raw, |
a567e93b NIS |
65 | int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp, |
66 | I32 num_svs) | |
67 | { | |
27da23d5 | 68 | dVAR; |
890ce7af | 69 | register IO * const io = GvIOn(gv); |
4608196e RGS |
70 | PerlIO *saveifp = NULL; |
71 | PerlIO *saveofp = NULL; | |
ee518936 | 72 | int savefd = -1; |
9f37169a | 73 | char savetype = IoTYPE_CLOSED; |
c07a80fd | 74 | int writing = 0; |
760ac839 | 75 | PerlIO *fp; |
c07a80fd | 76 | int fd; |
77 | int result; | |
3500f679 | 78 | bool was_fdopen = FALSE; |
16fe6d59 | 79 | bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0; |
b931b1d9 | 80 | char *type = NULL; |
e9ed3d63 | 81 | char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */ |
ee518936 | 82 | SV *namesv; |
a687059c | 83 | |
7918f24d NC |
84 | PERL_ARGS_ASSERT_DO_OPENN; |
85 | ||
b931b1d9 | 86 | Zero(mode,sizeof(mode),char); |
3280af22 | 87 | PL_forkprocess = 1; /* assume true if no fork */ |
c07a80fd | 88 | |
b931b1d9 | 89 | /* Collect default raw/crlf info from the op */ |
16fe6d59 | 90 | if (PL_op && PL_op->op_type == OP_OPEN) { |
c2be40b1 | 91 | /* set up IO layers */ |
b464bac0 | 92 | const U8 flags = PL_op->op_private; |
16fe6d59 GS |
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 | ||
b931b1d9 | 99 | /* If currently open - close before we re-open */ |
a0d0e21e | 100 | if (IoIFP(io)) { |
760ac839 | 101 | fd = PerlIO_fileno(IoIFP(io)); |
ee518936 NIS |
102 | if (IoTYPE(io) == IoTYPE_STD) { |
103 | /* This is a clone of one of STD* handles */ | |
c2ab57d4 | 104 | result = 0; |
ee518936 NIS |
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); | |
8990e307 | 110 | savetype = IoTYPE(io); |
ee518936 NIS |
111 | savefd = fd; |
112 | result = 0; | |
6e21c824 | 113 | } |
50952442 | 114 | else if (IoTYPE(io) == IoTYPE_PIPE) |
3028581b | 115 | result = PerlProc_pclose(IoIFP(io)); |
8990e307 LW |
116 | else if (IoIFP(io) != IoOFP(io)) { |
117 | if (IoOFP(io)) { | |
760ac839 | 118 | result = PerlIO_close(IoOFP(io)); |
6170680b | 119 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
c2ab57d4 LW |
120 | } |
121 | else | |
760ac839 | 122 | result = PerlIO_close(IoIFP(io)); |
a687059c | 123 | } |
a687059c | 124 | else |
760ac839 | 125 | result = PerlIO_close(IoIFP(io)); |
ee518936 NIS |
126 | if (result == EOF && fd > PL_maxsysfd) { |
127 | /* Why is this not Perl_warn*() call ? */ | |
bf49b057 | 128 | PerlIO_printf(Perl_error_log, |
d0c0e7dd FC |
129 | "Warning: unable to close filehandle %"HEKf" properly.\n", |
130 | HEKfARG(GvENAME_HEK(gv)) | |
131 | ); | |
ee518936 | 132 | } |
4608196e | 133 | IoOFP(io) = IoIFP(io) = NULL; |
a687059c | 134 | } |
c07a80fd | 135 | |
136 | if (as_raw) { | |
b931b1d9 | 137 | /* sysopen style args, i.e. integer mode and permissions */ |
ee518936 | 138 | STRLEN ix = 0; |
e1ec3a88 | 139 | const int appendtrunc = |
3dccc55c | 140 | 0 |
d1da7611 | 141 | #ifdef O_APPEND /* Not fully portable. */ |
3dccc55c | 142 | |O_APPEND |
d1da7611 JH |
143 | #endif |
144 | #ifdef O_TRUNC /* Not fully portable. */ | |
3dccc55c | 145 | |O_TRUNC |
d1da7611 | 146 | #endif |
3dccc55c | 147 | ; |
6867be6d | 148 | const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc; |
3dccc55c JH |
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 | } | |
3b6c1aba | 176 | mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */ |
b931b1d9 | 177 | |
09458382 | 178 | #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE) |
b94c04ac | 179 | rawmode |= O_LARGEFILE; /* Transparently largefiley. */ |
5ff3f7a4 GS |
180 | #endif |
181 | ||
06c7082d | 182 | IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing); |
ee518936 | 183 | |
59cd0e26 | 184 | namesv = newSVpvn_flags(oname, len, SVs_TEMP); |
ee518936 NIS |
185 | num_svs = 1; |
186 | svp = &namesv; | |
bd61b366 | 187 | type = NULL; |
b94c04ac | 188 | fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp); |
a687059c | 189 | } |
c07a80fd | 190 | else { |
b931b1d9 | 191 | /* Regular (non-sys) open */ |
2fbb330f | 192 | char *name; |
faecd977 | 193 | STRLEN olen = len; |
b931b1d9 NIS |
194 | char *tend; |
195 | int dodup = 0; | |
c07a80fd | 196 | |
2fbb330f | 197 | type = savepvn(oname, len); |
b931b1d9 | 198 | tend = type+len; |
faecd977 | 199 | SAVEFREEPV(type); |
eb649f83 AMS |
200 | |
201 | /* Lose leading and trailing white space */ | |
294b3b39 AL |
202 | while (isSPACE(*type)) |
203 | type++; | |
eb649f83 AMS |
204 | while (tend > type && isSPACE(tend[-1])) |
205 | *--tend = '\0'; | |
206 | ||
6170680b | 207 | if (num_svs) { |
c2be40b1 | 208 | /* New style explicit name, type is just mode and layer info */ |
9a869a14 | 209 | #ifdef USE_STDIO |
2fbb330f | 210 | if (SvROK(*svp) && !strchr(oname,'&')) { |
9a869a14 RGS |
211 | if (ckWARN(WARN_IO)) |
212 | Perl_warner(aTHX_ packWARN(WARN_IO), | |
213 | "Can't open a reference"); | |
93189314 | 214 | SETERRNO(EINVAL, LIB_INVARG); |
9a869a14 RGS |
215 | goto say_false; |
216 | } | |
217 | #endif /* USE_STDIO */ | |
d71e3dc3 DM |
218 | name = (SvOK(*svp) || SvGMAGICAL(*svp)) ? |
219 | savesvpv (*svp) : savepvs (""); | |
faecd977 | 220 | SAVEFREEPV(name); |
6170680b | 221 | } |
faecd977 | 222 | else { |
faecd977 | 223 | name = type; |
b931b1d9 | 224 | len = tend-type; |
faecd977 | 225 | } |
6170680b | 226 | IoTYPE(io) = *type; |
516a5887 | 227 | if ((*type == IoTYPE_RDWR) && /* scary */ |
01a8ea99 | 228 | (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) && |
516a5887 | 229 | ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) { |
c2be40b1 | 230 | TAINT_PROPER("open"); |
6170680b | 231 | mode[1] = *type++; |
c07a80fd | 232 | writing = 1; |
a687059c | 233 | } |
c07a80fd | 234 | |
9f37169a | 235 | if (*type == IoTYPE_PIPE) { |
b931b1d9 NIS |
236 | if (num_svs) { |
237 | if (type[1] != IoTYPE_STD) { | |
c2be40b1 | 238 | unknown_open_mode: |
b931b1d9 NIS |
239 | Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname); |
240 | } | |
241 | type++; | |
6170680b | 242 | } |
294b3b39 AL |
243 | do { |
244 | type++; | |
245 | } while (isSPACE(*type)); | |
faecd977 | 246 | if (!num_svs) { |
6170680b | 247 | name = type; |
b931b1d9 | 248 | len = tend-type; |
faecd977 | 249 | } |
4a7d1889 NIS |
250 | if (*name == '\0') { |
251 | /* command is missing 19990114 */ | |
06eaf0bc | 252 | if (ckWARN(WARN_PIPE)) |
9014280d | 253 | Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open"); |
06eaf0bc GS |
254 | errno = EPIPE; |
255 | goto say_false; | |
256 | } | |
f27977c3 | 257 | if (!(*name == '-' && name[1] == '\0') || num_svs) |
c07a80fd | 258 | TAINT_ENV(); |
259 | TAINT_PROPER("piped open"); | |
b931b1d9 | 260 | if (!num_svs && name[len-1] == '|') { |
faecd977 | 261 | name[--len] = '\0' ; |
599cee73 | 262 | if (ckWARN(WARN_PIPE)) |
9014280d | 263 | Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe"); |
7b8d334a | 264 | } |
a1d180c4 | 265 | mode[0] = 'w'; |
c07a80fd | 266 | writing = 1; |
0c19750d | 267 | if (out_raw) |
5686ee58 | 268 | mode[1] = 'b'; |
0c19750d | 269 | else if (out_crlf) |
5686ee58 | 270 | mode[1] = 't'; |
4a7d1889 NIS |
271 | if (num_svs > 1) { |
272 | fp = PerlProc_popen_list(mode, num_svs, svp); | |
273 | } | |
274 | else { | |
275 | fp = PerlProc_popen(name,mode); | |
276 | } | |
1771866f NIS |
277 | if (num_svs) { |
278 | if (*type) { | |
279 | if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) { | |
280 | goto say_false; | |
281 | } | |
282 | } | |
283 | } | |
c2be40b1 | 284 | } /* IoTYPE_PIPE */ |
9f37169a | 285 | else if (*type == IoTYPE_WRONLY) { |
c07a80fd | 286 | TAINT_PROPER("open"); |
6170680b | 287 | type++; |
9f37169a JH |
288 | if (*type == IoTYPE_WRONLY) { |
289 | /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */ | |
50952442 | 290 | mode[0] = IoTYPE(io) = IoTYPE_APPEND; |
6170680b | 291 | type++; |
a0d0e21e | 292 | } |
ee518936 | 293 | else { |
c07a80fd | 294 | mode[0] = 'w'; |
ee518936 | 295 | } |
c07a80fd | 296 | writing = 1; |
297 | ||
0c19750d | 298 | if (out_raw) |
5686ee58 | 299 | mode[1] = 'b'; |
0c19750d | 300 | else if (out_crlf) |
5686ee58 | 301 | mode[1] = 't'; |
6170680b | 302 | if (*type == '&') { |
c07a80fd | 303 | duplicity: |
ecdeb87c | 304 | dodup = PERLIO_DUP_FD; |
e620cd72 NIS |
305 | type++; |
306 | if (*type == '=') { | |
c07a80fd | 307 | dodup = 0; |
e620cd72 | 308 | type++; |
4a7d1889 | 309 | } |
ee518936 | 310 | if (!num_svs && !*type && supplied_fp) { |
4a7d1889 | 311 | /* "<+&" etc. is used by typemaps */ |
c07a80fd | 312 | fp = supplied_fp; |
ee518936 | 313 | } |
a0d0e21e | 314 | else { |
35da51f7 | 315 | PerlIO *that_fp = NULL; |
e620cd72 | 316 | if (num_svs > 1) { |
fe13d51d | 317 | /* diag_listed_as: More than one argument to '%s' open */ |
e620cd72 NIS |
318 | Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io)); |
319 | } | |
294b3b39 AL |
320 | while (isSPACE(*type)) |
321 | type++; | |
1771866f | 322 | if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) { |
e620cd72 | 323 | fd = SvUV(*svp); |
24a7a40d | 324 | num_svs = 0; |
ee518936 | 325 | } |
e620cd72 | 326 | else if (isDIGIT(*type)) { |
e620cd72 NIS |
327 | fd = atoi(type); |
328 | } | |
c07a80fd | 329 | else { |
e1ec3a88 | 330 | const IO* thatio; |
e620cd72 NIS |
331 | if (num_svs) { |
332 | thatio = sv_2io(*svp); | |
333 | } | |
334 | else { | |
35da51f7 | 335 | GV * const thatgv = gv_fetchpvn_flags(type, tend - type, |
90e5519e | 336 | 0, SVt_PVIO); |
e620cd72 NIS |
337 | thatio = GvIO(thatgv); |
338 | } | |
c07a80fd | 339 | if (!thatio) { |
6e21c824 | 340 | #ifdef EINVAL |
93189314 | 341 | SETERRNO(EINVAL,SS_IVCHAN); |
6e21c824 | 342 | #endif |
c07a80fd | 343 | goto say_false; |
344 | } | |
f4e789af | 345 | if ((that_fp = IoIFP(thatio))) { |
7211d486 JH |
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 */ | |
2c534a3f NC |
351 | #ifdef USE_SFIO |
352 | /* sfio fails to clear error on next | |
353 | sfwrite, contrary to documentation. | |
04d26ece | 354 | -- Nicholas Clark */ |
ecdeb87c NIS |
355 | if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1) |
356 | PerlIO_clearerr(that_fp); | |
2c534a3f | 357 | #endif |
7211d486 JH |
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 */ | |
ecdeb87c NIS |
362 | PerlIO_flush(that_fp); |
363 | fd = PerlIO_fileno(that_fp); | |
0759c907 JH |
364 | /* When dup()ing STDIN, STDOUT or STDERR |
365 | * explicitly set appropriate access mode */ | |
f4e789af NC |
366 | if (that_fp == PerlIO_stdout() |
367 | || that_fp == PerlIO_stderr()) | |
0759c907 | 368 | IoTYPE(io) = IoTYPE_WRONLY; |
f4e789af | 369 | else if (that_fp == PerlIO_stdin()) |
0759c907 JH |
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) | |
50952442 | 374 | IoTYPE(io) = IoTYPE_SOCKET; |
c07a80fd | 375 | } |
376 | else | |
377 | fd = -1; | |
a0d0e21e | 378 | } |
ee518936 | 379 | if (!num_svs) |
bd61b366 | 380 | type = NULL; |
ecdeb87c NIS |
381 | if (that_fp) { |
382 | fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup); | |
383 | } | |
384 | else { | |
c07a80fd | 385 | if (dodup) |
ecdeb87c NIS |
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))) { | |
b42969c0 | 390 | if (dodup && fd >= 0) |
ecdeb87c NIS |
391 | PerlLIO_close(fd); |
392 | } | |
faecd977 | 393 | } |
c07a80fd | 394 | } |
ee518936 | 395 | } /* & */ |
c07a80fd | 396 | else { |
294b3b39 AL |
397 | while (isSPACE(*type)) |
398 | type++; | |
b931b1d9 | 399 | if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) { |
b931b1d9 | 400 | type++; |
760ac839 | 401 | fp = PerlIO_stdout(); |
50952442 | 402 | IoTYPE(io) = IoTYPE_STD; |
7cf31beb | 403 | if (num_svs > 1) { |
fe13d51d | 404 | /* diag_listed_as: More than one argument to '%s' open */ |
7cf31beb NIS |
405 | Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD); |
406 | } | |
c07a80fd | 407 | } |
408 | else { | |
ee518936 | 409 | if (!num_svs) { |
59cd0e26 | 410 | namesv = newSVpvn_flags(type, tend - type, SVs_TEMP); |
ee518936 NIS |
411 | num_svs = 1; |
412 | svp = &namesv; | |
bd61b366 | 413 | type = NULL; |
ee518936 | 414 | } |
6e60e805 | 415 | fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp); |
c07a80fd | 416 | } |
ee518936 | 417 | } /* !& */ |
7e72d509 JH |
418 | if (!fp && type && *type && *type != ':' && !isIDFIRST(*type)) |
419 | goto unknown_open_mode; | |
c2be40b1 | 420 | } /* IoTYPE_WRONLY */ |
9f37169a | 421 | else if (*type == IoTYPE_RDONLY) { |
294b3b39 AL |
422 | do { |
423 | type++; | |
424 | } while (isSPACE(*type)); | |
bf38876a | 425 | mode[0] = 'r'; |
0c19750d | 426 | if (in_raw) |
5686ee58 | 427 | mode[1] = 'b'; |
0c19750d | 428 | else if (in_crlf) |
5686ee58 | 429 | mode[1] = 't'; |
6170680b | 430 | if (*type == '&') { |
bf38876a | 431 | goto duplicity; |
6170680b | 432 | } |
b931b1d9 | 433 | if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) { |
b931b1d9 | 434 | type++; |
760ac839 | 435 | fp = PerlIO_stdin(); |
50952442 | 436 | IoTYPE(io) = IoTYPE_STD; |
7cf31beb | 437 | if (num_svs > 1) { |
fe13d51d | 438 | /* diag_listed_as: More than one argument to '%s' open */ |
7cf31beb NIS |
439 | Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD); |
440 | } | |
a687059c | 441 | } |
ee518936 NIS |
442 | else { |
443 | if (!num_svs) { | |
59cd0e26 | 444 | namesv = newSVpvn_flags(type, tend - type, SVs_TEMP); |
ee518936 NIS |
445 | num_svs = 1; |
446 | svp = &namesv; | |
bd61b366 | 447 | type = NULL; |
ee518936 | 448 | } |
6e60e805 | 449 | fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp); |
ee518936 | 450 | } |
7e72d509 JH |
451 | if (!fp && type && *type && *type != ':' && !isIDFIRST(*type)) |
452 | goto unknown_open_mode; | |
c2be40b1 JH |
453 | } /* IoTYPE_RDONLY */ |
454 | else if ((num_svs && /* '-|...' or '...|' */ | |
455 | type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) || | |
b931b1d9 | 456 | (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) { |
6170680b | 457 | if (num_svs) { |
b931b1d9 | 458 | type += 2; /* skip over '-|' */ |
6170680b IZ |
459 | } |
460 | else { | |
b931b1d9 NIS |
461 | *--tend = '\0'; |
462 | while (tend > type && isSPACE(tend[-1])) | |
463 | *--tend = '\0'; | |
a6e20a40 AL |
464 | for (; isSPACE(*type); type++) |
465 | ; | |
6170680b | 466 | name = type; |
b931b1d9 | 467 | len = tend-type; |
6170680b | 468 | } |
4a7d1889 NIS |
469 | if (*name == '\0') { |
470 | /* command is missing 19990114 */ | |
06eaf0bc | 471 | if (ckWARN(WARN_PIPE)) |
9014280d | 472 | Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open"); |
06eaf0bc GS |
473 | errno = EPIPE; |
474 | goto say_false; | |
475 | } | |
770526c1 | 476 | if (!(*name == '-' && name[1] == '\0') || num_svs) |
79072805 LW |
477 | TAINT_ENV(); |
478 | TAINT_PROPER("piped open"); | |
a1d180c4 | 479 | mode[0] = 'r'; |
0c19750d | 480 | |
0c19750d | 481 | if (in_raw) |
5686ee58 | 482 | mode[1] = 'b'; |
0c19750d | 483 | else if (in_crlf) |
5686ee58 | 484 | mode[1] = 't'; |
0c19750d | 485 | |
4a7d1889 NIS |
486 | if (num_svs > 1) { |
487 | fp = PerlProc_popen_list(mode,num_svs,svp); | |
488 | } | |
e620cd72 | 489 | else { |
4a7d1889 NIS |
490 | fp = PerlProc_popen(name,mode); |
491 | } | |
50952442 | 492 | IoTYPE(io) = IoTYPE_PIPE; |
1771866f | 493 | if (num_svs) { |
294b3b39 AL |
494 | while (isSPACE(*type)) |
495 | type++; | |
1771866f NIS |
496 | if (*type) { |
497 | if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) { | |
498 | goto say_false; | |
499 | } | |
500 | } | |
501 | } | |
a687059c | 502 | } |
c2be40b1 | 503 | else { /* layer(Args) */ |
6170680b | 504 | if (num_svs) |
c2be40b1 | 505 | goto unknown_open_mode; |
6170680b | 506 | name = type; |
50952442 | 507 | IoTYPE(io) = IoTYPE_RDONLY; |
a6e20a40 AL |
508 | for (; isSPACE(*name); name++) |
509 | ; | |
88b61e10 | 510 | mode[0] = 'r'; |
0c19750d | 511 | |
0c19750d | 512 | if (in_raw) |
5686ee58 | 513 | mode[1] = 'b'; |
0c19750d | 514 | else if (in_crlf) |
5686ee58 | 515 | mode[1] = 't'; |
0c19750d | 516 | |
770526c1 | 517 | if (*name == '-' && name[1] == '\0') { |
760ac839 | 518 | fp = PerlIO_stdin(); |
50952442 | 519 | IoTYPE(io) = IoTYPE_STD; |
a687059c | 520 | } |
16fe6d59 | 521 | else { |
ee518936 | 522 | if (!num_svs) { |
59cd0e26 | 523 | namesv = newSVpvn_flags(type, tend - type, SVs_TEMP); |
ee518936 NIS |
524 | num_svs = 1; |
525 | svp = &namesv; | |
bd61b366 | 526 | type = NULL; |
ee518936 | 527 | } |
6e60e805 | 528 | fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp); |
16fe6d59 | 529 | } |
a687059c LW |
530 | } |
531 | } | |
bee1dbe2 | 532 | if (!fp) { |
ce44635a | 533 | if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE) |
2fbb330f | 534 | && strchr(oname, '\n') |
ce44635a | 535 | |
041457d9 | 536 | ) |
9014280d | 537 | Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open"); |
6e21c824 | 538 | goto say_false; |
bee1dbe2 | 539 | } |
a00b5bd3 NIS |
540 | |
541 | if (ckWARN(WARN_IO)) { | |
542 | if ((IoTYPE(io) == IoTYPE_RDONLY) && | |
543 | (fp == PerlIO_stdout() || fp == PerlIO_stderr())) { | |
9014280d | 544 | Perl_warner(aTHX_ packWARN(WARN_IO), |
d0c0e7dd FC |
545 | "Filehandle STD%s reopened as %"HEKf |
546 | " only for input", | |
97828cef | 547 | ((fp == PerlIO_stdout()) ? "OUT" : "ERR"), |
d0c0e7dd | 548 | HEKfARG(GvENAME_HEK(gv))); |
a00b5bd3 | 549 | } |
ee518936 | 550 | else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) { |
9014280d | 551 | Perl_warner(aTHX_ packWARN(WARN_IO), |
d0c0e7dd FC |
552 | "Filehandle STDIN reopened as %"HEKf" only for output", |
553 | HEKfARG(GvENAME_HEK(gv)) | |
554 | ); | |
a00b5bd3 NIS |
555 | } |
556 | } | |
557 | ||
e99cca91 | 558 | fd = PerlIO_fileno(fp); |
e934609f JH |
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 | |
e99cca91 NIS |
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); | |
6e21c824 | 567 | goto say_false; |
a687059c | 568 | } |
7114a2d2 | 569 | #ifndef PERL_MICRO |
3280af22 | 570 | if (S_ISSOCK(PL_statbuf.st_mode)) |
50952442 | 571 | IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */ |
99b89507 LW |
572 | #ifdef HAS_SOCKET |
573 | else if ( | |
c623bd54 | 574 | #ifdef S_IFMT |
3280af22 | 575 | !(PL_statbuf.st_mode & S_IFMT) |
99b89507 | 576 | #else |
b28d0864 | 577 | !PL_statbuf.st_mode |
99b89507 | 578 | #endif |
0759c907 JH |
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 */ | |
e99cca91 NIS |
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 */ | |
99b89507 | 588 | } |
e99cca91 | 589 | #endif /* HAS_SOCKET */ |
7114a2d2 | 590 | #endif /* !PERL_MICRO */ |
a687059c | 591 | } |
e99cca91 NIS |
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 | ||
6e21c824 | 598 | if (saveifp) { /* must use old fp? */ |
f5b9d040 | 599 | /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR |
24c23ab4 | 600 | then dup the new fileno down |
f5b9d040 | 601 | */ |
6e21c824 | 602 | if (saveofp) { |
f5b9d040 | 603 | PerlIO_flush(saveofp); /* emulate PerlIO_close() */ |
6e21c824 | 604 | if (saveofp != saveifp) { /* was a socket? */ |
760ac839 | 605 | PerlIO_close(saveofp); |
6e21c824 LW |
606 | } |
607 | } | |
6e60e805 | 608 | if (savefd != fd) { |
e934609f | 609 | /* Still a small can-of-worms here if (say) PerlIO::scalar |
ecdeb87c NIS |
610 | is assigned to (say) STDOUT - for now let dup2() fail |
611 | and provide the error | |
612 | */ | |
bd4a5668 NIS |
613 | if (PerlLIO_dup2(fd, savefd) < 0) { |
614 | (void)PerlIO_close(fp); | |
615 | goto say_false; | |
616 | } | |
d082dcd6 | 617 | #ifdef VMS |
6e60e805 | 618 | if (savefd != PerlIO_fileno(PerlIO_stdin())) { |
d0e2cf63 AMS |
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 | } | |
d082dcd6 JH |
626 | } |
627 | #endif | |
d0e2cf63 AMS |
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 | ||
d0e2cf63 | 636 | sv = *av_fetch(PL_fdpid,fd,TRUE); |
862a34c6 | 637 | SvUPGRADE(sv, SVt_IV); |
d0e2cf63 | 638 | pid = SvIVX(sv); |
45977657 | 639 | SvIV_set(sv, 0); |
d0e2cf63 | 640 | sv = *av_fetch(PL_fdpid,savefd,TRUE); |
862a34c6 | 641 | SvUPGRADE(sv, SVt_IV); |
45977657 | 642 | SvIV_set(sv, pid); |
d0e2cf63 AMS |
643 | } |
644 | #endif | |
645 | ||
e212fc47 AMS |
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); | |
03e631df JH |
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 | |
e212fc47 AMS |
654 | PerlIO_close(fp); |
655 | PerlLIO_dup2(dupfd,ofd); | |
03e631df JH |
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 | |
e212fc47 | 660 | PerlLIO_close(dupfd); |
ecdeb87c | 661 | } |
e212fc47 AMS |
662 | else |
663 | PerlIO_close(fp); | |
6e21c824 LW |
664 | } |
665 | fp = saveifp; | |
760ac839 | 666 | PerlIO_clearerr(fp); |
e99cca91 | 667 | fd = PerlIO_fileno(fp); |
6e21c824 | 668 | } |
a0d0e21e | 669 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
e99cca91 | 670 | if (fd >= 0) { |
4ee39169 | 671 | dSAVE_ERRNO; |
a8710ca1 | 672 | fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */ |
4ee39169 | 673 | RESTORE_ERRNO; |
a8710ca1 | 674 | } |
1462b684 | 675 | #endif |
8990e307 | 676 | IoIFP(io) = fp; |
b931b1d9 | 677 | |
684bef36 | 678 | IoFLAGS(io) &= ~IOf_NOLINE; |
bf38876a | 679 | if (writing) { |
50952442 | 680 | if (IoTYPE(io) == IoTYPE_SOCKET |
e99cca91 | 681 | || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) { |
a33cf58c | 682 | char *s = mode; |
3b6c1aba JH |
683 | if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC) |
684 | s++; | |
a33cf58c | 685 | *s = 'w'; |
1feb1812 | 686 | if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) { |
760ac839 | 687 | PerlIO_close(fp); |
4608196e | 688 | IoIFP(io) = NULL; |
6e21c824 | 689 | goto say_false; |
fe14fcc3 | 690 | } |
1462b684 LW |
691 | } |
692 | else | |
8990e307 | 693 | IoOFP(io) = fp; |
bf38876a | 694 | } |
a687059c | 695 | return TRUE; |
6e21c824 LW |
696 | |
697 | say_false: | |
8990e307 LW |
698 | IoIFP(io) = saveifp; |
699 | IoOFP(io) = saveofp; | |
700 | IoTYPE(io) = savetype; | |
6e21c824 | 701 | return FALSE; |
a687059c LW |
702 | } |
703 | ||
760ac839 | 704 | PerlIO * |
864dbfa3 | 705 | Perl_nextargv(pTHX_ register GV *gv) |
a687059c | 706 | { |
97aff369 | 707 | dVAR; |
79072805 | 708 | register SV *sv; |
99b89507 | 709 | #ifndef FLEXFILENAMES |
c623bd54 LW |
710 | int filedev; |
711 | int fileino; | |
99b89507 | 712 | #endif |
761237fe JB |
713 | Uid_t fileuid; |
714 | Gid_t filegid; | |
2d03de9c | 715 | IO * const io = GvIOp(gv); |
fe14fcc3 | 716 | |
7918f24d NC |
717 | PERL_ARGS_ASSERT_NEXTARGV; |
718 | ||
3280af22 | 719 | if (!PL_argvoutgv) |
fafc274c | 720 | PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
18708f5a GS |
721 | if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) { |
722 | IoFLAGS(io) &= ~IOf_START; | |
7a1c5554 | 723 | if (PL_inplace) { |
294b3b39 | 724 | assert(PL_defoutgv); |
29a861e7 NC |
725 | Perl_av_create_and_push(aTHX_ &PL_argvout_stack, |
726 | SvREFCNT_inc_simple_NN(PL_defoutgv)); | |
7a1c5554 | 727 | } |
18708f5a | 728 | } |
3280af22 NIS |
729 | if (PL_filemode & (S_ISUID|S_ISGID)) { |
730 | PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */ | |
fe14fcc3 | 731 | #ifdef HAS_FCHMOD |
c797f2d8 DM |
732 | if (PL_lastfd != -1) |
733 | (void)fchmod(PL_lastfd,PL_filemode); | |
fe14fcc3 | 734 | #else |
b28d0864 | 735 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
fe14fcc3 LW |
736 | #endif |
737 | } | |
c797f2d8 | 738 | PL_lastfd = -1; |
3280af22 | 739 | PL_filemode = 0; |
5c501b37 | 740 | if (!GvAV(gv)) |
4608196e | 741 | return NULL; |
79072805 | 742 | while (av_len(GvAV(gv)) >= 0) { |
85aff577 | 743 | STRLEN oldlen; |
79072805 | 744 | sv = av_shift(GvAV(gv)); |
8990e307 | 745 | SAVEFREESV(sv); |
e203899d | 746 | sv_setsv(GvSVn(gv),sv); |
79072805 | 747 | SvSETMAGIC(GvSV(gv)); |
3280af22 | 748 | PL_oldname = SvPVx(GvSV(gv), oldlen); |
4608196e | 749 | if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) { |
3280af22 | 750 | if (PL_inplace) { |
79072805 | 751 | TAINT_PROPER("inplace open"); |
3280af22 | 752 | if (oldlen == 1 && *PL_oldname == '-') { |
fafc274c NC |
753 | setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, |
754 | SVt_PVIO)); | |
a0d0e21e | 755 | return IoIFP(GvIOp(gv)); |
c623bd54 | 756 | } |
99b89507 | 757 | #ifndef FLEXFILENAMES |
b28d0864 NIS |
758 | filedev = PL_statbuf.st_dev; |
759 | fileino = PL_statbuf.st_ino; | |
99b89507 | 760 | #endif |
3280af22 NIS |
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)) { | |
9b387841 NC |
765 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
766 | "Can't do inplace edit: %s is not a regular file", | |
767 | PL_oldname ); | |
79072805 | 768 | do_close(gv,FALSE); |
c623bd54 LW |
769 | continue; |
770 | } | |
c9930541 | 771 | if (*PL_inplace && strNE(PL_inplace, "*")) { |
2d03de9c | 772 | const char *star = strchr(PL_inplace, '*'); |
2d259d92 | 773 | if (star) { |
2d03de9c | 774 | const char *begin = PL_inplace; |
76f68e9b | 775 | sv_setpvs(sv, ""); |
2d259d92 CK |
776 | do { |
777 | sv_catpvn(sv, begin, star - begin); | |
3280af22 | 778 | sv_catpvn(sv, PL_oldname, oldlen); |
2d259d92 CK |
779 | begin = ++star; |
780 | } while ((star = strchr(begin, '*'))); | |
3d66d7bb GS |
781 | if (*begin) |
782 | sv_catpv(sv,begin); | |
2d259d92 CK |
783 | } |
784 | else { | |
3280af22 | 785 | sv_catpv(sv,PL_inplace); |
2d259d92 | 786 | } |
c623bd54 | 787 | #ifndef FLEXFILENAMES |
95a20fc0 | 788 | if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0 |
5f74f29c JH |
789 | && PL_statbuf.st_dev == filedev |
790 | && PL_statbuf.st_ino == fileino) | |
39e571d4 | 791 | #ifdef DJGPP |
5f74f29c | 792 | || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0) |
39e571d4 | 793 | #endif |
f248d071 GS |
794 | ) |
795 | { | |
9b387841 NC |
796 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
797 | "Can't do inplace edit: %"SVf" would not be unique", | |
798 | SVfARG(sv)); | |
79072805 | 799 | do_close(gv,FALSE); |
c623bd54 LW |
800 | continue; |
801 | } | |
802 | #endif | |
fe14fcc3 | 803 | #ifdef HAS_RENAME |
2585f9a3 | 804 | #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC) |
95a20fc0 | 805 | if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) { |
9b387841 NC |
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)); | |
79072805 | 809 | do_close(gv,FALSE); |
c623bd54 LW |
810 | continue; |
811 | } | |
a687059c | 812 | #else |
79072805 | 813 | do_close(gv,FALSE); |
95a20fc0 SP |
814 | (void)PerlLIO_unlink(SvPVX_const(sv)); |
815 | (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv)); | |
30fc4309 | 816 | do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),TRUE,O_RDONLY,0,NULL); |
55497cff | 817 | #endif /* DOSISH */ |
ff8e2863 | 818 | #else |
95a20fc0 SP |
819 | (void)UNLINK(SvPVX_const(sv)); |
820 | if (link(PL_oldname,SvPVX_const(sv)) < 0) { | |
9b387841 NC |
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) ); | |
79072805 | 824 | do_close(gv,FALSE); |
c623bd54 LW |
825 | continue; |
826 | } | |
b28d0864 | 827 | (void)UNLINK(PL_oldname); |
a687059c LW |
828 | #endif |
829 | } | |
830 | else { | |
c030f24b | 831 | #if !defined(DOSISH) && !defined(AMIGAOS) |
edc7bc49 | 832 | # ifndef VMS /* Don't delete; use automatic file versioning */ |
3280af22 | 833 | if (UNLINK(PL_oldname) < 0) { |
9b387841 NC |
834 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
835 | "Can't remove %s: %s, skipping file", | |
836 | PL_oldname, Strerror(errno) ); | |
79072805 | 837 | do_close(gv,FALSE); |
fe14fcc3 LW |
838 | continue; |
839 | } | |
edc7bc49 | 840 | # endif |
ff8e2863 | 841 | #else |
cea2e8a9 | 842 | Perl_croak(aTHX_ "Can't do inplace edit without backup"); |
ff8e2863 | 843 | #endif |
a687059c LW |
844 | } |
845 | ||
30fc4309 | 846 | sv_setpvn(sv,PL_oldname,oldlen); |
748a9306 | 847 | SETERRNO(0,0); /* in case sprintf set errno */ |
7f8ee4be NC |
848 | if (!Perl_do_openn(aTHX_ PL_argvoutgv, (char*)SvPVX_const(sv), |
849 | SvCUR(sv), TRUE, | |
4119ab01 | 850 | #ifdef VMS |
7f8ee4be | 851 | O_WRONLY|O_CREAT|O_TRUNC,0, |
4119ab01 | 852 | #else |
7f8ee4be | 853 | O_WRONLY|O_CREAT|OPEN_EXCL,0600, |
4119ab01 | 854 | #endif |
7f8ee4be | 855 | NULL, NULL, 0)) { |
9b387841 NC |
856 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s", |
857 | PL_oldname, Strerror(errno) ); | |
79072805 | 858 | do_close(gv,FALSE); |
fe14fcc3 LW |
859 | continue; |
860 | } | |
3280af22 NIS |
861 | setdefout(PL_argvoutgv); |
862 | PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv))); | |
863 | (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf); | |
fe14fcc3 | 864 | #ifdef HAS_FCHMOD |
3280af22 | 865 | (void)fchmod(PL_lastfd,PL_filemode); |
a687059c | 866 | #else |
b28d0864 | 867 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
a687059c | 868 | #endif |
3280af22 | 869 | if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) { |
fe14fcc3 | 870 | #ifdef HAS_FCHOWN |
3280af22 | 871 | (void)fchown(PL_lastfd,fileuid,filegid); |
a687059c | 872 | #else |
fe14fcc3 | 873 | #ifdef HAS_CHOWN |
b28d0864 | 874 | (void)PerlLIO_chown(PL_oldname,fileuid,filegid); |
a687059c | 875 | #endif |
b1248f16 | 876 | #endif |
fe14fcc3 | 877 | } |
a687059c | 878 | } |
a0d0e21e | 879 | return IoIFP(GvIOp(gv)); |
a687059c | 880 | } |
4d61ec05 | 881 | else { |
4d61ec05 | 882 | if (ckWARN_d(WARN_INPLACE)) { |
4373e329 | 883 | const int eno = errno; |
6af84f9f GS |
884 | if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0 |
885 | && !S_ISREG(PL_statbuf.st_mode)) | |
886 | { | |
9014280d | 887 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), |
4d61ec05 | 888 | "Can't do inplace edit: %s is not a regular file", |
9a7dcd9c | 889 | PL_oldname); |
6af84f9f | 890 | } |
4d61ec05 | 891 | else |
9014280d | 892 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s", |
6af84f9f | 893 | PL_oldname, Strerror(eno)); |
4d61ec05 GS |
894 | } |
895 | } | |
a687059c | 896 | } |
18708f5a GS |
897 | if (io && (IoFLAGS(io) & IOf_ARGV)) |
898 | IoFLAGS(io) |= IOf_START; | |
3280af22 NIS |
899 | if (PL_inplace) { |
900 | (void)do_close(PL_argvoutgv,FALSE); | |
7a1c5554 GS |
901 | if (io && (IoFLAGS(io) & IOf_ARGV) |
902 | && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0) | |
903 | { | |
159b6efe | 904 | GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack)); |
18708f5a GS |
905 | setdefout(oldout); |
906 | SvREFCNT_dec(oldout); | |
4608196e | 907 | return NULL; |
18708f5a | 908 | } |
fafc274c | 909 | setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO)); |
a687059c | 910 | } |
4608196e | 911 | return NULL; |
a687059c LW |
912 | } |
913 | ||
517844ec | 914 | /* explicit renamed to avoid C++ conflict -- kja */ |
a687059c | 915 | bool |
864dbfa3 | 916 | Perl_do_close(pTHX_ GV *gv, bool not_implicit) |
a687059c | 917 | { |
97aff369 | 918 | dVAR; |
1193dd27 IZ |
919 | bool retval; |
920 | IO *io; | |
a687059c | 921 | |
79072805 | 922 | if (!gv) |
3280af22 | 923 | gv = PL_argvgv; |
6e592b3a | 924 | if (!gv || !isGV_with_GP(gv)) { |
1d2dff63 | 925 | if (not_implicit) |
93189314 | 926 | SETERRNO(EBADF,SS_IVCHAN); |
c2ab57d4 | 927 | return FALSE; |
99b89507 | 928 | } |
79072805 LW |
929 | io = GvIO(gv); |
930 | if (!io) { /* never opened */ | |
1d2dff63 | 931 | if (not_implicit) { |
51087808 | 932 | report_evil_fh(gv); |
93189314 | 933 | SETERRNO(EBADF,SS_IVCHAN); |
1d2dff63 | 934 | } |
a687059c LW |
935 | return FALSE; |
936 | } | |
f2b5be74 | 937 | retval = io_close(io, not_implicit); |
517844ec | 938 | if (not_implicit) { |
1193dd27 IZ |
939 | IoLINES(io) = 0; |
940 | IoPAGE(io) = 0; | |
941 | IoLINES_LEFT(io) = IoPAGE_LEN(io); | |
942 | } | |
50952442 | 943 | IoTYPE(io) = IoTYPE_CLOSED; |
1193dd27 IZ |
944 | return retval; |
945 | } | |
946 | ||
947 | bool | |
f2b5be74 | 948 | Perl_io_close(pTHX_ IO *io, bool not_implicit) |
1193dd27 | 949 | { |
97aff369 | 950 | dVAR; |
1193dd27 | 951 | bool retval = FALSE; |
1193dd27 | 952 | |
7918f24d NC |
953 | PERL_ARGS_ASSERT_IO_CLOSE; |
954 | ||
8990e307 | 955 | if (IoIFP(io)) { |
50952442 | 956 | if (IoTYPE(io) == IoTYPE_PIPE) { |
4373e329 | 957 | const int status = PerlProc_pclose(IoIFP(io)); |
f2b5be74 | 958 | if (not_implicit) { |
37038d91 | 959 | STATUS_NATIVE_CHILD_SET(status); |
e5218da5 | 960 | retval = (STATUS_UNIX == 0); |
f2b5be74 GS |
961 | } |
962 | else { | |
963 | retval = (status != -1); | |
964 | } | |
a687059c | 965 | } |
50952442 | 966 | else if (IoTYPE(io) == IoTYPE_STD) |
a687059c LW |
967 | retval = TRUE; |
968 | else { | |
8990e307 | 969 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */ |
0bcc34c2 | 970 | const bool prev_err = PerlIO_error(IoOFP(io)); |
e199e3be | 971 | retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err); |
760ac839 | 972 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
c2ab57d4 | 973 | } |
e199e3be | 974 | else { |
0bcc34c2 | 975 | const bool prev_err = PerlIO_error(IoIFP(io)); |
e199e3be RGS |
976 | retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err); |
977 | } | |
a687059c | 978 | } |
4608196e | 979 | IoOFP(io) = IoIFP(io) = NULL; |
79072805 | 980 | } |
f2b5be74 | 981 | else if (not_implicit) { |
93189314 | 982 | SETERRNO(EBADF,SS_IVCHAN); |
20408e3c | 983 | } |
1193dd27 | 984 | |
a687059c LW |
985 | return retval; |
986 | } | |
987 | ||
988 | bool | |
864dbfa3 | 989 | Perl_do_eof(pTHX_ GV *gv) |
a687059c | 990 | { |
97aff369 | 991 | dVAR; |
0bcc34c2 | 992 | register IO * const io = GvIO(gv); |
a687059c | 993 | |
7918f24d NC |
994 | PERL_ARGS_ASSERT_DO_EOF; |
995 | ||
79072805 | 996 | if (!io) |
a687059c | 997 | return TRUE; |
7716c5c5 | 998 | else if (IoTYPE(io) == IoTYPE_WRONLY) |
a5390457 | 999 | report_wrongway_fh(gv, '>'); |
a687059c | 1000 | |
8990e307 | 1001 | while (IoIFP(io)) { |
760ac839 | 1002 | if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */ |
a20bf0c3 | 1003 | if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */ |
760ac839 LW |
1004 | return FALSE; /* this is the most usual case */ |
1005 | } | |
a687059c | 1006 | |
79852593 NC |
1007 | { |
1008 | /* getc and ungetc can stomp on errno */ | |
4ee39169 | 1009 | dSAVE_ERRNO; |
79852593 NC |
1010 | const int ch = PerlIO_getc(IoIFP(io)); |
1011 | if (ch != EOF) { | |
1012 | (void)PerlIO_ungetc(IoIFP(io),ch); | |
4ee39169 | 1013 | RESTORE_ERRNO; |
79852593 NC |
1014 | return FALSE; |
1015 | } | |
4ee39169 | 1016 | RESTORE_ERRNO; |
a687059c | 1017 | } |
fab3f3a7 | 1018 | |
760ac839 | 1019 | if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) { |
a20bf0c3 JH |
1020 | if (PerlIO_get_cnt(IoIFP(io)) < -1) |
1021 | PerlIO_set_cnt(IoIFP(io),-1); | |
760ac839 | 1022 | } |
533c011a | 1023 | if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */ |
ed2c6b9b | 1024 | if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */ |
a687059c LW |
1025 | return TRUE; |
1026 | } | |
1027 | else | |
1028 | return TRUE; /* normal fp, definitely end of file */ | |
1029 | } | |
1030 | return TRUE; | |
1031 | } | |
1032 | ||
5ff3f7a4 | 1033 | Off_t |
864dbfa3 | 1034 | Perl_do_tell(pTHX_ GV *gv) |
a687059c | 1035 | { |
97aff369 | 1036 | dVAR; |
9c9f25b8 | 1037 | IO *const io = GvIO(gv); |
96e4d5b1 | 1038 | register PerlIO *fp; |
a687059c | 1039 | |
7918f24d NC |
1040 | PERL_ARGS_ASSERT_DO_TELL; |
1041 | ||
9c9f25b8 | 1042 | if (io && (fp = IoIFP(io))) { |
bee1dbe2 | 1043 | #ifdef ULTRIX_STDIO_BOTCH |
96e4d5b1 | 1044 | if (PerlIO_eof(fp)) |
1045 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ | |
bee1dbe2 | 1046 | #endif |
8903cb82 | 1047 | return PerlIO_tell(fp); |
96e4d5b1 | 1048 | } |
51087808 | 1049 | report_evil_fh(gv); |
93189314 | 1050 | SETERRNO(EBADF,RMS_IFI); |
5ff3f7a4 | 1051 | return (Off_t)-1; |
a687059c LW |
1052 | } |
1053 | ||
1054 | bool | |
864dbfa3 | 1055 | Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence) |
a687059c | 1056 | { |
97aff369 | 1057 | dVAR; |
9c9f25b8 | 1058 | IO *const io = GvIO(gv); |
137443ea | 1059 | register PerlIO *fp; |
a687059c | 1060 | |
9c9f25b8 | 1061 | if (io && (fp = IoIFP(io))) { |
bee1dbe2 | 1062 | #ifdef ULTRIX_STDIO_BOTCH |
137443ea | 1063 | if (PerlIO_eof(fp)) |
1064 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ | |
bee1dbe2 | 1065 | #endif |
8903cb82 | 1066 | return PerlIO_seek(fp, pos, whence) >= 0; |
137443ea | 1067 | } |
51087808 | 1068 | report_evil_fh(gv); |
93189314 | 1069 | SETERRNO(EBADF,RMS_IFI); |
a687059c LW |
1070 | return FALSE; |
1071 | } | |
1072 | ||
97cc44eb | 1073 | Off_t |
864dbfa3 | 1074 | Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence) |
8903cb82 | 1075 | { |
97aff369 | 1076 | dVAR; |
9c9f25b8 | 1077 | IO *const io = GvIO(gv); |
8903cb82 | 1078 | register PerlIO *fp; |
1079 | ||
7918f24d NC |
1080 | PERL_ARGS_ASSERT_DO_SYSSEEK; |
1081 | ||
9c9f25b8 | 1082 | if (io && (fp = IoIFP(io))) |
3028581b | 1083 | return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence); |
51087808 | 1084 | report_evil_fh(gv); |
93189314 | 1085 | SETERRNO(EBADF,RMS_IFI); |
d9b3e12d | 1086 | return (Off_t)-1; |
8903cb82 | 1087 | } |
1088 | ||
6ff81951 | 1089 | int |
a79b25b7 | 1090 | Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len) |
16fe6d59 GS |
1091 | { |
1092 | int mode = O_BINARY; | |
a79b25b7 | 1093 | if (s) { |
16fe6d59 GS |
1094 | while (*s) { |
1095 | if (*s == ':') { | |
1096 | switch (s[1]) { | |
1097 | case 'r': | |
e963d6d2 | 1098 | if (s[2] == 'a' && s[3] == 'w' |
16fe6d59 GS |
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': | |
e963d6d2 | 1108 | if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f' |
16fe6d59 GS |
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 { | |
4373e329 | 1126 | const char *end; |
16fe6d59 GS |
1127 | fail_discipline: |
1128 | end = strchr(s+1, ':'); | |
1129 | if (!end) | |
1130 | end = s+len; | |
60382766 | 1131 | #ifndef PERLIO_LAYERS |
363c40c4 | 1132 | Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s); |
60382766 | 1133 | #else |
18a33fb5 | 1134 | len -= end-s; |
60382766 NIS |
1135 | s = end; |
1136 | #endif | |
16fe6d59 GS |
1137 | } |
1138 | } | |
1139 | } | |
1140 | return mode; | |
1141 | } | |
1142 | ||
58e24eff | 1143 | #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) |
27da23d5 JH |
1144 | I32 |
1145 | my_chsize(int fd, Off_t length) | |
6eb13c3b | 1146 | { |
58e24eff SH |
1147 | #ifdef F_FREESP |
1148 | /* code courtesy of William Kucharski */ | |
1149 | #define HAS_CHSIZE | |
1150 | ||
c623ac67 | 1151 | Stat_t filebuf; |
6eb13c3b | 1152 | |
3028581b | 1153 | if (PerlLIO_fstat(fd, &filebuf) < 0) |
6eb13c3b LW |
1154 | return -1; |
1155 | ||
1156 | if (filebuf.st_size < length) { | |
1157 | ||
1158 | /* extend file length */ | |
1159 | ||
3028581b | 1160 | if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0) |
6eb13c3b LW |
1161 | return -1; |
1162 | ||
1163 | /* write a "0" byte */ | |
1164 | ||
3028581b | 1165 | if ((PerlLIO_write(fd, "", 1)) != 1) |
6eb13c3b LW |
1166 | return -1; |
1167 | } | |
1168 | else { | |
1169 | /* truncate length */ | |
35da51f7 | 1170 | struct flock fl; |
6eb13c3b LW |
1171 | fl.l_whence = 0; |
1172 | fl.l_len = 0; | |
1173 | fl.l_start = length; | |
a0d0e21e | 1174 | fl.l_type = F_WRLCK; /* write lock on file space */ |
6eb13c3b LW |
1175 | |
1176 | /* | |
a0d0e21e | 1177 | * This relies on the UNDOCUMENTED F_FREESP argument to |
6eb13c3b LW |
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 | ||
a0d0e21e | 1184 | if (fcntl(fd, F_FREESP, &fl) < 0) |
6eb13c3b LW |
1185 | return -1; |
1186 | ||
1187 | } | |
6eb13c3b | 1188 | return 0; |
58e24eff | 1189 | #else |
27da23d5 | 1190 | Perl_croak_nocontext("truncate not implemented"); |
a0d0e21e | 1191 | #endif /* F_FREESP */ |
27da23d5 | 1192 | return -1; |
58e24eff SH |
1193 | } |
1194 | #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */ | |
ff8e2863 | 1195 | |
a687059c | 1196 | bool |
864dbfa3 | 1197 | Perl_do_print(pTHX_ register SV *sv, PerlIO *fp) |
a687059c | 1198 | { |
97aff369 | 1199 | dVAR; |
7918f24d NC |
1200 | |
1201 | PERL_ARGS_ASSERT_DO_PRINT; | |
1202 | ||
79072805 LW |
1203 | /* assuming fp is checked earlier */ |
1204 | if (!sv) | |
1205 | return TRUE; | |
e9950d3b NC |
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; | |
676f44e7 | 1216 | /* Do this first to trigger any overloading. */ |
e9950d3b NC |
1217 | const char *tmps = SvPV_const(sv, len); |
1218 | U8 *tmpbuf = NULL; | |
1219 | bool happy = TRUE; | |
1220 | ||
7d59b7e4 | 1221 | if (PerlIO_isutf8(fp)) { |
676f44e7 NC |
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 | } | |
0876b9a0 KW |
1227 | else if (ckWARN_d(WARN_UTF8)) { |
1228 | (void) check_utf8_print((const U8*) tmps, len); | |
1229 | } | |
7d59b7e4 | 1230 | } |
ae798467 | 1231 | else if (DO_UTF8(sv)) { |
676f44e7 NC |
1232 | STRLEN tmplen = len; |
1233 | bool utf8 = TRUE; | |
35da51f7 | 1234 | U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8); |
676f44e7 NC |
1235 | if (!utf8) { |
1236 | tmpbuf = result; | |
1237 | tmps = (char *) tmpbuf; | |
1238 | len = tmplen; | |
1239 | } | |
1240 | else { | |
1241 | assert((char *)result == tmps); | |
9b387841 | 1242 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), |
21630838 FC |
1243 | "Wide character in %s", |
1244 | PL_op ? OP_DESC(PL_op) : "print" | |
1245 | ); | |
0876b9a0 KW |
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 */ | |
ae798467 NIS |
1249 | } |
1250 | } | |
e9950d3b NC |
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; | |
ff8e2863 | 1261 | } |
a687059c LW |
1262 | } |
1263 | ||
79072805 | 1264 | I32 |
0d7d409d | 1265 | Perl_my_stat_flags(pTHX_ const U32 flags) |
a687059c | 1266 | { |
97aff369 | 1267 | dVAR; |
39644a26 | 1268 | dSP; |
79072805 | 1269 | IO *io; |
2dd78f96 | 1270 | GV* gv; |
79072805 | 1271 | |
533c011a | 1272 | if (PL_op->op_flags & OPf_REF) { |
924508f0 | 1273 | EXTEND(SP,1); |
2dd78f96 | 1274 | gv = cGVOP_gv; |
748a9306 | 1275 | do_fstat: |
5228a96c SP |
1276 | if (gv == PL_defgv) |
1277 | return PL_laststatval; | |
2dd78f96 | 1278 | io = GvIO(gv); |
ad02613c | 1279 | do_fstat_have_io: |
5228a96c SP |
1280 | PL_laststype = OP_STAT; |
1281 | PL_statgv = gv; | |
76f68e9b | 1282 | sv_setpvs(PL_statname, ""); |
5228a96c SP |
1283 | if(io) { |
1284 | if (IoIFP(io)) { | |
1285 | return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache)); | |
1286 | } else if (IoDIRP(io)) { | |
3497a01f | 1287 | return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache)); |
5228a96c | 1288 | } else { |
51087808 | 1289 | report_evil_fh(gv); |
5228a96c SP |
1290 | return (PL_laststatval = -1); |
1291 | } | |
1292 | } else { | |
51087808 | 1293 | report_evil_fh(gv); |
5228a96c SP |
1294 | return (PL_laststatval = -1); |
1295 | } | |
a687059c | 1296 | } |
fbb0b3b3 RGS |
1297 | else if (PL_op->op_private & OPpFT_STACKED) { |
1298 | return PL_laststatval; | |
1299 | } | |
a687059c | 1300 | else { |
0bcc34c2 | 1301 | SV* const sv = POPs; |
4373e329 | 1302 | const char *s; |
4ecd490c | 1303 | STRLEN len; |
79072805 | 1304 | PUTBACK; |
094a3eec | 1305 | if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) { |
748a9306 LW |
1306 | goto do_fstat; |
1307 | } | |
ad02613c | 1308 | else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) { |
a45c7426 | 1309 | io = MUTABLE_IO(SvRV(sv)); |
7f39519f | 1310 | gv = NULL; |
ad02613c SP |
1311 | goto do_fstat_have_io; |
1312 | } | |
748a9306 | 1313 | |
0d7d409d | 1314 | s = SvPV_flags_const(sv, len, flags); |
a0714e2c | 1315 | PL_statgv = NULL; |
4ecd490c | 1316 | sv_setpvn(PL_statname, s, len); |
95a20fc0 | 1317 | s = SvPVX_const(PL_statname); /* s now NUL-terminated */ |
3280af22 NIS |
1318 | PL_laststype = OP_STAT; |
1319 | PL_laststatval = PerlLIO_stat(s, &PL_statcache); | |
ce44635a | 1320 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n')) |
9014280d | 1321 | Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat"); |
3280af22 | 1322 | return PL_laststatval; |
a687059c LW |
1323 | } |
1324 | } | |
1325 | ||
fbb0b3b3 | 1326 | |
79072805 | 1327 | I32 |
0d7d409d | 1328 | Perl_my_lstat_flags(pTHX_ const U32 flags) |
c623bd54 | 1329 | { |
97aff369 | 1330 | dVAR; |
0bd48802 | 1331 | static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat"; |
39644a26 | 1332 | dSP; |
79072805 | 1333 | SV *sv; |
b16276bb | 1334 | const char *file; |
533c011a | 1335 | if (PL_op->op_flags & OPf_REF) { |
924508f0 | 1336 | EXTEND(SP,1); |
638eceb6 | 1337 | if (cGVOP_gv == PL_defgv) { |
3280af22 | 1338 | if (PL_laststype != OP_LSTAT) |
fbb0b3b3 | 1339 | Perl_croak(aTHX_ no_prev_lstat); |
3280af22 | 1340 | return PL_laststatval; |
fe14fcc3 | 1341 | } |
5d3e98de | 1342 | if (ckWARN(WARN_IO)) { |
d0c0e7dd FC |
1343 | Perl_warner(aTHX_ packWARN(WARN_IO), |
1344 | "Use of -l on filehandle %"HEKf, | |
1345 | HEKfARG(GvENAME_HEK(cGVOP_gv))); | |
5d3e98de | 1346 | } |
ca867162 | 1347 | return (PL_laststatval = -1); |
fe14fcc3 | 1348 | } |
1f26655e FC |
1349 | else if (PL_op->op_private & OPpFT_STACKED) { |
1350 | if (PL_laststype != OP_LSTAT) | |
fbb0b3b3 | 1351 | Perl_croak(aTHX_ no_prev_lstat); |
1f26655e FC |
1352 | return PL_laststatval; |
1353 | } | |
c623bd54 | 1354 | |
3280af22 | 1355 | PL_laststype = OP_LSTAT; |
a0714e2c | 1356 | PL_statgv = NULL; |
79072805 LW |
1357 | sv = POPs; |
1358 | PUTBACK; | |
0d7d409d | 1359 | file = SvPV_flags_const_nolen(sv, flags); |
b16276bb VP |
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')) | |
9014280d | 1363 | Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat"); |
3280af22 | 1364 | return PL_laststatval; |
c623bd54 LW |
1365 | } |
1366 | ||
a0f2c8ec JD |
1367 | static void |
1368 | S_exec_failed(pTHX_ const char *cmd, int fd, int do_report) | |
1369 | { | |
1370 | const int e = errno; | |
7918f24d | 1371 | PERL_ARGS_ASSERT_EXEC_FAILED; |
a0f2c8ec JD |
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 | ||
d5a9bfb0 | 1381 | bool |
2aa1486d GS |
1382 | Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp, |
1383 | int fd, int do_report) | |
d5a9bfb0 | 1384 | { |
27da23d5 | 1385 | dVAR; |
7918f24d | 1386 | PERL_ARGS_ASSERT_DO_AEXEC5; |
e37778c2 | 1387 | #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__) |
cd39f2b6 JH |
1388 | Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system"); |
1389 | #else | |
79072805 | 1390 | if (sp > mark) { |
360ea906 | 1391 | const char **a; |
6136c704 | 1392 | const char *tmps = NULL; |
360ea906 | 1393 | Newx(PL_Argv, sp - mark + 1, const char*); |
c3c5fad7 | 1394 | a = PL_Argv; |
890ce7af | 1395 | |
79072805 LW |
1396 | while (++mark <= sp) { |
1397 | if (*mark) | |
360ea906 | 1398 | *a++ = SvPV_nolen_const(*mark); |
a687059c LW |
1399 | else |
1400 | *a++ = ""; | |
1401 | } | |
6136c704 | 1402 | *a = NULL; |
91b2752f | 1403 | if (really) |
e62f0680 | 1404 | tmps = SvPV_nolen_const(really); |
91b2752f RG |
1405 | if ((!really && *PL_Argv[0] != '/') || |
1406 | (really && *tmps != '/')) /* will execvp use PATH? */ | |
79072805 | 1407 | TAINT_ENV(); /* testing IFS here is overkill, probably */ |
b35112e7 | 1408 | PERL_FPU_PRE_EXEC |
91b2752f | 1409 | if (really && *tmps) |
b4748376 | 1410 | PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv)); |
a687059c | 1411 | else |
b4748376 | 1412 | PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv)); |
b35112e7 | 1413 | PERL_FPU_POST_EXEC |
a0f2c8ec | 1414 | S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report); |
a687059c | 1415 | } |
bee1dbe2 | 1416 | do_execfree(); |
cd39f2b6 | 1417 | #endif |
a687059c LW |
1418 | return FALSE; |
1419 | } | |
1420 | ||
fe14fcc3 | 1421 | void |
864dbfa3 | 1422 | Perl_do_execfree(pTHX) |
ff8e2863 | 1423 | { |
97aff369 | 1424 | dVAR; |
43c5f42d | 1425 | Safefree(PL_Argv); |
4608196e | 1426 | PL_Argv = NULL; |
43c5f42d | 1427 | Safefree(PL_Cmd); |
bd61b366 | 1428 | PL_Cmd = NULL; |
ff8e2863 LW |
1429 | } |
1430 | ||
9555a685 | 1431 | #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION |
e446cec8 IZ |
1432 | |
1433 | bool | |
2fbb330f | 1434 | Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report) |
e446cec8 | 1435 | { |
27da23d5 | 1436 | dVAR; |
360ea906 | 1437 | register const char **a; |
a687059c | 1438 | register char *s; |
15db3ae2 | 1439 | char *buf; |
2fbb330f | 1440 | char *cmd; |
2fbb330f | 1441 | /* Make a copy so we can change it */ |
6fca0082 | 1442 | const Size_t cmdlen = strlen(incmd) + 1; |
7918f24d NC |
1443 | |
1444 | PERL_ARGS_ASSERT_DO_EXEC3; | |
1445 | ||
15db3ae2 DM |
1446 | Newx(buf, cmdlen, char); |
1447 | cmd = buf; | |
cfff9797 | 1448 | memcpy(cmd, incmd, cmdlen); |
a687059c | 1449 | |
748a9306 LW |
1450 | while (*cmd && isSPACE(*cmd)) |
1451 | cmd++; | |
1452 | ||
a687059c LW |
1453 | /* save an extra exec if possible */ |
1454 | ||
bf38876a | 1455 | #ifdef CSH |
d05c1ba0 | 1456 | { |
0c19750d | 1457 | char flags[PERL_FLAGS_MAX]; |
d05c1ba0 JH |
1458 | if (strnEQ(cmd,PL_cshname,PL_cshlen) && |
1459 | strnEQ(cmd+PL_cshlen," -c",3)) { | |
28f0d0ec | 1460 | my_strlcpy(flags, "-c", PERL_FLAGS_MAX); |
d05c1ba0 JH |
1461 | s = cmd+PL_cshlen+3; |
1462 | if (*s == 'f') { | |
1463 | s++; | |
28f0d0ec | 1464 | my_strlcat(flags, "f", PERL_FLAGS_MAX - 2); |
d05c1ba0 JH |
1465 | } |
1466 | if (*s == ' ') | |
1467 | s++; | |
1468 | if (*s++ == '\'') { | |
0bcc34c2 | 1469 | char * const ncmd = s; |
d05c1ba0 JH |
1470 | |
1471 | while (*s) | |
1472 | s++; | |
1473 | if (s[-1] == '\n') | |
1474 | *--s = '\0'; | |
1475 | if (s[-1] == '\'') { | |
1476 | *--s = '\0'; | |
b35112e7 | 1477 | PERL_FPU_PRE_EXEC |
7c0dd7ca | 1478 | PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL); |
b35112e7 | 1479 | PERL_FPU_POST_EXEC |
d05c1ba0 | 1480 | *s = '\''; |
a0f2c8ec | 1481 | S_exec_failed(aTHX_ PL_cshname, fd, do_report); |
15db3ae2 | 1482 | Safefree(buf); |
d05c1ba0 JH |
1483 | return FALSE; |
1484 | } | |
1485 | } | |
a687059c LW |
1486 | } |
1487 | } | |
bf38876a | 1488 | #endif /* CSH */ |
a687059c LW |
1489 | |
1490 | /* see if there are shell metacharacters in it */ | |
1491 | ||
748a9306 LW |
1492 | if (*cmd == '.' && isSPACE(cmd[1])) |
1493 | goto doshell; | |
1494 | ||
1495 | if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4])) | |
1496 | goto doshell; | |
1497 | ||
294b3b39 AL |
1498 | s = cmd; |
1499 | while (isALNUM(*s)) | |
1500 | s++; /* catch VAR=val gizmo */ | |
63f2c1e1 LW |
1501 | if (*s == '=') |
1502 | goto doshell; | |
748a9306 | 1503 | |
a687059c | 1504 | for (s = cmd; *s; s++) { |
d05c1ba0 JH |
1505 | if (*s != ' ' && !isALPHA(*s) && |
1506 | strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) { | |
a687059c LW |
1507 | if (*s == '\n' && !s[1]) { |
1508 | *s = '\0'; | |
1509 | break; | |
1510 | } | |
603a98b0 IZ |
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 | { | |
6867be6d | 1516 | const char *t = s + 3; |
603a98b0 IZ |
1517 | |
1518 | while (*t && isSPACE(*t)) | |
1519 | ++t; | |
943bbd07 | 1520 | if (!*t && (PerlLIO_dup2(1,2) != -1)) { |
603a98b0 IZ |
1521 | s[-2] = '\0'; |
1522 | break; | |
1523 | } | |
1524 | } | |
a687059c | 1525 | doshell: |
b35112e7 | 1526 | PERL_FPU_PRE_EXEC |
7c0dd7ca | 1527 | PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL); |
b35112e7 | 1528 | PERL_FPU_POST_EXEC |
a0f2c8ec | 1529 | S_exec_failed(aTHX_ PL_sh_path, fd, do_report); |
15db3ae2 | 1530 | Safefree(buf); |
a687059c LW |
1531 | return FALSE; |
1532 | } | |
1533 | } | |
748a9306 | 1534 | |
360ea906 | 1535 | Newx(PL_Argv, (s - cmd) / 2 + 2, const char*); |
3280af22 NIS |
1536 | PL_Cmd = savepvn(cmd, s-cmd); |
1537 | a = PL_Argv; | |
1538 | for (s = PL_Cmd; *s;) { | |
294b3b39 AL |
1539 | while (isSPACE(*s)) |
1540 | s++; | |
a687059c LW |
1541 | if (*s) |
1542 | *(a++) = s; | |
294b3b39 AL |
1543 | while (*s && !isSPACE(*s)) |
1544 | s++; | |
a687059c LW |
1545 | if (*s) |
1546 | *s++ = '\0'; | |
1547 | } | |
6136c704 | 1548 | *a = NULL; |
3280af22 | 1549 | if (PL_Argv[0]) { |
b35112e7 | 1550 | PERL_FPU_PRE_EXEC |
360ea906 | 1551 | PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv)); |
b35112e7 | 1552 | PERL_FPU_POST_EXEC |
b1248f16 | 1553 | if (errno == ENOEXEC) { /* for system V NIH syndrome */ |
ff8e2863 | 1554 | do_execfree(); |
a687059c | 1555 | goto doshell; |
b1248f16 | 1556 | } |
a0f2c8ec | 1557 | S_exec_failed(aTHX_ PL_Argv[0], fd, do_report); |
a687059c | 1558 | } |
ff8e2863 | 1559 | do_execfree(); |
15db3ae2 | 1560 | Safefree(buf); |
a687059c LW |
1561 | return FALSE; |
1562 | } | |
1563 | ||
6890e559 | 1564 | #endif /* OS2 || WIN32 */ |
760ac839 | 1565 | |
79072805 | 1566 | I32 |
864dbfa3 | 1567 | Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp) |
a687059c | 1568 | { |
97aff369 | 1569 | dVAR; |
79072805 | 1570 | register I32 val; |
79072805 | 1571 | register I32 tot = 0; |
4634a855 | 1572 | const char *const what = PL_op_name[type]; |
5c144d81 | 1573 | const char *s; |
84c7b88c | 1574 | STRLEN len; |
890ce7af | 1575 | SV ** const oldmark = mark; |
a687059c | 1576 | |
7918f24d NC |
1577 | PERL_ARGS_ASSERT_APPLY; |
1578 | ||
1444765e NC |
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) | |
4634a855 | 1584 | Perl_die(aTHX_ PL_no_func, what); |
1444765e NC |
1585 | #endif |
1586 | #ifndef HAS_CHOWN | |
1587 | if (type == OP_CHOWN) | |
4634a855 | 1588 | Perl_die(aTHX_ PL_no_func, what); |
1444765e NC |
1589 | #endif |
1590 | ||
1591 | ||
20408e3c | 1592 | #define APPLY_TAINT_PROPER() \ |
3280af22 | 1593 | STMT_START { \ |
17406bd6 | 1594 | if (PL_tainted) { TAINT_PROPER(what); } \ |
873ef191 | 1595 | } STMT_END |
20408e3c GS |
1596 | |
1597 | /* This is a first heuristic; it doesn't catch tainting magic. */ | |
3280af22 | 1598 | if (PL_tainting) { |
463ee0b2 | 1599 | while (++mark <= sp) { |
bbce6d69 | 1600 | if (SvTAINTED(*mark)) { |
1601 | TAINT; | |
1602 | break; | |
1603 | } | |
463ee0b2 LW |
1604 | } |
1605 | mark = oldmark; | |
1606 | } | |
a687059c | 1607 | switch (type) { |
79072805 | 1608 | case OP_CHMOD: |
20408e3c | 1609 | APPLY_TAINT_PROPER(); |
79072805 | 1610 | if (++mark <= sp) { |
4ea561bc | 1611 | val = SvIV(*mark); |
20408e3c GS |
1612 | APPLY_TAINT_PROPER(); |
1613 | tot = sp - mark; | |
79072805 | 1614 | while (++mark <= sp) { |
c4aca7d0 | 1615 | GV* gv; |
2ea1cce7 | 1616 | if ((gv = MAYBE_DEREF_GV(*mark))) { |
c4aca7d0 GA |
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 | |
b9c6780e | 1623 | Perl_die(aTHX_ PL_no_func, "fchmod"); |
c4aca7d0 GA |
1624 | #endif |
1625 | } | |
1626 | else { | |
1627 | tot--; | |
1628 | } | |
1629 | } | |
c4aca7d0 | 1630 | else { |
93564729 | 1631 | const char *name = SvPV_nomg_const_nolen(*mark); |
c4aca7d0 GA |
1632 | APPLY_TAINT_PROPER(); |
1633 | if (PerlLIO_chmod(name, val)) | |
1634 | tot--; | |
1635 | } | |
a687059c LW |
1636 | } |
1637 | } | |
1638 | break; | |
fe14fcc3 | 1639 | #ifdef HAS_CHOWN |
79072805 | 1640 | case OP_CHOWN: |
20408e3c | 1641 | APPLY_TAINT_PROPER(); |
79072805 | 1642 | if (sp - mark > 2) { |
6867be6d | 1643 | register I32 val2; |
463ee0b2 LW |
1644 | val = SvIVx(*++mark); |
1645 | val2 = SvIVx(*++mark); | |
20408e3c | 1646 | APPLY_TAINT_PROPER(); |
a0d0e21e | 1647 | tot = sp - mark; |
79072805 | 1648 | while (++mark <= sp) { |
c4aca7d0 | 1649 | GV* gv; |
2ea1cce7 | 1650 | if ((gv = MAYBE_DEREF_GV(*mark))) { |
c4aca7d0 GA |
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 | |
b9c6780e | 1657 | Perl_die(aTHX_ PL_no_func, "fchown"); |
c4aca7d0 GA |
1658 | #endif |
1659 | } | |
1660 | else { | |
1661 | tot--; | |
1662 | } | |
1663 | } | |
c4aca7d0 | 1664 | else { |
93564729 | 1665 | const char *name = SvPV_nomg_const_nolen(*mark); |
c4aca7d0 GA |
1666 | APPLY_TAINT_PROPER(); |
1667 | if (PerlLIO_chown(name, val, val2)) | |
1668 | tot--; | |
1669 | } | |
a687059c LW |
1670 | } |
1671 | } | |
1672 | break; | |
b1248f16 | 1673 | #endif |
a1d180c4 | 1674 | /* |
dd64f1c3 AD |
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 | */ | |
fe14fcc3 | 1680 | #ifdef HAS_KILL |
79072805 | 1681 | case OP_KILL: |
20408e3c | 1682 | APPLY_TAINT_PROPER(); |
55497cff | 1683 | if (mark == sp) |
1684 | break; | |
84c7b88c | 1685 | s = SvPVx_const(*++mark, len); |
e02bfb16 | 1686 | if (isALPHA(*s)) { |
84c7b88c | 1687 | if (*s == 'S' && s[1] == 'I' && s[2] == 'G') { |
79072805 | 1688 | s += 3; |
84c7b88c BF |
1689 | len -= 3; |
1690 | } | |
1691 | if ((val = whichsig_pvn(s, len)) < 0) | |
1692 | Perl_croak(aTHX_ "Unrecognized signal name \"%"SVf"\"", SVfARG(*mark)); | |
79072805 LW |
1693 | } |
1694 | else | |
4ea561bc | 1695 | val = SvIV(*mark); |
20408e3c GS |
1696 | APPLY_TAINT_PROPER(); |
1697 | tot = sp - mark; | |
3595fcef | 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) { | |
5f521955 JH |
1708 | I32 proc; |
1709 | register unsigned long int __vmssts; | |
8af710eb | 1710 | SvGETMAGIC(*mark); |
e2c0f81f DG |
1711 | if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark))) |
1712 | Perl_croak(aTHX_ "Can't kill a non-numeric process ID"); | |
8af710eb | 1713 | proc = SvIV_nomg(*mark); |
20408e3c | 1714 | APPLY_TAINT_PROPER(); |
3595fcef | 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 | } | |
8165faea | 1730 | PERL_ASYNC_CHECK(); |
3595fcef | 1731 | break; |
1732 | } | |
1733 | #endif | |
79072805 LW |
1734 | if (val < 0) { |
1735 | val = -val; | |
1736 | while (++mark <= sp) { | |
5f521955 | 1737 | I32 proc; |
8af710eb | 1738 | SvGETMAGIC(*mark); |
e2c0f81f DG |
1739 | if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark))) |
1740 | Perl_croak(aTHX_ "Can't kill a non-numeric process ID"); | |
8af710eb | 1741 | proc = SvIV_nomg(*mark); |
20408e3c | 1742 | APPLY_TAINT_PROPER(); |
fe14fcc3 | 1743 | #ifdef HAS_KILLPG |
3028581b | 1744 | if (PerlProc_killpg(proc,val)) /* BSD */ |
a687059c | 1745 | #else |
3028581b | 1746 | if (PerlProc_kill(-proc,val)) /* SYSV */ |
a687059c | 1747 | #endif |
79072805 | 1748 | tot--; |
a687059c | 1749 | } |
79072805 LW |
1750 | } |
1751 | else { | |
1752 | while (++mark <= sp) { | |
5f521955 | 1753 | I32 proc; |
8af710eb | 1754 | SvGETMAGIC(*mark); |
e2c0f81f DG |
1755 | if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark))) |
1756 | Perl_croak(aTHX_ "Can't kill a non-numeric process ID"); | |
8af710eb | 1757 | proc = SvIV_nomg(*mark); |
20408e3c GS |
1758 | APPLY_TAINT_PROPER(); |
1759 | if (PerlProc_kill(proc, val)) | |
79072805 | 1760 | tot--; |
a687059c LW |
1761 | } |
1762 | } | |
8165faea | 1763 | PERL_ASYNC_CHECK(); |
a687059c | 1764 | break; |
b1248f16 | 1765 | #endif |
79072805 | 1766 | case OP_UNLINK: |
20408e3c | 1767 | APPLY_TAINT_PROPER(); |
79072805 LW |
1768 | tot = sp - mark; |
1769 | while (++mark <= sp) { | |
e62f0680 | 1770 | s = SvPV_nolen_const(*mark); |
20408e3c | 1771 | APPLY_TAINT_PROPER(); |
3280af22 | 1772 | if (PL_euid || PL_unsafe) { |
b8ffc8df | 1773 | if (UNLINK(s)) |
a687059c LW |
1774 | tot--; |
1775 | } | |
1776 | else { /* don't let root wipe out directories without -U */ | |
3280af22 | 1777 | if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode)) |
a687059c LW |
1778 | tot--; |
1779 | else { | |
b8ffc8df | 1780 | if (UNLINK(s)) |
a687059c LW |
1781 | tot--; |
1782 | } | |
1783 | } | |
1784 | } | |
1785 | break; | |
e96b369d | 1786 | #if defined(HAS_UTIME) || defined(HAS_FUTIMES) |
79072805 | 1787 | case OP_UTIME: |
20408e3c | 1788 | APPLY_TAINT_PROPER(); |
79072805 | 1789 | if (sp - mark > 2) { |
e96b369d GA |
1790 | #if defined(HAS_FUTIMES) |
1791 | struct timeval utbuf[2]; | |
1792 | void *utbufp = utbuf; | |
1793 | #elif defined(I_UTIME) || defined(VMS) | |
663a0e37 | 1794 | struct utimbuf utbuf; |
07409e01 | 1795 | struct utimbuf *utbufp = &utbuf; |
663a0e37 | 1796 | #else |
a687059c | 1797 | struct { |
dd2821f6 GS |
1798 | Time_t actime; |
1799 | Time_t modtime; | |
a687059c | 1800 | } utbuf; |
07409e01 | 1801 | void *utbufp = &utbuf; |
663a0e37 | 1802 | #endif |
a687059c | 1803 | |
0bcc34c2 AL |
1804 | SV* const accessed = *++mark; |
1805 | SV* const modified = *++mark; | |
c6f7b413 | 1806 | |
6ec06612 SB |
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". */ | |
c6f7b413 RS |
1810 | |
1811 | if ( accessed == &PL_sv_undef && modified == &PL_sv_undef ) | |
6ec06612 SB |
1812 | utbufp = NULL; |
1813 | else { | |
1814 | Zero(&utbuf, sizeof utbuf, char); | |
e96b369d | 1815 | #ifdef HAS_FUTIMES |
4ea561bc | 1816 | utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */ |
e96b369d | 1817 | utbuf[0].tv_usec = 0; |
4ea561bc | 1818 | utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */ |
e96b369d GA |
1819 | utbuf[1].tv_usec = 0; |
1820 | #elif defined(BIG_TIME) | |
4ea561bc NC |
1821 | utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */ |
1822 | utbuf.modtime = (Time_t)SvNV(modified); /* time modified */ | |
517844ec | 1823 | #else |
4ea561bc NC |
1824 | utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */ |
1825 | utbuf.modtime = (Time_t)SvIV(modified); /* time modified */ | |
517844ec | 1826 | #endif |
6ec06612 | 1827 | } |
4373e329 | 1828 | APPLY_TAINT_PROPER(); |
79072805 LW |
1829 | tot = sp - mark; |
1830 | while (++mark <= sp) { | |
e96b369d | 1831 | GV* gv; |
64617da9 | 1832 | if ((gv = MAYBE_DEREF_GV(*mark))) { |
e96b369d GA |
1833 | if (GvIO(gv) && IoIFP(GvIOp(gv))) { |
1834 | #ifdef HAS_FUTIMES | |
1835 | APPLY_TAINT_PROPER(); | |
8b7231d9 SP |
1836 | if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))), |
1837 | (struct timeval *) utbufp)) | |
e96b369d GA |
1838 | tot--; |
1839 | #else | |
1840 | Perl_die(aTHX_ PL_no_func, "futimes"); | |
1841 | #endif | |
1842 | } | |
1843 | else { | |
1844 | tot--; | |
1845 | } | |
1846 | } | |
e96b369d | 1847 | else { |
64617da9 | 1848 | const char * const name = SvPV_nomg_const_nolen(*mark); |
e96b369d GA |
1849 | APPLY_TAINT_PROPER(); |
1850 | #ifdef HAS_FUTIMES | |
8b7231d9 | 1851 | if (utimes(name, (struct timeval *)utbufp)) |
e96b369d GA |
1852 | #else |
1853 | if (PerlLIO_utime(name, utbufp)) | |
1854 | #endif | |
1855 | tot--; | |
1856 | } | |
1857 | ||
a687059c | 1858 | } |
a687059c LW |
1859 | } |
1860 | else | |
79072805 | 1861 | tot = 0; |
a687059c | 1862 | break; |
a0d0e21e | 1863 | #endif |
a687059c LW |
1864 | } |
1865 | return tot; | |
20408e3c | 1866 | |
20408e3c | 1867 | #undef APPLY_TAINT_PROPER |
a687059c LW |
1868 | } |
1869 | ||
1870 | /* Do the permissions allow some operation? Assumes statcache already set. */ | |
a0d0e21e | 1871 | #ifndef VMS /* VMS' cando is in vms.c */ |
7f4774ae | 1872 | bool |
ae1951c1 NC |
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 | */ | |
a687059c | 1877 | { |
97aff369 | 1878 | dVAR; |
7918f24d NC |
1879 | |
1880 | PERL_ARGS_ASSERT_CANDO; | |
1881 | ||
bee1dbe2 | 1882 | #ifdef DOSISH |
fe14fcc3 LW |
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 | |
486ec47a | 1886 | * by the Microsoft C library stat() function for the following: |
fe14fcc3 LW |
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 | ||
bee1dbe2 LW |
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 | */ | |
7f4774ae | 1904 | return (mode & statbufp->st_mode) ? TRUE : FALSE; |
fe14fcc3 | 1905 | |
55497cff | 1906 | #else /* ! DOSISH */ |
b595cd4b RU |
1907 | # ifdef __CYGWIN__ |
1908 | if (ingroup(544,effective)) { /* member of Administrators */ | |
1909 | # else | |
3280af22 | 1910 | if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */ |
b595cd4b | 1911 | # endif |
7f4774ae | 1912 | if (mode == S_IXUSR) { |
c623bd54 | 1913 | if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode)) |
a687059c LW |
1914 | return TRUE; |
1915 | } | |
1916 | else | |
1917 | return TRUE; /* root reads and writes anything */ | |
1918 | return FALSE; | |
1919 | } | |
3280af22 | 1920 | if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) { |
7f4774ae | 1921 | if (statbufp->st_mode & mode) |
a687059c LW |
1922 | return TRUE; /* ok as "user" */ |
1923 | } | |
d8eceb89 | 1924 | else if (ingroup(statbufp->st_gid,effective)) { |
7f4774ae | 1925 | if (statbufp->st_mode & mode >> 3) |
a687059c LW |
1926 | return TRUE; /* ok as "group" */ |
1927 | } | |
7f4774ae | 1928 | else if (statbufp->st_mode & mode >> 6) |
a687059c LW |
1929 | return TRUE; /* ok as "other" */ |
1930 | return FALSE; | |
55497cff | 1931 | #endif /* ! DOSISH */ |
a687059c | 1932 | } |
a0d0e21e | 1933 | #endif /* ! VMS */ |
a687059c | 1934 | |
1f676739 | 1935 | static bool |
0da8eb3a | 1936 | S_ingroup(pTHX_ Gid_t testgid, bool effective) |
a687059c | 1937 | { |
97aff369 | 1938 | dVAR; |
3280af22 | 1939 | if (testgid == (effective ? PL_egid : PL_gid)) |
a687059c | 1940 | return TRUE; |
fe14fcc3 | 1941 | #ifdef HAS_GETGROUPS |
a687059c | 1942 | { |
331b57bc | 1943 | Groups_t *gary = NULL; |
79072805 | 1944 | I32 anum; |
331b57bc | 1945 | bool rc = FALSE; |
a687059c | 1946 | |
331b57bc SP |
1947 | anum = getgroups(0, gary); |
1948 | Newx(gary, anum, Groups_t); | |
1949 | anum = getgroups(anum, gary); | |
a687059c | 1950 | while (--anum >= 0) |
331b57bc SP |
1951 | if (gary[anum] == testgid) { |
1952 | rc = TRUE; | |
1953 | break; | |
1954 | } | |
1955 | ||
1956 | Safefree(gary); | |
1957 | return rc; | |
a687059c | 1958 | } |
c685562b | 1959 | #else |
a687059c | 1960 | return FALSE; |
cd39f2b6 | 1961 | #endif |
a687059c | 1962 | } |
c2ab57d4 | 1963 | |
fe14fcc3 | 1964 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
c2ab57d4 | 1965 | |
79072805 | 1966 | I32 |
864dbfa3 | 1967 | Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 1968 | { |
97aff369 | 1969 | dVAR; |
0bcc34c2 | 1970 | const key_t key = (key_t)SvNVx(*++mark); |
c3312966 | 1971 | SV *nsv = optype == OP_MSGGET ? NULL : *++mark; |
6867be6d | 1972 | const I32 flags = SvIVx(*++mark); |
294a48e9 | 1973 | |
7918f24d | 1974 | PERL_ARGS_ASSERT_DO_IPCGET; |
294a48e9 | 1975 | PERL_UNUSED_ARG(sp); |
c2ab57d4 | 1976 | |
748a9306 | 1977 | SETERRNO(0,0); |
c2ab57d4 LW |
1978 | switch (optype) |
1979 | { | |
fe14fcc3 | 1980 | #ifdef HAS_MSG |
79072805 | 1981 | case OP_MSGGET: |
c2ab57d4 | 1982 | return msgget(key, flags); |
e5d73d77 | 1983 | #endif |
fe14fcc3 | 1984 | #ifdef HAS_SEM |
79072805 | 1985 | case OP_SEMGET: |
c3312966 | 1986 | return semget(key, (int) SvIV(nsv), flags); |
e5d73d77 | 1987 | #endif |
fe14fcc3 | 1988 | #ifdef HAS_SHM |
79072805 | 1989 | case OP_SHMGET: |
c3312966 | 1990 | return shmget(key, (size_t) SvUV(nsv), flags); |
e5d73d77 | 1991 | #endif |
fe14fcc3 | 1992 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 | 1993 | default: |
fe13d51d | 1994 | /* diag_listed_as: msg%s not implemented */ |
cea2e8a9 | 1995 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 | 1996 | #endif |
c2ab57d4 LW |
1997 | } |
1998 | return -1; /* should never happen */ | |
1999 | } | |
2000 | ||
79072805 | 2001 | I32 |
864dbfa3 | 2002 | Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 2003 | { |
97aff369 | 2004 | dVAR; |
c2ab57d4 | 2005 | char *a; |
a0d0e21e | 2006 | I32 ret = -1; |
6867be6d | 2007 | const I32 id = SvIVx(*++mark); |
95b63a38 | 2008 | #ifdef Semctl |
6867be6d | 2009 | const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0; |
95b63a38 | 2010 | #endif |
6867be6d | 2011 | const I32 cmd = SvIVx(*++mark); |
0bcc34c2 AL |
2012 | SV * const astr = *++mark; |
2013 | STRLEN infosize = 0; | |
2014 | I32 getinfo = (cmd == IPC_STAT); | |
c2ab57d4 | 2015 | |
7918f24d | 2016 | PERL_ARGS_ASSERT_DO_IPCCTL; |
0bcc34c2 | 2017 | PERL_UNUSED_ARG(sp); |
c2ab57d4 LW |
2018 | |
2019 | switch (optype) | |
2020 | { | |
fe14fcc3 | 2021 | #ifdef HAS_MSG |
79072805 | 2022 | case OP_MSGCTL: |
c2ab57d4 LW |
2023 | if (cmd == IPC_STAT || cmd == IPC_SET) |
2024 | infosize = sizeof(struct msqid_ds); | |
2025 | break; | |
e5d73d77 | 2026 | #endif |
fe14fcc3 | 2027 | #ifdef HAS_SHM |
79072805 | 2028 | case OP_SHMCTL: |
c2ab57d4 LW |
2029 | if (cmd == IPC_STAT || cmd == IPC_SET) |
2030 | infosize = sizeof(struct shmid_ds); | |
2031 | break; | |
e5d73d77 | 2032 | #endif |
fe14fcc3 | 2033 | #ifdef HAS_SEM |
79072805 | 2034 | case OP_SEMCTL: |
39398f3f | 2035 | #ifdef Semctl |
c2ab57d4 LW |
2036 | if (cmd == IPC_STAT || cmd == IPC_SET) |
2037 | infosize = sizeof(struct semid_ds); | |
2038 | else if (cmd == GETALL || cmd == SETALL) | |
2039 | { | |
8e591e46 | 2040 | struct semid_ds semds; |
bd89102f | 2041 | union semun semun; |
e6f0bdd6 GS |
2042 | #ifdef EXTRA_F_IN_SEMUN_BUF |
2043 | semun.buff = &semds; | |
2044 | #else | |
84902520 | 2045 | semun.buf = &semds; |
e6f0bdd6 | 2046 | #endif |
c2ab57d4 | 2047 | getinfo = (cmd == GETALL); |
9b89d93d GB |
2048 | if (Semctl(id, 0, IPC_STAT, semun) == -1) |
2049 | return -1; | |
6e21c824 LW |
2050 | infosize = semds.sem_nsems * sizeof(short); |
2051 | /* "short" is technically wrong but much more portable | |
2052 | than guessing about u_?short(_t)? */ | |
c2ab57d4 | 2053 | } |
39398f3f | 2054 | #else |
fe13d51d | 2055 | /* diag_listed_as: sem%s not implemented */ |
cea2e8a9 | 2056 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f | 2057 | #endif |
c2ab57d4 | 2058 | break; |
e5d73d77 | 2059 | #endif |
fe14fcc3 | 2060 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 | 2061 | default: |
fe13d51d | 2062 | /* diag_listed_as: shm%s not implemented */ |
cea2e8a9 | 2063 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 | 2064 | #endif |
c2ab57d4 LW |
2065 | } |
2066 | ||
2067 | if (infosize) | |
2068 | { | |
2069 | if (getinfo) | |
2070 | { | |
93524f2b | 2071 | SvPV_force_nolen(astr); |
a0d0e21e | 2072 | a = SvGROW(astr, infosize+1); |
c2ab57d4 LW |
2073 | } |
2074 | else | |
2075 | { | |
93524f2b | 2076 | STRLEN len; |
463ee0b2 LW |
2077 | a = SvPV(astr, len); |
2078 | if (len != infosize) | |
cea2e8a9 | 2079 | Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld", |
4ec43091 JH |
2080 | PL_op_desc[optype], |
2081 | (unsigned long)len, | |
2082 | (long)infosize); | |
c2ab57d4 LW |
2083 | } |
2084 | } | |
2085 | else | |
2086 | { | |
0bcc34c2 | 2087 | const IV i = SvIV(astr); |
56431972 | 2088 | a = INT2PTR(char *,i); /* ouch */ |
c2ab57d4 | 2089 | } |
748a9306 | 2090 | SETERRNO(0,0); |
c2ab57d4 LW |
2091 | switch (optype) |
2092 | { | |
fe14fcc3 | 2093 | #ifdef HAS_MSG |
79072805 | 2094 | case OP_MSGCTL: |
bee1dbe2 | 2095 | ret = msgctl(id, cmd, (struct msqid_ds *)a); |
c2ab57d4 | 2096 | break; |
e5d73d77 | 2097 | #endif |
fe14fcc3 | 2098 | #ifdef HAS_SEM |
bd89102f | 2099 | case OP_SEMCTL: { |
39398f3f | 2100 | #ifdef Semctl |
bd89102f AD |
2101 | union semun unsemds; |
2102 | ||
e6f0bdd6 GS |
2103 | #ifdef EXTRA_F_IN_SEMUN_BUF |
2104 | unsemds.buff = (struct semid_ds *)a; | |
2105 | #else | |
bd89102f | 2106 | unsemds.buf = (struct semid_ds *)a; |
e6f0bdd6 | 2107 | #endif |
bd89102f | 2108 | ret = Semctl(id, n, cmd, unsemds); |
39398f3f | 2109 | #else |
fe13d51d | 2110 | /* diag_listed_as: sem%s not implemented */ |
cea2e8a9 | 2111 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f | 2112 | #endif |
bd89102f | 2113 | } |
c2ab57d4 | 2114 | break; |
e5d73d77 | 2115 | #endif |
fe14fcc3 | 2116 | #ifdef HAS_SHM |
79072805 | 2117 | case OP_SHMCTL: |
bee1dbe2 | 2118 | ret = shmctl(id, cmd, (struct shmid_ds *)a); |
c2ab57d4 | 2119 | break; |
e5d73d77 | 2120 | #endif |
c2ab57d4 LW |
2121 | } |
2122 | if (getinfo && ret >= 0) { | |
79072805 LW |
2123 | SvCUR_set(astr, infosize); |
2124 | *SvEND(astr) = '\0'; | |
a0d0e21e | 2125 | SvSETMAGIC(astr); |
c2ab57d4 LW |
2126 | } |
2127 | return ret; | |
2128 | } | |
2129 | ||
79072805 | 2130 | I32 |
864dbfa3 | 2131 | Perl_do_msgsnd(pTHX_ SV **mark, SV **sp) |
c2ab57d4 | 2132 | { |
97aff369 | 2133 | dVAR; |
fe14fcc3 | 2134 | #ifdef HAS_MSG |
463ee0b2 | 2135 | STRLEN len; |
6867be6d | 2136 | const I32 id = SvIVx(*++mark); |
0bcc34c2 AL |
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 | ||
7918f24d | 2142 | PERL_ARGS_ASSERT_DO_MSGSND; |
890ce7af | 2143 | PERL_UNUSED_ARG(sp); |
c2ab57d4 | 2144 | |
0bcc34c2 | 2145 | if (msize < 0) |
cea2e8a9 | 2146 | Perl_croak(aTHX_ "Arg too short for msgsnd"); |
748a9306 | 2147 | SETERRNO(0,0); |
bee1dbe2 | 2148 | return msgsnd(id, (struct msgbuf *)mbuf, msize, flags); |
e5d73d77 | 2149 | #else |
2d51fa4d RGS |
2150 | PERL_UNUSED_ARG(sp); |
2151 | PERL_UNUSED_ARG(mark); | |
fe13d51d | 2152 | /* diag_listed_as: msg%s not implemented */ |
cea2e8a9 | 2153 | Perl_croak(aTHX_ "msgsnd not implemented"); |
e5d73d77 | 2154 | #endif |
c2ab57d4 LW |
2155 | } |
2156 | ||
79072805 | 2157 | I32 |
864dbfa3 | 2158 | Perl_do_msgrcv(pTHX_ SV **mark, SV **sp) |
c2ab57d4 | 2159 | { |
fe14fcc3 | 2160 | #ifdef HAS_MSG |
97aff369 | 2161 | dVAR; |
c2ab57d4 LW |
2162 | char *mbuf; |
2163 | long mtype; | |
6867be6d | 2164 | I32 msize, flags, ret; |
6867be6d | 2165 | const I32 id = SvIVx(*++mark); |
0bcc34c2 | 2166 | SV * const mstr = *++mark; |
7918f24d NC |
2167 | |
2168 | PERL_ARGS_ASSERT_DO_MSGRCV; | |
890ce7af | 2169 | PERL_UNUSED_ARG(sp); |
79072805 | 2170 | |
c2e66d9e GS |
2171 | /* suppress warning when reading into undef var --jhi */ |
2172 | if (! SvOK(mstr)) | |
76f68e9b | 2173 | sv_setpvs(mstr, ""); |
463ee0b2 LW |
2174 | msize = SvIVx(*++mark); |
2175 | mtype = (long)SvIVx(*++mark); | |
2176 | flags = SvIVx(*++mark); | |
93524f2b | 2177 | SvPV_force_nolen(mstr); |
a0d0e21e | 2178 | mbuf = SvGROW(mstr, sizeof(long)+msize+1); |
a1d180c4 | 2179 | |
748a9306 | 2180 | SETERRNO(0,0); |
bee1dbe2 | 2181 | ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags); |
c2ab57d4 | 2182 | if (ret >= 0) { |
79072805 LW |
2183 | SvCUR_set(mstr, sizeof(long)+ret); |
2184 | *SvEND(mstr) = '\0'; | |
41d6edb2 JH |
2185 | #ifndef INCOMPLETE_TAINTS |
2186 | /* who knows who has been playing with this message? */ | |
2187 | SvTAINTED_on(mstr); | |
2188 | #endif | |
c2ab57d4 LW |
2189 | } |
2190 | return ret; | |
e5d73d77 | 2191 | #else |
2d51fa4d RGS |
2192 | PERL_UNUSED_ARG(sp); |
2193 | PERL_UNUSED_ARG(mark); | |
fe13d51d | 2194 | /* diag_listed_as: msg%s not implemented */ |
cea2e8a9 | 2195 | Perl_croak(aTHX_ "msgrcv not implemented"); |
e5d73d77 | 2196 | #endif |
c2ab57d4 LW |
2197 | } |
2198 | ||
79072805 | 2199 | I32 |
864dbfa3 | 2200 | Perl_do_semop(pTHX_ SV **mark, SV **sp) |
c2ab57d4 | 2201 | { |
fe14fcc3 | 2202 | #ifdef HAS_SEM |
97aff369 | 2203 | dVAR; |
463ee0b2 | 2204 | STRLEN opsize; |
6867be6d | 2205 | const I32 id = SvIVx(*++mark); |
0bcc34c2 AL |
2206 | SV * const opstr = *++mark; |
2207 | const char * const opbuf = SvPV_const(opstr, opsize); | |
7918f24d NC |
2208 | |
2209 | PERL_ARGS_ASSERT_DO_SEMOP; | |
890ce7af | 2210 | PERL_UNUSED_ARG(sp); |
c2ab57d4 | 2211 | |
248ff010 NC |
2212 | if (opsize < 3 * SHORTSIZE |
2213 | || (opsize % (3 * SHORTSIZE))) { | |
93189314 | 2214 | SETERRNO(EINVAL,LIB_INVARG); |
c2ab57d4 LW |
2215 | return -1; |
2216 | } | |
748a9306 | 2217 | SETERRNO(0,0); |
248ff010 NC |
2218 | /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */ |
2219 | { | |
6867be6d | 2220 | const int nsops = opsize / (3 * sizeof (short)); |
248ff010 | 2221 | int i = nsops; |
0bcc34c2 | 2222 | short * const ops = (short *) opbuf; |
248ff010 NC |
2223 | short *o = ops; |
2224 | struct sembuf *temps, *t; | |
2225 | I32 result; | |
2226 | ||
a02a5408 | 2227 | Newx (temps, nsops, struct sembuf); |
248ff010 NC |
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 | } | |
e5d73d77 | 2248 | #else |
fe13d51d | 2249 | /* diag_listed_as: sem%s not implemented */ |
cea2e8a9 | 2250 | Perl_croak(aTHX_ "semop not implemented"); |
e5d73d77 | 2251 | #endif |
c2ab57d4 LW |
2252 | } |
2253 | ||
79072805 | 2254 | I32 |
864dbfa3 | 2255 | Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 2256 | { |
fe14fcc3 | 2257 | #ifdef HAS_SHM |
97aff369 | 2258 | dVAR; |
4373e329 | 2259 | char *shm; |
c2ab57d4 | 2260 | struct shmid_ds shmds; |
6867be6d | 2261 | const I32 id = SvIVx(*++mark); |
0bcc34c2 AL |
2262 | SV * const mstr = *++mark; |
2263 | const I32 mpos = SvIVx(*++mark); | |
2264 | const I32 msize = SvIVx(*++mark); | |
7918f24d NC |
2265 | |
2266 | PERL_ARGS_ASSERT_DO_SHMIO; | |
890ce7af | 2267 | PERL_UNUSED_ARG(sp); |
c2ab57d4 | 2268 | |
748a9306 | 2269 | SETERRNO(0,0); |
c2ab57d4 LW |
2270 | if (shmctl(id, IPC_STAT, &shmds) == -1) |
2271 | return -1; | |
7f39519f NC |
2272 | if (mpos < 0 || msize < 0 |
2273 | || (size_t)mpos + msize > (size_t)shmds.shm_segsz) { | |
93189314 | 2274 | SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */ |
c2ab57d4 LW |
2275 | return -1; |
2276 | } | |
d4c19fe8 | 2277 | shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0); |
c2ab57d4 LW |
2278 | if (shm == (char *)-1) /* I hate System V IPC, I really do */ |
2279 | return -1; | |
79072805 | 2280 | if (optype == OP_SHMREAD) { |
c8ae91a8 | 2281 | char *mbuf; |
9f538c04 GS |
2282 | /* suppress warning when reading into undef var (tchrist 3/Mar/00) */ |
2283 | if (! SvOK(mstr)) | |
76f68e9b | 2284 | sv_setpvs(mstr, ""); |
0f18891d | 2285 | SvUPGRADE(mstr, SVt_PV); |
af8ff727 | 2286 | SvPOK_only(mstr); |
bb7a0f54 | 2287 | mbuf = SvGROW(mstr, (STRLEN)msize+1); |
a0d0e21e | 2288 | |
bee1dbe2 | 2289 | Copy(shm + mpos, mbuf, msize, char); |
79072805 LW |
2290 | SvCUR_set(mstr, msize); |
2291 | *SvEND(mstr) = '\0'; | |
a0d0e21e | 2292 | SvSETMAGIC(mstr); |
d929ce6f JH |
2293 | #ifndef INCOMPLETE_TAINTS |
2294 | /* who knows who has been playing with this shared memory? */ | |
2295 | SvTAINTED_on(mstr); | |
2296 | #endif | |
c2ab57d4 LW |
2297 | } |
2298 | else { | |
93524f2b | 2299 | STRLEN len; |
c2ab57d4 | 2300 | |
93524f2b | 2301 | const char *mbuf = SvPV_const(mstr, len); |
027aa12d | 2302 | const I32 n = ((I32)len > msize) ? msize : (I32)len; |
bee1dbe2 | 2303 | Copy(mbuf, shm + mpos, n, char); |
c2ab57d4 | 2304 | if (n < msize) |
bee1dbe2 | 2305 | memzero(shm + mpos + n, msize - n); |
c2ab57d4 LW |
2306 | } |
2307 | return shmdt(shm); | |
e5d73d77 | 2308 | #else |
fe13d51d | 2309 | /* diag_listed_as: shm%s not implemented */ |
cea2e8a9 | 2310 | Perl_croak(aTHX_ "shm I/O not implemented"); |
e5d73d77 | 2311 | #endif |
c2ab57d4 LW |
2312 | } |
2313 | ||
fe14fcc3 | 2314 | #endif /* SYSV IPC */ |
4e35701f | 2315 | |
0d44d22b | 2316 | /* |
ccfc67b7 JH |
2317 | =head1 IO Functions |
2318 | ||
0d44d22b NC |
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> | |
210b36aa | 2323 | this glob starter is only used by miniperl during the build process. |
0d44d22b | 2324 | Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up. |
fab3f3a7 | 2325 | |
0d44d22b NC |
2326 | =cut |
2327 | */ | |
2328 | ||
2329 | PerlIO * | |
2330 | Perl_start_glob (pTHX_ SV *tmpglob, IO *io) | |
2331 | { | |
27da23d5 | 2332 | dVAR; |
561b68a9 | 2333 | SV * const tmpcmd = newSV(0); |
0d44d22b | 2334 | PerlIO *fp; |
7918f24d NC |
2335 | |
2336 | PERL_ARGS_ASSERT_START_GLOB; | |
2337 | ||
0d44d22b NC |
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 */ | |
dca5a913 JM |
2342 | |
2343 | PerlIO * | |
2344 | Perl_vms_start_glob | |
2345 | (pTHX_ SV *tmpglob, | |
2346 | IO *io); | |
2347 | ||
49a7a762 | 2348 | fp = Perl_vms_start_glob(aTHX_ tmpglob, io); |
dca5a913 | 2349 | |
0d44d22b | 2350 | #else /* !VMS */ |
0d44d22b NC |
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 */ | |
93b2dae1 FC |
2382 | { |
2383 | GV * const envgv = gv_fetchpvs("ENV", 0, SVt_PVHV); | |
2384 | SV ** const home = hv_fetchs(GvHV(envgv), "HOME", 0); | |
2385 | if (home && *home) SvGETMAGIC(*home); | |
2386 | save_hash(gv_fetchpvs("ENV", 0, SVt_PVHV)); | |
2387 | if (home && *home) SvSETMAGIC(*home); | |
2388 | } | |
93524f2b | 2389 | (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd), |
4608196e | 2390 | FALSE, O_RDONLY, 0, NULL); |
0d44d22b NC |
2391 | fp = IoIFP(io); |
2392 | #endif /* !VMS */ | |
2393 | LEAVE; | |
2394 | return fp; | |
2395 | } | |
66610fdd RGS |
2396 | |
2397 | /* | |
2398 | * Local variables: | |
2399 | * c-indentation-style: bsd | |
2400 | * c-basic-offset: 4 | |
2401 | * indent-tabs-mode: t | |
2402 | * End: | |
2403 | * | |
37442d52 RGS |
2404 | * ex: set ts=8 sts=4 sw=4 noet: |
2405 | */ |