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