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