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