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