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