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 |
493 | ) { |
0453d815 |
494 | if (ckWARN_d(WARN_INPLACE)) |
495 | Perl_warner(aTHX_ WARN_INPLACE, |
496 | "Can't do inplace edit: %s would not be unique", |
463ee0b2 |
497 | SvPVX(sv) ); |
79072805 |
498 | do_close(gv,FALSE); |
c623bd54 |
499 | continue; |
500 | } |
501 | #endif |
fe14fcc3 |
502 | #ifdef HAS_RENAME |
bee1dbe2 |
503 | #ifndef DOSISH |
3280af22 |
504 | if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) { |
0453d815 |
505 | if (ckWARN_d(WARN_INPLACE)) |
506 | Perl_warner(aTHX_ WARN_INPLACE, |
507 | "Can't rename %s to %s: %s, skipping file", |
508 | PL_oldname, SvPVX(sv), Strerror(errno) ); |
79072805 |
509 | do_close(gv,FALSE); |
c623bd54 |
510 | continue; |
511 | } |
a687059c |
512 | #else |
79072805 |
513 | do_close(gv,FALSE); |
3028581b |
514 | (void)PerlLIO_unlink(SvPVX(sv)); |
b28d0864 |
515 | (void)PerlLIO_rename(PL_oldname,SvPVX(sv)); |
9d116dd7 |
516 | do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp); |
55497cff |
517 | #endif /* DOSISH */ |
ff8e2863 |
518 | #else |
463ee0b2 |
519 | (void)UNLINK(SvPVX(sv)); |
b28d0864 |
520 | if (link(PL_oldname,SvPVX(sv)) < 0) { |
0453d815 |
521 | if (ckWARN_d(WARN_INPLACE)) |
522 | Perl_warner(aTHX_ WARN_INPLACE, |
523 | "Can't rename %s to %s: %s, skipping file", |
524 | PL_oldname, SvPVX(sv), Strerror(errno) ); |
79072805 |
525 | do_close(gv,FALSE); |
c623bd54 |
526 | continue; |
527 | } |
b28d0864 |
528 | (void)UNLINK(PL_oldname); |
a687059c |
529 | #endif |
530 | } |
531 | else { |
a8c18271 |
532 | #if !defined(DOSISH) && !defined(AMIGAOS) |
edc7bc49 |
533 | # ifndef VMS /* Don't delete; use automatic file versioning */ |
3280af22 |
534 | if (UNLINK(PL_oldname) < 0) { |
0453d815 |
535 | if (ckWARN_d(WARN_INPLACE)) |
536 | Perl_warner(aTHX_ WARN_INPLACE, |
537 | "Can't remove %s: %s, skipping file", |
538 | PL_oldname, Strerror(errno) ); |
79072805 |
539 | do_close(gv,FALSE); |
fe14fcc3 |
540 | continue; |
541 | } |
edc7bc49 |
542 | # endif |
ff8e2863 |
543 | #else |
cea2e8a9 |
544 | Perl_croak(aTHX_ "Can't do inplace edit without backup"); |
ff8e2863 |
545 | #endif |
a687059c |
546 | } |
547 | |
3280af22 |
548 | sv_setpvn(sv,">",!PL_inplace); |
549 | sv_catpvn(sv,PL_oldname,oldlen); |
748a9306 |
550 | SETERRNO(0,0); /* in case sprintf set errno */ |
4119ab01 |
551 | #ifdef VMS |
552 | if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0, |
553 | O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp)) { |
554 | #else |
3280af22 |
555 | if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0, |
85aff577 |
556 | O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) { |
4119ab01 |
557 | #endif |
0453d815 |
558 | if (ckWARN_d(WARN_INPLACE)) |
559 | Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s", |
560 | PL_oldname, Strerror(errno) ); |
79072805 |
561 | do_close(gv,FALSE); |
fe14fcc3 |
562 | continue; |
563 | } |
3280af22 |
564 | setdefout(PL_argvoutgv); |
565 | PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv))); |
566 | (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf); |
fe14fcc3 |
567 | #ifdef HAS_FCHMOD |
3280af22 |
568 | (void)fchmod(PL_lastfd,PL_filemode); |
a687059c |
569 | #else |
3e3baf6d |
570 | # if !(defined(WIN32) && defined(__BORLANDC__)) |
571 | /* Borland runtime creates a readonly file! */ |
b28d0864 |
572 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
3e3baf6d |
573 | # endif |
a687059c |
574 | #endif |
3280af22 |
575 | if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) { |
fe14fcc3 |
576 | #ifdef HAS_FCHOWN |
3280af22 |
577 | (void)fchown(PL_lastfd,fileuid,filegid); |
a687059c |
578 | #else |
fe14fcc3 |
579 | #ifdef HAS_CHOWN |
b28d0864 |
580 | (void)PerlLIO_chown(PL_oldname,fileuid,filegid); |
a687059c |
581 | #endif |
b1248f16 |
582 | #endif |
fe14fcc3 |
583 | } |
a687059c |
584 | } |
a0d0e21e |
585 | return IoIFP(GvIOp(gv)); |
a687059c |
586 | } |
587 | else |
22fae026 |
588 | PerlIO_printf(PerlIO_stderr(), "Can't open %s: %s\n", |
2d8e6c8d |
589 | SvPV(sv, oldlen), Strerror(errno)); |
a687059c |
590 | } |
3280af22 |
591 | if (PL_inplace) { |
592 | (void)do_close(PL_argvoutgv,FALSE); |
4633a7c4 |
593 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a687059c |
594 | } |
595 | return Nullfp; |
596 | } |
597 | |
fe14fcc3 |
598 | #ifdef HAS_PIPE |
afd9f252 |
599 | void |
864dbfa3 |
600 | Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv) |
afd9f252 |
601 | { |
79072805 |
602 | register IO *rstio; |
603 | register IO *wstio; |
afd9f252 |
604 | int fd[2]; |
605 | |
79072805 |
606 | if (!rgv) |
afd9f252 |
607 | goto badexit; |
79072805 |
608 | if (!wgv) |
afd9f252 |
609 | goto badexit; |
610 | |
a0d0e21e |
611 | rstio = GvIOn(rgv); |
612 | wstio = GvIOn(wgv); |
afd9f252 |
613 | |
a0d0e21e |
614 | if (IoIFP(rstio)) |
79072805 |
615 | do_close(rgv,FALSE); |
a0d0e21e |
616 | if (IoIFP(wstio)) |
79072805 |
617 | do_close(wgv,FALSE); |
afd9f252 |
618 | |
3028581b |
619 | if (PerlProc_pipe(fd) < 0) |
afd9f252 |
620 | goto badexit; |
760ac839 |
621 | IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"); |
622 | IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"); |
8990e307 |
623 | IoIFP(wstio) = IoOFP(wstio); |
624 | IoTYPE(rstio) = '<'; |
625 | IoTYPE(wstio) = '>'; |
626 | if (!IoIFP(rstio) || !IoOFP(wstio)) { |
760ac839 |
627 | if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio)); |
3028581b |
628 | else PerlLIO_close(fd[0]); |
760ac839 |
629 | if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio)); |
3028581b |
630 | else PerlLIO_close(fd[1]); |
fe14fcc3 |
631 | goto badexit; |
632 | } |
afd9f252 |
633 | |
3280af22 |
634 | sv_setsv(sv,&PL_sv_yes); |
afd9f252 |
635 | return; |
636 | |
637 | badexit: |
3280af22 |
638 | sv_setsv(sv,&PL_sv_undef); |
afd9f252 |
639 | return; |
640 | } |
b1248f16 |
641 | #endif |
afd9f252 |
642 | |
517844ec |
643 | /* explicit renamed to avoid C++ conflict -- kja */ |
a687059c |
644 | bool |
864dbfa3 |
645 | Perl_do_close(pTHX_ GV *gv, bool not_implicit) |
a687059c |
646 | { |
1193dd27 |
647 | bool retval; |
648 | IO *io; |
a687059c |
649 | |
79072805 |
650 | if (!gv) |
3280af22 |
651 | gv = PL_argvgv; |
a0d0e21e |
652 | if (!gv || SvTYPE(gv) != SVt_PVGV) { |
1d2dff63 |
653 | if (not_implicit) |
654 | SETERRNO(EBADF,SS$_IVCHAN); |
c2ab57d4 |
655 | return FALSE; |
99b89507 |
656 | } |
79072805 |
657 | io = GvIO(gv); |
658 | if (!io) { /* never opened */ |
1d2dff63 |
659 | if (not_implicit) { |
d008e5eb |
660 | dTHR; |
599cee73 |
661 | if (ckWARN(WARN_UNOPENED)) |
cea2e8a9 |
662 | Perl_warner(aTHX_ WARN_UNOPENED, |
599cee73 |
663 | "Close on unopened file <%s>",GvENAME(gv)); |
1d2dff63 |
664 | SETERRNO(EBADF,SS$_IVCHAN); |
665 | } |
a687059c |
666 | return FALSE; |
667 | } |
1193dd27 |
668 | retval = io_close(io); |
517844ec |
669 | if (not_implicit) { |
1193dd27 |
670 | IoLINES(io) = 0; |
671 | IoPAGE(io) = 0; |
672 | IoLINES_LEFT(io) = IoPAGE_LEN(io); |
673 | } |
674 | IoTYPE(io) = ' '; |
675 | return retval; |
676 | } |
677 | |
678 | bool |
864dbfa3 |
679 | Perl_io_close(pTHX_ IO *io) |
1193dd27 |
680 | { |
681 | bool retval = FALSE; |
682 | int status; |
683 | |
8990e307 |
684 | if (IoIFP(io)) { |
685 | if (IoTYPE(io) == '|') { |
3028581b |
686 | status = PerlProc_pclose(IoIFP(io)); |
f86702cc |
687 | STATUS_NATIVE_SET(status); |
1e422769 |
688 | retval = (STATUS_POSIX == 0); |
a687059c |
689 | } |
8990e307 |
690 | else if (IoTYPE(io) == '-') |
a687059c |
691 | retval = TRUE; |
692 | else { |
8990e307 |
693 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */ |
760ac839 |
694 | retval = (PerlIO_close(IoOFP(io)) != EOF); |
695 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
c2ab57d4 |
696 | } |
697 | else |
760ac839 |
698 | retval = (PerlIO_close(IoIFP(io)) != EOF); |
a687059c |
699 | } |
8990e307 |
700 | IoOFP(io) = IoIFP(io) = Nullfp; |
79072805 |
701 | } |
20408e3c |
702 | else { |
703 | SETERRNO(EBADF,SS$_IVCHAN); |
704 | } |
1193dd27 |
705 | |
a687059c |
706 | return retval; |
707 | } |
708 | |
709 | bool |
864dbfa3 |
710 | Perl_do_eof(pTHX_ GV *gv) |
a687059c |
711 | { |
11343788 |
712 | dTHR; |
79072805 |
713 | register IO *io; |
a687059c |
714 | int ch; |
715 | |
79072805 |
716 | io = GvIO(gv); |
a687059c |
717 | |
79072805 |
718 | if (!io) |
a687059c |
719 | return TRUE; |
af8c498a |
720 | else if (ckWARN(WARN_IO) |
721 | && (IoTYPE(io) == '>' || IoIFP(io) == PerlIO_stdout() |
722 | || IoIFP(io) == PerlIO_stderr())) |
723 | { |
724 | SV* sv = sv_newmortal(); |
725 | gv_efullname3(sv, gv, Nullch); |
726 | Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output", |
727 | SvPV_nolen(sv)); |
728 | } |
a687059c |
729 | |
8990e307 |
730 | while (IoIFP(io)) { |
a687059c |
731 | |
760ac839 |
732 | if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */ |
733 | if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */ |
734 | return FALSE; /* this is the most usual case */ |
735 | } |
a687059c |
736 | |
760ac839 |
737 | ch = PerlIO_getc(IoIFP(io)); |
a687059c |
738 | if (ch != EOF) { |
760ac839 |
739 | (void)PerlIO_ungetc(IoIFP(io),ch); |
a687059c |
740 | return FALSE; |
741 | } |
760ac839 |
742 | if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) { |
743 | if (PerlIO_get_cnt(IoIFP(io)) < -1) |
744 | PerlIO_set_cnt(IoIFP(io),-1); |
745 | } |
533c011a |
746 | if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */ |
3280af22 |
747 | if (!nextargv(PL_argvgv)) /* get another fp handy */ |
a687059c |
748 | return TRUE; |
749 | } |
750 | else |
751 | return TRUE; /* normal fp, definitely end of file */ |
752 | } |
753 | return TRUE; |
754 | } |
755 | |
5ff3f7a4 |
756 | Off_t |
864dbfa3 |
757 | Perl_do_tell(pTHX_ GV *gv) |
a687059c |
758 | { |
79072805 |
759 | register IO *io; |
96e4d5b1 |
760 | register PerlIO *fp; |
a687059c |
761 | |
96e4d5b1 |
762 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 |
763 | #ifdef ULTRIX_STDIO_BOTCH |
96e4d5b1 |
764 | if (PerlIO_eof(fp)) |
765 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ |
bee1dbe2 |
766 | #endif |
8903cb82 |
767 | return PerlIO_tell(fp); |
96e4d5b1 |
768 | } |
d008e5eb |
769 | { |
770 | dTHR; |
771 | if (ckWARN(WARN_UNOPENED)) |
cea2e8a9 |
772 | Perl_warner(aTHX_ WARN_UNOPENED, "tell() on unopened file"); |
d008e5eb |
773 | } |
748a9306 |
774 | SETERRNO(EBADF,RMS$_IFI); |
5ff3f7a4 |
775 | return (Off_t)-1; |
a687059c |
776 | } |
777 | |
778 | bool |
864dbfa3 |
779 | Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence) |
a687059c |
780 | { |
79072805 |
781 | register IO *io; |
137443ea |
782 | register PerlIO *fp; |
a687059c |
783 | |
137443ea |
784 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 |
785 | #ifdef ULTRIX_STDIO_BOTCH |
137443ea |
786 | if (PerlIO_eof(fp)) |
787 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ |
bee1dbe2 |
788 | #endif |
8903cb82 |
789 | return PerlIO_seek(fp, pos, whence) >= 0; |
137443ea |
790 | } |
d008e5eb |
791 | { |
792 | dTHR; |
793 | if (ckWARN(WARN_UNOPENED)) |
cea2e8a9 |
794 | Perl_warner(aTHX_ WARN_UNOPENED, "seek() on unopened file"); |
d008e5eb |
795 | } |
748a9306 |
796 | SETERRNO(EBADF,RMS$_IFI); |
a687059c |
797 | return FALSE; |
798 | } |
799 | |
97cc44eb |
800 | Off_t |
864dbfa3 |
801 | Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence) |
8903cb82 |
802 | { |
803 | register IO *io; |
804 | register PerlIO *fp; |
805 | |
806 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) |
3028581b |
807 | return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence); |
d008e5eb |
808 | { |
809 | dTHR; |
810 | if (ckWARN(WARN_UNOPENED)) |
cea2e8a9 |
811 | Perl_warner(aTHX_ WARN_UNOPENED, "sysseek() on unopened file"); |
d008e5eb |
812 | } |
8903cb82 |
813 | SETERRNO(EBADF,RMS$_IFI); |
814 | return -1L; |
815 | } |
816 | |
6ff81951 |
817 | int |
864dbfa3 |
818 | Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int flag) |
6ff81951 |
819 | { |
820 | if (flag != TRUE) |
cea2e8a9 |
821 | Perl_croak(aTHX_ "panic: unsetting binmode"); /* Not implemented yet */ |
6ff81951 |
822 | #ifdef DOSISH |
61ae2fbf |
823 | #if defined(atarist) || defined(__MINT__) |
6ff81951 |
824 | if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN)) |
825 | return 1; |
826 | else |
827 | return 0; |
828 | #else |
829 | if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) { |
830 | #if defined(WIN32) && defined(__BORLANDC__) |
831 | /* The translation mode of the stream is maintained independent |
832 | * of the translation mode of the fd in the Borland RTL (heavy |
833 | * digging through their runtime sources reveal). User has to |
834 | * set the mode explicitly for the stream (though they don't |
835 | * document this anywhere). GSAR 97-5-24 |
836 | */ |
837 | PerlIO_seek(fp,0L,0); |
873ef191 |
838 | ((FILE*)fp)->flags |= _F_BIN; |
6ff81951 |
839 | #endif |
840 | return 1; |
841 | } |
842 | else |
843 | return 0; |
844 | #endif |
845 | #else |
846 | #if defined(USEMYBINMODE) |
1cab015a |
847 | if (my_binmode(fp,iotype) != FALSE) |
6ff81951 |
848 | return 1; |
849 | else |
850 | return 0; |
851 | #else |
852 | return 1; |
853 | #endif |
854 | #endif |
855 | } |
856 | |
a0d0e21e |
857 | #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP) |
c2ab57d4 |
858 | /* code courtesy of William Kucharski */ |
fe14fcc3 |
859 | #define HAS_CHSIZE |
6eb13c3b |
860 | |
517844ec |
861 | I32 my_chsize(fd, length) |
79072805 |
862 | I32 fd; /* file descriptor */ |
85e6fe83 |
863 | Off_t length; /* length to set file to */ |
6eb13c3b |
864 | { |
6eb13c3b |
865 | struct flock fl; |
866 | struct stat filebuf; |
867 | |
3028581b |
868 | if (PerlLIO_fstat(fd, &filebuf) < 0) |
6eb13c3b |
869 | return -1; |
870 | |
871 | if (filebuf.st_size < length) { |
872 | |
873 | /* extend file length */ |
874 | |
3028581b |
875 | if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0) |
6eb13c3b |
876 | return -1; |
877 | |
878 | /* write a "0" byte */ |
879 | |
3028581b |
880 | if ((PerlLIO_write(fd, "", 1)) != 1) |
6eb13c3b |
881 | return -1; |
882 | } |
883 | else { |
884 | /* truncate length */ |
885 | |
886 | fl.l_whence = 0; |
887 | fl.l_len = 0; |
888 | fl.l_start = length; |
a0d0e21e |
889 | fl.l_type = F_WRLCK; /* write lock on file space */ |
6eb13c3b |
890 | |
891 | /* |
a0d0e21e |
892 | * This relies on the UNDOCUMENTED F_FREESP argument to |
6eb13c3b |
893 | * fcntl(2), which truncates the file so that it ends at the |
894 | * position indicated by fl.l_start. |
895 | * |
896 | * Will minor miracles never cease? |
897 | */ |
898 | |
a0d0e21e |
899 | if (fcntl(fd, F_FREESP, &fl) < 0) |
6eb13c3b |
900 | return -1; |
901 | |
902 | } |
903 | |
904 | return 0; |
905 | } |
a0d0e21e |
906 | #endif /* F_FREESP */ |
ff8e2863 |
907 | |
a687059c |
908 | bool |
864dbfa3 |
909 | Perl_do_print(pTHX_ register SV *sv, PerlIO *fp) |
a687059c |
910 | { |
911 | register char *tmps; |
463ee0b2 |
912 | STRLEN len; |
a687059c |
913 | |
79072805 |
914 | /* assuming fp is checked earlier */ |
915 | if (!sv) |
916 | return TRUE; |
3280af22 |
917 | if (PL_ofmt) { |
8990e307 |
918 | if (SvGMAGICAL(sv)) |
79072805 |
919 | mg_get(sv); |
463ee0b2 |
920 | if (SvIOK(sv) && SvIVX(sv) != 0) { |
65202027 |
921 | PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv)); |
760ac839 |
922 | return !PerlIO_error(fp); |
79072805 |
923 | } |
463ee0b2 |
924 | if ( (SvNOK(sv) && SvNVX(sv) != 0.0) |
79072805 |
925 | || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) { |
3280af22 |
926 | PerlIO_printf(fp, PL_ofmt, SvNVX(sv)); |
760ac839 |
927 | return !PerlIO_error(fp); |
79072805 |
928 | } |
a687059c |
929 | } |
79072805 |
930 | switch (SvTYPE(sv)) { |
931 | case SVt_NULL: |
d008e5eb |
932 | { |
933 | dTHR; |
934 | if (ckWARN(WARN_UNINITIALIZED)) |
cea2e8a9 |
935 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit); |
d008e5eb |
936 | } |
ff8e2863 |
937 | return TRUE; |
79072805 |
938 | case SVt_IV: |
a0d0e21e |
939 | if (SvIOK(sv)) { |
940 | if (SvGMAGICAL(sv)) |
941 | mg_get(sv); |
25da4f38 |
942 | if (SvIsUV(sv)) /* XXXX 64-bit? */ |
943 | PerlIO_printf(fp, "%lu", (unsigned long)SvUVX(sv)); |
944 | else |
945 | PerlIO_printf(fp, "%ld", (long)SvIVX(sv)); |
760ac839 |
946 | return !PerlIO_error(fp); |
a0d0e21e |
947 | } |
948 | /* FALL THROUGH */ |
79072805 |
949 | default: |
463ee0b2 |
950 | tmps = SvPV(sv, len); |
79072805 |
951 | break; |
ff8e2863 |
952 | } |
760ac839 |
953 | if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp))) |
a687059c |
954 | return FALSE; |
760ac839 |
955 | return !PerlIO_error(fp); |
a687059c |
956 | } |
957 | |
79072805 |
958 | I32 |
cea2e8a9 |
959 | Perl_my_stat(pTHX) |
a687059c |
960 | { |
4e35701f |
961 | djSP; |
79072805 |
962 | IO *io; |
748a9306 |
963 | GV* tmpgv; |
79072805 |
964 | |
533c011a |
965 | if (PL_op->op_flags & OPf_REF) { |
924508f0 |
966 | EXTEND(SP,1); |
748a9306 |
967 | tmpgv = cGVOP->op_gv; |
968 | do_fstat: |
969 | io = GvIO(tmpgv); |
8990e307 |
970 | if (io && IoIFP(io)) { |
3280af22 |
971 | PL_statgv = tmpgv; |
972 | sv_setpv(PL_statname,""); |
973 | PL_laststype = OP_STAT; |
974 | return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache)); |
a687059c |
975 | } |
976 | else { |
3280af22 |
977 | if (tmpgv == PL_defgv) |
978 | return PL_laststatval; |
599cee73 |
979 | if (ckWARN(WARN_UNOPENED)) |
cea2e8a9 |
980 | Perl_warner(aTHX_ WARN_UNOPENED, "Stat on unopened file <%s>", |
748a9306 |
981 | GvENAME(tmpgv)); |
3280af22 |
982 | PL_statgv = Nullgv; |
983 | sv_setpv(PL_statname,""); |
984 | return (PL_laststatval = -1); |
a687059c |
985 | } |
986 | } |
987 | else { |
748a9306 |
988 | SV* sv = POPs; |
4b74e3fb |
989 | char *s; |
2d8e6c8d |
990 | STRLEN n_a; |
79072805 |
991 | PUTBACK; |
748a9306 |
992 | if (SvTYPE(sv) == SVt_PVGV) { |
993 | tmpgv = (GV*)sv; |
994 | goto do_fstat; |
995 | } |
996 | else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) { |
997 | tmpgv = (GV*)SvRV(sv); |
998 | goto do_fstat; |
999 | } |
1000 | |
2d8e6c8d |
1001 | s = SvPV(sv, n_a); |
3280af22 |
1002 | PL_statgv = Nullgv; |
1003 | sv_setpv(PL_statname, s); |
1004 | PL_laststype = OP_STAT; |
1005 | PL_laststatval = PerlLIO_stat(s, &PL_statcache); |
599cee73 |
1006 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n')) |
cea2e8a9 |
1007 | Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat"); |
3280af22 |
1008 | return PL_laststatval; |
a687059c |
1009 | } |
1010 | } |
1011 | |
79072805 |
1012 | I32 |
cea2e8a9 |
1013 | Perl_my_lstat(pTHX) |
c623bd54 |
1014 | { |
4e35701f |
1015 | djSP; |
79072805 |
1016 | SV *sv; |
2d8e6c8d |
1017 | STRLEN n_a; |
533c011a |
1018 | if (PL_op->op_flags & OPf_REF) { |
924508f0 |
1019 | EXTEND(SP,1); |
3280af22 |
1020 | if (cGVOP->op_gv == PL_defgv) { |
1021 | if (PL_laststype != OP_LSTAT) |
cea2e8a9 |
1022 | Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat"); |
3280af22 |
1023 | return PL_laststatval; |
fe14fcc3 |
1024 | } |
cea2e8a9 |
1025 | Perl_croak(aTHX_ "You can't use -l on a filehandle"); |
fe14fcc3 |
1026 | } |
c623bd54 |
1027 | |
3280af22 |
1028 | PL_laststype = OP_LSTAT; |
1029 | PL_statgv = Nullgv; |
79072805 |
1030 | sv = POPs; |
1031 | PUTBACK; |
2d8e6c8d |
1032 | sv_setpv(PL_statname,SvPV(sv, n_a)); |
2d8e6c8d |
1033 | PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache); |
2d8e6c8d |
1034 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n')) |
cea2e8a9 |
1035 | Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat"); |
3280af22 |
1036 | return PL_laststatval; |
c623bd54 |
1037 | } |
1038 | |
a687059c |
1039 | bool |
864dbfa3 |
1040 | Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp) |
a687059c |
1041 | { |
a687059c |
1042 | register char **a; |
a687059c |
1043 | char *tmps; |
2d8e6c8d |
1044 | STRLEN n_a; |
a687059c |
1045 | |
79072805 |
1046 | if (sp > mark) { |
11343788 |
1047 | dTHR; |
3280af22 |
1048 | New(401,PL_Argv, sp - mark + 1, char*); |
1049 | a = PL_Argv; |
79072805 |
1050 | while (++mark <= sp) { |
1051 | if (*mark) |
2d8e6c8d |
1052 | *a++ = SvPVx(*mark, n_a); |
a687059c |
1053 | else |
1054 | *a++ = ""; |
1055 | } |
1056 | *a = Nullch; |
3280af22 |
1057 | if (*PL_Argv[0] != '/') /* will execvp use PATH? */ |
79072805 |
1058 | TAINT_ENV(); /* testing IFS here is overkill, probably */ |
2d8e6c8d |
1059 | if (really && *(tmps = SvPV(really, n_a))) |
3280af22 |
1060 | PerlProc_execvp(tmps,PL_Argv); |
a687059c |
1061 | else |
3280af22 |
1062 | PerlProc_execvp(PL_Argv[0],PL_Argv); |
599cee73 |
1063 | if (ckWARN(WARN_EXEC)) |
cea2e8a9 |
1064 | Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s", |
599cee73 |
1065 | PL_Argv[0], Strerror(errno)); |
a687059c |
1066 | } |
bee1dbe2 |
1067 | do_execfree(); |
a687059c |
1068 | return FALSE; |
1069 | } |
1070 | |
fe14fcc3 |
1071 | void |
864dbfa3 |
1072 | Perl_do_execfree(pTHX) |
ff8e2863 |
1073 | { |
3280af22 |
1074 | if (PL_Argv) { |
1075 | Safefree(PL_Argv); |
1076 | PL_Argv = Null(char **); |
ff8e2863 |
1077 | } |
3280af22 |
1078 | if (PL_Cmd) { |
1079 | Safefree(PL_Cmd); |
1080 | PL_Cmd = Nullch; |
ff8e2863 |
1081 | } |
1082 | } |
1083 | |
4d2c4e07 |
1084 | #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) |
760ac839 |
1085 | |
a687059c |
1086 | bool |
864dbfa3 |
1087 | Perl_do_exec(pTHX_ char *cmd) |
a687059c |
1088 | { |
e446cec8 |
1089 | return do_exec3(cmd,0,0); |
1090 | } |
1091 | |
1092 | bool |
864dbfa3 |
1093 | Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report) |
e446cec8 |
1094 | { |
a687059c |
1095 | register char **a; |
1096 | register char *s; |
a687059c |
1097 | char flags[10]; |
1098 | |
748a9306 |
1099 | while (*cmd && isSPACE(*cmd)) |
1100 | cmd++; |
1101 | |
a687059c |
1102 | /* save an extra exec if possible */ |
1103 | |
bf38876a |
1104 | #ifdef CSH |
3280af22 |
1105 | if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) { |
a687059c |
1106 | strcpy(flags,"-c"); |
3280af22 |
1107 | s = cmd+PL_cshlen+3; |
a687059c |
1108 | if (*s == 'f') { |
1109 | s++; |
1110 | strcat(flags,"f"); |
1111 | } |
1112 | if (*s == ' ') |
1113 | s++; |
1114 | if (*s++ == '\'') { |
1115 | char *ncmd = s; |
1116 | |
1117 | while (*s) |
1118 | s++; |
1119 | if (s[-1] == '\n') |
1120 | *--s = '\0'; |
1121 | if (s[-1] == '\'') { |
1122 | *--s = '\0'; |
3280af22 |
1123 | PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0); |
a687059c |
1124 | *s = '\''; |
1125 | return FALSE; |
1126 | } |
1127 | } |
1128 | } |
bf38876a |
1129 | #endif /* CSH */ |
a687059c |
1130 | |
1131 | /* see if there are shell metacharacters in it */ |
1132 | |
748a9306 |
1133 | if (*cmd == '.' && isSPACE(cmd[1])) |
1134 | goto doshell; |
1135 | |
1136 | if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4])) |
1137 | goto doshell; |
1138 | |
99b89507 |
1139 | for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */ |
63f2c1e1 |
1140 | if (*s == '=') |
1141 | goto doshell; |
748a9306 |
1142 | |
a687059c |
1143 | for (s = cmd; *s; s++) { |
93a17b20 |
1144 | if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) { |
a687059c |
1145 | if (*s == '\n' && !s[1]) { |
1146 | *s = '\0'; |
1147 | break; |
1148 | } |
603a98b0 |
1149 | /* handle the 2>&1 construct at the end */ |
1150 | if (*s == '>' && s[1] == '&' && s[2] == '1' |
1151 | && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2]) |
1152 | && (!s[3] || isSPACE(s[3]))) |
1153 | { |
1154 | char *t = s + 3; |
1155 | |
1156 | while (*t && isSPACE(*t)) |
1157 | ++t; |
1158 | if (!*t && (dup2(1,2) != -1)) { |
1159 | s[-2] = '\0'; |
1160 | break; |
1161 | } |
1162 | } |
a687059c |
1163 | doshell: |
3280af22 |
1164 | PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0); |
a687059c |
1165 | return FALSE; |
1166 | } |
1167 | } |
748a9306 |
1168 | |
3280af22 |
1169 | New(402,PL_Argv, (s - cmd) / 2 + 2, char*); |
1170 | PL_Cmd = savepvn(cmd, s-cmd); |
1171 | a = PL_Argv; |
1172 | for (s = PL_Cmd; *s;) { |
99b89507 |
1173 | while (*s && isSPACE(*s)) s++; |
a687059c |
1174 | if (*s) |
1175 | *(a++) = s; |
99b89507 |
1176 | while (*s && !isSPACE(*s)) s++; |
a687059c |
1177 | if (*s) |
1178 | *s++ = '\0'; |
1179 | } |
1180 | *a = Nullch; |
3280af22 |
1181 | if (PL_Argv[0]) { |
1182 | PerlProc_execvp(PL_Argv[0],PL_Argv); |
b1248f16 |
1183 | if (errno == ENOEXEC) { /* for system V NIH syndrome */ |
ff8e2863 |
1184 | do_execfree(); |
a687059c |
1185 | goto doshell; |
b1248f16 |
1186 | } |
d008e5eb |
1187 | { |
1188 | dTHR; |
e446cec8 |
1189 | int e = errno; |
1190 | |
d008e5eb |
1191 | if (ckWARN(WARN_EXEC)) |
cea2e8a9 |
1192 | Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s", |
d008e5eb |
1193 | PL_Argv[0], Strerror(errno)); |
e446cec8 |
1194 | if (do_report) { |
1195 | PerlLIO_write(fd, (void*)&e, sizeof(int)); |
1196 | PerlLIO_close(fd); |
1197 | } |
d008e5eb |
1198 | } |
a687059c |
1199 | } |
ff8e2863 |
1200 | do_execfree(); |
a687059c |
1201 | return FALSE; |
1202 | } |
1203 | |
6890e559 |
1204 | #endif /* OS2 || WIN32 */ |
760ac839 |
1205 | |
79072805 |
1206 | I32 |
864dbfa3 |
1207 | Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp) |
a687059c |
1208 | { |
11343788 |
1209 | dTHR; |
79072805 |
1210 | register I32 val; |
1211 | register I32 val2; |
1212 | register I32 tot = 0; |
20408e3c |
1213 | char *what; |
a687059c |
1214 | char *s; |
79072805 |
1215 | SV **oldmark = mark; |
2d8e6c8d |
1216 | STRLEN n_a; |
a687059c |
1217 | |
20408e3c |
1218 | #define APPLY_TAINT_PROPER() \ |
3280af22 |
1219 | STMT_START { \ |
17406bd6 |
1220 | if (PL_tainted) { TAINT_PROPER(what); } \ |
873ef191 |
1221 | } STMT_END |
20408e3c |
1222 | |
1223 | /* This is a first heuristic; it doesn't catch tainting magic. */ |
3280af22 |
1224 | if (PL_tainting) { |
463ee0b2 |
1225 | while (++mark <= sp) { |
bbce6d69 |
1226 | if (SvTAINTED(*mark)) { |
1227 | TAINT; |
1228 | break; |
1229 | } |
463ee0b2 |
1230 | } |
1231 | mark = oldmark; |
1232 | } |
a687059c |
1233 | switch (type) { |
79072805 |
1234 | case OP_CHMOD: |
20408e3c |
1235 | what = "chmod"; |
1236 | APPLY_TAINT_PROPER(); |
79072805 |
1237 | if (++mark <= sp) { |
463ee0b2 |
1238 | val = SvIVx(*mark); |
20408e3c |
1239 | APPLY_TAINT_PROPER(); |
1240 | tot = sp - mark; |
79072805 |
1241 | while (++mark <= sp) { |
2d8e6c8d |
1242 | char *name = SvPVx(*mark, n_a); |
20408e3c |
1243 | APPLY_TAINT_PROPER(); |
1244 | if (PerlLIO_chmod(name, val)) |
a687059c |
1245 | tot--; |
1246 | } |
1247 | } |
1248 | break; |
fe14fcc3 |
1249 | #ifdef HAS_CHOWN |
79072805 |
1250 | case OP_CHOWN: |
20408e3c |
1251 | what = "chown"; |
1252 | APPLY_TAINT_PROPER(); |
79072805 |
1253 | if (sp - mark > 2) { |
463ee0b2 |
1254 | val = SvIVx(*++mark); |
1255 | val2 = SvIVx(*++mark); |
20408e3c |
1256 | APPLY_TAINT_PROPER(); |
a0d0e21e |
1257 | tot = sp - mark; |
79072805 |
1258 | while (++mark <= sp) { |
2d8e6c8d |
1259 | char *name = SvPVx(*mark, n_a); |
20408e3c |
1260 | APPLY_TAINT_PROPER(); |
36660982 |
1261 | if (PerlLIO_chown(name, val, val2)) |
a687059c |
1262 | tot--; |
1263 | } |
1264 | } |
1265 | break; |
b1248f16 |
1266 | #endif |
dd64f1c3 |
1267 | /* |
1268 | XXX Should we make lchown() directly available from perl? |
1269 | For now, we'll let Configure test for HAS_LCHOWN, but do |
1270 | nothing in the core. |
1271 | --AD 5/1998 |
1272 | */ |
fe14fcc3 |
1273 | #ifdef HAS_KILL |
79072805 |
1274 | case OP_KILL: |
20408e3c |
1275 | what = "kill"; |
1276 | APPLY_TAINT_PROPER(); |
55497cff |
1277 | if (mark == sp) |
1278 | break; |
2d8e6c8d |
1279 | s = SvPVx(*++mark, n_a); |
79072805 |
1280 | if (isUPPER(*s)) { |
1281 | if (*s == 'S' && s[1] == 'I' && s[2] == 'G') |
1282 | s += 3; |
1283 | if (!(val = whichsig(s))) |
cea2e8a9 |
1284 | Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s); |
79072805 |
1285 | } |
1286 | else |
463ee0b2 |
1287 | val = SvIVx(*mark); |
20408e3c |
1288 | APPLY_TAINT_PROPER(); |
1289 | tot = sp - mark; |
3595fcef |
1290 | #ifdef VMS |
1291 | /* kill() doesn't do process groups (job trees?) under VMS */ |
1292 | if (val < 0) val = -val; |
1293 | if (val == SIGKILL) { |
1294 | # include <starlet.h> |
1295 | /* Use native sys$delprc() to insure that target process is |
1296 | * deleted; supervisor-mode images don't pay attention to |
1297 | * CRTL's emulation of Unix-style signals and kill() |
1298 | */ |
1299 | while (++mark <= sp) { |
1300 | I32 proc = SvIVx(*mark); |
1301 | register unsigned long int __vmssts; |
20408e3c |
1302 | APPLY_TAINT_PROPER(); |
3595fcef |
1303 | if (!((__vmssts = sys$delprc(&proc,0)) & 1)) { |
1304 | tot--; |
1305 | switch (__vmssts) { |
1306 | case SS$_NONEXPR: |
1307 | case SS$_NOSUCHNODE: |
1308 | SETERRNO(ESRCH,__vmssts); |
1309 | break; |
1310 | case SS$_NOPRIV: |
1311 | SETERRNO(EPERM,__vmssts); |
1312 | break; |
1313 | default: |
1314 | SETERRNO(EVMSERR,__vmssts); |
1315 | } |
1316 | } |
1317 | } |
1318 | break; |
1319 | } |
1320 | #endif |
79072805 |
1321 | if (val < 0) { |
1322 | val = -val; |
1323 | while (++mark <= sp) { |
463ee0b2 |
1324 | I32 proc = SvIVx(*mark); |
20408e3c |
1325 | APPLY_TAINT_PROPER(); |
fe14fcc3 |
1326 | #ifdef HAS_KILLPG |
3028581b |
1327 | if (PerlProc_killpg(proc,val)) /* BSD */ |
a687059c |
1328 | #else |
3028581b |
1329 | if (PerlProc_kill(-proc,val)) /* SYSV */ |
a687059c |
1330 | #endif |
79072805 |
1331 | tot--; |
a687059c |
1332 | } |
79072805 |
1333 | } |
1334 | else { |
1335 | while (++mark <= sp) { |
20408e3c |
1336 | I32 proc = SvIVx(*mark); |
1337 | APPLY_TAINT_PROPER(); |
1338 | if (PerlProc_kill(proc, val)) |
79072805 |
1339 | tot--; |
a687059c |
1340 | } |
1341 | } |
1342 | break; |
b1248f16 |
1343 | #endif |
79072805 |
1344 | case OP_UNLINK: |
20408e3c |
1345 | what = "unlink"; |
1346 | APPLY_TAINT_PROPER(); |
79072805 |
1347 | tot = sp - mark; |
1348 | while (++mark <= sp) { |
2d8e6c8d |
1349 | s = SvPVx(*mark, n_a); |
20408e3c |
1350 | APPLY_TAINT_PROPER(); |
3280af22 |
1351 | if (PL_euid || PL_unsafe) { |
a687059c |
1352 | if (UNLINK(s)) |
1353 | tot--; |
1354 | } |
1355 | else { /* don't let root wipe out directories without -U */ |
3280af22 |
1356 | if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode)) |
a687059c |
1357 | tot--; |
1358 | else { |
1359 | if (UNLINK(s)) |
1360 | tot--; |
1361 | } |
1362 | } |
1363 | } |
1364 | break; |
a0d0e21e |
1365 | #ifdef HAS_UTIME |
79072805 |
1366 | case OP_UTIME: |
20408e3c |
1367 | what = "utime"; |
1368 | APPLY_TAINT_PROPER(); |
79072805 |
1369 | if (sp - mark > 2) { |
748a9306 |
1370 | #if defined(I_UTIME) || defined(VMS) |
663a0e37 |
1371 | struct utimbuf utbuf; |
1372 | #else |
a687059c |
1373 | struct { |
dd2821f6 |
1374 | Time_t actime; |
1375 | Time_t modtime; |
a687059c |
1376 | } utbuf; |
663a0e37 |
1377 | #endif |
a687059c |
1378 | |
afd9f252 |
1379 | Zero(&utbuf, sizeof utbuf, char); |
517844ec |
1380 | #ifdef BIG_TIME |
dd2821f6 |
1381 | utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */ |
1382 | utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */ |
517844ec |
1383 | #else |
dd2821f6 |
1384 | utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */ |
1385 | utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */ |
517844ec |
1386 | #endif |
20408e3c |
1387 | APPLY_TAINT_PROPER(); |
79072805 |
1388 | tot = sp - mark; |
1389 | while (++mark <= sp) { |
2d8e6c8d |
1390 | char *name = SvPVx(*mark, n_a); |
20408e3c |
1391 | APPLY_TAINT_PROPER(); |
1392 | if (PerlLIO_utime(name, &utbuf)) |
a687059c |
1393 | tot--; |
1394 | } |
a687059c |
1395 | } |
1396 | else |
79072805 |
1397 | tot = 0; |
a687059c |
1398 | break; |
a0d0e21e |
1399 | #endif |
a687059c |
1400 | } |
1401 | return tot; |
20408e3c |
1402 | |
20408e3c |
1403 | #undef APPLY_TAINT_PROPER |
a687059c |
1404 | } |
1405 | |
1406 | /* Do the permissions allow some operation? Assumes statcache already set. */ |
a0d0e21e |
1407 | #ifndef VMS /* VMS' cando is in vms.c */ |
79072805 |
1408 | I32 |
864dbfa3 |
1409 | Perl_cando(pTHX_ I32 bit, I32 effective, register struct stat *statbufp) |
a687059c |
1410 | { |
bee1dbe2 |
1411 | #ifdef DOSISH |
fe14fcc3 |
1412 | /* [Comments and code from Len Reed] |
1413 | * MS-DOS "user" is similar to UNIX's "superuser," but can't write |
1414 | * to write-protected files. The execute permission bit is set |
1415 | * by the Miscrosoft C library stat() function for the following: |
1416 | * .exe files |
1417 | * .com files |
1418 | * .bat files |
1419 | * directories |
1420 | * All files and directories are readable. |
1421 | * Directories and special files, e.g. "CON", cannot be |
1422 | * write-protected. |
1423 | * [Comment by Tom Dinger -- a directory can have the write-protect |
1424 | * bit set in the file system, but DOS permits changes to |
1425 | * the directory anyway. In addition, all bets are off |
1426 | * here for networked software, such as Novell and |
1427 | * Sun's PC-NFS.] |
1428 | */ |
1429 | |
bee1dbe2 |
1430 | /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat |
1431 | * too so it will actually look into the files for magic numbers |
1432 | */ |
fe14fcc3 |
1433 | return (bit & statbufp->st_mode) ? TRUE : FALSE; |
1434 | |
55497cff |
1435 | #else /* ! DOSISH */ |
3280af22 |
1436 | if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */ |
c623bd54 |
1437 | if (bit == S_IXUSR) { |
1438 | if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode)) |
a687059c |
1439 | return TRUE; |
1440 | } |
1441 | else |
1442 | return TRUE; /* root reads and writes anything */ |
1443 | return FALSE; |
1444 | } |
3280af22 |
1445 | if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) { |
a687059c |
1446 | if (statbufp->st_mode & bit) |
1447 | return TRUE; /* ok as "user" */ |
1448 | } |
79072805 |
1449 | else if (ingroup((I32)statbufp->st_gid,effective)) { |
a687059c |
1450 | if (statbufp->st_mode & bit >> 3) |
1451 | return TRUE; /* ok as "group" */ |
1452 | } |
1453 | else if (statbufp->st_mode & bit >> 6) |
1454 | return TRUE; /* ok as "other" */ |
1455 | return FALSE; |
55497cff |
1456 | #endif /* ! DOSISH */ |
a687059c |
1457 | } |
a0d0e21e |
1458 | #endif /* ! VMS */ |
a687059c |
1459 | |
79072805 |
1460 | I32 |
864dbfa3 |
1461 | Perl_ingroup(pTHX_ I32 testgid, I32 effective) |
a687059c |
1462 | { |
3280af22 |
1463 | if (testgid == (effective ? PL_egid : PL_gid)) |
a687059c |
1464 | return TRUE; |
fe14fcc3 |
1465 | #ifdef HAS_GETGROUPS |
a687059c |
1466 | #ifndef NGROUPS |
1467 | #define NGROUPS 32 |
1468 | #endif |
1469 | { |
a0d0e21e |
1470 | Groups_t gary[NGROUPS]; |
79072805 |
1471 | I32 anum; |
a687059c |
1472 | |
1473 | anum = getgroups(NGROUPS,gary); |
1474 | while (--anum >= 0) |
1475 | if (gary[anum] == testgid) |
1476 | return TRUE; |
1477 | } |
1478 | #endif |
1479 | return FALSE; |
1480 | } |
c2ab57d4 |
1481 | |
fe14fcc3 |
1482 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
c2ab57d4 |
1483 | |
79072805 |
1484 | I32 |
864dbfa3 |
1485 | Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 |
1486 | { |
11343788 |
1487 | dTHR; |
c2ab57d4 |
1488 | key_t key; |
79072805 |
1489 | I32 n, flags; |
c2ab57d4 |
1490 | |
463ee0b2 |
1491 | key = (key_t)SvNVx(*++mark); |
1492 | n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark); |
1493 | flags = SvIVx(*++mark); |
748a9306 |
1494 | SETERRNO(0,0); |
c2ab57d4 |
1495 | switch (optype) |
1496 | { |
fe14fcc3 |
1497 | #ifdef HAS_MSG |
79072805 |
1498 | case OP_MSGGET: |
c2ab57d4 |
1499 | return msgget(key, flags); |
e5d73d77 |
1500 | #endif |
fe14fcc3 |
1501 | #ifdef HAS_SEM |
79072805 |
1502 | case OP_SEMGET: |
c2ab57d4 |
1503 | return semget(key, n, flags); |
e5d73d77 |
1504 | #endif |
fe14fcc3 |
1505 | #ifdef HAS_SHM |
79072805 |
1506 | case OP_SHMGET: |
c2ab57d4 |
1507 | return shmget(key, n, flags); |
e5d73d77 |
1508 | #endif |
fe14fcc3 |
1509 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 |
1510 | default: |
cea2e8a9 |
1511 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 |
1512 | #endif |
c2ab57d4 |
1513 | } |
1514 | return -1; /* should never happen */ |
1515 | } |
1516 | |
79072805 |
1517 | I32 |
864dbfa3 |
1518 | Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 |
1519 | { |
11343788 |
1520 | dTHR; |
79072805 |
1521 | SV *astr; |
c2ab57d4 |
1522 | char *a; |
a0d0e21e |
1523 | I32 id, n, cmd, infosize, getinfo; |
1524 | I32 ret = -1; |
c2ab57d4 |
1525 | |
463ee0b2 |
1526 | id = SvIVx(*++mark); |
1527 | n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0; |
1528 | cmd = SvIVx(*++mark); |
79072805 |
1529 | astr = *++mark; |
c2ab57d4 |
1530 | infosize = 0; |
1531 | getinfo = (cmd == IPC_STAT); |
1532 | |
1533 | switch (optype) |
1534 | { |
fe14fcc3 |
1535 | #ifdef HAS_MSG |
79072805 |
1536 | case OP_MSGCTL: |
c2ab57d4 |
1537 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1538 | infosize = sizeof(struct msqid_ds); |
1539 | break; |
e5d73d77 |
1540 | #endif |
fe14fcc3 |
1541 | #ifdef HAS_SHM |
79072805 |
1542 | case OP_SHMCTL: |
c2ab57d4 |
1543 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1544 | infosize = sizeof(struct shmid_ds); |
1545 | break; |
e5d73d77 |
1546 | #endif |
fe14fcc3 |
1547 | #ifdef HAS_SEM |
79072805 |
1548 | case OP_SEMCTL: |
39398f3f |
1549 | #ifdef Semctl |
c2ab57d4 |
1550 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1551 | infosize = sizeof(struct semid_ds); |
1552 | else if (cmd == GETALL || cmd == SETALL) |
1553 | { |
8e591e46 |
1554 | struct semid_ds semds; |
bd89102f |
1555 | union semun semun; |
1556 | |
84902520 |
1557 | semun.buf = &semds; |
c2ab57d4 |
1558 | getinfo = (cmd == GETALL); |
9b89d93d |
1559 | if (Semctl(id, 0, IPC_STAT, semun) == -1) |
1560 | return -1; |
6e21c824 |
1561 | infosize = semds.sem_nsems * sizeof(short); |
1562 | /* "short" is technically wrong but much more portable |
1563 | than guessing about u_?short(_t)? */ |
c2ab57d4 |
1564 | } |
39398f3f |
1565 | #else |
cea2e8a9 |
1566 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f |
1567 | #endif |
c2ab57d4 |
1568 | break; |
e5d73d77 |
1569 | #endif |
fe14fcc3 |
1570 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 |
1571 | default: |
cea2e8a9 |
1572 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 |
1573 | #endif |
c2ab57d4 |
1574 | } |
1575 | |
1576 | if (infosize) |
1577 | { |
a0d0e21e |
1578 | STRLEN len; |
c2ab57d4 |
1579 | if (getinfo) |
1580 | { |
a0d0e21e |
1581 | SvPV_force(astr, len); |
1582 | a = SvGROW(astr, infosize+1); |
c2ab57d4 |
1583 | } |
1584 | else |
1585 | { |
463ee0b2 |
1586 | a = SvPV(astr, len); |
1587 | if (len != infosize) |
cea2e8a9 |
1588 | Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld", |
4ec43091 |
1589 | PL_op_desc[optype], |
1590 | (unsigned long)len, |
1591 | (long)infosize); |
c2ab57d4 |
1592 | } |
1593 | } |
1594 | else |
1595 | { |
c030ccd9 |
1596 | IV i = SvIV(astr); |
c2ab57d4 |
1597 | a = (char *)i; /* ouch */ |
1598 | } |
748a9306 |
1599 | SETERRNO(0,0); |
c2ab57d4 |
1600 | switch (optype) |
1601 | { |
fe14fcc3 |
1602 | #ifdef HAS_MSG |
79072805 |
1603 | case OP_MSGCTL: |
bee1dbe2 |
1604 | ret = msgctl(id, cmd, (struct msqid_ds *)a); |
c2ab57d4 |
1605 | break; |
e5d73d77 |
1606 | #endif |
fe14fcc3 |
1607 | #ifdef HAS_SEM |
bd89102f |
1608 | case OP_SEMCTL: { |
39398f3f |
1609 | #ifdef Semctl |
bd89102f |
1610 | union semun unsemds; |
1611 | |
1612 | unsemds.buf = (struct semid_ds *)a; |
1613 | ret = Semctl(id, n, cmd, unsemds); |
39398f3f |
1614 | #else |
cea2e8a9 |
1615 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f |
1616 | #endif |
bd89102f |
1617 | } |
c2ab57d4 |
1618 | break; |
e5d73d77 |
1619 | #endif |
fe14fcc3 |
1620 | #ifdef HAS_SHM |
79072805 |
1621 | case OP_SHMCTL: |
bee1dbe2 |
1622 | ret = shmctl(id, cmd, (struct shmid_ds *)a); |
c2ab57d4 |
1623 | break; |
e5d73d77 |
1624 | #endif |
c2ab57d4 |
1625 | } |
1626 | if (getinfo && ret >= 0) { |
79072805 |
1627 | SvCUR_set(astr, infosize); |
1628 | *SvEND(astr) = '\0'; |
a0d0e21e |
1629 | SvSETMAGIC(astr); |
c2ab57d4 |
1630 | } |
1631 | return ret; |
1632 | } |
1633 | |
79072805 |
1634 | I32 |
864dbfa3 |
1635 | Perl_do_msgsnd(pTHX_ SV **mark, SV **sp) |
c2ab57d4 |
1636 | { |
fe14fcc3 |
1637 | #ifdef HAS_MSG |
11343788 |
1638 | dTHR; |
79072805 |
1639 | SV *mstr; |
c2ab57d4 |
1640 | char *mbuf; |
79072805 |
1641 | I32 id, msize, flags; |
463ee0b2 |
1642 | STRLEN len; |
c2ab57d4 |
1643 | |
463ee0b2 |
1644 | id = SvIVx(*++mark); |
79072805 |
1645 | mstr = *++mark; |
463ee0b2 |
1646 | flags = SvIVx(*++mark); |
1647 | mbuf = SvPV(mstr, len); |
1648 | if ((msize = len - sizeof(long)) < 0) |
cea2e8a9 |
1649 | Perl_croak(aTHX_ "Arg too short for msgsnd"); |
748a9306 |
1650 | SETERRNO(0,0); |
bee1dbe2 |
1651 | return msgsnd(id, (struct msgbuf *)mbuf, msize, flags); |
e5d73d77 |
1652 | #else |
cea2e8a9 |
1653 | Perl_croak(aTHX_ "msgsnd not implemented"); |
e5d73d77 |
1654 | #endif |
c2ab57d4 |
1655 | } |
1656 | |
79072805 |
1657 | I32 |
864dbfa3 |
1658 | Perl_do_msgrcv(pTHX_ SV **mark, SV **sp) |
c2ab57d4 |
1659 | { |
fe14fcc3 |
1660 | #ifdef HAS_MSG |
11343788 |
1661 | dTHR; |
79072805 |
1662 | SV *mstr; |
c2ab57d4 |
1663 | char *mbuf; |
1664 | long mtype; |
79072805 |
1665 | I32 id, msize, flags, ret; |
463ee0b2 |
1666 | STRLEN len; |
79072805 |
1667 | |
463ee0b2 |
1668 | id = SvIVx(*++mark); |
79072805 |
1669 | mstr = *++mark; |
463ee0b2 |
1670 | msize = SvIVx(*++mark); |
1671 | mtype = (long)SvIVx(*++mark); |
1672 | flags = SvIVx(*++mark); |
a0d0e21e |
1673 | SvPV_force(mstr, len); |
1674 | mbuf = SvGROW(mstr, sizeof(long)+msize+1); |
1675 | |
748a9306 |
1676 | SETERRNO(0,0); |
bee1dbe2 |
1677 | ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags); |
c2ab57d4 |
1678 | if (ret >= 0) { |
79072805 |
1679 | SvCUR_set(mstr, sizeof(long)+ret); |
1680 | *SvEND(mstr) = '\0'; |
c2ab57d4 |
1681 | } |
1682 | return ret; |
e5d73d77 |
1683 | #else |
cea2e8a9 |
1684 | Perl_croak(aTHX_ "msgrcv not implemented"); |
e5d73d77 |
1685 | #endif |
c2ab57d4 |
1686 | } |
1687 | |
79072805 |
1688 | I32 |
864dbfa3 |
1689 | Perl_do_semop(pTHX_ SV **mark, SV **sp) |
c2ab57d4 |
1690 | { |
fe14fcc3 |
1691 | #ifdef HAS_SEM |
11343788 |
1692 | dTHR; |
79072805 |
1693 | SV *opstr; |
c2ab57d4 |
1694 | char *opbuf; |
463ee0b2 |
1695 | I32 id; |
1696 | STRLEN opsize; |
c2ab57d4 |
1697 | |
463ee0b2 |
1698 | id = SvIVx(*++mark); |
79072805 |
1699 | opstr = *++mark; |
463ee0b2 |
1700 | opbuf = SvPV(opstr, opsize); |
c2ab57d4 |
1701 | if (opsize < sizeof(struct sembuf) |
1702 | || (opsize % sizeof(struct sembuf)) != 0) { |
748a9306 |
1703 | SETERRNO(EINVAL,LIB$_INVARG); |
c2ab57d4 |
1704 | return -1; |
1705 | } |
748a9306 |
1706 | SETERRNO(0,0); |
6e21c824 |
1707 | return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf)); |
e5d73d77 |
1708 | #else |
cea2e8a9 |
1709 | Perl_croak(aTHX_ "semop not implemented"); |
e5d73d77 |
1710 | #endif |
c2ab57d4 |
1711 | } |
1712 | |
79072805 |
1713 | I32 |
864dbfa3 |
1714 | Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 |
1715 | { |
fe14fcc3 |
1716 | #ifdef HAS_SHM |
11343788 |
1717 | dTHR; |
79072805 |
1718 | SV *mstr; |
c2ab57d4 |
1719 | char *mbuf, *shm; |
79072805 |
1720 | I32 id, mpos, msize; |
463ee0b2 |
1721 | STRLEN len; |
c2ab57d4 |
1722 | struct shmid_ds shmds; |
c2ab57d4 |
1723 | |
463ee0b2 |
1724 | id = SvIVx(*++mark); |
79072805 |
1725 | mstr = *++mark; |
463ee0b2 |
1726 | mpos = SvIVx(*++mark); |
1727 | msize = SvIVx(*++mark); |
748a9306 |
1728 | SETERRNO(0,0); |
c2ab57d4 |
1729 | if (shmctl(id, IPC_STAT, &shmds) == -1) |
1730 | return -1; |
1731 | if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) { |
748a9306 |
1732 | SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */ |
c2ab57d4 |
1733 | return -1; |
1734 | } |
8ac85365 |
1735 | shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0); |
c2ab57d4 |
1736 | if (shm == (char *)-1) /* I hate System V IPC, I really do */ |
1737 | return -1; |
79072805 |
1738 | if (optype == OP_SHMREAD) { |
a0d0e21e |
1739 | SvPV_force(mstr, len); |
1740 | mbuf = SvGROW(mstr, msize+1); |
1741 | |
bee1dbe2 |
1742 | Copy(shm + mpos, mbuf, msize, char); |
79072805 |
1743 | SvCUR_set(mstr, msize); |
1744 | *SvEND(mstr) = '\0'; |
a0d0e21e |
1745 | SvSETMAGIC(mstr); |
c2ab57d4 |
1746 | } |
1747 | else { |
79072805 |
1748 | I32 n; |
c2ab57d4 |
1749 | |
a0d0e21e |
1750 | mbuf = SvPV(mstr, len); |
463ee0b2 |
1751 | if ((n = len) > msize) |
c2ab57d4 |
1752 | n = msize; |
bee1dbe2 |
1753 | Copy(mbuf, shm + mpos, n, char); |
c2ab57d4 |
1754 | if (n < msize) |
bee1dbe2 |
1755 | memzero(shm + mpos + n, msize - n); |
c2ab57d4 |
1756 | } |
1757 | return shmdt(shm); |
e5d73d77 |
1758 | #else |
cea2e8a9 |
1759 | Perl_croak(aTHX_ "shm I/O not implemented"); |
e5d73d77 |
1760 | #endif |
c2ab57d4 |
1761 | } |
1762 | |
fe14fcc3 |
1763 | #endif /* SYSV IPC */ |
4e35701f |
1764 | |