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