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