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