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