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