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); |
4608196e | 785 | IoIFP(io) = NULL; |
6e21c824 | 786 | goto say_false; |
fe14fcc3 | 787 | } |
1462b684 LW |
788 | } |
789 | else | |
8990e307 | 790 | IoOFP(io) = fp; |
bf38876a | 791 | } |
a687059c | 792 | return TRUE; |
6e21c824 LW |
793 | |
794 | say_false: | |
8990e307 LW |
795 | IoIFP(io) = saveifp; |
796 | IoOFP(io) = saveofp; | |
797 | IoTYPE(io) = savetype; | |
6e21c824 | 798 | return FALSE; |
a687059c LW |
799 | } |
800 | ||
760ac839 | 801 | PerlIO * |
5aaab254 | 802 | Perl_nextargv(pTHX_ GV *gv) |
a687059c | 803 | { |
2d03de9c | 804 | IO * const io = GvIOp(gv); |
fe14fcc3 | 805 | |
7918f24d NC |
806 | PERL_ARGS_ASSERT_NEXTARGV; |
807 | ||
3280af22 | 808 | if (!PL_argvoutgv) |
fafc274c | 809 | PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
18708f5a GS |
810 | if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) { |
811 | IoFLAGS(io) &= ~IOf_START; | |
7a1c5554 | 812 | if (PL_inplace) { |
294b3b39 | 813 | assert(PL_defoutgv); |
29a861e7 NC |
814 | Perl_av_create_and_push(aTHX_ &PL_argvout_stack, |
815 | SvREFCNT_inc_simple_NN(PL_defoutgv)); | |
7a1c5554 | 816 | } |
18708f5a | 817 | } |
3280af22 NIS |
818 | if (PL_filemode & (S_ISUID|S_ISGID)) { |
819 | PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */ | |
fe14fcc3 | 820 | #ifdef HAS_FCHMOD |
c797f2d8 DM |
821 | if (PL_lastfd != -1) |
822 | (void)fchmod(PL_lastfd,PL_filemode); | |
fe14fcc3 | 823 | #else |
b28d0864 | 824 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
fe14fcc3 LW |
825 | #endif |
826 | } | |
c797f2d8 | 827 | PL_lastfd = -1; |
3280af22 | 828 | PL_filemode = 0; |
5c501b37 | 829 | if (!GvAV(gv)) |
4608196e | 830 | return NULL; |
b9f2b683 | 831 | while (av_tindex(GvAV(gv)) >= 0) { |
85aff577 | 832 | STRLEN oldlen; |
1fa0529f | 833 | SV *const sv = av_shift(GvAV(gv)); |
8990e307 | 834 | SAVEFREESV(sv); |
4bac9ae4 | 835 | SvTAINTED_off(GvSVn(gv)); /* previous tainting irrelevant */ |
e203899d | 836 | sv_setsv(GvSVn(gv),sv); |
79072805 | 837 | SvSETMAGIC(GvSV(gv)); |
3280af22 | 838 | PL_oldname = SvPVx(GvSV(gv), oldlen); |
d8015975 NC |
839 | if (LIKELY(!PL_inplace)) { |
840 | if (do_open6(gv, PL_oldname, oldlen, NULL, NULL, 0)) { | |
841 | return IoIFP(GvIOp(gv)); | |
842 | } | |
843 | } | |
844 | else { | |
845 | /* This very long block ends with return IoIFP(GvIOp(gv)); | |
846 | Both this block and the block above fall through on open | |
847 | failure to the warning code, and then the while loop above tries | |
848 | the next entry. */ | |
849 | if (do_open_raw(gv, PL_oldname, oldlen, O_RDONLY, 0)) { | |
1fa0529f NC |
850 | #ifndef FLEXFILENAMES |
851 | int filedev; | |
852 | int fileino; | |
853 | #endif | |
854 | Uid_t fileuid; | |
855 | Gid_t filegid; | |
856 | ||
79072805 | 857 | TAINT_PROPER("inplace open"); |
3280af22 | 858 | if (oldlen == 1 && *PL_oldname == '-') { |
fafc274c NC |
859 | setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, |
860 | SVt_PVIO)); | |
a0d0e21e | 861 | return IoIFP(GvIOp(gv)); |
c623bd54 | 862 | } |
99b89507 | 863 | #ifndef FLEXFILENAMES |
b28d0864 NIS |
864 | filedev = PL_statbuf.st_dev; |
865 | fileino = PL_statbuf.st_ino; | |
99b89507 | 866 | #endif |
3280af22 NIS |
867 | PL_filemode = PL_statbuf.st_mode; |
868 | fileuid = PL_statbuf.st_uid; | |
869 | filegid = PL_statbuf.st_gid; | |
870 | if (!S_ISREG(PL_filemode)) { | |
9b387841 NC |
871 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
872 | "Can't do inplace edit: %s is not a regular file", | |
873 | PL_oldname ); | |
79072805 | 874 | do_close(gv,FALSE); |
c623bd54 LW |
875 | continue; |
876 | } | |
c9930541 | 877 | if (*PL_inplace && strNE(PL_inplace, "*")) { |
2d03de9c | 878 | const char *star = strchr(PL_inplace, '*'); |
2d259d92 | 879 | if (star) { |
2d03de9c | 880 | const char *begin = PL_inplace; |
76f68e9b | 881 | sv_setpvs(sv, ""); |
2d259d92 CK |
882 | do { |
883 | sv_catpvn(sv, begin, star - begin); | |
3280af22 | 884 | sv_catpvn(sv, PL_oldname, oldlen); |
2d259d92 CK |
885 | begin = ++star; |
886 | } while ((star = strchr(begin, '*'))); | |
3d66d7bb GS |
887 | if (*begin) |
888 | sv_catpv(sv,begin); | |
2d259d92 CK |
889 | } |
890 | else { | |
3280af22 | 891 | sv_catpv(sv,PL_inplace); |
2d259d92 | 892 | } |
c623bd54 | 893 | #ifndef FLEXFILENAMES |
95a20fc0 | 894 | if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0 |
5f74f29c JH |
895 | && PL_statbuf.st_dev == filedev |
896 | && PL_statbuf.st_ino == fileino) | |
39e571d4 | 897 | #ifdef DJGPP |
5f74f29c | 898 | || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0) |
39e571d4 | 899 | #endif |
f248d071 GS |
900 | ) |
901 | { | |
9b387841 NC |
902 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
903 | "Can't do inplace edit: %"SVf" would not be unique", | |
904 | SVfARG(sv)); | |
79072805 | 905 | do_close(gv,FALSE); |
c623bd54 LW |
906 | continue; |
907 | } | |
908 | #endif | |
fe14fcc3 | 909 | #ifdef HAS_RENAME |
739a0b84 | 910 | #if !defined(DOSISH) && !defined(__CYGWIN__) |
95a20fc0 | 911 | if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) { |
9b387841 NC |
912 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
913 | "Can't rename %s to %"SVf": %s, skipping file", | |
914 | PL_oldname, SVfARG(sv), Strerror(errno)); | |
79072805 | 915 | do_close(gv,FALSE); |
c623bd54 LW |
916 | continue; |
917 | } | |
a687059c | 918 | #else |
79072805 | 919 | do_close(gv,FALSE); |
95a20fc0 SP |
920 | (void)PerlLIO_unlink(SvPVX_const(sv)); |
921 | (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv)); | |
d5eb9a46 | 922 | do_open_raw(gv, SvPVX_const(sv), SvCUR(sv), O_RDONLY, 0); |
55497cff | 923 | #endif /* DOSISH */ |
ff8e2863 | 924 | #else |
95a20fc0 SP |
925 | (void)UNLINK(SvPVX_const(sv)); |
926 | if (link(PL_oldname,SvPVX_const(sv)) < 0) { | |
9b387841 NC |
927 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
928 | "Can't rename %s to %"SVf": %s, skipping file", | |
929 | PL_oldname, SVfARG(sv), Strerror(errno) ); | |
79072805 | 930 | do_close(gv,FALSE); |
c623bd54 LW |
931 | continue; |
932 | } | |
b28d0864 | 933 | (void)UNLINK(PL_oldname); |
a687059c LW |
934 | #endif |
935 | } | |
936 | else { | |
c030f24b | 937 | #if !defined(DOSISH) && !defined(AMIGAOS) |
edc7bc49 | 938 | # ifndef VMS /* Don't delete; use automatic file versioning */ |
3280af22 | 939 | if (UNLINK(PL_oldname) < 0) { |
9b387841 NC |
940 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), |
941 | "Can't remove %s: %s, skipping file", | |
942 | PL_oldname, Strerror(errno) ); | |
79072805 | 943 | do_close(gv,FALSE); |
fe14fcc3 LW |
944 | continue; |
945 | } | |
edc7bc49 | 946 | # endif |
ff8e2863 | 947 | #else |
cea2e8a9 | 948 | Perl_croak(aTHX_ "Can't do inplace edit without backup"); |
ff8e2863 | 949 | #endif |
a687059c LW |
950 | } |
951 | ||
30fc4309 | 952 | sv_setpvn(sv,PL_oldname,oldlen); |
748a9306 | 953 | SETERRNO(0,0); /* in case sprintf set errno */ |
d5eb9a46 NC |
954 | if (!Perl_do_open_raw(aTHX_ PL_argvoutgv, SvPVX_const(sv), |
955 | SvCUR(sv), | |
4119ab01 | 956 | #ifdef VMS |
d5eb9a46 | 957 | O_WRONLY|O_CREAT|O_TRUNC, 0 |
4119ab01 | 958 | #else |
d5eb9a46 | 959 | O_WRONLY|O_CREAT|OPEN_EXCL, 0600 |
4119ab01 | 960 | #endif |
d5eb9a46 | 961 | )) { |
9b387841 NC |
962 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s", |
963 | PL_oldname, Strerror(errno) ); | |
79072805 | 964 | do_close(gv,FALSE); |
fe14fcc3 LW |
965 | continue; |
966 | } | |
3280af22 NIS |
967 | setdefout(PL_argvoutgv); |
968 | PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv))); | |
375ed12a JH |
969 | if (PL_lastfd >= 0) { |
970 | (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf); | |
fe14fcc3 | 971 | #ifdef HAS_FCHMOD |
375ed12a | 972 | (void)fchmod(PL_lastfd,PL_filemode); |
a687059c | 973 | #else |
375ed12a | 974 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
a687059c | 975 | #endif |
375ed12a | 976 | if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) { |
b469f1e0 | 977 | /* XXX silently ignore failures */ |
fe14fcc3 | 978 | #ifdef HAS_FCHOWN |
b469f1e0 | 979 | PERL_UNUSED_RESULT(fchown(PL_lastfd,fileuid,filegid)); |
a687059c | 980 | #else |
fe14fcc3 | 981 | #ifdef HAS_CHOWN |
b469f1e0 | 982 | PERL_UNUSED_RESULT(PerlLIO_chown(PL_oldname,fileuid,filegid)); |
a687059c | 983 | #endif |
b1248f16 | 984 | #endif |
375ed12a | 985 | } |
fe14fcc3 | 986 | } |
d8015975 | 987 | return IoIFP(GvIOp(gv)); |
a687059c | 988 | } |
d8015975 NC |
989 | } /* successful do_open_raw(), PL_inplace non-NULL */ |
990 | ||
991 | if (ckWARN_d(WARN_INPLACE)) { | |
992 | const int eno = errno; | |
993 | if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0 | |
994 | && !S_ISREG(PL_statbuf.st_mode)) { | |
995 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), | |
996 | "Can't do inplace edit: %s is not a regular file", | |
997 | PL_oldname); | |
998 | } | |
999 | else { | |
1000 | Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s", | |
1001 | PL_oldname, Strerror(eno)); | |
1002 | } | |
4d61ec05 | 1003 | } |
a687059c | 1004 | } |
18708f5a GS |
1005 | if (io && (IoFLAGS(io) & IOf_ARGV)) |
1006 | IoFLAGS(io) |= IOf_START; | |
3280af22 NIS |
1007 | if (PL_inplace) { |
1008 | (void)do_close(PL_argvoutgv,FALSE); | |
7a1c5554 GS |
1009 | if (io && (IoFLAGS(io) & IOf_ARGV) |
1010 | && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0) | |
1011 | { | |
159b6efe | 1012 | GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack)); |
18708f5a | 1013 | setdefout(oldout); |
8e217d4a | 1014 | SvREFCNT_dec_NN(oldout); |
4608196e | 1015 | return NULL; |
18708f5a | 1016 | } |
fafc274c | 1017 | setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO)); |
a687059c | 1018 | } |
4608196e | 1019 | return NULL; |
a687059c LW |
1020 | } |
1021 | ||
517844ec | 1022 | /* explicit renamed to avoid C++ conflict -- kja */ |
a687059c | 1023 | bool |
864dbfa3 | 1024 | Perl_do_close(pTHX_ GV *gv, bool not_implicit) |
a687059c | 1025 | { |
1193dd27 IZ |
1026 | bool retval; |
1027 | IO *io; | |
a687059c | 1028 | |
79072805 | 1029 | if (!gv) |
3280af22 | 1030 | gv = PL_argvgv; |
6e592b3a | 1031 | if (!gv || !isGV_with_GP(gv)) { |
1d2dff63 | 1032 | if (not_implicit) |
93189314 | 1033 | SETERRNO(EBADF,SS_IVCHAN); |
c2ab57d4 | 1034 | return FALSE; |
99b89507 | 1035 | } |
79072805 LW |
1036 | io = GvIO(gv); |
1037 | if (!io) { /* never opened */ | |
1d2dff63 | 1038 | if (not_implicit) { |
51087808 | 1039 | report_evil_fh(gv); |
93189314 | 1040 | SETERRNO(EBADF,SS_IVCHAN); |
1d2dff63 | 1041 | } |
a687059c LW |
1042 | return FALSE; |
1043 | } | |
f2b5be74 | 1044 | retval = io_close(io, not_implicit); |
517844ec | 1045 | if (not_implicit) { |
1193dd27 IZ |
1046 | IoLINES(io) = 0; |
1047 | IoPAGE(io) = 0; | |
1048 | IoLINES_LEFT(io) = IoPAGE_LEN(io); | |
1049 | } | |
50952442 | 1050 | IoTYPE(io) = IoTYPE_CLOSED; |
1193dd27 IZ |
1051 | return retval; |
1052 | } | |
1053 | ||
1054 | bool | |
f2b5be74 | 1055 | Perl_io_close(pTHX_ IO *io, bool not_implicit) |
1193dd27 IZ |
1056 | { |
1057 | bool retval = FALSE; | |
1193dd27 | 1058 | |
7918f24d NC |
1059 | PERL_ARGS_ASSERT_IO_CLOSE; |
1060 | ||
8990e307 | 1061 | if (IoIFP(io)) { |
50952442 | 1062 | if (IoTYPE(io) == IoTYPE_PIPE) { |
4373e329 | 1063 | const int status = PerlProc_pclose(IoIFP(io)); |
f2b5be74 | 1064 | if (not_implicit) { |
37038d91 | 1065 | STATUS_NATIVE_CHILD_SET(status); |
e5218da5 | 1066 | retval = (STATUS_UNIX == 0); |
f2b5be74 GS |
1067 | } |
1068 | else { | |
1069 | retval = (status != -1); | |
1070 | } | |
a687059c | 1071 | } |
50952442 | 1072 | else if (IoTYPE(io) == IoTYPE_STD) |
a687059c LW |
1073 | retval = TRUE; |
1074 | else { | |
8990e307 | 1075 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */ |
0bcc34c2 | 1076 | const bool prev_err = PerlIO_error(IoOFP(io)); |
e199e3be | 1077 | retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err); |
760ac839 | 1078 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
c2ab57d4 | 1079 | } |
e199e3be | 1080 | else { |
0bcc34c2 | 1081 | const bool prev_err = PerlIO_error(IoIFP(io)); |
e199e3be RGS |
1082 | retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err); |
1083 | } | |
a687059c | 1084 | } |
4608196e | 1085 | IoOFP(io) = IoIFP(io) = NULL; |
79072805 | 1086 | } |
f2b5be74 | 1087 | else if (not_implicit) { |
93189314 | 1088 | SETERRNO(EBADF,SS_IVCHAN); |
20408e3c | 1089 | } |
1193dd27 | 1090 | |
a687059c LW |
1091 | return retval; |
1092 | } | |
1093 | ||
1094 | bool | |
864dbfa3 | 1095 | Perl_do_eof(pTHX_ GV *gv) |
a687059c | 1096 | { |
eb578fdb | 1097 | IO * const io = GvIO(gv); |
a687059c | 1098 | |
7918f24d NC |
1099 | PERL_ARGS_ASSERT_DO_EOF; |
1100 | ||
79072805 | 1101 | if (!io) |
a687059c | 1102 | return TRUE; |
7716c5c5 | 1103 | else if (IoTYPE(io) == IoTYPE_WRONLY) |
a5390457 | 1104 | report_wrongway_fh(gv, '>'); |
a687059c | 1105 | |
8990e307 | 1106 | while (IoIFP(io)) { |
760ac839 | 1107 | if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */ |
a20bf0c3 | 1108 | if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */ |
760ac839 LW |
1109 | return FALSE; /* this is the most usual case */ |
1110 | } | |
a687059c | 1111 | |
79852593 NC |
1112 | { |
1113 | /* getc and ungetc can stomp on errno */ | |
4ee39169 | 1114 | dSAVE_ERRNO; |
79852593 NC |
1115 | const int ch = PerlIO_getc(IoIFP(io)); |
1116 | if (ch != EOF) { | |
1117 | (void)PerlIO_ungetc(IoIFP(io),ch); | |
4ee39169 | 1118 | RESTORE_ERRNO; |
79852593 NC |
1119 | return FALSE; |
1120 | } | |
4ee39169 | 1121 | RESTORE_ERRNO; |
a687059c | 1122 | } |
fab3f3a7 | 1123 | |
760ac839 | 1124 | if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) { |
a20bf0c3 JH |
1125 | if (PerlIO_get_cnt(IoIFP(io)) < -1) |
1126 | PerlIO_set_cnt(IoIFP(io),-1); | |
760ac839 | 1127 | } |
533c011a | 1128 | if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */ |
ed2c6b9b | 1129 | if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */ |
a687059c LW |
1130 | return TRUE; |
1131 | } | |
1132 | else | |
1133 | return TRUE; /* normal fp, definitely end of file */ | |
1134 | } | |
1135 | return TRUE; | |
1136 | } | |
1137 | ||
5ff3f7a4 | 1138 | Off_t |
864dbfa3 | 1139 | Perl_do_tell(pTHX_ GV *gv) |
a687059c | 1140 | { |
9c9f25b8 | 1141 | IO *const io = GvIO(gv); |
eb578fdb | 1142 | PerlIO *fp; |
a687059c | 1143 | |
7918f24d NC |
1144 | PERL_ARGS_ASSERT_DO_TELL; |
1145 | ||
9c9f25b8 | 1146 | if (io && (fp = IoIFP(io))) { |
8903cb82 | 1147 | return PerlIO_tell(fp); |
96e4d5b1 | 1148 | } |
51087808 | 1149 | report_evil_fh(gv); |
93189314 | 1150 | SETERRNO(EBADF,RMS_IFI); |
5ff3f7a4 | 1151 | return (Off_t)-1; |
a687059c LW |
1152 | } |
1153 | ||
1154 | bool | |
864dbfa3 | 1155 | Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence) |
a687059c | 1156 | { |
9c9f25b8 | 1157 | IO *const io = GvIO(gv); |
eb578fdb | 1158 | PerlIO *fp; |
a687059c | 1159 | |
9c9f25b8 | 1160 | if (io && (fp = IoIFP(io))) { |
8903cb82 | 1161 | return PerlIO_seek(fp, pos, whence) >= 0; |
137443ea | 1162 | } |
51087808 | 1163 | report_evil_fh(gv); |
93189314 | 1164 | SETERRNO(EBADF,RMS_IFI); |
a687059c LW |
1165 | return FALSE; |
1166 | } | |
1167 | ||
97cc44eb | 1168 | Off_t |
864dbfa3 | 1169 | Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence) |
8903cb82 | 1170 | { |
9c9f25b8 | 1171 | IO *const io = GvIO(gv); |
eb578fdb | 1172 | PerlIO *fp; |
8903cb82 | 1173 | |
7918f24d NC |
1174 | PERL_ARGS_ASSERT_DO_SYSSEEK; |
1175 | ||
375ed12a JH |
1176 | if (io && (fp = IoIFP(io))) { |
1177 | int fd = PerlIO_fileno(fp); | |
1178 | if (fd >= 0) { | |
1179 | return PerlLIO_lseek(fd, pos, whence); | |
1180 | } | |
1181 | } | |
51087808 | 1182 | report_evil_fh(gv); |
93189314 | 1183 | SETERRNO(EBADF,RMS_IFI); |
d9b3e12d | 1184 | return (Off_t)-1; |
8903cb82 | 1185 | } |
1186 | ||
6ff81951 | 1187 | int |
a79b25b7 | 1188 | Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len) |
16fe6d59 GS |
1189 | { |
1190 | int mode = O_BINARY; | |
81611534 | 1191 | PERL_UNUSED_CONTEXT; |
a79b25b7 | 1192 | if (s) { |
16fe6d59 GS |
1193 | while (*s) { |
1194 | if (*s == ':') { | |
1195 | switch (s[1]) { | |
1196 | case 'r': | |
e963d6d2 | 1197 | if (s[2] == 'a' && s[3] == 'w' |
16fe6d59 GS |
1198 | && (!s[4] || s[4] == ':' || isSPACE(s[4]))) |
1199 | { | |
1200 | mode = O_BINARY; | |
1201 | s += 4; | |
1202 | len -= 4; | |
1203 | break; | |
1204 | } | |
924ba076 | 1205 | /* FALLTHROUGH */ |
16fe6d59 | 1206 | case 'c': |
e963d6d2 | 1207 | if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f' |
16fe6d59 GS |
1208 | && (!s[5] || s[5] == ':' || isSPACE(s[5]))) |
1209 | { | |
1210 | mode = O_TEXT; | |
1211 | s += 5; | |
1212 | len -= 5; | |
1213 | break; | |
1214 | } | |
924ba076 | 1215 | /* FALLTHROUGH */ |
16fe6d59 GS |
1216 | default: |
1217 | goto fail_discipline; | |
1218 | } | |
1219 | } | |
1220 | else if (isSPACE(*s)) { | |
1221 | ++s; | |
1222 | --len; | |
1223 | } | |
1224 | else { | |
4373e329 | 1225 | const char *end; |
16fe6d59 GS |
1226 | fail_discipline: |
1227 | end = strchr(s+1, ':'); | |
1228 | if (!end) | |
1229 | end = s+len; | |
60382766 | 1230 | #ifndef PERLIO_LAYERS |
363c40c4 | 1231 | Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s); |
60382766 | 1232 | #else |
18a33fb5 | 1233 | len -= end-s; |
60382766 NIS |
1234 | s = end; |
1235 | #endif | |
16fe6d59 GS |
1236 | } |
1237 | } | |
1238 | } | |
1239 | return mode; | |
1240 | } | |
1241 | ||
58e24eff | 1242 | #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) |
27da23d5 JH |
1243 | I32 |
1244 | my_chsize(int fd, Off_t length) | |
6eb13c3b | 1245 | { |
58e24eff SH |
1246 | #ifdef F_FREESP |
1247 | /* code courtesy of William Kucharski */ | |
1248 | #define HAS_CHSIZE | |
1249 | ||
c623ac67 | 1250 | Stat_t filebuf; |
6eb13c3b | 1251 | |
3028581b | 1252 | if (PerlLIO_fstat(fd, &filebuf) < 0) |
6eb13c3b LW |
1253 | return -1; |
1254 | ||
1255 | if (filebuf.st_size < length) { | |
1256 | ||
1257 | /* extend file length */ | |
1258 | ||
3028581b | 1259 | if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0) |
6eb13c3b LW |
1260 | return -1; |
1261 | ||
1262 | /* write a "0" byte */ | |
1263 | ||
3028581b | 1264 | if ((PerlLIO_write(fd, "", 1)) != 1) |
6eb13c3b LW |
1265 | return -1; |
1266 | } | |
1267 | else { | |
1268 | /* truncate length */ | |
35da51f7 | 1269 | struct flock fl; |
6eb13c3b LW |
1270 | fl.l_whence = 0; |
1271 | fl.l_len = 0; | |
1272 | fl.l_start = length; | |
a0d0e21e | 1273 | fl.l_type = F_WRLCK; /* write lock on file space */ |
6eb13c3b LW |
1274 | |
1275 | /* | |
a0d0e21e | 1276 | * This relies on the UNDOCUMENTED F_FREESP argument to |
6eb13c3b LW |
1277 | * fcntl(2), which truncates the file so that it ends at the |
1278 | * position indicated by fl.l_start. | |
1279 | * | |
1280 | * Will minor miracles never cease? | |
1281 | */ | |
1282 | ||
a0d0e21e | 1283 | if (fcntl(fd, F_FREESP, &fl) < 0) |
6eb13c3b LW |
1284 | return -1; |
1285 | ||
1286 | } | |
6eb13c3b | 1287 | return 0; |
58e24eff | 1288 | #else |
27da23d5 | 1289 | Perl_croak_nocontext("truncate not implemented"); |
a0d0e21e | 1290 | #endif /* F_FREESP */ |
27da23d5 | 1291 | return -1; |
58e24eff SH |
1292 | } |
1293 | #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */ | |
ff8e2863 | 1294 | |
a687059c | 1295 | bool |
5aaab254 | 1296 | Perl_do_print(pTHX_ SV *sv, PerlIO *fp) |
a687059c | 1297 | { |
7918f24d NC |
1298 | PERL_ARGS_ASSERT_DO_PRINT; |
1299 | ||
79072805 LW |
1300 | /* assuming fp is checked earlier */ |
1301 | if (!sv) | |
1302 | return TRUE; | |
e9950d3b NC |
1303 | if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) { |
1304 | assert(!SvGMAGICAL(sv)); | |
1305 | if (SvIsUV(sv)) | |
1306 | PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv)); | |
1307 | else | |
1308 | PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv)); | |
1309 | return !PerlIO_error(fp); | |
1310 | } | |
1311 | else { | |
1312 | STRLEN len; | |
676f44e7 | 1313 | /* Do this first to trigger any overloading. */ |
e9950d3b NC |
1314 | const char *tmps = SvPV_const(sv, len); |
1315 | U8 *tmpbuf = NULL; | |
1316 | bool happy = TRUE; | |
1317 | ||
d791f93f KW |
1318 | if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */ |
1319 | if (!SvUTF8(sv)) { /* Convert to utf8 if necessary */ | |
676f44e7 NC |
1320 | /* We don't modify the original scalar. */ |
1321 | tmpbuf = bytes_to_utf8((const U8*) tmps, &len); | |
1322 | tmps = (char *) tmpbuf; | |
1323 | } | |
a099aed4 | 1324 | else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) { |
0876b9a0 KW |
1325 | (void) check_utf8_print((const U8*) tmps, len); |
1326 | } | |
d791f93f KW |
1327 | } /* else stream isn't utf8 */ |
1328 | else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to | |
1329 | convert to bytes */ | |
676f44e7 NC |
1330 | STRLEN tmplen = len; |
1331 | bool utf8 = TRUE; | |
35da51f7 | 1332 | U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8); |
676f44e7 | 1333 | if (!utf8) { |
d791f93f KW |
1334 | |
1335 | /* Here, succeeded in downgrading from utf8. Set up to below | |
1336 | * output the converted value */ | |
676f44e7 NC |
1337 | tmpbuf = result; |
1338 | tmps = (char *) tmpbuf; | |
1339 | len = tmplen; | |
1340 | } | |
d791f93f KW |
1341 | else { /* Non-utf8 output stream, but string only representable in |
1342 | utf8 */ | |
676f44e7 | 1343 | assert((char *)result == tmps); |
9b387841 | 1344 | Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), |
21630838 FC |
1345 | "Wide character in %s", |
1346 | PL_op ? OP_DESC(PL_op) : "print" | |
1347 | ); | |
0876b9a0 KW |
1348 | /* Could also check that isn't one of the things to avoid |
1349 | * in utf8 by using check_utf8_print(), but not doing so, | |
1350 | * since the stream isn't a UTF8 stream */ | |
ae798467 NIS |
1351 | } |
1352 | } | |
e9950d3b NC |
1353 | /* To detect whether the process is about to overstep its |
1354 | * filesize limit we would need getrlimit(). We could then | |
1355 | * also transparently raise the limit with setrlimit() -- | |
1356 | * but only until the system hard limit/the filesystem limit, | |
1357 | * at which we would get EPERM. Note that when using buffered | |
1358 | * io the write failure can be delayed until the flush/close. --jhi */ | |
1359 | if (len && (PerlIO_write(fp,tmps,len) == 0)) | |
1360 | happy = FALSE; | |
1361 | Safefree(tmpbuf); | |
1362 | return happy ? !PerlIO_error(fp) : FALSE; | |
ff8e2863 | 1363 | } |
a687059c LW |
1364 | } |
1365 | ||
79072805 | 1366 | I32 |
0d7d409d | 1367 | Perl_my_stat_flags(pTHX_ const U32 flags) |
a687059c | 1368 | { |
39644a26 | 1369 | dSP; |
79072805 | 1370 | IO *io; |
2dd78f96 | 1371 | GV* gv; |
79072805 | 1372 | |
533c011a | 1373 | if (PL_op->op_flags & OPf_REF) { |
2dd78f96 | 1374 | gv = cGVOP_gv; |
748a9306 | 1375 | do_fstat: |
5228a96c SP |
1376 | if (gv == PL_defgv) |
1377 | return PL_laststatval; | |
2dd78f96 | 1378 | io = GvIO(gv); |
ad02613c | 1379 | do_fstat_have_io: |
5228a96c | 1380 | PL_laststype = OP_STAT; |
bd5f6c01 | 1381 | PL_statgv = gv ? gv : (GV *)io; |
76f68e9b | 1382 | sv_setpvs(PL_statname, ""); |
77616968 | 1383 | if (io) { |
5228a96c | 1384 | if (IoIFP(io)) { |
375ed12a | 1385 | int fd = PerlIO_fileno(IoIFP(io)); |
77616968 JH |
1386 | if (fd < 0) { |
1387 | /* E.g. PerlIO::scalar has no real fd. */ | |
1388 | return (PL_laststatval = -1); | |
1389 | } else { | |
375ed12a JH |
1390 | return (PL_laststatval = PerlLIO_fstat(fd, &PL_statcache)); |
1391 | } | |
5228a96c | 1392 | } else if (IoDIRP(io)) { |
3497a01f | 1393 | return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache)); |
5228a96c | 1394 | } |
5228a96c | 1395 | } |
3888144c FC |
1396 | PL_laststatval = -1; |
1397 | report_evil_fh(gv); | |
1398 | return -1; | |
a687059c | 1399 | } |
d2c4d2d1 | 1400 | else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t)) |
8db8f6b6 FC |
1401 | == OPpFT_STACKED) |
1402 | return PL_laststatval; | |
d2c4d2d1 FC |
1403 | else { |
1404 | SV* const sv = TOPs; | |
4373e329 | 1405 | const char *s; |
4ecd490c | 1406 | STRLEN len; |
094a3eec | 1407 | if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) { |
748a9306 LW |
1408 | goto do_fstat; |
1409 | } | |
ad02613c | 1410 | else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) { |
a45c7426 | 1411 | io = MUTABLE_IO(SvRV(sv)); |
7f39519f | 1412 | gv = NULL; |
ad02613c SP |
1413 | goto do_fstat_have_io; |
1414 | } | |
748a9306 | 1415 | |
0d7d409d | 1416 | s = SvPV_flags_const(sv, len, flags); |
a0714e2c | 1417 | PL_statgv = NULL; |
4ecd490c | 1418 | sv_setpvn(PL_statname, s, len); |
95a20fc0 | 1419 | s = SvPVX_const(PL_statname); /* s now NUL-terminated */ |
3280af22 NIS |
1420 | PL_laststype = OP_STAT; |
1421 | PL_laststatval = PerlLIO_stat(s, &PL_statcache); | |
7cb3f959 | 1422 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(s)) { |
5d37acd6 | 1423 | GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */ |
9014280d | 1424 | Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat"); |
5d37acd6 DM |
1425 | GCC_DIAG_RESTORE; |
1426 | } | |
3280af22 | 1427 | return PL_laststatval; |
a687059c LW |
1428 | } |
1429 | } | |
1430 | ||
fbb0b3b3 | 1431 | |
79072805 | 1432 | I32 |
0d7d409d | 1433 | Perl_my_lstat_flags(pTHX_ const U32 flags) |
c623bd54 | 1434 | { |
a1894d81 | 1435 | static const char* const no_prev_lstat = "The stat preceding -l _ wasn't an lstat"; |
39644a26 | 1436 | dSP; |
b16276bb | 1437 | const char *file; |
cd22fad3 | 1438 | SV* const sv = TOPs; |
5840701a | 1439 | bool isio = FALSE; |
533c011a | 1440 | if (PL_op->op_flags & OPf_REF) { |
638eceb6 | 1441 | if (cGVOP_gv == PL_defgv) { |
3280af22 | 1442 | if (PL_laststype != OP_LSTAT) |
0157ef98 | 1443 | Perl_croak(aTHX_ "%s", no_prev_lstat); |
3280af22 | 1444 | return PL_laststatval; |
fe14fcc3 | 1445 | } |
31b139ba | 1446 | PL_laststatval = -1; |
5d3e98de | 1447 | if (ckWARN(WARN_IO)) { |
5840701a | 1448 | /* diag_listed_as: Use of -l on filehandle%s */ |
d0c0e7dd FC |
1449 | Perl_warner(aTHX_ packWARN(WARN_IO), |
1450 | "Use of -l on filehandle %"HEKf, | |
1451 | HEKfARG(GvENAME_HEK(cGVOP_gv))); | |
5d3e98de | 1452 | } |
31b139ba | 1453 | return -1; |
fe14fcc3 | 1454 | } |
8db8f6b6 FC |
1455 | if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t)) |
1456 | == OPpFT_STACKED) { | |
1f26655e | 1457 | if (PL_laststype != OP_LSTAT) |
0157ef98 | 1458 | Perl_croak(aTHX_ "%s", no_prev_lstat); |
1f26655e | 1459 | return PL_laststatval; |
cd22fad3 | 1460 | } |
c623bd54 | 1461 | |
3280af22 | 1462 | PL_laststype = OP_LSTAT; |
a0714e2c | 1463 | PL_statgv = NULL; |
5840701a FC |
1464 | if ( ( (SvROK(sv) && ( isGV_with_GP(SvRV(sv)) |
1465 | || (isio = SvTYPE(SvRV(sv)) == SVt_PVIO) ) | |
1466 | ) | |
1467 | || isGV_with_GP(sv) | |
1468 | ) | |
1469 | && ckWARN(WARN_IO)) { | |
1470 | if (isio) | |
1471 | /* diag_listed_as: Use of -l on filehandle%s */ | |
1472 | Perl_warner(aTHX_ packWARN(WARN_IO), | |
1473 | "Use of -l on filehandle"); | |
1474 | else | |
1475 | /* diag_listed_as: Use of -l on filehandle%s */ | |
1476 | Perl_warner(aTHX_ packWARN(WARN_IO), | |
1477 | "Use of -l on filehandle %"HEKf, | |
10bafe90 BF |
1478 | HEKfARG(GvENAME_HEK((const GV *) |
1479 | (SvROK(sv) ? SvRV(sv) : sv)))); | |
cd22fad3 RS |
1480 | } |
1481 | file = SvPV_flags_const_nolen(sv, flags); | |
b16276bb VP |
1482 | sv_setpv(PL_statname,file); |
1483 | PL_laststatval = PerlLIO_lstat(file,&PL_statcache); | |
7cb3f959 | 1484 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(file)) { |
5d37acd6 DM |
1485 | GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */ |
1486 | Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat"); | |
1487 | GCC_DIAG_RESTORE; | |
1488 | } | |
3280af22 | 1489 | return PL_laststatval; |
c623bd54 LW |
1490 | } |
1491 | ||
a0f2c8ec JD |
1492 | static void |
1493 | S_exec_failed(pTHX_ const char *cmd, int fd, int do_report) | |
1494 | { | |
1495 | const int e = errno; | |
7918f24d | 1496 | PERL_ARGS_ASSERT_EXEC_FAILED; |
a0f2c8ec JD |
1497 | if (ckWARN(WARN_EXEC)) |
1498 | Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s", | |
1499 | cmd, Strerror(e)); | |
1500 | if (do_report) { | |
b469f1e0 JH |
1501 | /* XXX silently ignore failures */ |
1502 | PERL_UNUSED_RESULT(PerlLIO_write(fd, (void*)&e, sizeof(int))); | |
a0f2c8ec JD |
1503 | PerlLIO_close(fd); |
1504 | } | |
1505 | } | |
1506 | ||
d5a9bfb0 | 1507 | bool |
5aaab254 | 1508 | Perl_do_aexec5(pTHX_ SV *really, SV **mark, SV **sp, |
2aa1486d | 1509 | int fd, int do_report) |
d5a9bfb0 | 1510 | { |
27da23d5 | 1511 | dVAR; |
7918f24d | 1512 | PERL_ARGS_ASSERT_DO_AEXEC5; |
e37778c2 | 1513 | #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__) |
cd39f2b6 JH |
1514 | Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system"); |
1515 | #else | |
79072805 | 1516 | if (sp > mark) { |
360ea906 | 1517 | const char **a; |
6136c704 | 1518 | const char *tmps = NULL; |
360ea906 | 1519 | Newx(PL_Argv, sp - mark + 1, const char*); |
c3c5fad7 | 1520 | a = PL_Argv; |
890ce7af | 1521 | |
79072805 LW |
1522 | while (++mark <= sp) { |
1523 | if (*mark) | |
360ea906 | 1524 | *a++ = SvPV_nolen_const(*mark); |
a687059c LW |
1525 | else |
1526 | *a++ = ""; | |
1527 | } | |
6136c704 | 1528 | *a = NULL; |
91b2752f | 1529 | if (really) |
e62f0680 | 1530 | tmps = SvPV_nolen_const(really); |
91b2752f RG |
1531 | if ((!really && *PL_Argv[0] != '/') || |
1532 | (really && *tmps != '/')) /* will execvp use PATH? */ | |
79072805 | 1533 | TAINT_ENV(); /* testing IFS here is overkill, probably */ |
b35112e7 | 1534 | PERL_FPU_PRE_EXEC |
91b2752f | 1535 | if (really && *tmps) |
b4748376 | 1536 | PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv)); |
a687059c | 1537 | else |
b4748376 | 1538 | PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv)); |
b35112e7 | 1539 | PERL_FPU_POST_EXEC |
a0f2c8ec | 1540 | S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report); |
a687059c | 1541 | } |
bee1dbe2 | 1542 | do_execfree(); |
cd39f2b6 | 1543 | #endif |
a687059c LW |
1544 | return FALSE; |
1545 | } | |
1546 | ||
fe14fcc3 | 1547 | void |
864dbfa3 | 1548 | Perl_do_execfree(pTHX) |
ff8e2863 | 1549 | { |
43c5f42d | 1550 | Safefree(PL_Argv); |
4608196e | 1551 | PL_Argv = NULL; |
43c5f42d | 1552 | Safefree(PL_Cmd); |
bd61b366 | 1553 | PL_Cmd = NULL; |
ff8e2863 LW |
1554 | } |
1555 | ||
9555a685 | 1556 | #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION |
e446cec8 IZ |
1557 | |
1558 | bool | |
2fbb330f | 1559 | Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report) |
e446cec8 | 1560 | { |
27da23d5 | 1561 | dVAR; |
eb578fdb KW |
1562 | const char **a; |
1563 | char *s; | |
15db3ae2 | 1564 | char *buf; |
2fbb330f | 1565 | char *cmd; |
2fbb330f | 1566 | /* Make a copy so we can change it */ |
6fca0082 | 1567 | const Size_t cmdlen = strlen(incmd) + 1; |
7918f24d NC |
1568 | |
1569 | PERL_ARGS_ASSERT_DO_EXEC3; | |
1570 | ||
15db3ae2 DM |
1571 | Newx(buf, cmdlen, char); |
1572 | cmd = buf; | |
cfff9797 | 1573 | memcpy(cmd, incmd, cmdlen); |
a687059c | 1574 | |
748a9306 LW |
1575 | while (*cmd && isSPACE(*cmd)) |
1576 | cmd++; | |
1577 | ||
a687059c LW |
1578 | /* save an extra exec if possible */ |
1579 | ||
bf38876a | 1580 | #ifdef CSH |
d05c1ba0 | 1581 | { |
0c19750d | 1582 | char flags[PERL_FLAGS_MAX]; |
d05c1ba0 JH |
1583 | if (strnEQ(cmd,PL_cshname,PL_cshlen) && |
1584 | strnEQ(cmd+PL_cshlen," -c",3)) { | |
28f0d0ec | 1585 | my_strlcpy(flags, "-c", PERL_FLAGS_MAX); |
d05c1ba0 JH |
1586 | s = cmd+PL_cshlen+3; |
1587 | if (*s == 'f') { | |
1588 | s++; | |
28f0d0ec | 1589 | my_strlcat(flags, "f", PERL_FLAGS_MAX - 2); |
d05c1ba0 JH |
1590 | } |
1591 | if (*s == ' ') | |
1592 | s++; | |
1593 | if (*s++ == '\'') { | |
0bcc34c2 | 1594 | char * const ncmd = s; |
d05c1ba0 JH |
1595 | |
1596 | while (*s) | |
1597 | s++; | |
1598 | if (s[-1] == '\n') | |
1599 | *--s = '\0'; | |
1600 | if (s[-1] == '\'') { | |
1601 | *--s = '\0'; | |
b35112e7 | 1602 | PERL_FPU_PRE_EXEC |
7c0dd7ca | 1603 | PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL); |
b35112e7 | 1604 | PERL_FPU_POST_EXEC |
d05c1ba0 | 1605 | *s = '\''; |
a0f2c8ec | 1606 | S_exec_failed(aTHX_ PL_cshname, fd, do_report); |
15db3ae2 | 1607 | Safefree(buf); |
d05c1ba0 JH |
1608 | return FALSE; |
1609 | } | |
1610 | } | |
a687059c LW |
1611 | } |
1612 | } | |
bf38876a | 1613 | #endif /* CSH */ |
a687059c LW |
1614 | |
1615 | /* see if there are shell metacharacters in it */ | |
1616 | ||
748a9306 LW |
1617 | if (*cmd == '.' && isSPACE(cmd[1])) |
1618 | goto doshell; | |
1619 | ||
1620 | if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4])) | |
1621 | goto doshell; | |
1622 | ||
294b3b39 | 1623 | s = cmd; |
0eb30aeb | 1624 | while (isWORDCHAR(*s)) |
294b3b39 | 1625 | s++; /* catch VAR=val gizmo */ |
63f2c1e1 LW |
1626 | if (*s == '=') |
1627 | goto doshell; | |
748a9306 | 1628 | |
a687059c | 1629 | for (s = cmd; *s; s++) { |
d05c1ba0 JH |
1630 | if (*s != ' ' && !isALPHA(*s) && |
1631 | strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) { | |
a687059c LW |
1632 | if (*s == '\n' && !s[1]) { |
1633 | *s = '\0'; | |
1634 | break; | |
1635 | } | |
603a98b0 IZ |
1636 | /* handle the 2>&1 construct at the end */ |
1637 | if (*s == '>' && s[1] == '&' && s[2] == '1' | |
1638 | && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2]) | |
1639 | && (!s[3] || isSPACE(s[3]))) | |
1640 | { | |
6867be6d | 1641 | const char *t = s + 3; |
603a98b0 IZ |
1642 | |
1643 | while (*t && isSPACE(*t)) | |
1644 | ++t; | |
943bbd07 | 1645 | if (!*t && (PerlLIO_dup2(1,2) != -1)) { |
603a98b0 IZ |
1646 | s[-2] = '\0'; |
1647 | break; | |
1648 | } | |
1649 | } | |
a687059c | 1650 | doshell: |
b35112e7 | 1651 | PERL_FPU_PRE_EXEC |
7c0dd7ca | 1652 | PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL); |
b35112e7 | 1653 | PERL_FPU_POST_EXEC |
a0f2c8ec | 1654 | S_exec_failed(aTHX_ PL_sh_path, fd, do_report); |
15db3ae2 | 1655 | Safefree(buf); |
a687059c LW |
1656 | return FALSE; |
1657 | } | |
1658 | } | |
748a9306 | 1659 | |
360ea906 | 1660 | Newx(PL_Argv, (s - cmd) / 2 + 2, const char*); |
3280af22 NIS |
1661 | PL_Cmd = savepvn(cmd, s-cmd); |
1662 | a = PL_Argv; | |
1663 | for (s = PL_Cmd; *s;) { | |
294b3b39 AL |
1664 | while (isSPACE(*s)) |
1665 | s++; | |
a687059c LW |
1666 | if (*s) |
1667 | *(a++) = s; | |
294b3b39 AL |
1668 | while (*s && !isSPACE(*s)) |
1669 | s++; | |
a687059c LW |
1670 | if (*s) |
1671 | *s++ = '\0'; | |
1672 | } | |
6136c704 | 1673 | *a = NULL; |
3280af22 | 1674 | if (PL_Argv[0]) { |
b35112e7 | 1675 | PERL_FPU_PRE_EXEC |
360ea906 | 1676 | PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv)); |
b35112e7 | 1677 | PERL_FPU_POST_EXEC |
b1248f16 | 1678 | if (errno == ENOEXEC) { /* for system V NIH syndrome */ |
ff8e2863 | 1679 | do_execfree(); |
a687059c | 1680 | goto doshell; |
b1248f16 | 1681 | } |
a0f2c8ec | 1682 | S_exec_failed(aTHX_ PL_Argv[0], fd, do_report); |
a687059c | 1683 | } |
ff8e2863 | 1684 | do_execfree(); |
15db3ae2 | 1685 | Safefree(buf); |
a687059c LW |
1686 | return FALSE; |
1687 | } | |
1688 | ||
6890e559 | 1689 | #endif /* OS2 || WIN32 */ |
760ac839 | 1690 | |
25bbd826 CB |
1691 | #ifdef VMS |
1692 | #include <starlet.h> /* for sys$delprc */ | |
1693 | #endif | |
1694 | ||
79072805 | 1695 | I32 |
5aaab254 | 1696 | Perl_apply(pTHX_ I32 type, SV **mark, SV **sp) |
a687059c | 1697 | { |
eb578fdb KW |
1698 | I32 val; |
1699 | I32 tot = 0; | |
4634a855 | 1700 | const char *const what = PL_op_name[type]; |
5c144d81 | 1701 | const char *s; |
84c7b88c | 1702 | STRLEN len; |
890ce7af | 1703 | SV ** const oldmark = mark; |
885b4b39 | 1704 | bool killgp = FALSE; |
a687059c | 1705 | |
7918f24d NC |
1706 | PERL_ARGS_ASSERT_APPLY; |
1707 | ||
9a9b5ec9 DM |
1708 | PERL_UNUSED_VAR(what); /* may not be used depending on compile options */ |
1709 | ||
1444765e NC |
1710 | /* Doing this ahead of the switch statement preserves the old behaviour, |
1711 | where attempting to use kill as a taint test test would fail on | |
1712 | platforms where kill was not defined. */ | |
1713 | #ifndef HAS_KILL | |
1714 | if (type == OP_KILL) | |
4634a855 | 1715 | Perl_die(aTHX_ PL_no_func, what); |
1444765e NC |
1716 | #endif |
1717 | #ifndef HAS_CHOWN | |
1718 | if (type == OP_CHOWN) | |
4634a855 | 1719 | Perl_die(aTHX_ PL_no_func, what); |
1444765e NC |
1720 | #endif |
1721 | ||
1722 | ||
20408e3c | 1723 | #define APPLY_TAINT_PROPER() \ |
3280af22 | 1724 | STMT_START { \ |
284167a5 | 1725 | if (TAINT_get) { TAINT_PROPER(what); } \ |
873ef191 | 1726 | } STMT_END |
20408e3c GS |
1727 | |
1728 | /* This is a first heuristic; it doesn't catch tainting magic. */ | |
284167a5 | 1729 | if (TAINTING_get) { |
463ee0b2 | 1730 | while (++mark <= sp) { |
bbce6d69 | 1731 | if (SvTAINTED(*mark)) { |
1732 | TAINT; | |
1733 | break; | |
1734 | } | |
463ee0b2 LW |
1735 | } |
1736 | mark = oldmark; | |
1737 | } | |
a687059c | 1738 | switch (type) { |
79072805 | 1739 | case OP_CHMOD: |
20408e3c | 1740 | APPLY_TAINT_PROPER(); |
79072805 | 1741 | if (++mark <= sp) { |
4ea561bc | 1742 | val = SvIV(*mark); |
20408e3c GS |
1743 | APPLY_TAINT_PROPER(); |
1744 | tot = sp - mark; | |
79072805 | 1745 | while (++mark <= sp) { |
c4aca7d0 | 1746 | GV* gv; |
2ea1cce7 | 1747 | if ((gv = MAYBE_DEREF_GV(*mark))) { |
c4aca7d0 GA |
1748 | if (GvIO(gv) && IoIFP(GvIOp(gv))) { |
1749 | #ifdef HAS_FCHMOD | |
375ed12a | 1750 | int fd = PerlIO_fileno(IoIFP(GvIOn(gv))); |
c4aca7d0 | 1751 | APPLY_TAINT_PROPER(); |
375ed12a JH |
1752 | if (fd < 0) { |
1753 | SETERRNO(EBADF,RMS_IFI); | |
1754 | tot--; | |
1755 | } else if (fchmod(fd, val)) | |
1756 | tot--; | |
c4aca7d0 | 1757 | #else |
b9c6780e | 1758 | Perl_die(aTHX_ PL_no_func, "fchmod"); |
c4aca7d0 GA |
1759 | #endif |
1760 | } | |
1761 | else { | |
1762 | tot--; | |
1763 | } | |
1764 | } | |
c4aca7d0 | 1765 | else { |
41188aa0 | 1766 | const char *name = SvPV_nomg_const(*mark, len); |
c4aca7d0 | 1767 | APPLY_TAINT_PROPER(); |
41188aa0 | 1768 | if (!IS_SAFE_PATHNAME(name, len, "chmod") || |
c8028aa6 TC |
1769 | PerlLIO_chmod(name, val)) { |
1770 | tot--; | |
1771 | } | |
c4aca7d0 | 1772 | } |
a687059c LW |
1773 | } |
1774 | } | |
1775 | break; | |
fe14fcc3 | 1776 | #ifdef HAS_CHOWN |
79072805 | 1777 | case OP_CHOWN: |
20408e3c | 1778 | APPLY_TAINT_PROPER(); |
79072805 | 1779 | if (sp - mark > 2) { |
eb578fdb | 1780 | I32 val2; |
463ee0b2 LW |
1781 | val = SvIVx(*++mark); |
1782 | val2 = SvIVx(*++mark); | |
20408e3c | 1783 | APPLY_TAINT_PROPER(); |
a0d0e21e | 1784 | tot = sp - mark; |
79072805 | 1785 | while (++mark <= sp) { |
c4aca7d0 | 1786 | GV* gv; |
2ea1cce7 | 1787 | if ((gv = MAYBE_DEREF_GV(*mark))) { |
c4aca7d0 GA |
1788 | if (GvIO(gv) && IoIFP(GvIOp(gv))) { |
1789 | #ifdef HAS_FCHOWN | |
375ed12a | 1790 | int fd = PerlIO_fileno(IoIFP(GvIOn(gv))); |
c4aca7d0 | 1791 | APPLY_TAINT_PROPER(); |
375ed12a JH |
1792 | if (fd < 0) { |
1793 | SETERRNO(EBADF,RMS_IFI); | |
1794 | tot--; | |
1795 | } else if (fchown(fd, val, val2)) | |
c4aca7d0 GA |
1796 | tot--; |
1797 | #else | |
b9c6780e | 1798 | Perl_die(aTHX_ PL_no_func, "fchown"); |
c4aca7d0 GA |
1799 | #endif |
1800 | } | |
1801 | else { | |
1802 | tot--; | |
1803 | } | |
1804 | } | |
c4aca7d0 | 1805 | else { |
41188aa0 | 1806 | const char *name = SvPV_nomg_const(*mark, len); |
c4aca7d0 | 1807 | APPLY_TAINT_PROPER(); |
41188aa0 | 1808 | if (!IS_SAFE_PATHNAME(name, len, "chown") || |
c8028aa6 | 1809 | PerlLIO_chown(name, val, val2)) { |
c4aca7d0 | 1810 | tot--; |
c8028aa6 | 1811 | } |
c4aca7d0 | 1812 | } |
a687059c LW |
1813 | } |
1814 | } | |
1815 | break; | |
b1248f16 | 1816 | #endif |
a1d180c4 | 1817 | /* |
dd64f1c3 AD |
1818 | XXX Should we make lchown() directly available from perl? |
1819 | For now, we'll let Configure test for HAS_LCHOWN, but do | |
1820 | nothing in the core. | |
1821 | --AD 5/1998 | |
1822 | */ | |
fe14fcc3 | 1823 | #ifdef HAS_KILL |
79072805 | 1824 | case OP_KILL: |
20408e3c | 1825 | APPLY_TAINT_PROPER(); |
55497cff | 1826 | if (mark == sp) |
1827 | break; | |
84c7b88c | 1828 | s = SvPVx_const(*++mark, len); |
c2fd40cb DM |
1829 | if (*s == '-' && isALPHA(s[1])) |
1830 | { | |
1831 | s++; | |
1832 | len--; | |
885b4b39 | 1833 | killgp = TRUE; |
c2fd40cb | 1834 | } |
e02bfb16 | 1835 | if (isALPHA(*s)) { |
84c7b88c | 1836 | if (*s == 'S' && s[1] == 'I' && s[2] == 'G') { |
79072805 | 1837 | s += 3; |
84c7b88c BF |
1838 | len -= 3; |
1839 | } | |
1840 | if ((val = whichsig_pvn(s, len)) < 0) | |
1841 | Perl_croak(aTHX_ "Unrecognized signal name \"%"SVf"\"", SVfARG(*mark)); | |
79072805 LW |
1842 | } |
1843 | else | |
c2fd40cb | 1844 | { |
4ea561bc | 1845 | val = SvIV(*mark); |
c2fd40cb DM |
1846 | if (val < 0) |
1847 | { | |
885b4b39 | 1848 | killgp = TRUE; |
c2fd40cb DM |
1849 | val = -val; |
1850 | } | |
1851 | } | |
20408e3c GS |
1852 | APPLY_TAINT_PROPER(); |
1853 | tot = sp - mark; | |
3595fcef | 1854 | #ifdef VMS |
1855 | /* kill() doesn't do process groups (job trees?) under VMS */ | |
3595fcef | 1856 | if (val == SIGKILL) { |
3595fcef | 1857 | /* Use native sys$delprc() to insure that target process is |
1858 | * deleted; supervisor-mode images don't pay attention to | |
1859 | * CRTL's emulation of Unix-style signals and kill() | |
1860 | */ | |
1861 | while (++mark <= sp) { | |
5f521955 | 1862 | I32 proc; |
eb578fdb | 1863 | unsigned long int __vmssts; |
8af710eb | 1864 | SvGETMAGIC(*mark); |
e2c0f81f DG |
1865 | if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark))) |
1866 | Perl_croak(aTHX_ "Can't kill a non-numeric process ID"); | |
8af710eb | 1867 | proc = SvIV_nomg(*mark); |
20408e3c | 1868 | APPLY_TAINT_PROPER(); |
3595fcef | 1869 | if (!((__vmssts = sys$delprc(&proc,0)) & 1)) { |
1870 | tot--; | |
1871 | switch (__vmssts) { | |
1872 | case SS$_NONEXPR: | |
1873 | case SS$_NOSUCHNODE: | |
1874 | SETERRNO(ESRCH,__vmssts); | |
1875 | break; | |
1876 | case SS$_NOPRIV: | |
1877 | SETERRNO(EPERM,__vmssts); | |
1878 | break; | |
1879 | default: | |
1880 | SETERRNO(EVMSERR,__vmssts); | |
1881 | } | |
1882 | } | |
1883 | } | |
8165faea | 1884 | PERL_ASYNC_CHECK(); |
3595fcef | 1885 | break; |
1886 | } | |
1887 | #endif | |
c2fd40cb | 1888 | while (++mark <= sp) { |
60082291 | 1889 | Pid_t proc; |
c2fd40cb | 1890 | SvGETMAGIC(*mark); |
60082291 | 1891 | if (!(SvNIOK(*mark) || looks_like_number(*mark))) |
c2fd40cb DM |
1892 | Perl_croak(aTHX_ "Can't kill a non-numeric process ID"); |
1893 | proc = SvIV_nomg(*mark); | |
c2fd40cb | 1894 | APPLY_TAINT_PROPER(); |
111f73b5 DM |
1895 | #ifdef HAS_KILLPG |
1896 | /* use killpg in preference, as the killpg() wrapper for Win32 | |
1897 | * understands process groups, but the kill() wrapper doesn't */ | |
1898 | if (killgp ? PerlProc_killpg(proc, val) | |
1899 | : PerlProc_kill(proc, val)) | |
1900 | #else | |
1901 | if (PerlProc_kill(killgp ? -proc: proc, val)) | |
1902 | #endif | |
c2fd40cb | 1903 | tot--; |
a687059c | 1904 | } |
8165faea | 1905 | PERL_ASYNC_CHECK(); |
a687059c | 1906 | break; |
b1248f16 | 1907 | #endif |
79072805 | 1908 | case OP_UNLINK: |
20408e3c | 1909 | APPLY_TAINT_PROPER(); |
79072805 LW |
1910 | tot = sp - mark; |
1911 | while (++mark <= sp) { | |
41188aa0 | 1912 | s = SvPV_const(*mark, len); |
20408e3c | 1913 | APPLY_TAINT_PROPER(); |
41188aa0 | 1914 | if (!IS_SAFE_PATHNAME(s, len, "unlink")) { |
c8028aa6 TC |
1915 | tot--; |
1916 | } | |
f0d85c30 | 1917 | else if (PL_unsafe) { |
b8ffc8df | 1918 | if (UNLINK(s)) |
a687059c LW |
1919 | tot--; |
1920 | } | |
1921 | else { /* don't let root wipe out directories without -U */ | |
1dcae8b8 | 1922 | if (PerlLIO_lstat(s,&PL_statbuf) < 0) |
a687059c | 1923 | tot--; |
1dcae8b8 EZ |
1924 | else if (S_ISDIR(PL_statbuf.st_mode)) { |
1925 | tot--; | |
cd52bc19 | 1926 | SETERRNO(EISDIR, SS_NOPRIV); |
1dcae8b8 | 1927 | } |
a687059c | 1928 | else { |
b8ffc8df | 1929 | if (UNLINK(s)) |
a687059c LW |
1930 | tot--; |
1931 | } | |
1932 | } | |
1933 | } | |
1934 | break; | |
e96b369d | 1935 | #if defined(HAS_UTIME) || defined(HAS_FUTIMES) |
79072805 | 1936 | case OP_UTIME: |
20408e3c | 1937 | APPLY_TAINT_PROPER(); |
79072805 | 1938 | if (sp - mark > 2) { |
e96b369d GA |
1939 | #if defined(HAS_FUTIMES) |
1940 | struct timeval utbuf[2]; | |
1941 | void *utbufp = utbuf; | |
1942 | #elif defined(I_UTIME) || defined(VMS) | |
663a0e37 | 1943 | struct utimbuf utbuf; |
07409e01 | 1944 | struct utimbuf *utbufp = &utbuf; |
663a0e37 | 1945 | #else |
a687059c | 1946 | struct { |
dd2821f6 GS |
1947 | Time_t actime; |
1948 | Time_t modtime; | |
a687059c | 1949 | } utbuf; |
07409e01 | 1950 | void *utbufp = &utbuf; |
663a0e37 | 1951 | #endif |
a687059c | 1952 | |
0bcc34c2 AL |
1953 | SV* const accessed = *++mark; |
1954 | SV* const modified = *++mark; | |
c6f7b413 | 1955 | |
6ec06612 SB |
1956 | /* Be like C, and if both times are undefined, let the C |
1957 | * library figure out what to do. This usually means | |
1958 | * "current time". */ | |
c6f7b413 RS |
1959 | |
1960 | if ( accessed == &PL_sv_undef && modified == &PL_sv_undef ) | |
6ec06612 SB |
1961 | utbufp = NULL; |
1962 | else { | |
1963 | Zero(&utbuf, sizeof utbuf, char); | |
e96b369d | 1964 | #ifdef HAS_FUTIMES |
4ea561bc | 1965 | utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */ |
e96b369d | 1966 | utbuf[0].tv_usec = 0; |
4ea561bc | 1967 | utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */ |
e96b369d GA |
1968 | utbuf[1].tv_usec = 0; |
1969 | #elif defined(BIG_TIME) | |
4ea561bc NC |
1970 | utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */ |
1971 | utbuf.modtime = (Time_t)SvNV(modified); /* time modified */ | |
517844ec | 1972 | #else |
4ea561bc NC |
1973 | utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */ |
1974 | utbuf.modtime = (Time_t)SvIV(modified); /* time modified */ | |
517844ec | 1975 | #endif |
6ec06612 | 1976 | } |
4373e329 | 1977 | APPLY_TAINT_PROPER(); |
79072805 LW |
1978 | tot = sp - mark; |
1979 | while (++mark <= sp) { | |
e96b369d | 1980 | GV* gv; |
64617da9 | 1981 | if ((gv = MAYBE_DEREF_GV(*mark))) { |
e96b369d GA |
1982 | if (GvIO(gv) && IoIFP(GvIOp(gv))) { |
1983 | #ifdef HAS_FUTIMES | |
375ed12a | 1984 | int fd = PerlIO_fileno(IoIFP(GvIOn(gv))); |
e96b369d | 1985 | APPLY_TAINT_PROPER(); |
375ed12a JH |
1986 | if (fd < 0) { |
1987 | SETERRNO(EBADF,RMS_IFI); | |
1988 | tot--; | |
1989 | } else if (futimes(fd, (struct timeval *) utbufp)) | |
e96b369d GA |
1990 | tot--; |
1991 | #else | |
1992 | Perl_die(aTHX_ PL_no_func, "futimes"); | |
1993 | #endif | |
1994 | } | |
1995 | else { | |
1996 | tot--; | |
1997 | } | |
1998 | } | |
e96b369d | 1999 | else { |
41188aa0 | 2000 | const char * const name = SvPV_nomg_const(*mark, len); |
e96b369d | 2001 | APPLY_TAINT_PROPER(); |
41188aa0 | 2002 | if (!IS_SAFE_PATHNAME(name, len, "utime")) { |
c8028aa6 TC |
2003 | tot--; |
2004 | } | |
2005 | else | |
e96b369d | 2006 | #ifdef HAS_FUTIMES |
8b7231d9 | 2007 | if (utimes(name, (struct timeval *)utbufp)) |
e96b369d GA |
2008 | #else |
2009 | if (PerlLIO_utime(name, utbufp)) | |
2010 | #endif | |
2011 | tot--; | |
2012 | } | |
2013 | ||
a687059c | 2014 | } |
a687059c LW |
2015 | } |
2016 | else | |
79072805 | 2017 | tot = 0; |
a687059c | 2018 | break; |
a0d0e21e | 2019 | #endif |
a687059c LW |
2020 | } |
2021 | return tot; | |
20408e3c | 2022 | |
20408e3c | 2023 | #undef APPLY_TAINT_PROPER |
a687059c LW |
2024 | } |
2025 | ||
2026 | /* Do the permissions allow some operation? Assumes statcache already set. */ | |
a0d0e21e | 2027 | #ifndef VMS /* VMS' cando is in vms.c */ |
7f4774ae | 2028 | bool |
5aaab254 | 2029 | Perl_cando(pTHX_ Mode_t mode, bool effective, const Stat_t *statbufp) |
ae1951c1 NC |
2030 | /* effective is a flag, true for EUID, or for checking if the effective gid |
2031 | * is in the list of groups returned from getgroups(). | |
2032 | */ | |
a687059c | 2033 | { |
7918f24d | 2034 | PERL_ARGS_ASSERT_CANDO; |
81611534 | 2035 | PERL_UNUSED_CONTEXT; |
7918f24d | 2036 | |
bee1dbe2 | 2037 | #ifdef DOSISH |
fe14fcc3 LW |
2038 | /* [Comments and code from Len Reed] |
2039 | * MS-DOS "user" is similar to UNIX's "superuser," but can't write | |
2040 | * to write-protected files. The execute permission bit is set | |
486ec47a | 2041 | * by the Microsoft C library stat() function for the following: |
fe14fcc3 LW |
2042 | * .exe files |
2043 | * .com files | |
2044 | * .bat files | |
2045 | * directories | |
2046 | * All files and directories are readable. | |
2047 | * Directories and special files, e.g. "CON", cannot be | |
2048 | * write-protected. | |
2049 | * [Comment by Tom Dinger -- a directory can have the write-protect | |
2050 | * bit set in the file system, but DOS permits changes to | |
2051 | * the directory anyway. In addition, all bets are off | |
2052 | * here for networked software, such as Novell and | |
2053 | * Sun's PC-NFS.] | |
2054 | */ | |
2055 | ||
bee1dbe2 LW |
2056 | /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat |
2057 | * too so it will actually look into the files for magic numbers | |
2058 | */ | |
7f4774ae | 2059 | return (mode & statbufp->st_mode) ? TRUE : FALSE; |
fe14fcc3 | 2060 | |
55497cff | 2061 | #else /* ! DOSISH */ |
b595cd4b RU |
2062 | # ifdef __CYGWIN__ |
2063 | if (ingroup(544,effective)) { /* member of Administrators */ | |
2064 | # else | |
985213f2 | 2065 | if ((effective ? PerlProc_geteuid() : PerlProc_getuid()) == 0) { /* root is special */ |
b595cd4b | 2066 | # endif |
7f4774ae | 2067 | if (mode == S_IXUSR) { |
c623bd54 | 2068 | if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode)) |
a687059c LW |
2069 | return TRUE; |
2070 | } | |
2071 | else | |
2072 | return TRUE; /* root reads and writes anything */ | |
2073 | return FALSE; | |
2074 | } | |
985213f2 | 2075 | if (statbufp->st_uid == (effective ? PerlProc_geteuid() : PerlProc_getuid()) ) { |
7f4774ae | 2076 | if (statbufp->st_mode & mode) |
a687059c LW |
2077 | return TRUE; /* ok as "user" */ |
2078 | } | |
d8eceb89 | 2079 | else if (ingroup(statbufp->st_gid,effective)) { |
7f4774ae | 2080 | if (statbufp->st_mode & mode >> 3) |
a687059c LW |
2081 | return TRUE; /* ok as "group" */ |
2082 | } | |
7f4774ae | 2083 | else if (statbufp->st_mode & mode >> 6) |
a687059c LW |
2084 | return TRUE; /* ok as "other" */ |
2085 | return FALSE; | |
55497cff | 2086 | #endif /* ! DOSISH */ |
a687059c | 2087 | } |
a0d0e21e | 2088 | #endif /* ! VMS */ |
a687059c | 2089 | |
1f676739 | 2090 | static bool |
0da8eb3a | 2091 | S_ingroup(pTHX_ Gid_t testgid, bool effective) |
a687059c | 2092 | { |
81611534 JH |
2093 | #ifndef PERL_IMPLICIT_SYS |
2094 | /* PERL_IMPLICIT_SYS like Win32: getegid() etc. require the context. */ | |
2095 | PERL_UNUSED_CONTEXT; | |
2096 | #endif | |
985213f2 | 2097 | if (testgid == (effective ? PerlProc_getegid() : PerlProc_getgid())) |
a687059c | 2098 | return TRUE; |
fe14fcc3 | 2099 | #ifdef HAS_GETGROUPS |
a687059c | 2100 | { |
331b57bc | 2101 | Groups_t *gary = NULL; |
79072805 | 2102 | I32 anum; |
331b57bc | 2103 | bool rc = FALSE; |
a687059c | 2104 | |
331b57bc | 2105 | anum = getgroups(0, gary); |
375ed12a JH |
2106 | if (anum > 0) { |
2107 | Newx(gary, anum, Groups_t); | |
2108 | anum = getgroups(anum, gary); | |
2109 | while (--anum >= 0) | |
2110 | if (gary[anum] == testgid) { | |
2111 | rc = TRUE; | |
2112 | break; | |
2113 | } | |
331b57bc | 2114 | |
375ed12a JH |
2115 | Safefree(gary); |
2116 | } | |
331b57bc | 2117 | return rc; |
a687059c | 2118 | } |
c685562b | 2119 | #else |
a687059c | 2120 | return FALSE; |
cd39f2b6 | 2121 | #endif |
a687059c | 2122 | } |
c2ab57d4 | 2123 | |
fe14fcc3 | 2124 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
c2ab57d4 | 2125 | |
79072805 | 2126 | I32 |
864dbfa3 | 2127 | Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 2128 | { |
0bcc34c2 | 2129 | const key_t key = (key_t)SvNVx(*++mark); |
c3312966 | 2130 | SV *nsv = optype == OP_MSGGET ? NULL : *++mark; |
6867be6d | 2131 | const I32 flags = SvIVx(*++mark); |
294a48e9 | 2132 | |
7918f24d | 2133 | PERL_ARGS_ASSERT_DO_IPCGET; |
294a48e9 | 2134 | PERL_UNUSED_ARG(sp); |
c2ab57d4 | 2135 | |
748a9306 | 2136 | SETERRNO(0,0); |
c2ab57d4 LW |
2137 | switch (optype) |
2138 | { | |
fe14fcc3 | 2139 | #ifdef HAS_MSG |
79072805 | 2140 | case OP_MSGGET: |
c2ab57d4 | 2141 | return msgget(key, flags); |
e5d73d77 | 2142 | #endif |
fe14fcc3 | 2143 | #ifdef HAS_SEM |
79072805 | 2144 | case OP_SEMGET: |
c3312966 | 2145 | return semget(key, (int) SvIV(nsv), flags); |
e5d73d77 | 2146 | #endif |
fe14fcc3 | 2147 | #ifdef HAS_SHM |
79072805 | 2148 | case OP_SHMGET: |
c3312966 | 2149 | return shmget(key, (size_t) SvUV(nsv), flags); |
e5d73d77 | 2150 | #endif |
fe14fcc3 | 2151 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 | 2152 | default: |
fe13d51d | 2153 | /* diag_listed_as: msg%s not implemented */ |
cea2e8a9 | 2154 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 | 2155 | #endif |
c2ab57d4 LW |
2156 | } |
2157 | return -1; /* should never happen */ | |
2158 | } | |
2159 | ||
79072805 | 2160 | I32 |
864dbfa3 | 2161 | Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 2162 | { |
c2ab57d4 | 2163 | char *a; |
a0d0e21e | 2164 | I32 ret = -1; |
6867be6d | 2165 | const I32 id = SvIVx(*++mark); |
95b63a38 | 2166 | #ifdef Semctl |
6867be6d | 2167 | const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0; |
95b63a38 | 2168 | #endif |
6867be6d | 2169 | const I32 cmd = SvIVx(*++mark); |
0bcc34c2 AL |
2170 | SV * const astr = *++mark; |
2171 | STRLEN infosize = 0; | |
2172 | I32 getinfo = (cmd == IPC_STAT); | |
c2ab57d4 | 2173 | |
7918f24d | 2174 | PERL_ARGS_ASSERT_DO_IPCCTL; |
0bcc34c2 | 2175 | PERL_UNUSED_ARG(sp); |
c2ab57d4 LW |
2176 | |
2177 | switch (optype) | |
2178 | { | |
fe14fcc3 | 2179 | #ifdef HAS_MSG |
79072805 | 2180 | case OP_MSGCTL: |
c2ab57d4 LW |
2181 | if (cmd == IPC_STAT || cmd == IPC_SET) |
2182 | infosize = sizeof(struct msqid_ds); | |
2183 | break; | |
e5d73d77 | 2184 | #endif |
fe14fcc3 | 2185 | #ifdef HAS_SHM |
79072805 | 2186 | case OP_SHMCTL: |
c2ab57d4 LW |
2187 | if (cmd == IPC_STAT || cmd == IPC_SET) |
2188 | infosize = sizeof(struct shmid_ds); | |
2189 | break; | |
e5d73d77 | 2190 | #endif |
fe14fcc3 | 2191 | #ifdef HAS_SEM |
79072805 | 2192 | case OP_SEMCTL: |
39398f3f | 2193 | #ifdef Semctl |
c2ab57d4 LW |
2194 | if (cmd == IPC_STAT || cmd == IPC_SET) |
2195 | infosize = sizeof(struct semid_ds); | |
2196 | else if (cmd == GETALL || cmd == SETALL) | |
2197 | { | |
8e591e46 | 2198 | struct semid_ds semds; |
bd89102f | 2199 | union semun semun; |
e6f0bdd6 GS |
2200 | #ifdef EXTRA_F_IN_SEMUN_BUF |
2201 | semun.buff = &semds; | |
2202 | #else | |
84902520 | 2203 | semun.buf = &semds; |
e6f0bdd6 | 2204 | #endif |
c2ab57d4 | 2205 | getinfo = (cmd == GETALL); |
9b89d93d GB |
2206 | if (Semctl(id, 0, IPC_STAT, semun) == -1) |
2207 | return -1; | |
6e21c824 LW |
2208 | infosize = semds.sem_nsems * sizeof(short); |
2209 | /* "short" is technically wrong but much more portable | |
2210 | than guessing about u_?short(_t)? */ | |
c2ab57d4 | 2211 | } |
39398f3f | 2212 | #else |
fe13d51d | 2213 | /* diag_listed_as: sem%s not implemented */ |
cea2e8a9 | 2214 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f | 2215 | #endif |
c2ab57d4 | 2216 | break; |
e5d73d77 | 2217 | #endif |
fe14fcc3 | 2218 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 | 2219 | default: |
fe13d51d | 2220 | /* diag_listed_as: shm%s not implemented */ |
cea2e8a9 | 2221 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 | 2222 | #endif |
c2ab57d4 LW |
2223 | } |
2224 | ||
2225 | if (infosize) | |
2226 | { | |
2227 | if (getinfo) | |
2228 | { | |
93524f2b | 2229 | SvPV_force_nolen(astr); |
a0d0e21e | 2230 | a = SvGROW(astr, infosize+1); |
c2ab57d4 LW |
2231 | } |
2232 | else | |
2233 | { | |
93524f2b | 2234 | STRLEN len; |
463ee0b2 LW |
2235 | a = SvPV(astr, len); |
2236 | if (len != infosize) | |
cea2e8a9 | 2237 | Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld", |
4ec43091 JH |
2238 | PL_op_desc[optype], |
2239 | (unsigned long)len, | |
2240 | (long)infosize); | |
c2ab57d4 LW |
2241 | } |
2242 | } | |
2243 | else | |
2244 | { | |
0bcc34c2 | 2245 | const IV i = SvIV(astr); |
56431972 | 2246 | a = INT2PTR(char *,i); /* ouch */ |
c2ab57d4 | 2247 | } |
748a9306 | 2248 | SETERRNO(0,0); |
c2ab57d4 LW |
2249 | switch (optype) |
2250 | { | |
fe14fcc3 | 2251 | #ifdef HAS_MSG |
79072805 | 2252 | case OP_MSGCTL: |
bee1dbe2 | 2253 | ret = msgctl(id, cmd, (struct msqid_ds *)a); |
c2ab57d4 | 2254 | break; |
e5d73d77 | 2255 | #endif |
fe14fcc3 | 2256 | #ifdef HAS_SEM |
bd89102f | 2257 | case OP_SEMCTL: { |
39398f3f | 2258 | #ifdef Semctl |
bd89102f AD |
2259 | union semun unsemds; |
2260 | ||
64d76282 BC |
2261 | if(cmd == SETVAL) { |
2262 | unsemds.val = PTR2nat(a); | |
2263 | } | |
2264 | else { | |
e6f0bdd6 | 2265 | #ifdef EXTRA_F_IN_SEMUN_BUF |
64d76282 | 2266 | unsemds.buff = (struct semid_ds *)a; |
e6f0bdd6 | 2267 | #else |
64d76282 | 2268 | unsemds.buf = (struct semid_ds *)a; |
e6f0bdd6 | 2269 | #endif |
64d76282 | 2270 | } |
bd89102f | 2271 | ret = Semctl(id, n, cmd, unsemds); |
39398f3f | 2272 | #else |
fe13d51d | 2273 | /* diag_listed_as: sem%s not implemented */ |
cea2e8a9 | 2274 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f | 2275 | #endif |
bd89102f | 2276 | } |
c2ab57d4 | 2277 | break; |
e5d73d77 | 2278 | #endif |
fe14fcc3 | 2279 | #ifdef HAS_SHM |
79072805 | 2280 | case OP_SHMCTL: |
bee1dbe2 | 2281 | ret = shmctl(id, cmd, (struct shmid_ds *)a); |
c2ab57d4 | 2282 | break; |
e5d73d77 | 2283 | #endif |
c2ab57d4 LW |
2284 | } |
2285 | if (getinfo && ret >= 0) { | |
79072805 LW |
2286 | SvCUR_set(astr, infosize); |
2287 | *SvEND(astr) = '\0'; | |
a0d0e21e | 2288 | SvSETMAGIC(astr); |
c2ab57d4 LW |
2289 | } |
2290 | return ret; | |
2291 | } | |
2292 | ||
79072805 | 2293 | I32 |
864dbfa3 | 2294 | Perl_do_msgsnd(pTHX_ SV **mark, SV **sp) |
c2ab57d4 | 2295 | { |
fe14fcc3 | 2296 | #ifdef HAS_MSG |
463ee0b2 | 2297 | STRLEN len; |
6867be6d | 2298 | const I32 id = SvIVx(*++mark); |
0bcc34c2 AL |
2299 | SV * const mstr = *++mark; |
2300 | const I32 flags = SvIVx(*++mark); | |
2301 | const char * const mbuf = SvPV_const(mstr, len); | |
2302 | const I32 msize = len - sizeof(long); | |
2303 | ||
7918f24d | 2304 | PERL_ARGS_ASSERT_DO_MSGSND; |
890ce7af | 2305 | PERL_UNUSED_ARG(sp); |
c2ab57d4 | 2306 | |
0bcc34c2 | 2307 | if (msize < 0) |
cea2e8a9 | 2308 | Perl_croak(aTHX_ "Arg too short for msgsnd"); |
748a9306 | 2309 | SETERRNO(0,0); |
bee1dbe2 | 2310 | return msgsnd(id, (struct msgbuf *)mbuf, msize, flags); |
e5d73d77 | 2311 | #else |
2d51fa4d RGS |
2312 | PERL_UNUSED_ARG(sp); |
2313 | PERL_UNUSED_ARG(mark); | |
fe13d51d | 2314 | /* diag_listed_as: msg%s not implemented */ |
cea2e8a9 | 2315 | Perl_croak(aTHX_ "msgsnd not implemented"); |
7c522378 | 2316 | return -1; |
e5d73d77 | 2317 | #endif |
c2ab57d4 LW |
2318 | } |
2319 | ||
79072805 | 2320 | I32 |
864dbfa3 | 2321 | Perl_do_msgrcv(pTHX_ SV **mark, SV **sp) |
c2ab57d4 | 2322 | { |
fe14fcc3 | 2323 | #ifdef HAS_MSG |
c2ab57d4 LW |
2324 | char *mbuf; |
2325 | long mtype; | |
6867be6d | 2326 | I32 msize, flags, ret; |
6867be6d | 2327 | const I32 id = SvIVx(*++mark); |
0bcc34c2 | 2328 | SV * const mstr = *++mark; |
7918f24d NC |
2329 | |
2330 | PERL_ARGS_ASSERT_DO_MSGRCV; | |
890ce7af | 2331 | PERL_UNUSED_ARG(sp); |
79072805 | 2332 | |
c2e66d9e GS |
2333 | /* suppress warning when reading into undef var --jhi */ |
2334 | if (! SvOK(mstr)) | |
76f68e9b | 2335 | sv_setpvs(mstr, ""); |
463ee0b2 LW |
2336 | msize = SvIVx(*++mark); |
2337 | mtype = (long)SvIVx(*++mark); | |
2338 | flags = SvIVx(*++mark); | |
93524f2b | 2339 | SvPV_force_nolen(mstr); |
a0d0e21e | 2340 | mbuf = SvGROW(mstr, sizeof(long)+msize+1); |
a1d180c4 | 2341 | |
748a9306 | 2342 | SETERRNO(0,0); |
bee1dbe2 | 2343 | ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags); |
c2ab57d4 | 2344 | if (ret >= 0) { |
79072805 LW |
2345 | SvCUR_set(mstr, sizeof(long)+ret); |
2346 | *SvEND(mstr) = '\0'; | |
41d6edb2 JH |
2347 | /* who knows who has been playing with this message? */ |
2348 | SvTAINTED_on(mstr); | |
c2ab57d4 LW |
2349 | } |
2350 | return ret; | |
e5d73d77 | 2351 | #else |
2d51fa4d RGS |
2352 | PERL_UNUSED_ARG(sp); |
2353 | PERL_UNUSED_ARG(mark); | |
fe13d51d | 2354 | /* diag_listed_as: msg%s not implemented */ |
cea2e8a9 | 2355 | Perl_croak(aTHX_ "msgrcv not implemented"); |
7c522378 | 2356 | return -1; |
e5d73d77 | 2357 | #endif |
c2ab57d4 LW |
2358 | } |
2359 | ||
79072805 | 2360 | I32 |
864dbfa3 | 2361 | Perl_do_semop(pTHX_ SV **mark, SV **sp) |
c2ab57d4 | 2362 | { |
fe14fcc3 | 2363 | #ifdef HAS_SEM |
463ee0b2 | 2364 | STRLEN opsize; |
6867be6d | 2365 | const I32 id = SvIVx(*++mark); |
0bcc34c2 AL |
2366 | SV * const opstr = *++mark; |
2367 | const char * const opbuf = SvPV_const(opstr, opsize); | |
7918f24d NC |
2368 | |
2369 | PERL_ARGS_ASSERT_DO_SEMOP; | |
890ce7af | 2370 | PERL_UNUSED_ARG(sp); |
c2ab57d4 | 2371 | |
248ff010 NC |
2372 | if (opsize < 3 * SHORTSIZE |
2373 | || (opsize % (3 * SHORTSIZE))) { | |
93189314 | 2374 | SETERRNO(EINVAL,LIB_INVARG); |
c2ab57d4 LW |
2375 | return -1; |
2376 | } | |
748a9306 | 2377 | SETERRNO(0,0); |
248ff010 NC |
2378 | /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */ |
2379 | { | |
6867be6d | 2380 | const int nsops = opsize / (3 * sizeof (short)); |
248ff010 | 2381 | int i = nsops; |
0bcc34c2 | 2382 | short * const ops = (short *) opbuf; |
248ff010 NC |
2383 | short *o = ops; |
2384 | struct sembuf *temps, *t; | |
2385 | I32 result; | |
2386 | ||
a02a5408 | 2387 | Newx (temps, nsops, struct sembuf); |
248ff010 NC |
2388 | t = temps; |
2389 | while (i--) { | |
2390 | t->sem_num = *o++; | |
2391 | t->sem_op = *o++; | |
2392 | t->sem_flg = *o++; | |
2393 | t++; | |
2394 | } | |
2395 | result = semop(id, temps, nsops); | |
248ff010 NC |
2396 | Safefree(temps); |
2397 | return result; | |
2398 | } | |
e5d73d77 | 2399 | #else |
fe13d51d | 2400 | /* diag_listed_as: sem%s not implemented */ |
cea2e8a9 | 2401 | Perl_croak(aTHX_ "semop not implemented"); |
e5d73d77 | 2402 | #endif |
c2ab57d4 LW |
2403 | } |
2404 | ||
79072805 | 2405 | I32 |
864dbfa3 | 2406 | Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 2407 | { |
fe14fcc3 | 2408 | #ifdef HAS_SHM |
4373e329 | 2409 | char *shm; |
c2ab57d4 | 2410 | struct shmid_ds shmds; |
6867be6d | 2411 | const I32 id = SvIVx(*++mark); |
0bcc34c2 AL |
2412 | SV * const mstr = *++mark; |
2413 | const I32 mpos = SvIVx(*++mark); | |
2414 | const I32 msize = SvIVx(*++mark); | |
7918f24d NC |
2415 | |
2416 | PERL_ARGS_ASSERT_DO_SHMIO; | |
890ce7af | 2417 | PERL_UNUSED_ARG(sp); |
c2ab57d4 | 2418 | |
748a9306 | 2419 | SETERRNO(0,0); |
c2ab57d4 LW |
2420 | if (shmctl(id, IPC_STAT, &shmds) == -1) |
2421 | return -1; | |
7f39519f NC |
2422 | if (mpos < 0 || msize < 0 |
2423 | || (size_t)mpos + msize > (size_t)shmds.shm_segsz) { | |
93189314 | 2424 | SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */ |
c2ab57d4 LW |
2425 | return -1; |
2426 | } | |
d4c19fe8 | 2427 | shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0); |
c2ab57d4 LW |
2428 | if (shm == (char *)-1) /* I hate System V IPC, I really do */ |
2429 | return -1; | |
79072805 | 2430 | if (optype == OP_SHMREAD) { |
c8ae91a8 | 2431 | char *mbuf; |
9f538c04 | 2432 | /* suppress warning when reading into undef var (tchrist 3/Mar/00) */ |
b399897d CS |
2433 | SvGETMAGIC(mstr); |
2434 | SvUPGRADE(mstr, SVt_PV); | |
9f538c04 | 2435 | if (! SvOK(mstr)) |
76f68e9b | 2436 | sv_setpvs(mstr, ""); |
af8ff727 | 2437 | SvPOK_only(mstr); |
bb7a0f54 | 2438 | mbuf = SvGROW(mstr, (STRLEN)msize+1); |
a0d0e21e | 2439 | |
bee1dbe2 | 2440 | Copy(shm + mpos, mbuf, msize, char); |
79072805 LW |
2441 | SvCUR_set(mstr, msize); |
2442 | *SvEND(mstr) = '\0'; | |
a0d0e21e | 2443 | SvSETMAGIC(mstr); |
d929ce6f JH |
2444 | /* who knows who has been playing with this shared memory? */ |
2445 | SvTAINTED_on(mstr); | |
c2ab57d4 LW |
2446 | } |
2447 | else { | |
93524f2b | 2448 | STRLEN len; |
c2ab57d4 | 2449 | |
93524f2b | 2450 | const char *mbuf = SvPV_const(mstr, len); |
027aa12d | 2451 | const I32 n = ((I32)len > msize) ? msize : (I32)len; |
bee1dbe2 | 2452 | Copy(mbuf, shm + mpos, n, char); |
c2ab57d4 | 2453 | if (n < msize) |
bee1dbe2 | 2454 | memzero(shm + mpos + n, msize - n); |
c2ab57d4 LW |
2455 | } |
2456 | return shmdt(shm); | |
e5d73d77 | 2457 | #else |
fe13d51d | 2458 | /* diag_listed_as: shm%s not implemented */ |
cea2e8a9 | 2459 | Perl_croak(aTHX_ "shm I/O not implemented"); |
7c522378 | 2460 | return -1; |
e5d73d77 | 2461 | #endif |
c2ab57d4 LW |
2462 | } |
2463 | ||
fe14fcc3 | 2464 | #endif /* SYSV IPC */ |
4e35701f | 2465 | |
0d44d22b | 2466 | /* |
ccfc67b7 JH |
2467 | =head1 IO Functions |
2468 | ||
0d44d22b NC |
2469 | =for apidoc start_glob |
2470 | ||
2471 | Function called by C<do_readline> to spawn a glob (or do the glob inside | |
154e47c8 | 2472 | perl on VMS). This code used to be inline, but now perl uses C<File::Glob> |
210b36aa | 2473 | this glob starter is only used by miniperl during the build process. |
0d44d22b | 2474 | Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up. |
fab3f3a7 | 2475 | |
0d44d22b NC |
2476 | =cut |
2477 | */ | |
2478 | ||
2479 | PerlIO * | |
2480 | Perl_start_glob (pTHX_ SV *tmpglob, IO *io) | |
2481 | { | |
561b68a9 | 2482 | SV * const tmpcmd = newSV(0); |
0d44d22b | 2483 | PerlIO *fp; |
41188aa0 TC |
2484 | STRLEN len; |
2485 | const char *s = SvPV(tmpglob, len); | |
7918f24d NC |
2486 | |
2487 | PERL_ARGS_ASSERT_START_GLOB; | |
2488 | ||
41188aa0 | 2489 | if (!IS_SAFE_SYSCALL(s, len, "pattern", "glob")) |
c8028aa6 TC |
2490 | return NULL; |
2491 | ||
0d44d22b NC |
2492 | ENTER; |
2493 | SAVEFREESV(tmpcmd); | |
2494 | #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */ | |
2495 | /* since spawning off a process is a real performance hit */ | |
dca5a913 JM |
2496 | |
2497 | PerlIO * | |
2498 | Perl_vms_start_glob | |
2499 | (pTHX_ SV *tmpglob, | |
2500 | IO *io); | |
2501 | ||
49a7a762 | 2502 | fp = Perl_vms_start_glob(aTHX_ tmpglob, io); |
dca5a913 | 2503 | |
0d44d22b | 2504 | #else /* !VMS */ |
0d44d22b NC |
2505 | #ifdef DOSISH |
2506 | #ifdef OS2 | |
2507 | sv_setpv(tmpcmd, "for a in "); | |
2508 | sv_catsv(tmpcmd, tmpglob); | |
2509 | sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |"); | |
2510 | #else | |
2511 | #ifdef DJGPP | |
2512 | sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */ | |
2513 | sv_catsv(tmpcmd, tmpglob); | |
2514 | #else | |
2515 | sv_setpv(tmpcmd, "perlglob "); | |
2516 | sv_catsv(tmpcmd, tmpglob); | |
2517 | sv_catpv(tmpcmd, " |"); | |
2518 | #endif /* !DJGPP */ | |
2519 | #endif /* !OS2 */ | |
2520 | #else /* !DOSISH */ | |
2521 | #if defined(CSH) | |
2522 | sv_setpvn(tmpcmd, PL_cshname, PL_cshlen); | |
2523 | sv_catpv(tmpcmd, " -cf 'set nonomatch; glob "); | |
2524 | sv_catsv(tmpcmd, tmpglob); | |
2525 | sv_catpv(tmpcmd, "' 2>/dev/null |"); | |
2526 | #else | |
2527 | sv_setpv(tmpcmd, "echo "); | |
2528 | sv_catsv(tmpcmd, tmpglob); | |
0d44d22b | 2529 | sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|"); |
0d44d22b NC |
2530 | #endif /* !CSH */ |
2531 | #endif /* !DOSISH */ | |
93b2dae1 FC |
2532 | { |
2533 | GV * const envgv = gv_fetchpvs("ENV", 0, SVt_PVHV); | |
2534 | SV ** const home = hv_fetchs(GvHV(envgv), "HOME", 0); | |
25018358 | 2535 | SV ** const path = hv_fetchs(GvHV(envgv), "PATH", 0); |
93b2dae1 | 2536 | if (home && *home) SvGETMAGIC(*home); |
25018358 | 2537 | if (path && *path) SvGETMAGIC(*path); |
93b2dae1 FC |
2538 | save_hash(gv_fetchpvs("ENV", 0, SVt_PVHV)); |
2539 | if (home && *home) SvSETMAGIC(*home); | |
25018358 | 2540 | if (path && *path) SvSETMAGIC(*path); |
93b2dae1 | 2541 | } |
d5eb9a46 NC |
2542 | (void)do_open6(PL_last_in_gv, SvPVX_const(tmpcmd), SvCUR(tmpcmd), |
2543 | NULL, NULL, 0); | |
0d44d22b NC |
2544 | fp = IoIFP(io); |
2545 | #endif /* !VMS */ | |
2546 | LEAVE; | |
de7dabb6 TC |
2547 | |
2548 | if (!fp && ckWARN(WARN_GLOB)) { | |
2549 | Perl_warner(aTHX_ packWARN(WARN_GLOB), "glob failed (can't start child: %s)", | |
2550 | Strerror(errno)); | |
2551 | } | |
2552 | ||
0d44d22b NC |
2553 | return fp; |
2554 | } | |
66610fdd RGS |
2555 | |
2556 | /* | |
2557 | * Local variables: | |
2558 | * c-indentation-style: bsd | |
2559 | * c-basic-offset: 4 | |
14d04a33 | 2560 | * indent-tabs-mode: nil |
66610fdd RGS |
2561 | * End: |
2562 | * | |
14d04a33 | 2563 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 2564 | */ |