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