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