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