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