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