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