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