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