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