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