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