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