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