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