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