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