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