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